]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
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 SourceFileReplacer(String path
, String outpath
, ModuleInfo moduleinfo
) {
26 private String modulepath
;
27 private String outputpath
;
28 private ModuleInfo mi
;
29 private boolean showdetails
= false;
31 private class r8tor9
{
32 r8tor9(String r8
, String r9
) {
36 public String r8thing
;
37 public String r9thing
;
40 // these sets are used only for printing log of the changes in current file
41 private Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
42 private Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
43 private Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
44 private Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
45 private Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
46 private Set
<String
> filer8only
= new HashSet
<String
>();
48 private static final String r8only
= "EfiLibInstallDriverBinding " +
49 "EfiLibInstallAllDriverProtocols " +
50 "EfiLibCompareLanguage " +
52 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!
62 "GetDxeCoreHobInfo " +
63 "GetNextFirmwareVolumeHob " +
65 "GetPalEntryHobInfo " +
66 "GetIoPortSpaceAddressHobInfo ";
68 public void flush() throws Exception
{
69 String outname
= null;
71 if (MigrationTool
.ui
.yesOrNo("Changes will be made to the Source Code. View details?")) {
75 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
76 while (di
.hasNext()) {
78 if (inname
.contains(".c") || inname
.contains(".C")) {
79 if (inname
.contains(".C")) {
80 outname
= inname
.replaceFirst(".C", ".c");
84 MigrationTool
.ui
.println("\nModifying file: " + inname
);
85 Common
.string2file(sourcefilereplace(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
86 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || inname
.contains(".uni")) {
87 if (inname
.contains(".H")) {
88 outname
= inname
.replaceFirst(".H", ".h");
92 MigrationTool
.ui
.println("\nCopying file: " + inname
);
93 Common
.string2file(Common
.file2string(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
97 if (!mi
.hashr8only
.isEmpty()) {
102 private void addr8only() throws Exception
{
103 String paragraph
= null;
104 String line
= Common
.file2string(Database
.defaultpath
+ File
.separator
+ "R8Lib.c");
105 Common
.ensureDir(modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.c");
106 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
107 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
108 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
109 Matcher mtrr8only
= ptnr8only
.matcher(line
);
110 Matcher mtrr8onlyhead
;
111 while (mtrr8only
.find()) {
112 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
113 paragraph
= mtrr8only
.group();
114 outfile1
.append(paragraph
+ "\n\n");
115 if (mtrr8only
.group(1).length() != 0) {
116 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
119 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
120 paragraph
= mtrr8onlyhead
.replaceAll(";");
122 outfile2
.append(paragraph
+ "\n\n");
130 mi
.localmodulesources
.add("R8Lib.h");
131 mi
.localmodulesources
.add("R8Lib.c");
134 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
135 private String
sourcefilereplace(String filename
) throws Exception
{
136 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
137 StringBuffer wholefile
= new StringBuffer();
142 boolean addr8
= false;
144 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
145 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
147 while ((line
= rd
.readLine()) != null) {
148 wholefile
.append(line
+ "\n");
150 line
= wholefile
.toString();
152 // replace BS -> gBS , RT -> gRT
153 Matcher mat
= pat
.matcher(line
);
154 if (mat
.find()) { // add a library here
155 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
156 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
160 if (mat
.group(1).matches("BS")) {
161 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
163 if (mat
.group(1).matches("RT")) {
164 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
168 // remove EFI_DRIVER_ENTRY_POINT
169 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
170 Matcher matentrypoint = patentrypoint.matcher(line);
171 if (matentrypoint.find()) {
172 MigrationTool.ui.println("Deleting Entry_Point");
173 line = matentrypoint.replaceAll("");
176 // start replacing names
178 // Converting non-locla function
179 it
= mi
.hashnonlocalfunc
.iterator();
180 while (it
.hasNext()) {
182 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
183 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
184 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
185 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
186 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
187 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
188 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
190 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
193 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
194 if (!r8thing
.equals(r9thing
)) {
195 if (line
.contains(r8thing
)) {
196 line
= line
.replaceAll(r8thing
, r9thing
);
197 filefunc
.add(new r8tor9(r8thing
, r9thing
));
198 Iterator
<r8tor9
> rt
= filefunc
.iterator();
199 while (rt
.hasNext()) {
201 if (r8only
.contains(temp
.r8thing
)) {
202 filer8only
.add(r8thing
);
203 mi
.hashr8only
.add(r8thing
);
210 } //is any of the guids changed?
212 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
216 it
= mi
.hashnonlocalmacro
.iterator();
217 while (it
.hasNext()) { //macros are all assumed MdePkg currently
219 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
220 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
221 if (line
.contains(r8thing
)) {
222 line
= line
.replaceAll(r8thing
, r9thing
);
223 filemacro
.add(new r8tor9(r8thing
, r9thing
));
229 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
230 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
231 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
234 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
235 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
236 if (mi
.moduletype
.contains("PEIM")) {
237 Matcher mtrpei
= ptnpei
.matcher(line
);
238 while (mtrpei
.find()) { // ! add a library here !
239 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
240 mi
.hashrequiredr9libs
.add("PeiServicesLib");
243 if (line
.contains("PeiServicesCopyMem")) {
244 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
245 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
247 if (line
.contains("PeiServicesSetMem")) {
248 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
249 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
252 // Second , find all #% to drop the arg "PeiServices"
253 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
254 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
255 while (mtrpeiarg
.find()) {
256 line
= mtrpeiarg
.replaceAll("$1");
261 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
263 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
265 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
267 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
269 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
271 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
273 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
275 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
277 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
278 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
281 show(filefunc
, "function");
282 show(filemacro
, "macro");
283 show(fileguid
, "guid");
284 show(fileppi
, "ppi");
285 show(fileprotocol
, "protocol");
286 if (!filer8only
.isEmpty()) {
287 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
294 fileprotocol
.clear();
300 private void show(Set
<r8tor9
> hash
, String sh
) {
301 Iterator
<r8tor9
> it
= hash
.iterator();
304 if (!hash
.isEmpty()) {
305 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
306 while (it
.hasNext()) {
308 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
310 MigrationTool
.ui
.println("");
315 private void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
319 it
= hash
.iterator();
320 while (it
.hasNext()) {
322 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
323 if (!r8thing
.equals(r9thing
)) {
324 if (line
.contains(r8thing
)) {
325 line
= line
.replaceAll(r8thing
, r9thing
);
326 filehash
.add(new r8tor9(r8thing
, r9thing
));