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