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 |
5 | Copyright (c) 2006, Intel Corporation\r |
6 | All rights reserved. This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r |
9 | http://opensource.org/licenses/bsd-license.php\r |
10 | \r |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
13 | **/\r |
14 | package org.tianocore.build;\r |
15 | \r |
16 | import java.io.File;\r |
17 | import java.util.Set;\r |
18 | \r |
a29c47e0 |
19 | import org.apache.tools.ant.BuildException;\r |
878ddf1f |
20 | import org.apache.tools.ant.Project;\r |
21 | import org.w3c.dom.Document;\r |
22 | import org.w3c.dom.Element;\r |
23 | import 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 |
55 | public 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 |
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 |
77 | {".i", "", "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 |
86 | private Set<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 |
a29c47e0 |
111 | public void init(Project project, Set<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 |
124 | public synchronized void parseFile(String filename, Node root, boolean unicodeFirst) {\r |
125 | this.unicodeFirst = unicodeFirst;\r |
126 | parseFile(filename, root);\r |
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 |
146 | public synchronized void parseFile(String filename, String filetype, Node root, boolean unicodeFirst) {\r |
147 | this.unicodeFirst = unicodeFirst;\r |
148 | parseFile(filename, filetype, root);\r |
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 |
a29c47e0 |
157 | public synchronized void parseFile(String filename, Node root) throws BuildException {\r |
878ddf1f |
158 | boolean flag = false;\r |
159 | for (int i = 0; i < fileTypes.length; i++) {\r |
a29c47e0 |
160 | if (filename.endsWith(fileTypes[i][0])) {\r |
878ddf1f |
161 | flag = true;\r |
162 | parseFile(filename, fileTypes[i][2], root);\r |
163 | }\r |
164 | }\r |
165 | if (!flag) {\r |
a29c47e0 |
166 | throw new BuildException("File [" + filename + "] is not known from its suffix.");\r |
878ddf1f |
167 | }\r |
168 | }\r |
169 | \r |
170 | /**\r |
171 | Parse file. If flag <code>unicodeFirst</code> is true, then build all\r |
172 | unicode files firstly. \r |
173 | \r |
174 | <p>Note that AutoGen.c is processed specially. It's output path is always\r |
175 | <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>\r |
176 | and relative to module path. </p>\r |
177 | \r |
178 | @param filename Source file name\r |
179 | @param filetype Source file type\r |
180 | @param root Root node\r |
181 | **/\r |
182 | public synchronized void parseFile(String filename, String filetype, Node root) {\r |
183 | if (unicodeFirst) {\r |
a29c47e0 |
184 | if ( ! filetype.equalsIgnoreCase("UNI")){\r |
878ddf1f |
185 | return ;\r |
186 | }\r |
187 | unicodeExist= true;\r |
188 | } else {\r |
a29c47e0 |
189 | if (filetype.equalsIgnoreCase("UNI")){\r |
878ddf1f |
190 | return ;\r |
191 | }\r |
192 | }\r |
a29c47e0 |
193 | \r |
194 | //\r |
195 | // If file is C or ASM header file, skip it\r |
196 | //\r |
197 | if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {\r |
878ddf1f |
198 | return;\r |
199 | }\r |
a29c47e0 |
200 | \r |
201 | //\r |
202 | // If file is pre-processor file, skip it\r |
203 | // \r |
204 | if (filetype.equalsIgnoreCase("PPCode")) {\r |
878ddf1f |
205 | return;\r |
206 | }\r |
a29c47e0 |
207 | \r |
208 | //\r |
209 | // If define CC_EXT in tools_def.txt file, the source file with \r |
210 | // different suffix is skipped\r |
211 | //\r |
212 | String toolsDefExtName = project.getProperty(filetype + "_EXT");\r |
213 | if (toolsDefExtName != null) {\r |
214 | String[] exts = toolsDefExtName.split(" ");\r |
215 | for (int i = 0; i < exts.length; i++) {\r |
216 | if ( ! filename.endsWith(exts[i])) {\r |
217 | return ;\r |
218 | }\r |
219 | }\r |
220 | }\r |
221 | \r |
878ddf1f |
222 | String module_path = project.getProperty("MODULE_DIR");\r |
223 | File moduleFile = new File(module_path);\r |
224 | File sourceFile = new File(filename);\r |
a29c47e0 |
225 | \r |
226 | //\r |
878ddf1f |
227 | // If source file is AutoGen.c, then Filepath is .\r |
a29c47e0 |
228 | //\r |
229 | String sourceFilepath = "";\r |
230 | String sourceFilename = "";\r |
231 | String sourceFileext = "";\r |
878ddf1f |
232 | if (sourceFile.getPath().endsWith("AutoGen.c")) {\r |
233 | sourceFilepath = ".";\r |
234 | sourceFilename = "AutoGen";\r |
a29c47e0 |
235 | sourceFileext = ".c";\r |
878ddf1f |
236 | filetype = "AUTOGEN";\r |
237 | } else {\r |
238 | // sourceFile.\r |
239 | String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);\r |
240 | int index = str.lastIndexOf(File.separatorChar);\r |
241 | sourceFilepath = ".";\r |
242 | if (index > 0) {\r |
243 | sourceFilepath = str.substring(0, index);\r |
244 | str = str.substring(index + 1);\r |
245 | }\r |
246 | sourceFilename = str;\r |
247 | index = str.lastIndexOf('.');\r |
248 | if (index > 0) {\r |
249 | sourceFilename = str.substring(0, index);\r |
a29c47e0 |
250 | sourceFileext = str.substring(index);\r |
878ddf1f |
251 | }\r |
252 | }\r |
253 | // <Build_filetype FILEPATH="" FILENAME="" />\r |
254 | Element ele = document.createElement("Build_" + filetype);\r |
255 | ele.setAttribute("FILEPATH", sourceFilepath);\r |
256 | ele.setAttribute("FILENAME", sourceFilename);\r |
a29c47e0 |
257 | ele.setAttribute("FILEEXT", sourceFileext.substring(1));\r |
878ddf1f |
258 | String[] includePaths = includes.toArray(new String[includes.size()]);\r |
259 | Element includesEle = document.createElement("EXTRA.INC");\r |
260 | for (int i = 0; i < includePaths.length; i++) {\r |
261 | Element includeEle = document.createElement("includepath");\r |
262 | includeEle.setAttribute("path", includePaths[i]);\r |
263 | includesEle.appendChild(includeEle);\r |
264 | }\r |
265 | ele.appendChild(includesEle);\r |
266 | root.appendChild(ele);\r |
267 | }\r |
268 | } |