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