]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
3238e29df9397241f96a0f8dcaa762b0470e2e6d
[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 final Set<Common.Laplace> Laplaces = new HashSet<Common.Laplace>();
24
25 // these sets are used only for printing log of the changes in current file
26 private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();
27 private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();
28 private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();
29 private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();
30 private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
31 private static final Set<String> filer8only = new HashSet<String>();
32
33 private static final String[] specialhoblibfunc = {
34 "BuildModuleHob",
35 "BuildResourceDescriptorHob",
36 "BuildFvHob",
37 "BuildCpuHob",
38 "BuildStackHob",
39 "BuildBspStoreHob",
40 "BuildMemoryAllocationHob"
41 };
42
43 //---------------------------------------inner classes---------------------------------------//
44 private static class r8tor9 {
45 r8tor9(String r8, String r9) {
46 r8thing = r8;
47 r9thing = r9;
48 }
49 public String r8thing;
50 public String r9thing;
51 }
52
53 private class IdleLaplace extends Common.Laplace {
54 public String operation(String wholeline) {
55 return wholeline;
56 }
57
58 public boolean recognize(String filename) {
59 return filename.contains(".h") || filename.contains(".H") || filename.contains(".uni");
60 }
61
62 public String namechange(String oldname) {
63 if (oldname.contains(".H")) {
64 return oldname.replaceFirst(".H", ".h");
65 } else {
66 return oldname;
67 }
68 }
69 }
70 private class DxsLaplace extends Common.Laplace {
71 public String operation(String wholeline) {
72 if (mi.getModuleType().equals("PEIM")) {
73 return addincludefile(wholeline, "\\<PeimDepex.h\\>");
74 } else {
75 return addincludefile(wholeline, "\\<DxeDepex.h\\>");
76 }
77 }
78
79 public boolean recognize(String filename) {
80 return filename.contains(".dxs");
81 }
82
83 public String namechange(String oldname) {
84 return oldname;
85 }
86 }
87
88 private class CLaplace extends Common.Laplace {
89 public String operation(String wholeline) {
90 boolean addr8 = false;
91
92 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
93 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
94
95 // replace BS -> gBS , RT -> gRT
96 Matcher mat = pat.matcher(wholeline);
97 if (mat.find()) { // add a library here
98 MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");
99 wholeline = mat.replaceAll("g$1$2$3"); //unknown correctiveness
100 }
101 mat.reset();
102 while (mat.find()) {
103 if (mat.group(1).matches("BS")) {
104 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
105 }
106 if (mat.group(1).matches("RT")) {
107 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
108 }
109 }
110 // remove EFI_DRIVER_ENTRY_POINT
111 wholeline = wholeline.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool.MIGRATIONCOMMENT + " $1");
112
113 // start replacing names
114 String r8thing;
115 String r9thing;
116 Iterator<String> it;
117 // Converting non-locla function
118 it = mi.hashnonlocalfunc.iterator();
119 while (it.hasNext()) {
120 r8thing = it.next();
121 if (r8thing.matches("EfiInitializeDriverLib")) { //s
122 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
123 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
124 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c
125 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
126 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
127 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
128 } else { //
129 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here
130 }
131
132 r8tor9 temp;
133 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {
134 if (!r8thing.equals(r9thing)) {
135 if (wholeline.contains(r8thing)) {
136 wholeline = wholeline.replaceAll(r8thing, r9thing);
137 filefunc.add(new r8tor9(r8thing, r9thing));
138 Iterator<r8tor9> rt = filefunc.iterator();
139 while (rt.hasNext()) {
140 temp = rt.next();
141 if (MigrationTool.db.r8only.contains(temp.r8thing)) {
142 filer8only.add(r8thing);
143 mi.hashr8only.add(r8thing);
144 addr8 = true;
145 }
146 }
147 }
148 }
149 }
150 } //is any of the guids changed?
151 if (addr8 == true) {
152 wholeline = addincludefile(wholeline, "\"R8Lib.h\"");
153 }
154
155 // Converting macro
156 it = mi.hashnonlocalmacro.iterator();
157 while (it.hasNext()) { //macros are all assumed MdePkg currently
158 r8thing = it.next();
159 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
160 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {
161 if (wholeline.contains(r8thing)) {
162 wholeline = wholeline.replaceAll(r8thing, r9thing);
163 filemacro.add(new r8tor9(r8thing, r9thing));
164 }
165 }
166 }
167
168 // Converting guid
169 replaceGuid(wholeline, mi.guid, "guid", fileguid);
170 replaceGuid(wholeline, mi.ppi, "ppi", fileppi);
171 replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);
172
173 // Converting Pei
174 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
175 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
176 if (mi.getModuleType().matches("PEIM")) {
177 //if (mi.moduletype.contains("PEIM")) {
178 Matcher mtrpei = ptnpei.matcher(wholeline);
179 while (mtrpei.find()) { // ! add a library here !
180 wholeline = mtrpei.replaceAll("PeiServices$1#%$2");
181 mi.hashrequiredr9libs.add("PeiServicesLib");
182 }
183 mtrpei.reset();
184 if (wholeline.contains("PeiServicesCopyMem")) {
185 wholeline = wholeline.replaceAll("PeiServicesCopyMem#%", "CopyMem");
186 mi.hashrequiredr9libs.add("BaseMemoryLib");
187 }
188 if (wholeline.contains("PeiServicesSetMem")) {
189 wholeline = wholeline.replaceAll("PeiServicesSetMem#%", "SetMem");
190 mi.hashrequiredr9libs.add("BaseMemoryLib");
191 }
192
193 // Second , find all #% to drop the arg "PeiServices"
194 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
195 Matcher mtrpeiarg = ptnpeiarg.matcher(wholeline);
196 while (mtrpeiarg.find()) {
197 wholeline = mtrpeiarg.replaceAll("$1");
198 }
199 }
200
201 wholeline = hobLibFuncDropStatus(wholeline);
202
203 Matcher mtrmac;
204 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);
205 if (mtrmac.find()) {
206 wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
207 }
208 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);
209 if (mtrmac.find()) {
210 wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
211 }
212 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);
213 if (mtrmac.find()) {
214 wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
215 }
216 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);
217 if (mtrmac.find()) {
218 wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
219 }
220 if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {
221 wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
222 }
223
224 show(filefunc, "function");
225 show(filemacro, "macro");
226 show(fileguid, "guid");
227 show(fileppi, "ppi");
228 show(fileprotocol, "protocol");
229 if (!filer8only.isEmpty()) {
230 MigrationTool.ui.println("Converting r8only : " + filer8only);
231 }
232
233 filefunc.clear();
234 filemacro.clear();
235 fileguid.clear();
236 fileppi.clear();
237 fileprotocol.clear();
238 filer8only.clear();
239
240 return wholeline;
241 }
242
243 public boolean recognize(String filename) {
244 return filename.contains(".c") || filename.contains(".C");
245 }
246
247 public String namechange(String oldname) {
248 if (oldname.contains(".C")) {
249 return oldname.replaceFirst(".C", ".c");
250 } else {
251 return oldname;
252 }
253 }
254 }
255 //---------------------------------------inner classes---------------------------------------//
256
257 //-------------------------------------process functions-------------------------------------//
258 private static final String addincludefile(String wholeline, String hfile) {
259 return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");
260 }
261
262 private static final void show(Set<r8tor9> hash, String sh) {
263 Iterator<r8tor9> it = hash.iterator();
264 r8tor9 temp;
265 if (!hash.isEmpty()) {
266 MigrationTool.ui.print("Converting " + sh + " : ");
267 while (it.hasNext()) {
268 temp = it.next();
269 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");
270 }
271 MigrationTool.ui.println("");
272 }
273 }
274
275 private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {
276 Iterator<String> it;
277 String r8thing;
278 String r9thing;
279 it = hash.iterator();
280 while (it.hasNext()) {
281 r8thing = it.next();
282 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {
283 if (!r8thing.equals(r9thing)) {
284 if (line.contains(r8thing)) {
285 line = line.replaceAll(r8thing, r9thing);
286 filehash.add(new r8tor9(r8thing, r9thing));
287 }
288 }
289 }
290 }
291 }
292
293 private final String hobLibFuncDropStatus(String wholeline) { // or use regex to find pattern "Status = ..."
294 Pattern ptnhobstatus;
295 Matcher mtrhobstatus;
296 String templine = wholeline;
297 for (int i = 0; i < specialhoblibfunc.length; i++) {
298 ptnhobstatus = Pattern.compile("(Status\\s*=\\s*)?" + specialhoblibfunc[i] + "(.*?\\)\\s*;)", Pattern.DOTALL);
299 mtrhobstatus = ptnhobstatus.matcher(templine);
300 if (mtrhobstatus.find()) {
301 templine = mtrhobstatus.replaceAll(specialhoblibfunc[i] + mtrhobstatus.group(2) + "\n //Migration comments: R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");
302 }
303 }
304 return templine;
305 }
306
307 private final void addr8only() throws Exception {
308 String paragraph = null;
309 String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");
310 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));
311 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));
312 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern.DOTALL);
313 Matcher mtrr8only = ptnr8only.matcher(line);
314 Matcher mtrr8onlyhead;
315
316 //add head comment
317 Matcher mtrr8onlyheadcomment = Critic.PTN_NEW_HEAD_COMMENT.matcher(line);
318 if (mtrr8onlyheadcomment.find()) {
319 outfile1.append(mtrr8onlyheadcomment.group() + "\n\n");
320 outfile2.append(mtrr8onlyheadcomment.group() + "\n\n");
321 }
322
323 //add functions body
324 while (mtrr8only.find()) {
325 if (mi.hashr8only.contains(mtrr8only.group(3))) {
326 paragraph = mtrr8only.group(2);
327 outfile1.append(paragraph + "\n\n");
328 if (mtrr8only.group(1).length() != 0) {
329 mi.hashrequiredr9libs.add(mtrr8only.group(1));
330 }
331 //generate R8lib.h
332 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {
333 paragraph = mtrr8onlyhead.replaceAll(";");
334 }
335 outfile2.append(paragraph + "\n\n");
336 }
337 }
338 outfile1.flush();
339 outfile1.close();
340 outfile2.flush();
341 outfile2.close();
342
343 mi.localmodulesources.add("R8Lib.h");
344 mi.localmodulesources.add("R8Lib.c");
345 }
346 //-------------------------------------process functions-------------------------------------//
347
348 //-----------------------------------ForDoAll-----------------------------------//
349 public void run(String filepath) throws Exception {
350 String inname = filepath.replace(mi.modulepath + File.separator, "");
351 String tempinpath = mi.modulepath + File.separator + "temp" + File.separator;
352 String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;
353
354 Iterator<Common.Laplace> itLaplace = Laplaces.iterator();
355 while (itLaplace.hasNext()) {
356 Common.Laplace lap = itLaplace.next();
357 if (lap.recognize(inname)) {
358 MigrationTool.ui.println("\nHandling file: " + inname);
359 lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));
360 }
361 }
362 }
363
364 public boolean filter(File dir) {
365 return true;
366 }
367 //-----------------------------------ForDoAll-----------------------------------//
368
369 private final void setModuleInfo(ModuleInfo moduleinfo) {
370 mi = moduleinfo;
371 }
372
373 private final void start() throws Exception {
374 Laplaces.add(new DxsLaplace());
375 Laplaces.add(new CLaplace());
376 Laplaces.add(new IdleLaplace());
377
378 Common.toDoAll(mi.localmodulesources, this);
379
380 if (!mi.hashr8only.isEmpty()) {
381 addr8only();
382 }
383
384 Laplaces.clear();
385 }
386
387 public static final void fireAt(ModuleInfo moduleinfo) throws Exception {
388 SFReplacer.setModuleInfo(moduleinfo);
389 SFReplacer.start();
390 }
391 }