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 class SourceFileReplacer
{
21 SourceFileReplacer(String path
, ModuleInfo moduleinfo
, Database database
, UI fp
) {
27 private String modulepath
;
28 private ModuleInfo mi
;
31 private boolean showdetails
= false;
33 private class r8tor9
{
34 r8tor9(String r8
, String r9
) {
38 public String r8thing
;
39 public String r9thing
;
42 // these sets are used only for printing log of the changes in current file
43 private Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
44 private Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
45 private Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
46 private Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
47 private Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
48 private Set
<String
> filer8only
= new HashSet
<String
>();
50 private String r8only
= "EfiLibInstallDriverBinding " +
51 "EfiLibInstallAllDriverProtocols " +
52 "EfiLibCompareLanguage " +
54 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!
64 "GetDxeCoreHobInfo " +
65 "GetNextFirmwareVolumeHob " +
67 "GetPalEntryHobInfo " +
68 "GetIoPortSpaceAddressHobInfo ";
70 public void flush() throws Exception
{
72 String outname
= null;
74 if (ui
.yesOrNo("Changes will be made to the Source Code. View details?")) {
78 Iterator
<String
> di
= mi
.localmodulesources
.iterator();
79 while (di
.hasNext()) {
81 if (inname
.contains(".c") || inname
.contains(".C")) {
82 if (inname
.contains(".C")) {
83 outname
= inname
.replaceFirst(".C", ".c");
87 ui
.println("\nModifying file: " + inname
);
88 Common
.ensureDir(modulepath
+ File
.separator
+ "result" + File
.separator
+ outname
);
89 outfile
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "result" + File
.separator
+ outname
)));
90 outfile
.append(sourcefilereplace(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
));
93 } else if (inname
.contains(".h") || inname
.contains(".H") || inname
.contains(".dxs") || inname
.contains(".uni")) {
94 if (inname
.contains(".H")) {
95 outname
= inname
.replaceFirst(".H", ".h");
99 ui
.println("\nCopying file: " + inname
);
100 Common
.ensureDir(modulepath
+ File
.separator
+ "result" + File
.separator
+ outname
);
101 outfile
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "result" + File
.separator
+ outname
)));
102 outfile
.append(Common
.sourcefiletostring(modulepath
+ File
.separator
+ "temp" + File
.separator
+ inname
));
108 if (!mi
.hashr8only
.isEmpty()) {
113 private void addr8only() throws Exception
{
114 String paragraph
= null;
115 String line
= Common
.sourcefiletostring(Database
.defaultpath
+ File
.separator
+ "R8Lib.c");
116 Common
.ensureDir(modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.c");
117 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.c")));
118 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "result" + File
.separator
+ "R8Lib.h")));
119 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
120 Matcher mtrr8only
= ptnr8only
.matcher(line
);
121 Matcher mtrr8onlyhead
;
122 while (mtrr8only
.find()) {
123 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
124 paragraph
= mtrr8only
.group();
125 outfile1
.append(paragraph
+ "\n\n");
126 if (mtrr8only
.group(1).length() != 0) {
127 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
130 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
131 paragraph
= mtrr8onlyhead
.replaceAll(";");
133 outfile2
.append(paragraph
+ "\n\n");
141 mi
.localmodulesources
.add("R8Lib.h");
142 mi
.localmodulesources
.add("R8Lib.c");
145 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
146 private String
sourcefilereplace(String filename
) throws Exception
{
147 BufferedReader rd
= new BufferedReader(new FileReader(filename
));
148 StringBuffer wholefile
= new StringBuffer();
153 boolean addr8
= false;
155 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
156 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
158 while ((line
= rd
.readLine()) != null) {
159 wholefile
.append(line
+ "\n");
161 line
= wholefile
.toString();
163 // replace BS -> gBS , RT -> gRT
164 Matcher mat
= pat
.matcher(line
);
165 if (mat
.find()) { // add a library here
166 ui
.println("Converting all BS->gBS, RT->gRT");
167 line
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
171 if (mat
.group(1).matches("BS")) {
172 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
174 if (mat
.group(1).matches("RT")) {
175 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
179 // remove EFI_DRIVER_ENTRY_POINT
180 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
181 Matcher matentrypoint = patentrypoint.matcher(line);
182 if (matentrypoint.find()) {
183 ui.println("Deleting Entry_Point");
184 line = matentrypoint.replaceAll("");
187 // start replacing names
189 // Converting non-locla function
190 it
= mi
.hashnonlocalfunc
.iterator();
191 while (it
.hasNext()) {
193 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
194 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
195 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
196 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
197 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
198 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
199 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
201 mi
.hashrequiredr9libs
.add(db
.getR9Lib(r8thing
)); // add a library here
204 if ((r9thing
= db
.getR9Func(r8thing
)) != null) {
205 if (!r8thing
.equals(r9thing
)) {
206 if (line
.contains(r8thing
)) {
207 line
= line
.replaceAll(r8thing
, r9thing
);
208 filefunc
.add(new r8tor9(r8thing
, r9thing
));
209 Iterator
<r8tor9
> rt
= filefunc
.iterator();
210 while (rt
.hasNext()) {
212 if (r8only
.contains(temp
.r8thing
)) {
213 filer8only
.add(r8thing
);
214 mi
.hashr8only
.add(r8thing
);
221 } //is any of the guids changed?
223 line
= line
.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
227 it
= mi
.hashnonlocalmacro
.iterator();
228 while (it
.hasNext()) { //macros are all assumed MdePkg currently
230 //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));
231 if ((r9thing
= db
.getR9Macro(r8thing
)) != null) {
232 if (line
.contains(r8thing
)) {
233 line
= line
.replaceAll(r8thing
, r9thing
);
234 filemacro
.add(new r8tor9(r8thing
, r9thing
));
240 replaceGuid(line
, mi
.guid
, "guid", fileguid
);
241 replaceGuid(line
, mi
.ppi
, "ppi", fileppi
);
242 replaceGuid(line
, mi
.protocol
, "protocol", fileprotocol
);
245 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
246 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
247 if (mi
.moduletype
.contains("PEIM")) {
248 Matcher mtrpei
= ptnpei
.matcher(line
);
249 while (mtrpei
.find()) { // ! add a library here !
250 line
= mtrpei
.replaceAll("PeiServices$1#%$2");
251 mi
.hashrequiredr9libs
.add("PeiServicesLib");
254 if (line
.contains("PeiServicesCopyMem")) {
255 line
= line
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
256 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
258 if (line
.contains("PeiServicesSetMem")) {
259 line
= line
.replaceAll("PeiServicesSetMem#%", "SetMem");
260 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
263 // Second , find all #% to drop the arg "PeiServices"
264 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
265 Matcher mtrpeiarg
= ptnpeiarg
.matcher(line
);
266 while (mtrpeiarg
.find()) {
267 line
= mtrpeiarg
.replaceAll("$1");
272 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line
);
274 line
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
276 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line
);
278 line
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
280 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line
);
282 line
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
284 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line
);
286 line
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
288 if (line
.contains("EFI_UINTN_ALIGN_MASK")) {
289 line
= line
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
292 show(filefunc
, "function");
293 show(filemacro
, "macro");
294 show(fileguid
, "guid");
295 show(fileppi
, "ppi");
296 show(fileprotocol
, "protocol");
297 if (!filer8only
.isEmpty()) {
298 ui
.println("Converting r8only : " + filer8only
);
305 fileprotocol
.clear();
311 private void show(Set
<r8tor9
> hash
, String sh
) {
312 Iterator
<r8tor9
> it
= hash
.iterator();
315 if (!hash
.isEmpty()) {
316 ui
.print("Converting " + sh
+ " : ");
317 while (it
.hasNext()) {
319 ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
326 private void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
330 it
= hash
.iterator();
331 while (it
.hasNext()) {
333 if ((r9thing
= db
.getR9Guidname(r8thing
)) != null) {
334 if (!r8thing
.equals(r9thing
)) {
335 if (line
.contains(r8thing
)) {
336 line
= line
.replaceAll(r8thing
, r9thing
);
337 filehash
.add(new r8tor9(r8thing
, r9thing
));