]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
Fix EDKT191.
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.Matcher;\r
18import java.util.regex.Pattern;\r
19\r
20public class SourceFileReplacer {\r
21 SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {\r
22 modulepath = path;\r
23 mi = moduleinfo;\r
24 db = database;\r
25 ui = fp;\r
26 }\r
27 private String modulepath;\r
28 private ModuleInfo mi;\r
29 private Database db;\r
30 private UI ui;\r
31 private boolean showdetails = false;\r
32 \r
33 private class r8tor9 {\r
34 r8tor9(String r8, String r9) {\r
35 r8thing = r8;\r
36 r9thing = r9;\r
37 }\r
38 public String r8thing;\r
39 public String r9thing;\r
40 }\r
41 \r
42 // these sets are used only for printing log of the changes in current file\r
43 private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
44 private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
45 private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
46 private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
47 private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
48 private Set<String> filer8only = new HashSet<String>();\r
49 \r
50 private String r8only = "EfiLibInstallDriverBinding " +\r
51 "EfiLibInstallAllDriverProtocols " +\r
52 "EfiLibCompareLanguage " +\r
53 "BufToHexString " +\r
e6a5df3b 54 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!\r
0dc8c589 55 "EfiValueToHexStr " +\r
56 "HexStringToBuf " +\r
57 "IsHexDigit " +\r
58 "NibbleToHexChar " +\r
59 "GetHob " +\r
60 "GetHobListSize " +\r
61 "GetHobVersion " +\r
62 "GetHobBootMode " +\r
63 "GetCpuHobInfo " +\r
64 "GetDxeCoreHobInfo " +\r
65 "GetNextFirmwareVolumeHob " +\r
66 "GetNextGuidHob " +\r
67 "GetPalEntryHobInfo " +\r
68 "GetIoPortSpaceAddressHobInfo ";\r
69 \r
70 public void flush() throws Exception {\r
71 PrintWriter outfile;\r
e6a5df3b 72 String outname = null;\r
73 String inname = null;\r
f7fee0ea 74 if (ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
0dc8c589 75 showdetails = true;\r
76 }\r
e6a5df3b 77 \r
78 Iterator<String> di = mi.localmodulesources.iterator();\r
79 while (di.hasNext()) {\r
80 inname = di.next();\r
81 if (inname.contains(".c") || inname.contains(".C")) {\r
82 if (inname.contains(".C")) {\r
83 outname = inname.replaceFirst(".C", ".c");\r
84 } else {\r
85 outname = inname;\r
86 }\r
f7fee0ea 87 ui.println("\nModifying file: " + inname);\r
fed802b1 88 Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), modulepath + File.separator + "result" + File.separator + outname);\r
89 /*\r
90503bad 90 Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
e6a5df3b 91 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
92 outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname));\r
0dc8c589 93 outfile.flush();\r
94 outfile.close();\r
fed802b1 95 */\r
e6a5df3b 96 } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
97 if (inname.contains(".H")) {\r
98 outname = inname.replaceFirst(".H", ".h");\r
0dc8c589 99 } else {\r
e6a5df3b 100 outname = inname;\r
0dc8c589 101 }\r
f7fee0ea 102 ui.println("\nCopying file: " + inname);\r
fed802b1 103 Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), modulepath + File.separator + "result" + File.separator + outname);\r
104 /*\r
90503bad 105 Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
e6a5df3b 106 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
fed802b1 107 outfile.append(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname));\r
0dc8c589 108 outfile.flush();\r
109 outfile.close();\r
fed802b1 110 */\r
0dc8c589 111 }\r
112 }\r
113\r
114 if (!mi.hashr8only.isEmpty()) {\r
115 addr8only();\r
116 }\r
117 }\r
118 \r
119 private void addr8only() throws Exception {\r
120 String paragraph = null;\r
fed802b1 121 String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c");\r
90503bad 122 Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
0dc8c589 123 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
124 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
0dc8c589 125 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
126 Matcher mtrr8only = ptnr8only.matcher(line);\r
127 Matcher mtrr8onlyhead;\r
128 while (mtrr8only.find()) {\r
129 if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
130 paragraph = mtrr8only.group();\r
131 outfile1.append(paragraph + "\n\n");\r
132 if (mtrr8only.group(1).length() != 0) {\r
133 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
134 }\r
135 //generate R8lib.h\r
136 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
137 paragraph = mtrr8onlyhead.replaceAll(";");\r
138 }\r
139 outfile2.append(paragraph + "\n\n");\r
140 }\r
141 }\r
142 outfile1.flush();\r
143 outfile1.close();\r
144 outfile2.flush();\r
145 outfile2.close();\r
e6a5df3b 146 \r
147 mi.localmodulesources.add("R8Lib.h");\r
148 mi.localmodulesources.add("R8Lib.c");\r
0dc8c589 149 }\r
150 \r
0dc8c589 151 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
152 private String sourcefilereplace(String filename) throws Exception {\r
153 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
154 StringBuffer wholefile = new StringBuffer();\r
155 String line;\r
156 String r8thing;\r
157 String r9thing;\r
158 r8tor9 temp;\r
159 boolean addr8 = false;\r
160\r
161 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
162 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
163\r
164 while ((line = rd.readLine()) != null) {\r
165 wholefile.append(line + "\n");\r
166 }\r
167 line = wholefile.toString();\r
168 \r
169 // replace BS -> gBS , RT -> gRT\r
170 Matcher mat = pat.matcher(line);\r
171 if (mat.find()) { // add a library here\r
f7fee0ea 172 ui.println("Converting all BS->gBS, RT->gRT");\r
0dc8c589 173 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
174 }\r
175 mat.reset();\r
176 while (mat.find()) {\r
177 if (mat.group(1).matches("BS")) {\r
178 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
179 }\r
180 if (mat.group(1).matches("RT")) {\r
181 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
182 }\r
183 }\r
184 /*\r
185 // remove EFI_DRIVER_ENTRY_POINT\r
186 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
187 Matcher matentrypoint = patentrypoint.matcher(line);\r
188 if (matentrypoint.find()) {\r
189 ui.println("Deleting Entry_Point");\r
190 line = matentrypoint.replaceAll("");\r
191 }\r
192 */\r
193 // start replacing names\r
194 Iterator<String> it;\r
195 // Converting non-locla function\r
196 it = mi.hashnonlocalfunc.iterator();\r
197 while (it.hasNext()) {\r
198 r8thing = it.next();\r
199 if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
200 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
201 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
202 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
203 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
204 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
205 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
206 } else { //\r
207 mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
208 }\r
209 \r
210 if ((r9thing = db.getR9Func(r8thing)) != null) {\r
211 if (!r8thing.equals(r9thing)) {\r
212 if (line.contains(r8thing)) {\r
213 line = line.replaceAll(r8thing, r9thing);\r
214 filefunc.add(new r8tor9(r8thing, r9thing));\r
215 Iterator<r8tor9> rt = filefunc.iterator();\r
216 while (rt.hasNext()) {\r
217 temp = rt.next();\r
218 if (r8only.contains(temp.r8thing)) {\r
0dc8c589 219 filer8only.add(r8thing);\r
220 mi.hashr8only.add(r8thing);\r
221 addr8 = true;\r
222 }\r
223 }\r
224 }\r
225 }\r
226 }\r
227 } //is any of the guids changed?\r
228 if (addr8 == true) {\r
229 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
230 }\r
231 \r
232 // Converting macro\r
233 it = mi.hashnonlocalmacro.iterator();\r
234 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
235 r8thing = it.next();\r
236 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
237 if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
238 if (line.contains(r8thing)) {\r
239 line = line.replaceAll(r8thing, r9thing);\r
240 filemacro.add(new r8tor9(r8thing, r9thing));\r
241 }\r
242 }\r
243 }\r
244\r
245 // Converting guid\r
246 replaceGuid(line, mi.guid, "guid", fileguid);\r
247 replaceGuid(line, mi.ppi, "ppi", fileppi);\r
248 replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
249\r
250 // Converting Pei\r
251 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
252 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
253 if (mi.moduletype.contains("PEIM")) {\r
254 Matcher mtrpei = ptnpei.matcher(line);\r
255 while (mtrpei.find()) { // ! add a library here !\r
256 line = mtrpei.replaceAll("PeiServices$1#%$2");\r
257 mi.hashrequiredr9libs.add("PeiServicesLib");\r
258 }\r
259 mtrpei.reset();\r
260 if (line.contains("PeiServicesCopyMem")) {\r
261 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
262 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
263 }\r
264 if (line.contains("PeiServicesSetMem")) {\r
265 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
266 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
267 }\r
268\r
269 // Second , find all #% to drop the arg "PeiServices"\r
270 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
271 Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
272 while (mtrpeiarg.find()) {\r
273 line = mtrpeiarg.replaceAll("$1");\r
274 }\r
275 }\r
276 \r
277 Matcher mtrmac;\r
278 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
279 if (mtrmac.find()) {\r
280 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
281 }\r
282 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
283 if (mtrmac.find()) {\r
284 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
285 }\r
286 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
287 if (mtrmac.find()) {\r
288 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
289 }\r
290 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
291 if (mtrmac.find()) {\r
292 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
293 }\r
294 if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
295 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
296 }\r
297\r
298 show(filefunc, "function");\r
299 show(filemacro, "macro");\r
300 show(fileguid, "guid");\r
301 show(fileppi, "ppi");\r
302 show(fileprotocol, "protocol");\r
303 if (!filer8only.isEmpty()) {\r
304 ui.println("Converting r8only : " + filer8only);\r
305 }\r
306\r
307 filefunc.clear();\r
308 filemacro.clear();\r
309 fileguid.clear();\r
310 fileppi.clear();\r
311 fileprotocol.clear();\r
312 filer8only.clear();\r
e6a5df3b 313\r
0dc8c589 314 return line;\r
315 }\r
316 \r
317 private void show(Set<r8tor9> hash, String sh) {\r
318 Iterator<r8tor9> it = hash.iterator();\r
319 r8tor9 temp;\r
320 if (showdetails) {\r
321 if (!hash.isEmpty()) {\r
322 ui.print("Converting " + sh + " : ");\r
323 while (it.hasNext()) {\r
324 temp = it.next();\r
325 ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
326 }\r
327 ui.println("");\r
328 }\r
329 }\r
330 }\r
331 \r
332 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
333 Iterator<String> it;\r
334 String r8thing;\r
335 String r9thing;\r
336 it = hash.iterator();\r
337 while (it.hasNext()) {\r
338 r8thing = it.next();\r
339 if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
340 if (!r8thing.equals(r9thing)) {\r
341 if (line.contains(r8thing)) {\r
342 line = line.replaceAll(r8thing, r9thing);\r
343 filehash.add(new r8tor9(r8thing, r9thing));\r
344 }\r
345 }\r
346 }\r
347 }\r
348 }\r
349}\r