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