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