]>
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 showdetails
= true; // set this as default now, may be changed in the future
50 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
51 while (di
.hasNext()) {
53 if (inname
.contains(".c") || inname
.contains(".C")) {
54 if (inname
.contains(".C")) {
55 outname
= inname
.replaceFirst(".C", ".c");
59 MigrationTool
.ui
.println("\nModifying file: " + inname
);
60 Common
.string2file(sourcefilereplace(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
61 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || inname
.contains(".uni")) {
62 if (inname
.contains(".H")) {
63 outname
= inname
.replaceFirst(".H", ".h");
67 MigrationTool
.ui
.println("\nCopying file: " + inname
);
68 Common
.string2file(Common
.file2string(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
), MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ outname
);
72 if (!mi
.hashr8only
.isEmpty()) {
77 private static final void addr8only() throws Exception
{
78 String paragraph
= null;
79 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
80 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
81 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
82 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
83 Matcher mtrr8only
= ptnr8only
.matcher(line
);
84 Matcher mtrr8onlyhead
;
85 while (mtrr8only
.find()) {
86 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
87 paragraph
= mtrr8only
.group();
88 outfile1
.append(paragraph
+ "\n\n");
89 if (mtrr8only
.group(1).length() != 0) {
90 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
93 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
94 paragraph
= mtrr8onlyhead
.replaceAll(";");
96 outfile2
.append(paragraph
+ "\n\n");
104 mi
.localmodulesources
.add("R8Lib.h");
105 mi
.localmodulesources
.add("R8Lib.c");
108 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
109 private static final String
sourcefilereplace(String filename
) throws Exception
{
110 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
111 StringBuffer wholefile
= new StringBuffer();
113 boolean addr8
= false;
115 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
116 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
118 while ((line
= rd
.readLine()) != null) {
119 wholefile
.append(line
+ "\n");
121 line
= wholefile
.toString();
123 // replace BS -> gBS , RT -> gRT
124 Matcher mat
= pat
.matcher(line
);
125 if (mat
.find()) { // add a library here
126 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
127 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
131 if (mat
.group(1).matches("BS")) {
132 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
134 if (mat
.group(1).matches("RT")) {
135 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
139 // remove EFI_DRIVER_ENTRY_POINT
140 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
141 Matcher matentrypoint = patentrypoint.matcher(line);
142 if (matentrypoint.find()) {
143 MigrationTool.ui.println("Deleting Entry_Point");
144 line = matentrypoint.replaceAll("");
147 // start replacing names
151 // Converting non-locla function
152 it
= mi
.hashnonlocalfunc
.iterator();
153 while (it
.hasNext()) {
155 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
156 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
157 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
158 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
159 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
160 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
161 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
163 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
167 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
168 if (!r8thing
.equals(r9thing
)) {
169 if (line
.contains(r8thing
)) {
170 line
= line
.replaceAll(r8thing
, r9thing
);
171 filefunc
.add(new r8tor9(r8thing
, r9thing
));
172 Iterator
<r8tor9
> rt
= filefunc
.iterator();
173 while (rt
.hasNext()) {
175 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
176 filer8only
.add(r8thing
);
177 mi
.hashr8only
.add(r8thing
);
184 } //is any of the guids changed?
186 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
190 it
= mi
.hashnonlocalmacro
.iterator();
191 while (it
.hasNext()) { //macros are all assumed MdePkg currently
193 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
194 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
195 if (line
.contains(r8thing
)) {
196 line
= line
.replaceAll(r8thing
, r9thing
);
197 filemacro
.add(new r8tor9(r8thing
, r9thing
));
203 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
204 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
205 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
208 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
209 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
210 if (mi
.moduletype
.contains("PEIM")) {
211 Matcher mtrpei
= ptnpei
.matcher(line
);
212 while (mtrpei
.find()) { // ! add a library here !
213 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
214 mi
.hashrequiredr9libs
.add("PeiServicesLib");
217 if (line
.contains("PeiServicesCopyMem")) {
218 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
219 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
221 if (line
.contains("PeiServicesSetMem")) {
222 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
223 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
226 // Second , find all #% to drop the arg "PeiServices"
227 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
228 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
229 while (mtrpeiarg
.find()) {
230 line
= mtrpeiarg
.replaceAll("$1");
235 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
237 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
239 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
241 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
243 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
245 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
247 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
249 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
251 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
252 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
255 show(filefunc
, "function");
256 show(filemacro
, "macro");
257 show(fileguid
, "guid");
258 show(fileppi
, "ppi");
259 show(fileprotocol
, "protocol");
260 if (!filer8only
.isEmpty()) {
261 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
268 fileprotocol
.clear();
274 private static final void show(Set
<r8tor9
> hash
, String sh
) {
275 Iterator
<r8tor9
> it
= hash
.iterator();
278 if (!hash
.isEmpty()) {
279 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
280 while (it
.hasNext()) {
282 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
284 MigrationTool
.ui
.println("");
289 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
293 it
= hash
.iterator();
294 while (it
.hasNext()) {
296 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
297 if (!r8thing
.equals(r9thing
)) {
298 if (line
.contains(r8thing
)) {
299 line
= line
.replaceAll(r8thing
, r9thing
);
300 filehash
.add(new r8tor9(r8thing
, r9thing
));