]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /** @file\r |
2 | This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate\r | |
3 | dependency files for source code.\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 | **/\r | |
15 | package org.tianocore.framework.tasks;\r | |
16 | \r | |
878ddf1f | 17 | import java.io.File;\r |
18 | import java.io.FileReader;\r | |
19 | import java.io.FileWriter;\r | |
20 | import java.io.IOException;\r | |
21 | import java.io.LineNumberReader;\r | |
22 | import java.util.ArrayList;\r | |
196ad8d7 | 23 | import java.util.HashSet;\r |
878ddf1f | 24 | import java.util.Iterator;\r |
25 | import java.util.List;\r | |
196ad8d7 | 26 | import java.util.Set;\r |
878ddf1f | 27 | import java.util.StringTokenizer;\r |
878ddf1f | 28 | \r |
196ad8d7 | 29 | import org.apache.tools.ant.BuildException;\r |
30 | import org.apache.tools.ant.Project;\r | |
31 | import org.apache.tools.ant.Task;\r | |
32 | import org.apache.tools.ant.taskdefs.Execute;\r | |
33 | import org.apache.tools.ant.taskdefs.LogStreamHandler;\r | |
34 | import org.apache.tools.ant.types.Commandline;\r | |
35 | import org.apache.tools.ant.types.Path;\r | |
ff225cbb | 36 | \r |
37 | import org.tianocore.common.logger.EdkLog;\r | |
196ad8d7 | 38 | \r |
878ddf1f | 39 | /**\r |
40 | Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.\r | |
41 | **/\r | |
42 | public class MakeDeps extends Task {\r | |
43 | \r | |
44 | //\r | |
45 | // private members, use set/get to access them\r | |
46 | //\r | |
47 | private static final String cmdName = "MakeDeps";\r | |
878ddf1f | 48 | private String depsFile = null;\r |
49 | private String subDir = null;\r | |
50 | private boolean quietMode = true;\r | |
51 | private boolean ignoreError = true;\r | |
52 | private String extraDeps = "";\r | |
53 | private List<IncludePath> includePathList = new ArrayList<IncludePath>();\r | |
54 | private List<Input> inputFileList = new ArrayList<Input>();\r | |
55 | \r | |
56 | public MakeDeps() {\r | |
57 | \r | |
58 | }\r | |
59 | \r | |
60 | /**\r | |
61 | The Standard execute method for ANT task. It will check if it's necessary\r | |
62 | to generate the dependency list file. If no file is found or the dependency\r | |
63 | is changed, it will compose the command line and call MakeDeps.exe to\r | |
64 | generate the dependency list file.\r | |
65 | \r | |
66 | @throws BuildException\r | |
67 | **/\r | |
68 | public void execute() throws BuildException {\r | |
69 | ///\r | |
70 | /// check if the dependency list file is uptodate or not\r | |
71 | ///\r | |
72 | if (isUptodate()) {\r | |
73 | return;\r | |
74 | }\r | |
75 | \r | |
76 | Project prj = this.getOwningTarget().getProject();\r | |
2da8968b | 77 | String toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r |
219e2247 | 78 | FrameworkLogger logger = new FrameworkLogger(prj, "makedeps");\r |
79 | EdkLog.setLogLevel(prj.getProperty("env.LOGLEVEL"));\r | |
80 | EdkLog.setLogger(logger);\r | |
81 | \r | |
878ddf1f | 82 | ///\r |
83 | /// compose full tool path\r | |
84 | ///\r | |
85 | if (toolPath == null || toolPath.length() == 0) {\r | |
82810f3b | 86 | toolPath = cmdName;\r |
878ddf1f | 87 | } else {\r |
88 | if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {\r | |
89 | toolPath = toolPath + cmdName;\r | |
90 | } else {\r | |
82810f3b | 91 | toolPath = toolPath + File.separator + cmdName;\r |
878ddf1f | 92 | }\r |
93 | }\r | |
94 | \r | |
95 | ///\r | |
96 | /// compose tool arguments\r | |
97 | ///\r | |
98 | StringBuffer args = new StringBuffer(4096);\r | |
99 | if (ignoreError) {\r | |
82810f3b | 100 | args.append(" -ignorenotfound ");\r |
878ddf1f | 101 | }\r |
102 | if (quietMode) {\r | |
82810f3b | 103 | args.append(" -q ");\r |
878ddf1f | 104 | }\r |
105 | if (subDir != null && subDir.length() > 0) {\r | |
106 | args.append(" -s ");\r | |
107 | args.append(subDir);\r | |
108 | }\r | |
109 | \r | |
110 | ///\r | |
111 | /// if there's no source files, we can do nothing about dependency\r | |
ff225cbb | 112 | ///\r |
878ddf1f | 113 | if (inputFileList.size() == 0) {\r |
114 | throw new BuildException("No source files specified to scan");\r | |
115 | }\r | |
116 | \r | |
117 | ///\r | |
118 | /// compose source file arguments\r | |
119 | ///\r | |
82810f3b | 120 | for (int i = 0, listLength = inputFileList.size(); i < listLength; ++i) {\r |
121 | args.append(inputFileList.get(i).toString());\r | |
878ddf1f | 122 | }\r |
123 | \r | |
82810f3b | 124 | for (int i = 0, listLength = includePathList.size(); i < listLength; ++i) {\r |
125 | args.append(includePathList.get(i).toString());\r | |
878ddf1f | 126 | }\r |
878ddf1f | 127 | \r |
128 | ///\r | |
129 | /// We don't need a real target. So just a "dummy" is given\r | |
130 | ///\r | |
131 | args.append(" -target dummy");\r | |
132 | args.append(" -o ");\r | |
82810f3b | 133 | args.append(depsFile);\r |
878ddf1f | 134 | \r |
135 | ///\r | |
136 | /// prepare to execute the tool\r | |
137 | ///\r | |
138 | Commandline cmd = new Commandline();\r | |
139 | cmd.setExecutable(toolPath);\r | |
140 | cmd.createArgument().setLine(args.toString());\r | |
141 | \r | |
142 | LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);\r | |
143 | Execute runner = new Execute(streamHandler, null);\r | |
144 | \r | |
145 | runner.setAntRun(prj);\r | |
146 | runner.setCommandline(cmd.getCommandline());\r | |
147 | \r | |
219e2247 | 148 | EdkLog.log(EdkLog.EDK_VERBOSE, Commandline.toString(cmd.getCommandline()));\r |
219e2247 | 149 | \r |
878ddf1f | 150 | int result = 0;\r |
151 | try {\r | |
152 | result = runner.execute();\r | |
153 | } catch (IOException e) {\r | |
154 | throw new BuildException(e.getMessage());\r | |
155 | }\r | |
156 | \r | |
157 | if (result != 0) {\r | |
219e2247 | 158 | EdkLog.log(EdkLog.EDK_INFO, "MakeDeps failed!");\r |
82810f3b | 159 | throw new BuildException("MakeDeps: failed to generate dependency file!");\r |
878ddf1f | 160 | }\r |
878ddf1f | 161 | }\r |
162 | \r | |
163 | ///\r | |
164 | /// Remove any duplicated path separator or inconsistent path separator\r | |
165 | ///\r | |
166 | private String cleanupPathName(String path) {\r | |
52cbbdbc | 167 | String separator = "\\" + File.separator;\r |
168 | String duplicateSeparator = separator + "{2}";\r | |
169 | path = Path.translateFile(path);\r | |
170 | path = path.replaceAll(duplicateSeparator, separator);\r | |
878ddf1f | 171 | return path;\r |
172 | }\r | |
173 | \r | |
174 | /**\r | |
175 | Set method for "DepsFile" attribute\r | |
176 | \r | |
177 | @param name The name of dependency list file\r | |
178 | **/\r | |
179 | public void setDepsFile(String name) {\r | |
180 | depsFile = cleanupPathName(name);\r | |
181 | }\r | |
182 | \r | |
183 | /**\r | |
184 | Get method for "DepsFile" attribute\r | |
185 | \r | |
186 | @returns The name of dependency list file\r | |
187 | **/\r | |
188 | public String getDepsFile() {\r | |
189 | return depsFile;\r | |
190 | }\r | |
191 | \r | |
192 | /**\r | |
193 | Set method for "IgnoreError" attribute\r | |
194 | \r | |
195 | @param ignore flag to control error handling (true/false)\r | |
196 | **/\r | |
197 | public void setIgnoreError(boolean ignore) {\r | |
198 | ignoreError = ignore;\r | |
199 | }\r | |
200 | \r | |
201 | /**\r | |
202 | Get method for "IgnoreError" attribute\r | |
203 | \r | |
204 | @returns The value of current IgnoreError flag\r | |
205 | **/\r | |
206 | public boolean getIgnoreError() {\r | |
207 | return ignoreError;\r | |
208 | }\r | |
209 | \r | |
210 | /**\r | |
211 | Set method for "QuietMode" attribute\r | |
212 | \r | |
213 | @param quiet flag to control the output information (true/false)\r | |
214 | **/\r | |
215 | public void setQuietMode(boolean quiet) {\r | |
216 | quietMode = quiet;\r | |
217 | }\r | |
218 | \r | |
219 | /**\r | |
220 | Get method for "QuietMode" attribute\r | |
221 | \r | |
222 | @returns value of current QuietMode flag\r | |
223 | **/\r | |
224 | public boolean getQuietMode() {\r | |
225 | return quietMode;\r | |
226 | }\r | |
227 | \r | |
228 | /**\r | |
229 | Set method for "SubDir" attribute\r | |
230 | \r | |
231 | @param dir The name of sub-directory in which source files will be scanned\r | |
232 | **/\r | |
233 | public void setSubDir(String dir) {\r | |
82810f3b | 234 | subDir = cleanupPathName(dir);\r |
878ddf1f | 235 | }\r |
236 | \r | |
237 | /**\r | |
238 | Get method for "SubDir" attribute\r | |
239 | \r | |
240 | @returns The name of sub-directory\r | |
241 | **/\r | |
242 | public String getSubDir() {\r | |
243 | return subDir;\r | |
244 | }\r | |
245 | \r | |
878ddf1f | 246 | /**\r |
247 | Set method for "ExtraDeps" attribute\r | |
248 | \r | |
249 | @param deps The name of dependency file specified separately\r | |
250 | **/\r | |
251 | public void setExtraDeps(String deps) {\r | |
82810f3b | 252 | extraDeps = cleanupPathName(deps);\r |
878ddf1f | 253 | }\r |
254 | \r | |
255 | /**\r | |
256 | Get method for "ExtraDeps" attribute\r | |
257 | \r | |
258 | @returns The name of dependency file specified separately\r | |
259 | **/\r | |
260 | public String getExtraDeps () {\r | |
261 | return extraDeps;\r | |
262 | }\r | |
263 | \r | |
264 | /**\r | |
265 | Add method for "IncludePath" nested element\r | |
266 | \r | |
267 | @param path The IncludePath object from nested IncludePath type of element\r | |
268 | **/\r | |
269 | public void addIncludepath(IncludePath path) {\r | |
270 | includePathList.add(path);\r | |
271 | }\r | |
272 | \r | |
273 | /**\r | |
274 | Add method for "Input" nested element\r | |
275 | \r | |
276 | @param input The Input object from nested Input type of element\r | |
277 | **/\r | |
278 | public void addInput(Input inputFile) {\r | |
279 | inputFileList.add(inputFile);\r | |
280 | }\r | |
281 | \r | |
878ddf1f | 282 | /**\r |
283 | Check if the dependency list file should be (re-)generated or not.\r | |
284 | \r | |
285 | @returns true The dependency list file is uptodate. No re-generation is needed.\r | |
286 | @returns false The dependency list file is outofdate. Re-generation is needed.\r | |
287 | **/\r | |
288 | private boolean isUptodate() {\r | |
289 | File df = new File(depsFile);\r | |
290 | if (!df.exists()) {\r | |
291 | return false;\r | |
292 | }\r | |
293 | \r | |
82810f3b | 294 | //\r |
295 | // If the source file(s) is newer than dependency list file, we need to\r | |
296 | // re-generate the dependency list file\r | |
297 | //\r | |
878ddf1f | 298 | long depsFileTimeStamp = df.lastModified();\r |
82810f3b | 299 | Iterator<Input> iterator = (Iterator<Input>)inputFileList.iterator();\r |
878ddf1f | 300 | while (iterator.hasNext()) {\r |
82810f3b | 301 | Input inputFile = iterator.next();\r |
302 | List<String> fileList = inputFile.getNameList();\r | |
303 | for (int i = 0, length = fileList.size(); i < length; ++i) {\r | |
304 | File sf = new File(fileList.get(i));\r | |
305 | if (sf.lastModified() > depsFileTimeStamp) {\r | |
306 | return false;\r | |
307 | }\r | |
878ddf1f | 308 | }\r |
309 | }\r | |
310 | \r | |
82810f3b | 311 | //\r |
312 | // If the source files haven't been changed since last time the dependency\r | |
313 | // list file was generated, we need to check each file in the file list to\r | |
314 | // see if any of them is changed or not. If anyone of them is newer than\r | |
315 | // the dependency list file, MakeDeps.exe is needed to run again.\r | |
316 | //\r | |
878ddf1f | 317 | LineNumberReader lineReader = null;\r |
318 | FileReader fileReader = null;\r | |
319 | boolean ret = true;\r | |
320 | try {\r | |
321 | fileReader = new FileReader(df);\r | |
322 | lineReader = new LineNumberReader(fileReader);\r | |
323 | \r | |
324 | String line = null;\r | |
325 | while ((line = lineReader.readLine()) != null) {\r | |
326 | File sourceFile = new File(line);\r | |
1f08e795 | 327 | //\r |
328 | // If a file cannot be found (moved or removed) or newer, regenerate the dep file\r | |
329 | // \r | |
330 | if ((!sourceFile.exists()) || (sourceFile.lastModified() > depsFileTimeStamp)) {\r | |
878ddf1f | 331 | ret = false;\r |
332 | break;\r | |
333 | }\r | |
334 | }\r | |
335 | lineReader.close();\r | |
336 | fileReader.close();\r | |
337 | } catch (IOException e) {\r | |
338 | log (e.getMessage());\r | |
339 | }\r | |
340 | \r | |
341 | return ret;\r | |
342 | }\r | |
343 | }\r | |
344 | \r |