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