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