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