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