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