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