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 |
13 | package org.tianocore.migration;\r |
14 | \r |
15 | import java.io.*;\r |
16 | import java.util.*;\r |
17 | import java.util.regex.Matcher;\r |
18 | import java.util.regex.Pattern;\r |
19 | \r |
20 | public 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 |
219 | mi.localmoduleheaders.add("R8Lib.h");\r |
220 | filer8only.add(r8thing);\r |
221 | mi.hashr8only.add(r8thing);\r |
222 | addr8 = true;\r |
223 | }\r |
224 | }\r |
225 | }\r |
226 | }\r |
227 | }\r |
228 | } //is any of the guids changed?\r |
229 | if (addr8 == true) {\r |
230 | line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r |
231 | }\r |
232 | \r |
233 | // Converting macro\r |
234 | it = mi.hashnonlocalmacro.iterator();\r |
235 | while (it.hasNext()) { //macros are all assumed MdePkg currently\r |
236 | r8thing = it.next();\r |
237 | //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r |
238 | if ((r9thing = db.getR9Macro(r8thing)) != null) {\r |
239 | if (line.contains(r8thing)) {\r |
240 | line = line.replaceAll(r8thing, r9thing);\r |
241 | filemacro.add(new r8tor9(r8thing, r9thing));\r |
242 | }\r |
243 | }\r |
244 | }\r |
245 | \r |
246 | // Converting guid\r |
247 | replaceGuid(line, mi.guid, "guid", fileguid);\r |
248 | replaceGuid(line, mi.ppi, "ppi", fileppi);\r |
249 | replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r |
250 | \r |
251 | // Converting Pei\r |
252 | // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r |
253 | Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r |
254 | if (mi.moduletype.contains("PEIM")) {\r |
255 | Matcher mtrpei = ptnpei.matcher(line);\r |
256 | while (mtrpei.find()) { // ! add a library here !\r |
257 | line = mtrpei.replaceAll("PeiServices$1#%$2");\r |
258 | mi.hashrequiredr9libs.add("PeiServicesLib");\r |
259 | }\r |
260 | mtrpei.reset();\r |
261 | if (line.contains("PeiServicesCopyMem")) {\r |
262 | line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r |
263 | mi.hashrequiredr9libs.add("BaseMemoryLib");\r |
264 | }\r |
265 | if (line.contains("PeiServicesSetMem")) {\r |
266 | line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r |
267 | mi.hashrequiredr9libs.add("BaseMemoryLib");\r |
268 | }\r |
269 | \r |
270 | // Second , find all #% to drop the arg "PeiServices"\r |
271 | Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r |
272 | Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r |
273 | while (mtrpeiarg.find()) {\r |
274 | line = mtrpeiarg.replaceAll("$1");\r |
275 | }\r |
276 | }\r |
277 | \r |
278 | Matcher mtrmac;\r |
279 | mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r |
280 | if (mtrmac.find()) {\r |
281 | line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r |
282 | }\r |
283 | mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r |
284 | if (mtrmac.find()) {\r |
285 | line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r |
286 | }\r |
287 | mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r |
288 | if (mtrmac.find()) {\r |
289 | line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r |
290 | }\r |
291 | mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r |
292 | if (mtrmac.find()) {\r |
293 | line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r |
294 | }\r |
295 | if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r |
296 | line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r |
297 | }\r |
298 | \r |
299 | show(filefunc, "function");\r |
300 | show(filemacro, "macro");\r |
301 | show(fileguid, "guid");\r |
302 | show(fileppi, "ppi");\r |
303 | show(fileprotocol, "protocol");\r |
304 | if (!filer8only.isEmpty()) {\r |
305 | ui.println("Converting r8only : " + filer8only);\r |
306 | }\r |
307 | \r |
308 | filefunc.clear();\r |
309 | filemacro.clear();\r |
310 | fileguid.clear();\r |
311 | fileppi.clear();\r |
312 | fileprotocol.clear();\r |
313 | filer8only.clear();\r |
314 | \r |
315 | return line;\r |
316 | }\r |
317 | \r |
318 | private void show(Set<r8tor9> hash, String sh) {\r |
319 | Iterator<r8tor9> it = hash.iterator();\r |
320 | r8tor9 temp;\r |
321 | if (showdetails) {\r |
322 | if (!hash.isEmpty()) {\r |
323 | ui.print("Converting " + sh + " : ");\r |
324 | while (it.hasNext()) {\r |
325 | temp = it.next();\r |
326 | ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r |
327 | }\r |
328 | ui.println("");\r |
329 | }\r |
330 | }\r |
331 | }\r |
332 | \r |
333 | private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r |
334 | Iterator<String> it;\r |
335 | String r8thing;\r |
336 | String r9thing;\r |
337 | it = hash.iterator();\r |
338 | while (it.hasNext()) {\r |
339 | r8thing = it.next();\r |
340 | if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r |
341 | if (!r8thing.equals(r9thing)) {\r |
342 | if (line.contains(r8thing)) {\r |
343 | line = line.replaceAll(r8thing, r9thing);\r |
344 | filehash.add(new r8tor9(r8thing, r9thing));\r |
345 | }\r |
346 | }\r |
347 | }\r |
348 | }\r |
349 | }\r |
350 | }\r |