]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/FileProcess.java
Fix T105. It is caused by cpptasks which change automati cally root directory like...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / FileProcess.java
1 /** @file
2 File is FileProcess class which is used to generate ANT script to build
3 source files.
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14 package org.tianocore.build;
15
16 import java.io.File;
17 import java.util.Set;
18
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Project;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24
25 /**
26 <p><code>FileProcess</code> is class to generate ANT script to build source
27 files.</p>
28
29 <p>If file does not specify file type, <code>FileProcess</code> will judge
30 by its extension. Following is the current supported extensions. </p>
31
32 <pre>
33 Source File Suffix File Type Description
34 .h CHeader C header file
35 .c CCode C source file
36 .inc ASMHeader Assembly header file
37 .asm ASM Assembly source file, usually for IA32 and X64 Arch and MSFT tool chain
38 .S ASM Assembly source file, usually for IPF Arch
39 .s ASM Assembly source file, usually for IA32 and X64 Arch and GCC tool chain
40 .uni UNI Unicode file
41 .vfr VFR Visual Forms Representation File
42 .fv FV Firmware Volume
43 .SEC FFS Firmware File System file
44 .PEI FFS Firmware File System file
45 .DXE FFS Firmware File System file
46 .APP FFS Firmware File System file
47 .FVI FFS Firmware File System file
48 .FFS FFS Firmware File System file
49 .bmp BMP Graphic File
50 .i PPCode IPF PreProcessor Code
51 </pre>
52
53 @since GenBuild 1.0
54 **/
55 public class FileProcess {
56 ///
57 /// The mapping information about source suffix, result suffix, file type.
58 ///
59 public final String[][] fileTypes = { {".h", "", "CHeader" },
60 {".c", "", "CCode" },
61 {".inc", "", "ASMHeader" },
62 {".asm", "", "ASM" },
63 {".S", "", "ASM" },
64 {".s", "", "ASM" },
65 {".uni", "", "UNI" },
66 {".vfr", "", "VFR" },
67 {".Vfr", "", "VFR" },
68 {".dxs", "", "DPX"},
69 {".fv", "", "FV" },
70 {".efi", "", "EFI" },
71 {".SEC", "", "FFS" },
72 {".PEI", "", "FFS" },
73 {".DXE", "", "FFS" },
74 {".APP", "", "FFS" },
75 {".FYI", "", "FFS" },
76 {".FFS", "", "FFS" },
77 {".bmp", "", "BMP" },
78 {".i", "", "PPCode"}};
79 ///
80 /// Current ANT context.
81 ///
82 private Project project;
83
84 ///
85 /// Current module's include pathes
86 ///
87 private Set<String> includes;
88
89 ///
90 /// Xml Document.
91 ///
92 private Document document;
93
94 ///
95 /// The flag to ensure all unicode files build before others.
96 ///
97 private boolean unicodeFirst = true;
98
99 ///
100 /// The flag present whether current module contains Unicode files or not.
101 ///
102 private boolean unicodeExist = false;
103
104 /**
105 Initialize the project, includes, sourceFiles, document members.
106
107 @param project ANT project
108 @param includes Module include pathes
109 @param sourceFiles Modules source files
110 @param document XML document
111 **/
112 public void init(Project project, Set<String> includes, Document document) {
113 this.document = document;
114 this.includes = includes;
115 this.project = project;
116 }
117
118 /**
119 Parse file without file type.
120
121 @param filename Source file name
122 @param root Root node
123 @param unicodeFirst whether build Unicode file firstly or not
124 **/
125 public synchronized void parseFile(String filename, Node root, boolean unicodeFirst) {
126 this.unicodeFirst = unicodeFirst;
127 parseFile(filename, root);
128 }
129
130 /**
131 Get whether current module contains Unicode files or not.
132
133 @return Whether current module contains Unicode files or not
134 **/
135 public boolean isUnicodeExist() {
136 return unicodeExist;
137 }
138
139 /**
140 Parse file.
141
142 @param filename Source file name
143 @param filetype Source file type
144 @param root Root node
145 @param unicodeFirst whether build Unicode file firstly or not
146 **/
147 public synchronized void parseFile(String filename, String filetype, Node root, boolean unicodeFirst) {
148 this.unicodeFirst = unicodeFirst;
149 parseFile(filename, filetype, root);
150 }
151
152 /**
153 Find out source file's type.
154
155 @param filename Source file name
156 @param root Root node
157 **/
158 public synchronized void parseFile(String filename, Node root) throws BuildException {
159 boolean flag = false;
160 for (int i = 0; i < fileTypes.length; i++) {
161 if (filename.endsWith(fileTypes[i][0])) {
162 flag = true;
163 parseFile(filename, fileTypes[i][2], root);
164 }
165 }
166 /*
167 if (!flag) {
168 throw new BuildException("File [" + filename + "] is not known from its suffix.");
169 }
170 */
171 }
172
173 /**
174 Parse file. If flag <code>unicodeFirst</code> is true, then build all
175 unicode files firstly.
176
177 <p>Note that AutoGen.c is processed specially. It's output path is always
178 <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>
179 and relative to module path. </p>
180
181 @param filename Source file name
182 @param filetype Source file type
183 @param root Root node
184 **/
185 public synchronized void parseFile(String filename, String filetype, Node root) {
186 if (unicodeFirst) {
187 if ( ! filetype.equalsIgnoreCase("UNI")){
188 return ;
189 }
190 unicodeExist= true;
191 } else {
192 if (filetype.equalsIgnoreCase("UNI")){
193 return ;
194 }
195 }
196
197 //
198 // If file is C or ASM header file, skip it
199 //
200 if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {
201 return;
202 }
203
204 //
205 // If file is pre-processor file, skip it
206 //
207 if (filetype.equalsIgnoreCase("PPCode")) {
208 return;
209 }
210
211 //
212 // If define CC_EXT in tools_def.txt file, the source file with
213 // different suffix is skipped
214 //
215 String toolsDefExtName = project.getProperty(filetype + "_EXT");
216 if (toolsDefExtName != null) {
217 String[] exts = toolsDefExtName.split(" ");
218 for (int i = 0; i < exts.length; i++) {
219 if ( ! filename.endsWith(exts[i])) {
220 return ;
221 }
222 }
223 }
224
225 String module_path = project.getProperty("MODULE_DIR");
226 File moduleFile = new File(module_path);
227 File sourceFile = new File(filename);
228
229 //
230 // If source file is AutoGen.c, then Filepath is .
231 //
232 String sourceFilepath = "";
233 String sourceFilename = "";
234 String sourceFileext = "";
235 if (sourceFile.getPath().endsWith("AutoGen.c")) {
236 sourceFilepath = ".";
237 sourceFilename = "AutoGen";
238 sourceFileext = ".c";
239 filetype = "AUTOGEN";
240 } else {
241 // sourceFile.
242 String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);
243 int index = str.lastIndexOf(File.separatorChar);
244 sourceFilepath = ".";
245 if (index > 0) {
246 sourceFilepath = str.substring(0, index);
247 str = str.substring(index + 1);
248 }
249 sourceFilename = str;
250 index = str.lastIndexOf('.');
251 if (index > 0) {
252 sourceFilename = str.substring(0, index);
253 sourceFileext = str.substring(index);
254 }
255 }
256 // <Build_filetype FILEPATH="" FILENAME="" />
257 Element ele = document.createElement("Build_" + filetype);
258 ele.setAttribute("FILEPATH", sourceFilepath);
259 ele.setAttribute("FILENAME", sourceFilename);
260 ele.setAttribute("FILEEXT", sourceFileext.substring(1));
261 String[] includePaths = includes.toArray(new String[includes.size()]);
262 Element includesEle = document.createElement("EXTRA.INC");
263 for (int i = 0; i < includePaths.length; i++) {
264 Element includeEle = document.createElement("includepath");
265 includeEle.setAttribute("path", project.replaceProperties(includePaths[i]));
266 includesEle.appendChild(includeEle);
267 }
268 ele.appendChild(includesEle);
269 root.appendChild(ele);
270 }
271 }