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