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