summaryrefslogtreecommitdiff
path: root/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
blob: bc579ac741945ccc984cec8a78a0a38f177a89f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class SourceFileReplacer {
	SourceFileReplacer(String path, String outpath, ModuleInfo moduleinfo) {
		modulepath = path;
		outputpath = outpath;
		mi = moduleinfo;
	}
	private String modulepath;
	private String outputpath;
	private ModuleInfo mi;
	private boolean showdetails = false;
	
	private class r8tor9 {
		r8tor9(String r8, String r9) {
			r8thing = r8;
			r9thing = r9;
		}
		public String r8thing;
		public String r9thing;
	}
	
	// these sets are used only for printing log of the changes in current file
	private Set<r8tor9> filefunc = new HashSet<r8tor9>();
	private Set<r8tor9> filemacro = new HashSet<r8tor9>();
	private Set<r8tor9> fileguid = new HashSet<r8tor9>();
	private Set<r8tor9> fileppi = new HashSet<r8tor9>();
	private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
	private Set<String> filer8only = new HashSet<String>();
	
	private static final String r8only = "EfiLibInstallDriverBinding " +
			"EfiLibInstallAllDriverProtocols " +
			"EfiLibCompareLanguage " +
			"BufToHexString " +
			"EfiStrTrim " +							//is the r8only lib going to be enlarged????  Caution !!!!
			"EfiValueToHexStr " +
			"HexStringToBuf " +
			"IsHexDigit " +
			"NibbleToHexChar " +
			"GetHob " +
			"GetHobListSize " +
			"GetHobVersion " +
			"GetHobBootMode " +
			"GetCpuHobInfo " +
			"GetDxeCoreHobInfo " +
			"GetNextFirmwareVolumeHob " +
			"GetNextGuidHob " +
			"GetPalEntryHobInfo " +
			"GetIoPortSpaceAddressHobInfo ";
	
	public void flush() throws Exception {
		String outname = null;
		String inname = null;
		if (MigrationTool.ui.yesOrNo("Changes will be made to the Source Code.  View details?")) {
			showdetails = true;
		}
		
		Iterator<String> di = mi.localmodulesources.iterator();
		while (di.hasNext()) {
			inname = di.next();
			if (inname.contains(".c") || inname.contains(".C")) {
				if (inname.contains(".C")) {
					outname = inname.replaceFirst(".C", ".c");
				} else {
					outname = inname;
				}
				MigrationTool.ui.println("\nModifying file: " + inname);
				Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);
			} else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {
				if (inname.contains(".H")) {
					outname = inname.replaceFirst(".H", ".h");
				} else {
					outname = inname;
				}
				MigrationTool.ui.println("\nCopying file: " + inname);
				Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);
			}
		}

		if (!mi.hashr8only.isEmpty()) {
			addr8only();
		}
	}
	
	private void addr8only() throws Exception {
		String paragraph = null;
		String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c");
		Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");
		PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));
		PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));
		Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);
		Matcher mtrr8only = ptnr8only.matcher(line);
		Matcher mtrr8onlyhead;
		while (mtrr8only.find()) {
			if (mi.hashr8only.contains(mtrr8only.group(2))) {
				paragraph = mtrr8only.group();
				outfile1.append(paragraph + "\n\n");
				if (mtrr8only.group(1).length() != 0) {
					mi.hashrequiredr9libs.add(mtrr8only.group(1));
				}
				//generate R8lib.h
				while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {
					paragraph = mtrr8onlyhead.replaceAll(";");
				}
				outfile2.append(paragraph + "\n\n");
			}
		}
		outfile1.flush();
		outfile1.close();
		outfile2.flush();
		outfile2.close();
		
		mi.localmodulesources.add("R8Lib.h");
		mi.localmodulesources.add("R8Lib.c");
	}
	
	// Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
	private String sourcefilereplace(String filename) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(filename));
		StringBuffer wholefile = new StringBuffer();
		String line;
		String r8thing;
		String r9thing;
		r8tor9 temp;
		boolean addr8 = false;

		Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE);					// ! only two level () bracket allowed !
		//Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);

		while ((line = rd.readLine()) != null) {
			wholefile.append(line + "\n");
		}
		line = wholefile.toString();
		
		// replace BS -> gBS , RT -> gRT
		Matcher mat = pat.matcher(line);
		if (mat.find()) {												// add a library here
			MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");
			line = mat.replaceAll("g$1$2$3");							//unknown correctiveness
		}
		mat.reset();
		while (mat.find()) {
			if (mat.group(1).matches("BS")) {
				mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
			}
			if (mat.group(1).matches("RT")) {
				mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
			}
		}
		/*
		// remove EFI_DRIVER_ENTRY_POINT
		Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
		Matcher matentrypoint = patentrypoint.matcher(line);
		if (matentrypoint.find()) {
			MigrationTool.ui.println("Deleting Entry_Point");
			line = matentrypoint.replaceAll("");
		}
		*/
		// start replacing names
		Iterator<String> it;
		// Converting non-locla function
		it = mi.hashnonlocalfunc.iterator();
		while (it.hasNext()) {
			r8thing = it.next();
			if (r8thing.matches("EfiInitializeDriverLib")) {					//s
				mi.hashrequiredr9libs.add("UefiBootServicesTableLib");			//p
				mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");		//e
			} else if (r8thing.matches("DxeInitializeDriverLib")) {				//c
				mi.hashrequiredr9libs.add("UefiBootServicesTableLib");			//i
				mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");		//a
				mi.hashrequiredr9libs.add("DxeServicesTableLib");				//l
			} else {															//
				mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));				// add a library here
			}
			
			if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {
				if (!r8thing.equals(r9thing)) {
					if (line.contains(r8thing)) {
						line = line.replaceAll(r8thing, r9thing);
						filefunc.add(new r8tor9(r8thing, r9thing));
						Iterator<r8tor9> rt = filefunc.iterator();
						while (rt.hasNext()) {
							temp = rt.next();
							if (r8only.contains(temp.r8thing)) {
								filer8only.add(r8thing);
								mi.hashr8only.add(r8thing);
								addr8 = true;
							}
						}
					}
				}
			}
		}															//is any of the guids changed?
		if (addr8 == true) {
			line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
		}
		
		// Converting macro
		it = mi.hashnonlocalmacro.iterator();
		while (it.hasNext()) {						//macros are all assumed MdePkg currently
			r8thing = it.next();
			//mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));		
			if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {
				if (line.contains(r8thing)) {
					line = line.replaceAll(r8thing, r9thing);
					filemacro.add(new r8tor9(r8thing, r9thing));
				}
			}
		}

		// Converting guid
		replaceGuid(line, mi.guid, "guid", fileguid);
		replaceGuid(line, mi.ppi, "ppi", fileppi);
		replaceGuid(line, mi.protocol, "protocol", fileprotocol);

		// Converting Pei
		// First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
		Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
		if (mi.moduletype.contains("PEIM")) {
			Matcher mtrpei = ptnpei.matcher(line);
			while (mtrpei.find()) {										// ! add a library here !
				line = mtrpei.replaceAll("PeiServices$1#%$2");
				mi.hashrequiredr9libs.add("PeiServicesLib");
			}
			mtrpei.reset();
			if (line.contains("PeiServicesCopyMem")) {
				line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");
				mi.hashrequiredr9libs.add("BaseMemoryLib");
			}
			if (line.contains("PeiServicesSetMem")) {
				line = line.replaceAll("PeiServicesSetMem#%", "SetMem");
				mi.hashrequiredr9libs.add("BaseMemoryLib");
			}

			// Second , find all #% to drop the arg "PeiServices"
			Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
			Matcher mtrpeiarg = ptnpeiarg.matcher(line);
			while (mtrpeiarg.find()) {
				line = mtrpeiarg.replaceAll("$1");
			}
		}
		
		Matcher mtrmac;
		mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);
		if (mtrmac.find()) {
			line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
		}
		mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);
		if (mtrmac.find()) {
			line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
		}
		mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);
		if (mtrmac.find()) {
			line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
		}
		mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);
		if (mtrmac.find()) {
			line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
		}
		if (line.contains("EFI_UINTN_ALIGN_MASK")) {
			line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
		}

		show(filefunc, "function");
		show(filemacro, "macro");
		show(fileguid, "guid");
		show(fileppi, "ppi");
		show(fileprotocol, "protocol");
		if (!filer8only.isEmpty()) {
			MigrationTool.ui.println("Converting r8only : " + filer8only);
		}

		filefunc.clear();
		filemacro.clear();
		fileguid.clear();
		fileppi.clear();
		fileprotocol.clear();
		filer8only.clear();

		return line;
	}
	
	private void show(Set<r8tor9> hash, String sh) {
		Iterator<r8tor9> it = hash.iterator();
		r8tor9 temp;
		if (showdetails) {
			if (!hash.isEmpty()) {
				MigrationTool.ui.print("Converting " + sh + " : ");
				while (it.hasNext()) {
					temp = it.next();
					MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");
				}
				MigrationTool.ui.println("");
			}
		}
	}
	
	private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {
		Iterator<String> it;
		String r8thing;
		String r9thing;
		it = hash.iterator();
		while (it.hasNext()) {
			r8thing = it.next();
			if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {
				if (!r8thing.equals(r9thing)) {
					if (line.contains(r8thing)) {
						line = line.replaceAll(r8thing, r9thing);
						filehash.add(new r8tor9(r8thing, r9thing));
					}
				}
			}
		}
	}
}