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