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