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