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