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