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