]>
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 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
{
45 String outname
= null;
48 if (ModuleInfo.ui.yesOrNo("Changes will be made to the Source Code. View details?")) {
52 showdetails
= true; // set this as default now, may be changed in the future
54 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
55 while (di
.hasNext()) {
57 if (inname
.contains(".c") || inname
.contains(".C")) {
58 if (inname
.contains(".C")) {
59 outname
= inname
.replaceFirst(".C", ".c");
63 ModuleInfo
.ui
.println("\nModifying file: " + inname
);
64 Common
.string2file(sourcefilereplace(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
65 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || inname
.contains(".uni")) {
66 if (inname
.contains(".H")) {
67 outname
= inname
.replaceFirst(".H", ".h");
71 ModuleInfo
.ui
.println("\nCopying file: " + inname
);
72 Common
.string2file(Common
.file2string(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
76 if (!mi
.hashr8only
.isEmpty()) {
81 private static final void addr8only() throws Exception
{
82 String paragraph
= null;
83 String line
= Common
.file2string(ModuleInfo
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
84 //Common.ensureDir(mi.modulepath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c");
85 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
86 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
87 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
88 Matcher mtrr8only
= ptnr8only
.matcher(line
);
89 Matcher mtrr8onlyhead
;
90 while (mtrr8only
.find()) {
91 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
92 paragraph
= mtrr8only
.group();
93 outfile1
.append(paragraph
+ "\n\n");
94 if (mtrr8only
.group(1).length() != 0) {
95 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
98 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
99 paragraph
= mtrr8onlyhead
.replaceAll(";");
101 outfile2
.append(paragraph
+ "\n\n");
109 mi
.localmodulesources
.add("R8Lib.h");
110 mi
.localmodulesources
.add("R8Lib.c");
113 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
114 private static final String
sourcefilereplace(String filename
) throws Exception
{
115 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
116 StringBuffer wholefile
= new StringBuffer();
121 boolean addr8
= false;
123 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
124 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
126 while ((line
= rd
.readLine()) != null) {
127 wholefile
.append(line
+ "\n");
129 line
= wholefile
.toString();
131 // replace BS -> gBS , RT -> gRT
132 Matcher mat
= pat
.matcher(line
);
133 if (mat
.find()) { // add a library here
134 ModuleInfo
.ui
.println("Converting all BS->gBS, RT->gRT");
135 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
139 if (mat
.group(1).matches("BS")) {
140 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
142 if (mat
.group(1).matches("RT")) {
143 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
147 // remove EFI_DRIVER_ENTRY_POINT
148 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
149 Matcher matentrypoint = patentrypoint.matcher(line);
150 if (matentrypoint.find()) {
151 ModuleInfo.ui.println("Deleting Entry_Point");
152 line = matentrypoint.replaceAll("");
155 // start replacing names
157 // Converting non-locla function
158 it
= mi
.hashnonlocalfunc
.iterator();
159 while (it
.hasNext()) {
161 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
162 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
163 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
164 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
165 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
166 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
167 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
169 mi
.hashrequiredr9libs
.add(ModuleInfo
.db
.getR9Lib(r8thing
)); // add a library here
172 if ((r9thing
= ModuleInfo
.db
.getR9Func(r8thing
)) != null) {
173 if (!r8thing
.equals(r9thing
)) {
174 if (line
.contains(r8thing
)) {
175 line
= line
.replaceAll(r8thing
, r9thing
);
176 filefunc
.add(new r8tor9(r8thing
, r9thing
));
177 Iterator
<r8tor9
> rt
= filefunc
.iterator();
178 while (rt
.hasNext()) {
180 if (ModuleInfo
.db
.r8only
.contains(temp
.r8thing
)) {
181 filer8only
.add(r8thing
);
182 mi
.hashr8only
.add(r8thing
);
189 } //is any of the guids changed?
191 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
195 it
= mi
.hashnonlocalmacro
.iterator();
196 while (it
.hasNext()) { //macros are all assumed MdePkg currently
198 //mi.hashrequiredr9libs.add(ModuleInfo.db.getR9Lib(r8thing));
199 if ((r9thing
= ModuleInfo
.db
.getR9Macro(r8thing
)) != null) {
200 if (line
.contains(r8thing
)) {
201 line
= line
.replaceAll(r8thing
, r9thing
);
202 filemacro
.add(new r8tor9(r8thing
, r9thing
));
208 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
209 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
210 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
213 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
214 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
215 if (mi
.moduletype
.contains("PEIM")) {
216 Matcher mtrpei
= ptnpei
.matcher(line
);
217 while (mtrpei
.find()) { // ! add a library here !
218 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
219 mi
.hashrequiredr9libs
.add("PeiServicesLib");
222 if (line
.contains("PeiServicesCopyMem")) {
223 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
224 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
226 if (line
.contains("PeiServicesSetMem")) {
227 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
228 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
231 // Second , find all #% to drop the arg "PeiServices"
232 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
233 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
234 while (mtrpeiarg
.find()) {
235 line
= mtrpeiarg
.replaceAll("$1");
240 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
242 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
244 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
246 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
248 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
250 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
252 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
254 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
256 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
257 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
260 show(filefunc
, "function");
261 show(filemacro
, "macro");
262 show(fileguid
, "guid");
263 show(fileppi
, "ppi");
264 show(fileprotocol
, "protocol");
265 if (!filer8only
.isEmpty()) {
266 ModuleInfo
.ui
.println("Converting r8only : " + filer8only
);
273 fileprotocol
.clear();
279 private static final void show(Set
<r8tor9
> hash
, String sh
) {
280 Iterator
<r8tor9
> it
= hash
.iterator();
283 if (!hash
.isEmpty()) {
284 ModuleInfo
.ui
.print("Converting " + sh
+ " : ");
285 while (it
.hasNext()) {
287 ModuleInfo
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
289 ModuleInfo
.ui
.println("");
294 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
298 it
= hash
.iterator();
299 while (it
.hasNext()) {
301 if ((r9thing
= ModuleInfo
.db
.getR9Guidname(r8thing
)) != null) {
302 if (!r8thing
.equals(r9thing
)) {
303 if (line
.contains(r8thing
)) {
304 line
= line
.replaceAll(r8thing
, r9thing
);
305 filehash
.add(new r8tor9(r8thing
, r9thing
));