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