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