]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
c1c36981d5d8ac78f86e1cadade3fa99e97d5937
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.migration
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 public final class SourceFileReplacer
{
21 SourceFileReplacer(String path
, String outpath
, ModuleInfo moduleinfo
, Database database
, UI fp
) {
28 private String modulepath
;
29 private String outputpath
;
30 private ModuleInfo mi
;
33 private boolean showdetails
= false;
35 private class r8tor9
{
36 r8tor9(String r8
, String r9
) {
40 public String r8thing
;
41 public String r9thing
;
44 // these sets are used only for printing log of the changes in current file
45 private Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
46 private Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
47 private Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
48 private Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
49 private Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
50 private Set
<String
> filer8only
= new HashSet
<String
>();
52 private static final String r8only
= "EfiLibInstallDriverBinding " +
53 "EfiLibInstallAllDriverProtocols " +
54 "EfiLibCompareLanguage " +
56 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!
66 "GetDxeCoreHobInfo " +
67 "GetNextFirmwareVolumeHob " +
69 "GetPalEntryHobInfo " +
70 "GetIoPortSpaceAddressHobInfo ";
72 public void flush() throws Exception
{
73 String outname
= null;
75 if (ui
.yesOrNo("Changes will be made to the Source Code. View details?")) {
79 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
80 while (di
.hasNext()) {
82 if (inname
.contains(".c") || inname
.contains(".C")) {
83 if (inname
.contains(".C")) {
84 outname
= inname
.replaceFirst(".C", ".c");
88 ui
.println("\nModifying file: " + inname
);
89 Common
.string2file(sourcefilereplace(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
90 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || inname
.contains(".uni")) {
91 if (inname
.contains(".H")) {
92 outname
= inname
.replaceFirst(".H", ".h");
96 ui
.println("\nCopying file: " + inname
);
97 Common
.string2file(Common
.file2string(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
101 if (!mi
.hashr8only
.isEmpty()) {
106 private void addr8only() throws Exception
{
107 String paragraph
= null;
108 String line
= Common
.file2string(Database
.defaultpath
+ File
.separator
+ "R8Lib.c");
109 Common
.ensureDir(modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.c");
110 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
111 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
112 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
113 Matcher mtrr8only
= ptnr8only
.matcher(line
);
114 Matcher mtrr8onlyhead
;
115 while (mtrr8only
.find()) {
116 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
117 paragraph
= mtrr8only
.group();
118 outfile1
.append(paragraph
+ "\n\n");
119 if (mtrr8only
.group(1).length() != 0) {
120 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
123 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
124 paragraph
= mtrr8onlyhead
.replaceAll(";");
126 outfile2
.append(paragraph
+ "\n\n");
134 mi
.localmodulesources
.add("R8Lib.h");
135 mi
.localmodulesources
.add("R8Lib.c");
138 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
139 private String
sourcefilereplace(String filename
) throws Exception
{
140 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
141 StringBuffer wholefile
= new StringBuffer();
146 boolean addr8
= false;
148 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
149 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
151 while ((line
= rd
.readLine()) != null) {
152 wholefile
.append(line
+ "\n");
154 line
= wholefile
.toString();
156 // replace BS -> gBS , RT -> gRT
157 Matcher mat
= pat
.matcher(line
);
158 if (mat
.find()) { // add a library here
159 ui
.println("Converting all BS->gBS, RT->gRT");
160 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
164 if (mat
.group(1).matches("BS")) {
165 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
167 if (mat
.group(1).matches("RT")) {
168 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
172 // remove EFI_DRIVER_ENTRY_POINT
173 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
174 Matcher matentrypoint = patentrypoint.matcher(line);
175 if (matentrypoint.find()) {
176 ui.println("Deleting Entry_Point");
177 line = matentrypoint.replaceAll("");
180 // start replacing names
182 // Converting non-locla function
183 it
= mi
.hashnonlocalfunc
.iterator();
184 while (it
.hasNext()) {
186 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
187 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
188 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
189 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
190 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
191 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
192 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
194 mi
.hashrequiredr9libs
.add(db
.getR9Lib(r8thing
)); // add a library here
197 if ((r9thing
= db
.getR9Func(r8thing
)) != null) {
198 if (!r8thing
.equals(r9thing
)) {
199 if (line
.contains(r8thing
)) {
200 line
= line
.replaceAll(r8thing
, r9thing
);
201 filefunc
.add(new r8tor9(r8thing
, r9thing
));
202 Iterator
<r8tor9
> rt
= filefunc
.iterator();
203 while (rt
.hasNext()) {
205 if (r8only
.contains(temp
.r8thing
)) {
206 filer8only
.add(r8thing
);
207 mi
.hashr8only
.add(r8thing
);
214 } //is any of the guids changed?
216 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
220 it
= mi
.hashnonlocalmacro
.iterator();
221 while (it
.hasNext()) { //macros are all assumed MdePkg currently
223 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));
224 if ((r9thing
= db
.getR9Macro(r8thing
)) != null) {
225 if (line
.contains(r8thing
)) {
226 line
= line
.replaceAll(r8thing
, r9thing
);
227 filemacro
.add(new r8tor9(r8thing
, r9thing
));
233 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
234 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
235 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
238 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
239 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
240 if (mi
.moduletype
.contains("PEIM")) {
241 Matcher mtrpei
= ptnpei
.matcher(line
);
242 while (mtrpei
.find()) { // ! add a library here !
243 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
244 mi
.hashrequiredr9libs
.add("PeiServicesLib");
247 if (line
.contains("PeiServicesCopyMem")) {
248 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
249 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
251 if (line
.contains("PeiServicesSetMem")) {
252 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
253 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
256 // Second , find all #% to drop the arg "PeiServices"
257 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
258 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
259 while (mtrpeiarg
.find()) {
260 line
= mtrpeiarg
.replaceAll("$1");
265 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
267 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
269 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
271 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
273 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
275 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
277 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
279 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
281 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
282 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
285 show(filefunc
, "function");
286 show(filemacro
, "macro");
287 show(fileguid
, "guid");
288 show(fileppi
, "ppi");
289 show(fileprotocol
, "protocol");
290 if (!filer8only
.isEmpty()) {
291 ui
.println("Converting r8only : " + filer8only
);
298 fileprotocol
.clear();
304 private void show(Set
<r8tor9
> hash
, String sh
) {
305 Iterator
<r8tor9
> it
= hash
.iterator();
308 if (!hash
.isEmpty()) {
309 ui
.print("Converting " + sh
+ " : ");
310 while (it
.hasNext()) {
312 ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
319 private void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
323 it
= hash
.iterator();
324 while (it
.hasNext()) {
326 if ((r9thing
= db
.getR9Guidname(r8thing
)) != null) {
327 if (!r8thing
.equals(r9thing
)) {
328 if (line
.contains(r8thing
)) {
329 line
= line
.replaceAll(r8thing
, r9thing
);
330 filehash
.add(new r8tor9(r8thing
, r9thing
));