git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1253 6f19259b...
[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
54 "EfiStrTrim " +\r
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
72 String temp = null;\r
73 if (ui.yesOrNo("Change Source Code is to be doing . See details ?")) {\r
74 showdetails = true;\r
75 }\r
76 File tempdir = new File(modulepath + File.separator + "result" + File.separator);\r
77 if (!tempdir.exists()) tempdir.mkdir();\r
78 String[] list = new File(modulepath + File.separator + "temp").list(); //what I change is the non-local .h commented-out files\r
79 for (int i = 0 ; i < list.length ; i++) {\r
80 if (list[i].contains(".c")) {\r
81 ui.println("\nModifying file : " + list[i]);\r
82 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + list[i])));\r
83 outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + list[i]));\r
84 outfile.flush();\r
85 outfile.close();\r
86 } else {\r
87 if (list[i].contains(".h")) {\r
88 temp = list[i];\r
89 } else if (list[i].contains(".C")) {\r
90 temp = list[i].replaceFirst(".C", ".c");\r
91 } else if (list[i].contains(".H")) {\r
92 temp = list[i].replaceFirst(".H", ".h");\r
93 } else {\r
94 continue;\r
95 }\r
96 ui.println("\nCopying file : " + temp);\r
97 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + temp)));\r
98 outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + list[i]));\r
99 outfile.flush();\r
100 outfile.close();\r
101 }\r
102 }\r
103\r
104 if (!mi.hashr8only.isEmpty()) {\r
105 addr8only();\r
106 }\r
107 }\r
108 \r
109 private void addr8only() throws Exception {\r
110 String paragraph = null;\r
111 String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
112 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
113 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
114 //outfile1.append("#include \"R8Lib.h\"\n\n");\r
115 //outfile2.append("#include \"R8Lib.h\"\n\n");\r
116 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
117 Matcher mtrr8only = ptnr8only.matcher(line);\r
118 Matcher mtrr8onlyhead;\r
119 while (mtrr8only.find()) {\r
120 if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
121 paragraph = mtrr8only.group();\r
122 outfile1.append(paragraph + "\n\n");\r
123 if (mtrr8only.group(1).length() != 0) {\r
124 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
125 }\r
126 //generate R8lib.h\r
127 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
128 paragraph = mtrr8onlyhead.replaceAll(";");\r
129 }\r
130 outfile2.append(paragraph + "\n\n");\r
131 }\r
132 }\r
133 outfile1.flush();\r
134 outfile1.close();\r
135 outfile2.flush();\r
136 outfile2.close();\r
137 }\r
138 \r
139 private String sourcefiletostring(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 while ((line = rd.readLine()) != null) {\r
144 wholefile.append(line + "\n");\r
145 }\r
146 return wholefile.toString();\r
147 }\r
148 \r
149 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
150 private String sourcefilereplace(String filename) throws Exception {\r
151 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
152 StringBuffer wholefile = new StringBuffer();\r
153 String line;\r
154 String r8thing;\r
155 String r9thing;\r
156 r8tor9 temp;\r
157 boolean addr8 = false;\r
158\r
159 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
160 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
161\r
162 while ((line = rd.readLine()) != null) {\r
163 wholefile.append(line + "\n");\r
164 }\r
165 line = wholefile.toString();\r
166 \r
167 // replace BS -> gBS , RT -> gRT\r
168 Matcher mat = pat.matcher(line);\r
169 if (mat.find()) { // add a library here\r
170 ui.println("Converting all BS->gBS,RT->gRT");\r
171 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
172 }\r
173 mat.reset();\r
174 while (mat.find()) {\r
175 if (mat.group(1).matches("BS")) {\r
176 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
177 }\r
178 if (mat.group(1).matches("RT")) {\r
179 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
180 }\r
181 }\r
182 /*\r
183 // remove EFI_DRIVER_ENTRY_POINT\r
184 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
185 Matcher matentrypoint = patentrypoint.matcher(line);\r
186 if (matentrypoint.find()) {\r
187 ui.println("Deleting Entry_Point");\r
188 line = matentrypoint.replaceAll("");\r
189 }\r
190 */\r
191 // start replacing names\r
192 Iterator<String> it;\r
193 // Converting non-locla function\r
194 it = mi.hashnonlocalfunc.iterator();\r
195 while (it.hasNext()) {\r
196 r8thing = it.next();\r
197 if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
198 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
199 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
200 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
201 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
202 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
203 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
204 } else { //\r
205 mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
206 }\r
207 \r
208 if ((r9thing = db.getR9Func(r8thing)) != null) {\r
209 if (!r8thing.equals(r9thing)) {\r
210 if (line.contains(r8thing)) {\r
211 line = line.replaceAll(r8thing, r9thing);\r
212 filefunc.add(new r8tor9(r8thing, r9thing));\r
213 Iterator<r8tor9> rt = filefunc.iterator();\r
214 while (rt.hasNext()) {\r
215 temp = rt.next();\r
216 if (r8only.contains(temp.r8thing)) {\r
217 mi.localmodulesources.add("R8Lib.h");\r
218 mi.localmodulesources.add("R8Lib.c");\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
313 \r
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