]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
remodel 1
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.*;
16 import java.util.*;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 public final class SourceFileReplacer {
21 SourceFileReplacer(String path, String outpath, ModuleInfo moduleinfo) {
22 modulepath = path;
23 outputpath = outpath;
24 mi = moduleinfo;
25 }
26 private String modulepath;
27 private String outputpath;
28 private ModuleInfo mi;
29 private boolean showdetails = false;
30
31 private class r8tor9 {
32 r8tor9(String r8, String r9) {
33 r8thing = r8;
34 r9thing = r9;
35 }
36 public String r8thing;
37 public String r9thing;
38 }
39
40 // these sets are used only for printing log of the changes in current file
41 private Set<r8tor9> filefunc = new HashSet<r8tor9>();
42 private Set<r8tor9> filemacro = new HashSet<r8tor9>();
43 private Set<r8tor9> fileguid = new HashSet<r8tor9>();
44 private Set<r8tor9> fileppi = new HashSet<r8tor9>();
45 private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
46 private Set<String> filer8only = new HashSet<String>();
47
48 private static final String r8only = "EfiLibInstallDriverBinding " +
49 "EfiLibInstallAllDriverProtocols " +
50 "EfiLibCompareLanguage " +
51 "BufToHexString " +
52 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!
53 "EfiValueToHexStr " +
54 "HexStringToBuf " +
55 "IsHexDigit " +
56 "NibbleToHexChar " +
57 "GetHob " +
58 "GetHobListSize " +
59 "GetHobVersion " +
60 "GetHobBootMode " +
61 "GetCpuHobInfo " +
62 "GetDxeCoreHobInfo " +
63 "GetNextFirmwareVolumeHob " +
64 "GetNextGuidHob " +
65 "GetPalEntryHobInfo " +
66 "GetIoPortSpaceAddressHobInfo ";
67
68 public void flush() throws Exception {
69 String outname = null;
70 String inname = null;
71 if (MigrationTool.ui.yesOrNo("Changes will be made to the Source Code. View details?")) {
72 showdetails = true;
73 }
74
75 Iterator<String> di = mi.localmodulesources.iterator();
76 while (di.hasNext()) {
77 inname = di.next();
78 if (inname.contains(".c") || inname.contains(".C")) {
79 if (inname.contains(".C")) {
80 outname = inname.replaceFirst(".C", ".c");
81 } else {
82 outname = inname;
83 }
84 MigrationTool.ui.println("\nModifying file: " + inname);
85 Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);
86 } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {
87 if (inname.contains(".H")) {
88 outname = inname.replaceFirst(".H", ".h");
89 } else {
90 outname = inname;
91 }
92 MigrationTool.ui.println("\nCopying file: " + inname);
93 Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);
94 }
95 }
96
97 if (!mi.hashr8only.isEmpty()) {
98 addr8only();
99 }
100 }
101
102 private void addr8only() throws Exception {
103 String paragraph = null;
104 String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c");
105 Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");
106 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));
107 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));
108 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);
109 Matcher mtrr8only = ptnr8only.matcher(line);
110 Matcher mtrr8onlyhead;
111 while (mtrr8only.find()) {
112 if (mi.hashr8only.contains(mtrr8only.group(2))) {
113 paragraph = mtrr8only.group();
114 outfile1.append(paragraph + "\n\n");
115 if (mtrr8only.group(1).length() != 0) {
116 mi.hashrequiredr9libs.add(mtrr8only.group(1));
117 }
118 //generate R8lib.h
119 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {
120 paragraph = mtrr8onlyhead.replaceAll(";");
121 }
122 outfile2.append(paragraph + "\n\n");
123 }
124 }
125 outfile1.flush();
126 outfile1.close();
127 outfile2.flush();
128 outfile2.close();
129
130 mi.localmodulesources.add("R8Lib.h");
131 mi.localmodulesources.add("R8Lib.c");
132 }
133
134 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
135 private String sourcefilereplace(String filename) throws Exception {
136 BufferedReader rd = new BufferedReader(new FileReader(filename));
137 StringBuffer wholefile = new StringBuffer();
138 String line;
139 String r8thing;
140 String r9thing;
141 r8tor9 temp;
142 boolean addr8 = false;
143
144 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
145 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
146
147 while ((line = rd.readLine()) != null) {
148 wholefile.append(line + "\n");
149 }
150 line = wholefile.toString();
151
152 // replace BS -> gBS , RT -> gRT
153 Matcher mat = pat.matcher(line);
154 if (mat.find()) { // add a library here
155 MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");
156 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness
157 }
158 mat.reset();
159 while (mat.find()) {
160 if (mat.group(1).matches("BS")) {
161 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
162 }
163 if (mat.group(1).matches("RT")) {
164 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
165 }
166 }
167 /*
168 // remove EFI_DRIVER_ENTRY_POINT
169 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
170 Matcher matentrypoint = patentrypoint.matcher(line);
171 if (matentrypoint.find()) {
172 MigrationTool.ui.println("Deleting Entry_Point");
173 line = matentrypoint.replaceAll("");
174 }
175 */
176 // start replacing names
177 Iterator<String> it;
178 // Converting non-locla function
179 it = mi.hashnonlocalfunc.iterator();
180 while (it.hasNext()) {
181 r8thing = it.next();
182 if (r8thing.matches("EfiInitializeDriverLib")) { //s
183 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
184 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
185 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c
186 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
187 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
188 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
189 } else { //
190 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here
191 }
192
193 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {
194 if (!r8thing.equals(r9thing)) {
195 if (line.contains(r8thing)) {
196 line = line.replaceAll(r8thing, r9thing);
197 filefunc.add(new r8tor9(r8thing, r9thing));
198 Iterator<r8tor9> rt = filefunc.iterator();
199 while (rt.hasNext()) {
200 temp = rt.next();
201 if (r8only.contains(temp.r8thing)) {
202 filer8only.add(r8thing);
203 mi.hashr8only.add(r8thing);
204 addr8 = true;
205 }
206 }
207 }
208 }
209 }
210 } //is any of the guids changed?
211 if (addr8 == true) {
212 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
213 }
214
215 // Converting macro
216 it = mi.hashnonlocalmacro.iterator();
217 while (it.hasNext()) { //macros are all assumed MdePkg currently
218 r8thing = it.next();
219 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
220 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {
221 if (line.contains(r8thing)) {
222 line = line.replaceAll(r8thing, r9thing);
223 filemacro.add(new r8tor9(r8thing, r9thing));
224 }
225 }
226 }
227
228 // Converting guid
229 replaceGuid(line, mi.guid, "guid", fileguid);
230 replaceGuid(line, mi.ppi, "ppi", fileppi);
231 replaceGuid(line, mi.protocol, "protocol", fileprotocol);
232
233 // Converting Pei
234 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
235 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
236 if (mi.moduletype.contains("PEIM")) {
237 Matcher mtrpei = ptnpei.matcher(line);
238 while (mtrpei.find()) { // ! add a library here !
239 line = mtrpei.replaceAll("PeiServices$1#%$2");
240 mi.hashrequiredr9libs.add("PeiServicesLib");
241 }
242 mtrpei.reset();
243 if (line.contains("PeiServicesCopyMem")) {
244 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");
245 mi.hashrequiredr9libs.add("BaseMemoryLib");
246 }
247 if (line.contains("PeiServicesSetMem")) {
248 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");
249 mi.hashrequiredr9libs.add("BaseMemoryLib");
250 }
251
252 // Second , find all #% to drop the arg "PeiServices"
253 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
254 Matcher mtrpeiarg = ptnpeiarg.matcher(line);
255 while (mtrpeiarg.find()) {
256 line = mtrpeiarg.replaceAll("$1");
257 }
258 }
259
260 Matcher mtrmac;
261 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);
262 if (mtrmac.find()) {
263 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
264 }
265 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);
266 if (mtrmac.find()) {
267 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
268 }
269 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);
270 if (mtrmac.find()) {
271 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
272 }
273 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);
274 if (mtrmac.find()) {
275 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
276 }
277 if (line.contains("EFI_UINTN_ALIGN_MASK")) {
278 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
279 }
280
281 show(filefunc, "function");
282 show(filemacro, "macro");
283 show(fileguid, "guid");
284 show(fileppi, "ppi");
285 show(fileprotocol, "protocol");
286 if (!filer8only.isEmpty()) {
287 MigrationTool.ui.println("Converting r8only : " + filer8only);
288 }
289
290 filefunc.clear();
291 filemacro.clear();
292 fileguid.clear();
293 fileppi.clear();
294 fileprotocol.clear();
295 filer8only.clear();
296
297 return line;
298 }
299
300 private void show(Set<r8tor9> hash, String sh) {
301 Iterator<r8tor9> it = hash.iterator();
302 r8tor9 temp;
303 if (showdetails) {
304 if (!hash.isEmpty()) {
305 MigrationTool.ui.print("Converting " + sh + " : ");
306 while (it.hasNext()) {
307 temp = it.next();
308 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");
309 }
310 MigrationTool.ui.println("");
311 }
312 }
313 }
314
315 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {
316 Iterator<String> it;
317 String r8thing;
318 String r9thing;
319 it = hash.iterator();
320 while (it.hasNext()) {
321 r8thing = it.next();
322 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {
323 if (!r8thing.equals(r9thing)) {
324 if (line.contains(r8thing)) {
325 line = line.replaceAll(r8thing, r9thing);
326 filehash.add(new r8tor9(r8thing, r9thing));
327 }
328 }
329 }
330 }
331 }
332 }