]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
868509a88b411da0e972907f46b95d2baeaad0c2
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;
47 if (MigrationTool
.ui
.yesOrNo("Changes will be made to the Source Code. View details?")) {
51 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
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(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
62 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || 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(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
73 if (!mi
.hashr8only
.isEmpty()) {
78 private static final void addr8only() throws Exception
{
79 String paragraph
= null;
80 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
81 Common
.ensureDir(mi
.modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.c");
82 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
83 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(mi
.outputpath
+ File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
84 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
85 Matcher mtrr8only
= ptnr8only
.matcher(line
);
86 Matcher mtrr8onlyhead
;
87 while (mtrr8only
.find()) {
88 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
89 paragraph
= mtrr8only
.group();
90 outfile1
.append(paragraph
+ "\n\n");
91 if (mtrr8only
.group(1).length() != 0) {
92 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
95 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
96 paragraph
= mtrr8onlyhead
.replaceAll(";");
98 outfile2
.append(paragraph
+ "\n\n");
106 mi
.localmodulesources
.add("R8Lib.h");
107 mi
.localmodulesources
.add("R8Lib.c");
110 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
111 private static final String
sourcefilereplace(String filename
) throws Exception
{
112 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
113 StringBuffer wholefile
= new StringBuffer();
118 boolean addr8
= false;
120 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
121 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
123 while ((line
= rd
.readLine()) != null) {
124 wholefile
.append(line
+ "\n");
126 line
= wholefile
.toString();
128 // replace BS -> gBS , RT -> gRT
129 Matcher mat
= pat
.matcher(line
);
130 if (mat
.find()) { // add a library here
131 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
132 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
136 if (mat
.group(1).matches("BS")) {
137 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
139 if (mat
.group(1).matches("RT")) {
140 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
144 // remove EFI_DRIVER_ENTRY_POINT
145 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
146 Matcher matentrypoint = patentrypoint.matcher(line);
147 if (matentrypoint.find()) {
148 MigrationTool.ui.println("Deleting Entry_Point");
149 line = matentrypoint.replaceAll("");
152 // start replacing names
154 // Converting non-locla function
155 it
= mi
.hashnonlocalfunc
.iterator();
156 while (it
.hasNext()) {
158 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
159 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
160 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
161 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
162 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
163 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
164 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
166 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
169 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
170 if (!r8thing
.equals(r9thing
)) {
171 if (line
.contains(r8thing
)) {
172 line
= line
.replaceAll(r8thing
, r9thing
);
173 filefunc
.add(new r8tor9(r8thing
, r9thing
));
174 Iterator
<r8tor9
> rt
= filefunc
.iterator();
175 while (rt
.hasNext()) {
177 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
178 filer8only
.add(r8thing
);
179 mi
.hashr8only
.add(r8thing
);
186 } //is any of the guids changed?
188 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
192 it
= mi
.hashnonlocalmacro
.iterator();
193 while (it
.hasNext()) { //macros are all assumed MdePkg currently
195 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
196 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
197 if (line
.contains(r8thing
)) {
198 line
= line
.replaceAll(r8thing
, r9thing
);
199 filemacro
.add(new r8tor9(r8thing
, r9thing
));
205 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
206 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
207 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
210 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
211 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
212 if (mi
.moduletype
.contains("PEIM")) {
213 Matcher mtrpei
= ptnpei
.matcher(line
);
214 while (mtrpei
.find()) { // ! add a library here !
215 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
216 mi
.hashrequiredr9libs
.add("PeiServicesLib");
219 if (line
.contains("PeiServicesCopyMem")) {
220 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
221 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
223 if (line
.contains("PeiServicesSetMem")) {
224 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
225 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
228 // Second , find all #% to drop the arg "PeiServices"
229 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
230 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
231 while (mtrpeiarg
.find()) {
232 line
= mtrpeiarg
.replaceAll("$1");
237 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
239 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
241 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
243 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
245 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
247 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
249 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
251 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
253 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
254 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
257 show(filefunc
, "function");
258 show(filemacro
, "macro");
259 show(fileguid
, "guid");
260 show(fileppi
, "ppi");
261 show(fileprotocol
, "protocol");
262 if (!filer8only
.isEmpty()) {
263 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
270 fileprotocol
.clear();
276 private static final void show(Set
<r8tor9
> hash
, String sh
) {
277 Iterator
<r8tor9
> it
= hash
.iterator();
280 if (!hash
.isEmpty()) {
281 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
282 while (it
.hasNext()) {
284 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
286 MigrationTool
.ui
.println("");
291 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
295 it
= hash
.iterator();
296 while (it
.hasNext()) {
298 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
299 if (!r8thing
.equals(r9thing
)) {
300 if (line
.contains(r8thing
)) {
301 line
= line
.replaceAll(r8thing
, r9thing
);
302 filehash
.add(new r8tor9(r8thing
, r9thing
));