c7e2fea3c0a30035536939529252dda613920efc
[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 class SourceFileReplacer {
21 SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {
22 modulepath = path;
23 mi = moduleinfo;
24 db = database;
25 ui = fp;
26 }
27 private String modulepath;
28 private ModuleInfo mi;
29 private Database db;
30 private UI ui;
31 private boolean showdetails = false;
32
33 private class r8tor9 {
34 r8tor9(String r8, String r9) {
35 r8thing = r8;
36 r9thing = r9;
37 }
38 public String r8thing;
39 public String r9thing;
40 }
41
42 // these sets are used only for printing log of the changes in current file
43 private Set<r8tor9> filefunc = new HashSet<r8tor9>();
44 private Set<r8tor9> filemacro = new HashSet<r8tor9>();
45 private Set<r8tor9> fileguid = new HashSet<r8tor9>();
46 private Set<r8tor9> fileppi = new HashSet<r8tor9>();
47 private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
48 private Set<String> filer8only = new HashSet<String>();
49
50 private String r8only = "EfiLibInstallDriverBinding " +
51 "EfiLibInstallAllDriverProtocols " +
52 "EfiLibCompareLanguage " +
53 "BufToHexString " +
54 "EfiStrTrim " +
55 "EfiValueToHexStr " +
56 "HexStringToBuf " +
57 "IsHexDigit " +
58 "NibbleToHexChar " +
59 "GetHob " +
60 "GetHobListSize " +
61 "GetHobVersion " +
62 "GetHobBootMode " +
63 "GetCpuHobInfo " +
64 "GetDxeCoreHobInfo " +
65 "GetNextFirmwareVolumeHob " +
66 "GetNextGuidHob " +
67 "GetPalEntryHobInfo " +
68 "GetIoPortSpaceAddressHobInfo ";
69
70 public void flush() throws Exception {
71 PrintWriter outfile;
72 String temp = null;
73 if (ui.yesOrNo("Change Source Code is to be doing . See details ?")) {
74 showdetails = true;
75 }
76 File tempdir = new File(modulepath + File.separator + "result" + File.separator);
77 if (!tempdir.exists()) tempdir.mkdir();
78 String[] list = new File(modulepath + File.separator + "temp").list(); //what I change is the non-local .h commented-out files
79 for (int i = 0 ; i < list.length ; i++) {
80 if (list[i].contains(".c")) {
81 ui.println("\nModifying file : " + list[i]);
82 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + list[i])));
83 outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + list[i]));
84 outfile.flush();
85 outfile.close();
86 } else {
87 if (list[i].contains(".h")) {
88 temp = list[i];
89 } else if (list[i].contains(".C")) {
90 temp = list[i].replaceFirst(".C", ".c");
91 } else if (list[i].contains(".H")) {
92 temp = list[i].replaceFirst(".H", ".h");
93 } else {
94 continue;
95 }
96 ui.println("\nCopying file : " + temp);
97 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + temp)));
98 outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + list[i]));
99 outfile.flush();
100 outfile.close();
101 }
102 }
103
104 if (!mi.hashr8only.isEmpty()) {
105 addr8only();
106 }
107 }
108
109 private void addr8only() throws Exception {
110 String paragraph = null;
111 String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");
112 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));
113 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));
114 //outfile1.append("#include \"R8Lib.h\"\n\n");
115 //outfile2.append("#include \"R8Lib.h\"\n\n");
116 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);
117 Matcher mtrr8only = ptnr8only.matcher(line);
118 Matcher mtrr8onlyhead;
119 while (mtrr8only.find()) {
120 if (mi.hashr8only.contains(mtrr8only.group(2))) {
121 paragraph = mtrr8only.group();
122 outfile1.append(paragraph + "\n\n");
123 if (mtrr8only.group(1).length() != 0) {
124 mi.hashrequiredr9libs.add(mtrr8only.group(1));
125 }
126 //generate R8lib.h
127 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {
128 paragraph = mtrr8onlyhead.replaceAll(";");
129 }
130 outfile2.append(paragraph + "\n\n");
131 }
132 }
133 outfile1.flush();
134 outfile1.close();
135 outfile2.flush();
136 outfile2.close();
137 }
138
139 private String sourcefiletostring(String filename) throws Exception {
140 BufferedReader rd = new BufferedReader(new FileReader(filename));
141 StringBuffer wholefile = new StringBuffer();
142 String line;
143 while ((line = rd.readLine()) != null) {
144 wholefile.append(line + "\n");
145 }
146 return wholefile.toString();
147 }
148
149 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
150 private String sourcefilereplace(String filename) throws Exception {
151 BufferedReader rd = new BufferedReader(new FileReader(filename));
152 StringBuffer wholefile = new StringBuffer();
153 String line;
154 String r8thing;
155 String r9thing;
156 r8tor9 temp;
157 boolean addr8 = false;
158
159 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
160 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
161
162 while ((line = rd.readLine()) != null) {
163 wholefile.append(line + "\n");
164 }
165 line = wholefile.toString();
166
167 // replace BS -> gBS , RT -> gRT
168 Matcher mat = pat.matcher(line);
169 if (mat.find()) { // add a library here
170 ui.println("Converting all BS->gBS,RT->gRT");
171 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness
172 }
173 mat.reset();
174 while (mat.find()) {
175 if (mat.group(1).matches("BS")) {
176 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
177 }
178 if (mat.group(1).matches("RT")) {
179 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
180 }
181 }
182 /*
183 // remove EFI_DRIVER_ENTRY_POINT
184 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
185 Matcher matentrypoint = patentrypoint.matcher(line);
186 if (matentrypoint.find()) {
187 ui.println("Deleting Entry_Point");
188 line = matentrypoint.replaceAll("");
189 }
190 */
191 // start replacing names
192 Iterator<String> it;
193 // Converting non-locla function
194 it = mi.hashnonlocalfunc.iterator();
195 while (it.hasNext()) {
196 r8thing = it.next();
197 if (r8thing.matches("EfiInitializeDriverLib")) { //s
198 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
199 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
200 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c
201 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
202 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
203 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
204 } else { //
205 mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here
206 }
207
208 if ((r9thing = db.getR9Func(r8thing)) != null) {
209 if (!r8thing.equals(r9thing)) {
210 if (line.contains(r8thing)) {
211 line = line.replaceAll(r8thing, r9thing);
212 filefunc.add(new r8tor9(r8thing, r9thing));
213 Iterator<r8tor9> rt = filefunc.iterator();
214 while (rt.hasNext()) {
215 temp = rt.next();
216 if (r8only.contains(temp.r8thing)) {
217 mi.localmodulesources.add("R8Lib.h");
218 mi.localmodulesources.add("R8Lib.c");
219 mi.localmoduleheaders.add("R8Lib.h");
220 filer8only.add(r8thing);
221 mi.hashr8only.add(r8thing);
222 addr8 = true;
223 }
224 }
225 }
226 }
227 }
228 } //is any of the guids changed?
229 if (addr8 == true) {
230 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
231 }
232
233 // Converting macro
234 it = mi.hashnonlocalmacro.iterator();
235 while (it.hasNext()) { //macros are all assumed MdePkg currently
236 r8thing = it.next();
237 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));
238 if ((r9thing = db.getR9Macro(r8thing)) != null) {
239 if (line.contains(r8thing)) {
240 line = line.replaceAll(r8thing, r9thing);
241 filemacro.add(new r8tor9(r8thing, r9thing));
242 }
243 }
244 }
245
246 // Converting guid
247 replaceGuid(line, mi.guid, "guid", fileguid);
248 replaceGuid(line, mi.ppi, "ppi", fileppi);
249 replaceGuid(line, mi.protocol, "protocol", fileprotocol);
250
251 // Converting Pei
252 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
253 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
254 if (mi.moduletype.contains("PEIM")) {
255 Matcher mtrpei = ptnpei.matcher(line);
256 while (mtrpei.find()) { // ! add a library here !
257 line = mtrpei.replaceAll("PeiServices$1#%$2");
258 mi.hashrequiredr9libs.add("PeiServicesLib");
259 }
260 mtrpei.reset();
261 if (line.contains("PeiServicesCopyMem")) {
262 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");
263 mi.hashrequiredr9libs.add("BaseMemoryLib");
264 }
265 if (line.contains("PeiServicesSetMem")) {
266 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");
267 mi.hashrequiredr9libs.add("BaseMemoryLib");
268 }
269
270 // Second , find all #% to drop the arg "PeiServices"
271 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
272 Matcher mtrpeiarg = ptnpeiarg.matcher(line);
273 while (mtrpeiarg.find()) {
274 line = mtrpeiarg.replaceAll("$1");
275 }
276 }
277
278 Matcher mtrmac;
279 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);
280 if (mtrmac.find()) {
281 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
282 }
283 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);
284 if (mtrmac.find()) {
285 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
286 }
287 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);
288 if (mtrmac.find()) {
289 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
290 }
291 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);
292 if (mtrmac.find()) {
293 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
294 }
295 if (line.contains("EFI_UINTN_ALIGN_MASK")) {
296 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
297 }
298
299 show(filefunc, "function");
300 show(filemacro, "macro");
301 show(fileguid, "guid");
302 show(fileppi, "ppi");
303 show(fileprotocol, "protocol");
304 if (!filer8only.isEmpty()) {
305 ui.println("Converting r8only : " + filer8only);
306 }
307
308 filefunc.clear();
309 filemacro.clear();
310 fileguid.clear();
311 fileppi.clear();
312 fileprotocol.clear();
313 filer8only.clear();
314
315 return line;
316 }
317
318 private void show(Set<r8tor9> hash, String sh) {
319 Iterator<r8tor9> it = hash.iterator();
320 r8tor9 temp;
321 if (showdetails) {
322 if (!hash.isEmpty()) {
323 ui.print("Converting " + sh + " : ");
324 while (it.hasNext()) {
325 temp = it.next();
326 ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");
327 }
328 ui.println("");
329 }
330 }
331 }
332
333 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {
334 Iterator<String> it;
335 String r8thing;
336 String r9thing;
337 it = hash.iterator();
338 while (it.hasNext()) {
339 r8thing = it.next();
340 if ((r9thing = db.getR9Guidname(r8thing)) != null) {
341 if (!r8thing.equals(r9thing)) {
342 if (line.contains(r8thing)) {
343 line = line.replaceAll(r8thing, r9thing);
344 filehash.add(new r8tor9(r8thing, r9thing));
345 }
346 }
347 }
348 }
349 }
350 }