]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
50606a716004c7c7dc99614b6d729becb8c76a77
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
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.
13 package org
.tianocore
.migration
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 public final class SourceFileReplacer
{
21 private static ModuleInfo mi
;
22 private static boolean showdetails
= false;
24 private static class r8tor9
{
25 r8tor9(String r8
, String r9
) {
29 public String r8thing
;
30 public String r9thing
;
33 // these sets are used only for printing log of the changes in current file
34 private static final Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
35 private static final Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
36 private static final Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
37 private static final Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
38 private static final Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
39 private static final Set
<String
> filer8only
= new HashSet
<String
>();
41 public static final void flush(ModuleInfo moduleinfo
) throws Exception
{
44 String outname
= null;
47 showdetails
= true; // set this as default now, may be changed in the future
49 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
50 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
51 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
52 while (di
.hasNext()) {
54 if (inname
.contains(".c") || inname
.contains(".C")) {
55 if (inname
.contains(".C")) {
56 outname
= inname
.replaceFirst(".C", ".c");
60 MigrationTool
.ui
.println("\nModifying file: " + inname
);
61 Common
.string2file(sourcefilereplace(Common
.file2string(tempinpath
+ inname
)), tempoutpath
+ outname
);
62 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".uni")) {
63 if (inname
.contains(".H")) {
64 outname
= inname
.replaceFirst(".H", ".h");
68 MigrationTool
.ui
.println("\nCopying file: " + inname
);
69 Common
.string2file(Common
.file2string(tempinpath
+ inname
), tempoutpath
+ outname
);
70 } else if (inname
.contains(".dxs")) {
72 MigrationTool
.ui
.println("\nModifying file: " + inname
);
73 Common
.string2file(convertdxs(Common
.file2string(tempinpath
+ inname
)), tempoutpath
+ outname
);
77 if (!mi
.hashr8only
.isEmpty()) {
82 private static final String
addincludefile(String wholeline
, String hfile
) {
83 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
+ "\n");
86 private static final String
convertdxs(String wholeline
) {
87 if (mi
.getModuleType().equals("PEIM")) {
88 return addincludefile(wholeline
, "\\<PeimDepex.h\\>");
90 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
94 private static final void addr8only() throws Exception
{
95 String paragraph
= null;
96 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
97 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
98 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
99 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
100 Matcher mtrr8only
= ptnr8only
.matcher(line
);
101 Matcher mtrr8onlyhead
;
102 while (mtrr8only
.find()) {
103 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
104 paragraph
= mtrr8only
.group();
105 outfile1
.append(paragraph
+ "\n\n");
106 if (mtrr8only
.group(1).length() != 0) {
107 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
110 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
111 paragraph
= mtrr8onlyhead
.replaceAll(";");
113 outfile2
.append(paragraph
+ "\n\n");
121 mi
.localmodulesources
.add("R8Lib.h");
122 mi
.localmodulesources
.add("R8Lib.c");
125 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
126 private static final String
sourcefilereplace(String wholeline
) throws Exception
{
127 boolean addr8
= false;
129 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
130 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
132 // replace BS -> gBS , RT -> gRT
133 Matcher mat
= pat
.matcher(wholeline
);
134 if (mat
.find()) { // add a library here
135 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
136 wholeline
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
140 if (mat
.group(1).matches("BS")) {
141 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
143 if (mat
.group(1).matches("RT")) {
144 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
148 // remove EFI_DRIVER_ENTRY_POINT
149 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
150 Matcher matentrypoint = patentrypoint.matcher(line);
151 if (matentrypoint.find()) {
152 MigrationTool.ui.println("Deleting Entry_Point");
153 line = matentrypoint.replaceAll("");
156 // start replacing names
160 // Converting non-locla function
161 it
= mi
.hashnonlocalfunc
.iterator();
162 while (it
.hasNext()) {
164 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
165 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
166 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
167 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
168 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
169 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
170 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
172 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
176 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
177 if (!r8thing
.equals(r9thing
)) {
178 if (wholeline
.contains(r8thing
)) {
179 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
180 filefunc
.add(new r8tor9(r8thing
, r9thing
));
181 Iterator
<r8tor9
> rt
= filefunc
.iterator();
182 while (rt
.hasNext()) {
184 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
185 filer8only
.add(r8thing
);
186 mi
.hashr8only
.add(r8thing
);
193 } //is any of the guids changed?
195 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
199 it
= mi
.hashnonlocalmacro
.iterator();
200 while (it
.hasNext()) { //macros are all assumed MdePkg currently
202 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
203 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
204 if (wholeline
.contains(r8thing
)) {
205 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
206 filemacro
.add(new r8tor9(r8thing
, r9thing
));
212 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
213 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
214 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
217 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
218 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
219 if (mi
.moduletype
.contains("PEIM")) {
220 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
221 while (mtrpei
.find()) { // ! add a library here !
222 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
223 mi
.hashrequiredr9libs
.add("PeiServicesLib");
226 if (wholeline
.contains("PeiServicesCopyMem")) {
227 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
228 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
230 if (wholeline
.contains("PeiServicesSetMem")) {
231 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
232 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
235 // Second , find all #% to drop the arg "PeiServices"
236 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
237 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
238 while (mtrpeiarg
.find()) {
239 wholeline
= mtrpeiarg
.replaceAll("$1");
244 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
246 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
248 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
250 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
252 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
254 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
256 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
258 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
260 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
261 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
264 show(filefunc
, "function");
265 show(filemacro
, "macro");
266 show(fileguid
, "guid");
267 show(fileppi
, "ppi");
268 show(fileprotocol
, "protocol");
269 if (!filer8only
.isEmpty()) {
270 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
277 fileprotocol
.clear();
283 private static final void show(Set
<r8tor9
> hash
, String sh
) {
284 Iterator
<r8tor9
> it
= hash
.iterator();
287 if (!hash
.isEmpty()) {
288 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
289 while (it
.hasNext()) {
291 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
293 MigrationTool
.ui
.println("");
298 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
302 it
= hash
.iterator();
303 while (it
.hasNext()) {
305 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
306 if (!r8thing
.equals(r9thing
)) {
307 if (line
.contains(r8thing
)) {
308 line
= line
.replaceAll(r8thing
, r9thing
);
309 filehash
.add(new r8tor9(r8thing
, r9thing
));