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