Support using @ToolChainFamily in <Filename> to do the filter. This is also to fix...
[mirror_edk2.git] / Tools / Java / 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
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Project;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23
24 /**
25 <p><code>FileProcess</code> is class to generate ANT script to build source
26 files.</p>
27
28 <p>If file does not specify file type, <code>FileProcess</code> will judge
29 by its extension. Following is the current supported extensions. </p>
30
31 <pre>
32 Source File Suffix File Type Description
33 .h CHeader C header file
34 .c CCode C source file
35 .inc ASMHeader Assembly header file
36 .asm ASM Assembly source file, usually for IA32 and X64 Arch and MSFT tool chain
37 .S ASM Assembly source file, usually for IPF Arch
38 .s ASM Assembly source file, usually for IA32 and X64 Arch and GCC tool chain
39 .uni UNI Unicode file
40 .vfr VFR Visual Forms Representation File
41 .fv FV Firmware Volume
42 .SEC FFS Firmware File System file
43 .PEI FFS Firmware File System file
44 .DXE FFS Firmware File System file
45 .APP FFS Firmware File System file
46 .FVI FFS Firmware File System file
47 .FFS FFS Firmware File System file
48 .bmp BMP Graphic File
49 .i PPCode IPF PreProcessor Code
50 </pre>
51
52 @since GenBuild 1.0
53 **/
54 public class FileProcess {
55 ///
56 /// The mapping information about source suffix, tool code, file type.
57 ///
58 public final String[][] fileTypes = { {".h", "", "CHeader" },
59 {".c", "CC", "CCode" },
60 {".inc", "", "ASMHeader" },
61 {".asm", "ASM", "ASM" },
62 {".S", "ASM", "ASM" },
63 {".s", "ASM", "ASM" },
64 {".uni", "", "UNI" },
65 {".vfr", "", "VFR" },
66 {".Vfr", "", "VFR" },
67 {".dxs", "", "DPX"},
68 {".fv", "", "FV" },
69 {".efi", "", "EFI" },
70 {".SEC", "", "FFS" },
71 {".PEI", "", "FFS" },
72 {".DXE", "", "FFS" },
73 {".APP", "", "FFS" },
74 {".FYI", "", "FFS" },
75 {".FFS", "", "FFS" },
76 {".bmp", "", "BMP" },
77 {".i", "PP", "PPCode"}};
78 ///
79 /// Current ANT context.
80 ///
81 private Project project;
82
83 ///
84 /// Current module's include pathes
85 ///
86 private String[] includes;
87
88 ///
89 /// Xml Document.
90 ///
91 private Document document;
92
93 ///
94 /// The flag to ensure all unicode files build before others.
95 ///
96 private boolean unicodeFirst = true;
97
98 ///
99 /// The flag present whether current module contains Unicode files or not.
100 ///
101 private boolean unicodeExist = false;
102
103 /**
104 Initialize the project, includes, sourceFiles, document members.
105
106 @param project ANT project
107 @param includes Module include pathes
108 @param sourceFiles Modules source files
109 @param document XML document
110 **/
111 public void init(Project project, String[] includes, Document document) {
112 this.document = document;
113 this.includes = includes;
114 this.project = project;
115 }
116
117 /**
118 Parse file without file type.
119
120 @param filename Source file name
121 @param root Root node
122 @param unicodeFirst whether build Unicode file firstly or not
123 **/
124 public synchronized void parseFile(String filename, String family, Node root, boolean unicodeFirst) {
125 this.unicodeFirst = unicodeFirst;
126 parseFile(filename, family, root);
127 }
128
129 /**
130 Get whether current module contains Unicode files or not.
131
132 @return Whether current module contains Unicode files or not
133 **/
134 public boolean isUnicodeExist() {
135 return unicodeExist;
136 }
137
138 /**
139 Parse file.
140
141 @param filename Source file name
142 @param filetype Source file type
143 @param root Root node
144 @param unicodeFirst whether build Unicode file firstly or not
145 **/
146 public synchronized void parseFile(String filename, String filetype, String family, Node root, boolean unicodeFirst) {
147 this.unicodeFirst = unicodeFirst;
148 parseFile(filename, filetype, family, root);
149 }
150
151 /**
152 Find out source file's type.
153
154 @param filename Source file name
155 @param root Root node
156 **/
157 public synchronized void parseFile(String filename, String family, Node root) throws BuildException {
158 for (int i = 0; i < fileTypes.length; i++) {
159 if (filename.endsWith(fileTypes[i][0])) {
160 parseFile(filename, fileTypes[i][2], family, root);
161 return ;
162 }
163 }
164 }
165
166 /**
167 Parse file. If flag <code>unicodeFirst</code> is true, then build all
168 unicode files firstly.
169
170 <p>Note that AutoGen.c is processed specially. It's output path is always
171 <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>
172 and relative to module path. </p>
173
174 @param filename Source file name
175 @param filetype Source file type
176 @param root Root node
177 **/
178 public synchronized void parseFile(String filename, String filetype, String family, Node root) {
179 //
180 // Filter file with family. Only family is specified in source file and
181 // not include current family will skip the file.
182 //
183 String toolCode = getToolCodeByFileType(filetype);
184 if (family != null && !family.trim().equalsIgnoreCase("")) {
185 String toolChainFamily = project.getProperty(toolCode + "_FAMILY");
186 if (toolChainFamily != null) {
187 if(!toolChainFamily.equalsIgnoreCase(family)) {
188 return ;
189 }
190 }
191 }
192
193 if (unicodeFirst) {
194 if ( ! filetype.equalsIgnoreCase("UNI")){
195 return ;
196 }
197 unicodeExist= true;
198 } else {
199 if (filetype.equalsIgnoreCase("UNI")){
200 return ;
201 }
202 }
203
204 //
205 // If file is C or ASM header file, skip it
206 //
207 if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {
208 return;
209 }
210
211 //
212 // If file is pre-processor file, skip it
213 //
214 if (filetype.equalsIgnoreCase("PPCode")) {
215 return;
216 }
217
218 //
219 // If define CC_EXT in tools_def.txt file, the source file with
220 // different suffix is skipped
221 //
222 String toolsDefExtName = project.getProperty(toolCode + "_EXT");
223 if (toolsDefExtName != null) {
224 String[] exts = toolsDefExtName.split(" ");
225 for (int i = 0; i < exts.length; i++) {
226 if ( ! filename.endsWith(exts[i])) {
227 return ;
228 }
229 }
230 }
231
232 String module_path = project.getProperty("MODULE_DIR");
233 File moduleFile = new File(module_path);
234 File sourceFile = new File(filename);
235
236 //
237 // If source file is AutoGen.c, then Filepath is .
238 //
239 String sourceFilepath = "";
240 String sourceFilename = "";
241 String sourceFileext = "";
242 if (sourceFile.getPath().endsWith("AutoGen.c")) {
243 sourceFilepath = ".";
244 sourceFilename = "AutoGen";
245 sourceFileext = ".c";
246 filetype = "AUTOGEN";
247 } else {
248 // sourceFile.
249 String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);
250 int index = str.lastIndexOf(File.separatorChar);
251 sourceFilepath = ".";
252 if (index > 0) {
253 sourceFilepath = str.substring(0, index);
254 str = str.substring(index + 1);
255 }
256 sourceFilename = str;
257 index = str.lastIndexOf('.');
258 if (index > 0) {
259 sourceFilename = str.substring(0, index);
260 sourceFileext = str.substring(index);
261 }
262 }
263 // <Build_filetype FILEPATH="" FILENAME="" />
264 Element ele = document.createElement("Build_" + filetype);
265 ele.setAttribute("FILEPATH", sourceFilepath);
266 ele.setAttribute("FILENAME", sourceFilename);
267 ele.setAttribute("FILEEXT", sourceFileext.substring(1));
268 Element includesEle = document.createElement("EXTRA.INC");
269 for (int i = 0; i < includes.length; i++) {
270 Element includeEle = document.createElement("includepath");
271 includeEle.setAttribute("path", project.replaceProperties(includes[i]));
272 includesEle.appendChild(includeEle);
273 }
274 ele.appendChild(includesEle);
275 root.appendChild(ele);
276 }
277
278 private String getToolCodeByFileType(String fileType) {
279 for (int i = 0; i < fileTypes.length; i++) {
280 if (fileTypes[i][2].equalsIgnoreCase(fileType)) {
281 return fileTypes[i][1];
282 }
283 }
284 return null;
285 }
286 }