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