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