]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/FileProcess.java
Change to new XML Schema.
[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 {".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", "", "PPCode"}};
78 ///
79 /// Current ANT context.
80 ///
81 private Project project;
82
83 ///
84 /// Current module's include pathes
85 ///
86 private Set<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, Set<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, Node root, boolean unicodeFirst) {
125 this.unicodeFirst = unicodeFirst;
126 parseFile(filename, 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, Node root, boolean unicodeFirst) {
147 this.unicodeFirst = unicodeFirst;
148 parseFile(filename, filetype, 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, Node root) throws BuildException {
158 boolean flag = false;
159 for (int i = 0; i < fileTypes.length; i++) {
160 if (filename.endsWith(fileTypes[i][0])) {
161 flag = true;
162 parseFile(filename, fileTypes[i][2], root);
163 }
164 }
165 if (!flag) {
166 throw new BuildException("File [" + filename + "] is not known from its suffix.");
167 }
168 }
169
170 /**
171 Parse file. If flag <code>unicodeFirst</code> is true, then build all
172 unicode files firstly.
173
174 <p>Note that AutoGen.c is processed specially. It's output path is always
175 <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>
176 and relative to module path. </p>
177
178 @param filename Source file name
179 @param filetype Source file type
180 @param root Root node
181 **/
182 public synchronized void parseFile(String filename, String filetype, Node root) {
183 if (unicodeFirst) {
184 if ( ! filetype.equalsIgnoreCase("UNI")){
185 return ;
186 }
187 unicodeExist= true;
188 } else {
189 if (filetype.equalsIgnoreCase("UNI")){
190 return ;
191 }
192 }
193
194 //
195 // If file is C or ASM header file, skip it
196 //
197 if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {
198 return;
199 }
200
201 //
202 // If file is pre-processor file, skip it
203 //
204 if (filetype.equalsIgnoreCase("PPCode")) {
205 return;
206 }
207
208 //
209 // If define CC_EXT in tools_def.txt file, the source file with
210 // different suffix is skipped
211 //
212 String toolsDefExtName = project.getProperty(filetype + "_EXT");
213 if (toolsDefExtName != null) {
214 String[] exts = toolsDefExtName.split(" ");
215 for (int i = 0; i < exts.length; i++) {
216 if ( ! filename.endsWith(exts[i])) {
217 return ;
218 }
219 }
220 }
221
222 String module_path = project.getProperty("MODULE_DIR");
223 File moduleFile = new File(module_path);
224 File sourceFile = new File(filename);
225
226 //
227 // If source file is AutoGen.c, then Filepath is .
228 //
229 String sourceFilepath = "";
230 String sourceFilename = "";
231 String sourceFileext = "";
232 if (sourceFile.getPath().endsWith("AutoGen.c")) {
233 sourceFilepath = ".";
234 sourceFilename = "AutoGen";
235 sourceFileext = ".c";
236 filetype = "AUTOGEN";
237 } else {
238 // sourceFile.
239 String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);
240 int index = str.lastIndexOf(File.separatorChar);
241 sourceFilepath = ".";
242 if (index > 0) {
243 sourceFilepath = str.substring(0, index);
244 str = str.substring(index + 1);
245 }
246 sourceFilename = str;
247 index = str.lastIndexOf('.');
248 if (index > 0) {
249 sourceFilename = str.substring(0, index);
250 sourceFileext = str.substring(index);
251 }
252 }
253 // <Build_filetype FILEPATH="" FILENAME="" />
254 Element ele = document.createElement("Build_" + filetype);
255 ele.setAttribute("FILEPATH", sourceFilepath);
256 ele.setAttribute("FILENAME", sourceFilename);
257 ele.setAttribute("FILEEXT", sourceFileext.substring(1));
258 String[] includePaths = includes.toArray(new String[includes.size()]);
259 Element includesEle = document.createElement("EXTRA.INC");
260 for (int i = 0; i < includePaths.length; i++) {
261 Element includeEle = document.createElement("includepath");
262 includeEle.setAttribute("path", includePaths[i]);
263 includesEle.appendChild(includeEle);
264 }
265 ele.appendChild(includesEle);
266 root.appendChild(ele);
267 }
268 }