Translate the Arch enum to an arch directory name. This is necessary for Linux builds...
[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.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 .c | C_Code
33 .asm | Assembly
34 .s | IPF_Assembly_Code
35 .h | Header
36 .lib | Static_Library
37 .i | IPF_PP_Code
38 .vfr | Vfr
39 .uni | Unicode
40 .dxs | Dependency_File
41 .bmp | Graphics
42 .efi | EFI
43 </pre>
44
45 @since GenBuild 1.0
46 **/
47 public class FileProcess {
48 ///
49 /// The mapping information about source suffix, result suffix, file type.
50 ///
51 public final String[][] fileTypes = { { ".c", ".obj", "C_Code" }, { ".asm", ".obj", "Assembly" },
52 { ".s", ".obj", "IPF_Assembly_Code" }, { ".h", "", "Header" },
53 { ".lib", "", "Static_Library" }, { ".src", ".c", "" },
54 { ".i", ".obj", "IPF_PP_Code" }, { ".vfr", ".obj", "Vfr" },
55 { ".uni", "", "Unicode" }, { ".dxs", "", "Dependency_File" },
56 { ".bmp", "", "Graphics" }, { ".efi", "", "EFI" } };
57
58 ///
59 /// Current ANT context.
60 ///
61 private Project project;
62
63 ///
64 /// Current module's include pathes
65 ///
66 private Set<String> includes;
67
68 ///
69 /// Current source files.
70 ///
71 private Set<String> sourceFiles;
72
73 ///
74 /// Xml Document.
75 ///
76 private Document document;
77
78 ///
79 /// The flag to ensure all unicode files build before others.
80 ///
81 private boolean unicodeFirst = true;
82
83 ///
84 /// The flag present whether current module contains Unicode files or not.
85 ///
86 private boolean unicodeExist = false;
87
88 /**
89 Initialize the project, includes, sourceFiles, document members.
90
91 @param project ANT project
92 @param includes Module include pathes
93 @param sourceFiles Modules source files
94 @param document XML document
95 **/
96 public void init(Project project, Set<String> includes, Set<String> sourceFiles, Document document) {
97 this.document = document;
98 this.includes = includes;
99 this.project = project;
100 this.sourceFiles = sourceFiles;
101 }
102
103 /**
104 Parse file without file type.
105
106 @param filename Source file name
107 @param root Root node
108 @param unicodeFirst whether build Unicode file firstly or not
109 **/
110 public synchronized void parseFile(String filename, Node root, boolean unicodeFirst) {
111 this.unicodeFirst = unicodeFirst;
112 parseFile(filename, root);
113 }
114
115 /**
116 Get whether current module contains Unicode files or not.
117
118 @return Whether current module contains Unicode files or not
119 **/
120 public boolean isUnicodeExist() {
121 return unicodeExist;
122 }
123
124 /**
125 Parse file.
126
127 @param filename Source file name
128 @param filetype Source file type
129 @param root Root node
130 @param unicodeFirst whether build Unicode file firstly or not
131 **/
132 public synchronized void parseFile(String filename, String filetype, Node root, boolean unicodeFirst) {
133 this.unicodeFirst = unicodeFirst;
134 parseFile(filename, filetype, root);
135 }
136
137 /**
138 Find out source file's type.
139
140 @param filename Source file name
141 @param root Root node
142 **/
143 public synchronized void parseFile(String filename, Node root) {
144 boolean flag = false;
145 for (int i = 0; i < fileTypes.length; i++) {
146 if (filename.toLowerCase().endsWith(fileTypes[i][0])) {
147 flag = true;
148 parseFile(filename, fileTypes[i][2], root);
149 }
150 }
151 if (!flag) {
152 System.out.println("Warning: File " + filename + " is not known from its suffix.");
153 }
154 }
155
156 /**
157 Parse file. If flag <code>unicodeFirst</code> is true, then build all
158 unicode files firstly.
159
160 <p>Note that AutoGen.c is processed specially. It's output path is always
161 <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>
162 and relative to module path. </p>
163
164 @param filename Source file name
165 @param filetype Source file type
166 @param root Root node
167 **/
168 public synchronized void parseFile(String filename, String filetype, Node root) {
169 if (unicodeFirst) {
170 if ( ! filetype.equalsIgnoreCase("Unicode")){
171 return ;
172 }
173 unicodeExist= true;
174 } else {
175 if (filetype.equalsIgnoreCase("Unicode")){
176 return ;
177 }
178 }
179 sourceFiles.add(filename);
180 if (filetype.equalsIgnoreCase("Header")) {
181 return;
182 }
183 if (filetype.equalsIgnoreCase("IPF_PP_Code")) {
184 return;
185 }
186 String module_path = project.getProperty("MODULE_DIR");
187 File moduleFile = new File(module_path);
188 File sourceFile = new File(filename);
189 // If source file is AutoGen.c, then Filepath is .
190 String sourceFilepath;
191 String sourceFilename;
192 if (sourceFile.getPath().endsWith("AutoGen.c")) {
193 sourceFilepath = ".";
194 sourceFilename = "AutoGen";
195 filetype = "AUTOGEN";
196 } else {
197 // sourceFile.
198 String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);
199 int index = str.lastIndexOf(File.separatorChar);
200 sourceFilepath = ".";
201 if (index > 0) {
202 sourceFilepath = str.substring(0, index);
203 str = str.substring(index + 1);
204 }
205 sourceFilename = str;
206 index = str.lastIndexOf('.');
207 if (index > 0) {
208 sourceFilename = str.substring(0, index);
209 }
210 }
211 // <Build_filetype FILEPATH="" FILENAME="" />
212 Element ele = document.createElement("Build_" + filetype);
213 ele.setAttribute("FILEPATH", sourceFilepath);
214 ele.setAttribute("FILENAME", sourceFilename);
215 String[] includePaths = includes.toArray(new String[includes.size()]);
216 Element includesEle = document.createElement("EXTRA.INC");
217 for (int i = 0; i < includePaths.length; i++) {
218 Element includeEle = document.createElement("includepath");
219 includeEle.setAttribute("path", includePaths[i]);
220 includesEle.appendChild(includeEle);
221 }
222 ele.appendChild(includesEle);
223 root.appendChild(ele);
224 }
225 }