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