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