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