Introduce a new property INCLUDE_PATHS. This property can used by customized build...
[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
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, result suffix, file type.
57 ///
58 public final String[][] fileTypes = { {".h", "", "CHeader" },
59 {".c", "", "CCode" },
60 {".inc", "", "ASMHeader" },
61 {".asm", "", "ASM" },
62 {".S", "", "ASM" },
63 {".s", "", "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", "", "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, 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 for (int i = 0; i < fileTypes.length; i++) {
159 if (filename.endsWith(fileTypes[i][0])) {
160 parseFile(filename, fileTypes[i][2], 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, Node root) {
179 if (unicodeFirst) {
180 if ( ! filetype.equalsIgnoreCase("UNI")){
181 return ;
182 }
183 unicodeExist= true;
184 } else {
185 if (filetype.equalsIgnoreCase("UNI")){
186 return ;
187 }
188 }
189
190 //
191 // If file is C or ASM header file, skip it
192 //
193 if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {
194 return;
195 }
196
197 //
198 // If file is pre-processor file, skip it
199 //
200 if (filetype.equalsIgnoreCase("PPCode")) {
201 return;
202 }
203
204 //
205 // If define CC_EXT in tools_def.txt file, the source file with
206 // different suffix is skipped
207 //
208 String toolsDefExtName = project.getProperty(filetype + "_EXT");
209 if (toolsDefExtName != null) {
210 String[] exts = toolsDefExtName.split(" ");
211 for (int i = 0; i < exts.length; i++) {
212 if ( ! filename.endsWith(exts[i])) {
213 return ;
214 }
215 }
216 }
217
218 String module_path = project.getProperty("MODULE_DIR");
219 File moduleFile = new File(module_path);
220 File sourceFile = new File(filename);
221
222 //
223 // If source file is AutoGen.c, then Filepath is .
224 //
225 String sourceFilepath = "";
226 String sourceFilename = "";
227 String sourceFileext = "";
228 if (sourceFile.getPath().endsWith("AutoGen.c")) {
229 sourceFilepath = ".";
230 sourceFilename = "AutoGen";
231 sourceFileext = ".c";
232 filetype = "AUTOGEN";
233 } else {
234 // sourceFile.
235 String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);
236 int index = str.lastIndexOf(File.separatorChar);
237 sourceFilepath = ".";
238 if (index > 0) {
239 sourceFilepath = str.substring(0, index);
240 str = str.substring(index + 1);
241 }
242 sourceFilename = str;
243 index = str.lastIndexOf('.');
244 if (index > 0) {
245 sourceFilename = str.substring(0, index);
246 sourceFileext = str.substring(index);
247 }
248 }
249 // <Build_filetype FILEPATH="" FILENAME="" />
250 Element ele = document.createElement("Build_" + filetype);
251 ele.setAttribute("FILEPATH", sourceFilepath);
252 ele.setAttribute("FILENAME", sourceFilename);
253 ele.setAttribute("FILEEXT", sourceFileext.substring(1));
254 Element includesEle = document.createElement("EXTRA.INC");
255 for (int i = 0; i < includes.length; i++) {
256 Element includeEle = document.createElement("includepath");
257 includeEle.setAttribute("path", project.replaceProperties(includes[i]));
258 includesEle.appendChild(includeEle);
259 }
260 ele.appendChild(includesEle);
261 root.appendChild(ele);
262 }
263 }