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