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
implements Common
.ForDoAll
{
21 private static final SourceFileReplacer SFReplacer
= new SourceFileReplacer();
22 private ModuleInfo mi
;
23 private static boolean showdetails
= true; // set this as default now, may be changed in the future
25 private static class r8tor9
{
26 r8tor9(String r8
, String r9
) {
30 public String r8thing
;
31 public String r9thing
;
34 // these sets are used only for printing log of the changes in current file
35 private static final Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
36 private static final Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
37 private static final Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
38 private static final Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
39 private static final Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
40 private static final Set
<String
> filer8only
= new HashSet
<String
>();
42 private static final String
addincludefile(String wholeline
, String hfile
) {
43 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
+ "\n");
46 private final String
convertdxs(String wholeline
) {
47 if (mi
.getModuleType().equals("PEIM")) {
48 return addincludefile(wholeline
, "\\<PeimDepex.h\\>");
50 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
54 private final void addr8only() throws Exception
{
55 String paragraph
= null;
56 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
57 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
58 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
59 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
60 Matcher mtrr8only
= ptnr8only
.matcher(line
);
61 Matcher mtrr8onlyhead
;
62 while (mtrr8only
.find()) {
63 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
64 paragraph
= mtrr8only
.group();
65 outfile1
.append(paragraph
+ "\n\n");
66 if (mtrr8only
.group(1).length() != 0) {
67 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
70 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
71 paragraph
= mtrr8onlyhead
.replaceAll(";");
73 outfile2
.append(paragraph
+ "\n\n");
81 mi
.localmodulesources
.add("R8Lib.h");
82 mi
.localmodulesources
.add("R8Lib.c");
85 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
86 private final String
sourcefilereplace(String wholeline
) throws Exception
{
87 boolean addr8
= false;
89 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
90 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
92 // replace BS -> gBS , RT -> gRT
93 Matcher mat
= pat
.matcher(wholeline
);
94 if (mat
.find()) { // add a library here
95 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
96 wholeline
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
100 if (mat
.group(1).matches("BS")) {
101 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
103 if (mat
.group(1).matches("RT")) {
104 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
108 // remove EFI_DRIVER_ENTRY_POINT
109 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
110 Matcher matentrypoint = patentrypoint.matcher(line);
111 if (matentrypoint.find()) {
112 MigrationTool.ui.println("Deleting Entry_Point");
113 line = matentrypoint.replaceAll("");
116 // start replacing names
120 // Converting non-locla function
121 it
= mi
.hashnonlocalfunc
.iterator();
122 while (it
.hasNext()) {
124 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
125 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
126 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
127 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
128 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
129 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
130 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
132 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
136 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
137 if (!r8thing
.equals(r9thing
)) {
138 if (wholeline
.contains(r8thing
)) {
139 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
140 filefunc
.add(new r8tor9(r8thing
, r9thing
));
141 Iterator
<r8tor9
> rt
= filefunc
.iterator();
142 while (rt
.hasNext()) {
144 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
145 filer8only
.add(r8thing
);
146 mi
.hashr8only
.add(r8thing
);
153 } //is any of the guids changed?
155 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
159 it
= mi
.hashnonlocalmacro
.iterator();
160 while (it
.hasNext()) { //macros are all assumed MdePkg currently
162 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
163 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
164 if (wholeline
.contains(r8thing
)) {
165 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
166 filemacro
.add(new r8tor9(r8thing
, r9thing
));
172 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
173 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
174 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
177 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
178 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
179 if (mi
.moduletype
.contains("PEIM")) {
180 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
181 while (mtrpei
.find()) { // ! add a library here !
182 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
183 mi
.hashrequiredr9libs
.add("PeiServicesLib");
186 if (wholeline
.contains("PeiServicesCopyMem")) {
187 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
188 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
190 if (wholeline
.contains("PeiServicesSetMem")) {
191 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
192 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
195 // Second , find all #% to drop the arg "PeiServices"
196 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
197 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
198 while (mtrpeiarg
.find()) {
199 wholeline
= mtrpeiarg
.replaceAll("$1");
204 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
206 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
208 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
210 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
212 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
214 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
216 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
218 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
220 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
221 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
224 show(filefunc
, "function");
225 show(filemacro
, "macro");
226 show(fileguid
, "guid");
227 show(fileppi
, "ppi");
228 show(fileprotocol
, "protocol");
229 if (!filer8only
.isEmpty()) {
230 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
237 fileprotocol
.clear();
243 private static final void show(Set
<r8tor9
> hash
, String sh
) {
244 Iterator
<r8tor9
> it
= hash
.iterator();
247 if (!hash
.isEmpty()) {
248 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
249 while (it
.hasNext()) {
251 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
253 MigrationTool
.ui
.println("");
258 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
262 it
= hash
.iterator();
263 while (it
.hasNext()) {
265 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
266 if (!r8thing
.equals(r9thing
)) {
267 if (line
.contains(r8thing
)) {
268 line
= line
.replaceAll(r8thing
, r9thing
);
269 filehash
.add(new r8tor9(r8thing
, r9thing
));
276 //-----------------------------------ForDoAll-----------------------------------//
277 public void run(String filepath
) throws Exception
{
278 String outname
= null;
279 String inname
= filepath
.replace(mi
.modulepath
+ File
.separator
, "");
280 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
281 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
283 if (inname
.contains(".c") || inname
.contains(".C")) {
284 if (inname
.contains(".C")) {
285 outname
= inname
.replaceFirst(".C", ".c");
289 MigrationTool
.ui
.println("\nModifying file: " + inname
);
290 Common
.string2file(sourcefilereplace(Common
.file2string(tempinpath
+ inname
)), tempoutpath
+ outname
);
291 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".uni")) {
292 if (inname
.contains(".H")) {
293 outname
= inname
.replaceFirst(".H", ".h");
297 MigrationTool
.ui
.println("\nCopying file: " + inname
);
298 Common
.string2file(Common
.file2string(tempinpath
+ inname
), tempoutpath
+ outname
);
299 } else if (inname
.contains(".dxs")) {
301 MigrationTool
.ui
.println("\nModifying file: " + inname
);
302 Common
.string2file(convertdxs(Common
.file2string(tempinpath
+ inname
)), tempoutpath
+ outname
);
306 public boolean dirFilter(String filepath
) {
310 public boolean fileFilter(String filepath
) {
313 //-----------------------------------ForDoAll-----------------------------------//
315 private final void setModuleInfo(ModuleInfo moduleinfo
) {
319 private final void start() throws Exception
{
320 Common
.toDoAll(mi
.localmodulesources
, this);
322 if (!mi
.hashr8only
.isEmpty()) {
327 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
328 SFReplacer
.setModuleInfo(moduleinfo
);