]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/MakeDeps.java
Fix cleanall can't clean all genereated files. Now .i files generated by VfrCompile...
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / MakeDeps.java
... / ...
CommitLineData
1/** @file\r
2This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate\r
3dependency files for source code.\r
4\r
5Copyright (c) 2006, Intel Corporation\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15package org.tianocore.framework.tasks;\r
16\r
17import java.io.File;\r
18import java.io.FileReader;\r
19import java.io.FileWriter;\r
20import java.io.IOException;\r
21import java.io.LineNumberReader;\r
22import java.util.ArrayList;\r
23import java.util.HashSet;\r
24import java.util.Iterator;\r
25import java.util.List;\r
26import java.util.Set;\r
27import java.util.StringTokenizer;\r
28\r
29import org.apache.tools.ant.BuildException;\r
30import org.apache.tools.ant.Project;\r
31import org.apache.tools.ant.Task;\r
32import org.apache.tools.ant.taskdefs.Execute;\r
33import org.apache.tools.ant.taskdefs.LogStreamHandler;\r
34import org.apache.tools.ant.types.Commandline;\r
35import org.apache.tools.ant.types.Path;\r
36\r
37import org.tianocore.common.logger.EdkLog;\r
38\r
39/**\r
40 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.\r
41 **/\r
42public 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
48 private String includePath = null;\r
49 private String depsFile = null;\r
50 private String subDir = null;\r
51 private boolean quietMode = true;\r
52 private boolean ignoreError = true;\r
53 private String extraDeps = "";\r
54 private List<IncludePath> includePathList = new ArrayList<IncludePath>();\r
55 private List<Input> inputFileList = new ArrayList<Input>();\r
56\r
57 public MakeDeps() {\r
58\r
59 }\r
60\r
61 /**\r
62 The Standard execute method for ANT task. It will check if it's necessary\r
63 to generate the dependency list file. If no file is found or the dependency\r
64 is changed, it will compose the command line and call MakeDeps.exe to\r
65 generate the dependency list file.\r
66\r
67 @throws BuildException\r
68 **/\r
69 public void execute() throws BuildException {\r
70 ///\r
71 /// check if the dependency list file is uptodate or not\r
72 ///\r
73 if (isUptodate()) {\r
74 return;\r
75 }\r
76\r
77 Project prj = this.getOwningTarget().getProject();\r
78 String toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r
79 FrameworkLogger logger = new FrameworkLogger(prj, "makedeps");\r
80 EdkLog.setLogLevel(prj.getProperty("env.LOGLEVEL"));\r
81 EdkLog.setLogger(logger);\r
82\r
83 ///\r
84 /// compose full tool path\r
85 ///\r
86 if (toolPath == null || toolPath.length() == 0) {\r
87 toolPath = "./" + cmdName;\r
88 } else {\r
89 if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {\r
90 toolPath = toolPath + cmdName;\r
91 } else {\r
92 toolPath = toolPath + "/" + cmdName;\r
93 }\r
94 }\r
95\r
96 ///\r
97 /// compose tool arguments\r
98 ///\r
99 StringBuffer args = new StringBuffer(4096);\r
100 if (ignoreError) {\r
101 args.append(" -ignorenotfound");\r
102 }\r
103 if (quietMode) {\r
104 args.append(" -q");\r
105 }\r
106 if (subDir != null && subDir.length() > 0) {\r
107 args.append(" -s ");\r
108 args.append(subDir);\r
109 }\r
110\r
111 ///\r
112 /// if there's no source files, we can do nothing about dependency\r
113 ///\r
114 if (inputFileList.size() == 0) {\r
115 throw new BuildException("No source files specified to scan");\r
116 }\r
117\r
118 ///\r
119 /// compose source file arguments\r
120 ///\r
121 Iterator iterator = inputFileList.iterator();\r
122 while (iterator.hasNext()) {\r
123 Input inputFile = (Input)iterator.next();\r
124 String inputFileString = cleanupPathName(inputFile.getFile());\r
125 args.append(" -f ");\r
126 args.append(inputFileString);\r
127 }\r
128\r
129 ///\r
130 /// compose search pathes argument\r
131 ///\r
132 StringBuffer includePathArg = new StringBuffer(4096);\r
133 if (includePath != null && includePath.length() > 0) {\r
134 StringTokenizer pathTokens = new StringTokenizer(includePath, ";");\r
135 while (pathTokens.hasMoreTokens()) {\r
136 String tmpPath = pathTokens.nextToken().trim();\r
137 if (tmpPath.length() == 0) {\r
138 continue;\r
139 }\r
140\r
141 includePathArg.append(" -i ");\r
142 includePathArg.append(cleanupPathName(tmpPath));\r
143 }\r
144 }\r
145 iterator = includePathList.iterator();\r
146 while (iterator.hasNext()) {\r
147 IncludePath path = (IncludePath)iterator.next();\r
148 includePathArg.append(cleanupPathName(path.getPath()));\r
149 }\r
150 args.append(includePathArg);\r
151\r
152 ///\r
153 /// We don't need a real target. So just a "dummy" is given\r
154 ///\r
155 args.append(" -target dummy");\r
156 args.append(" -o ");\r
157 args.append(cleanupPathName(depsFile));\r
158\r
159 ///\r
160 /// prepare to execute the tool\r
161 ///\r
162 Commandline cmd = new Commandline();\r
163 cmd.setExecutable(toolPath);\r
164 cmd.createArgument().setLine(args.toString());\r
165\r
166 LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);\r
167 Execute runner = new Execute(streamHandler, null);\r
168\r
169 runner.setAntRun(prj);\r
170 runner.setCommandline(cmd.getCommandline());\r
171\r
172 EdkLog.log(EdkLog.EDK_VERBOSE, Commandline.toString(cmd.getCommandline()));\r
173\r
174 int result = 0;\r
175 try {\r
176 result = runner.execute();\r
177 } catch (IOException e) {\r
178 throw new BuildException(e.getMessage());\r
179 }\r
180\r
181 if (result != 0) {\r
182 EdkLog.log(EdkLog.EDK_INFO, "MakeDeps failed!");\r
183 return;\r
184 }\r
185\r
186 // change the old DEP file format (makefile compatible) to just file list\r
187 if (!cleanup()) {\r
188 throw new BuildException(depsFile + " was not generated!");\r
189 }\r
190 }\r
191\r
192 ///\r
193 /// Remove any duplicated path separator or inconsistent path separator\r
194 ///\r
195 private String cleanupPathName(String path) {\r
196 String separator = "\\" + File.separator;\r
197 String duplicateSeparator = separator + "{2}";\r
198 path = Path.translateFile(path);\r
199 path = path.replaceAll(duplicateSeparator, separator);\r
200 return path;\r
201 }\r
202\r
203 /**\r
204 Set method for "DepsFile" attribute\r
205\r
206 @param name The name of dependency list file\r
207 **/\r
208 public void setDepsFile(String name) {\r
209 depsFile = cleanupPathName(name);\r
210 }\r
211\r
212 /**\r
213 Get method for "DepsFile" attribute\r
214\r
215 @returns The name of dependency list file\r
216 **/\r
217 public String getDepsFile() {\r
218 return depsFile;\r
219 }\r
220\r
221 /**\r
222 Set method for "IgnoreError" attribute\r
223\r
224 @param ignore flag to control error handling (true/false)\r
225 **/\r
226 public void setIgnoreError(boolean ignore) {\r
227 ignoreError = ignore;\r
228 }\r
229\r
230 /**\r
231 Get method for "IgnoreError" attribute\r
232\r
233 @returns The value of current IgnoreError flag\r
234 **/\r
235 public boolean getIgnoreError() {\r
236 return ignoreError;\r
237 }\r
238\r
239 /**\r
240 Set method for "QuietMode" attribute\r
241\r
242 @param quiet flag to control the output information (true/false)\r
243 **/\r
244 public void setQuietMode(boolean quiet) {\r
245 quietMode = quiet;\r
246 }\r
247\r
248 /**\r
249 Get method for "QuietMode" attribute\r
250\r
251 @returns value of current QuietMode flag\r
252 **/\r
253 public boolean getQuietMode() {\r
254 return quietMode;\r
255 }\r
256\r
257 /**\r
258 Set method for "SubDir" attribute\r
259\r
260 @param dir The name of sub-directory in which source files will be scanned\r
261 **/\r
262 public void setSubDir(String dir) {\r
263 subDir = dir;\r
264 }\r
265\r
266 /**\r
267 Get method for "SubDir" attribute\r
268\r
269 @returns The name of sub-directory\r
270 **/\r
271 public String getSubDir() {\r
272 return subDir;\r
273 }\r
274\r
275 /**\r
276 Set method for "IncludePath" attribute\r
277\r
278 @param path The name of include path\r
279 **/\r
280 public void setIncludePath(String path) {\r
281 includePath = cleanupPathName(path);\r
282 }\r
283\r
284 /**\r
285 Get method for "IncludePath" attribute\r
286\r
287 @returns The name of include path\r
288 **/\r
289 public String getIncludePath() {\r
290 return includePath;\r
291 }\r
292\r
293 /**\r
294 Set method for "ExtraDeps" attribute\r
295\r
296 @param deps The name of dependency file specified separately\r
297 **/\r
298 public void setExtraDeps(String deps) {\r
299 extraDeps = deps;\r
300 }\r
301\r
302 /**\r
303 Get method for "ExtraDeps" attribute\r
304\r
305 @returns The name of dependency file specified separately\r
306 **/\r
307 public String getExtraDeps () {\r
308 return extraDeps;\r
309 }\r
310\r
311 /**\r
312 Add method for "IncludePath" nested element\r
313\r
314 @param path The IncludePath object from nested IncludePath type of element\r
315 **/\r
316 public void addIncludepath(IncludePath path) {\r
317 includePathList.add(path);\r
318 }\r
319\r
320 /**\r
321 Add method for "Input" nested element\r
322\r
323 @param input The Input object from nested Input type of element\r
324 **/\r
325 public void addInput(Input inputFile) {\r
326 inputFileList.add(inputFile);\r
327 }\r
328\r
329 /**\r
330 The original file generated by MakeDeps.exe is for makefile uses. The target\r
331 part (before :) is not useful for ANT. This method will do the removal.\r
332\r
333 @returns true if cleaned files is saved successfully\r
334 @returns false if error occurs in file I/O system\r
335 **/\r
336 private boolean cleanup() {\r
337 File df = new File(depsFile);\r
338\r
339 if (!df.exists()) {\r
340 return false;\r
341 }\r
342\r
343 LineNumberReader lineReader = null;\r
344 FileReader fileReader = null;\r
345 Set<String> lineSet = new HashSet<String>(100); // used to remove duplicated lines\r
346 try {\r
347 fileReader = new FileReader(df);\r
348 lineReader = new LineNumberReader(fileReader);\r
349\r
350 ///\r
351 /// clean-up each line in deps file\r
352 //\r
353 String line = null;\r
354 while ((line = lineReader.readLine()) != null) {\r
355 String[] filePath = line.split(" : ");\r
356 if (filePath.length == 2) {\r
357 ///\r
358 /// keep the file name after ":"\r
359 ///\r
360 lineSet.add(cleanupPathName(filePath[1]));\r
361 }\r
362 }\r
363 lineReader.close();\r
364 fileReader.close();\r
365\r
366 ///\r
367 /// we may have explicitly specified dependency files\r
368 ///\r
369 StringTokenizer fileTokens = new StringTokenizer(extraDeps, ";");\r
370 while (fileTokens.hasMoreTokens()) {\r
371 lineSet.add(cleanupPathName(fileTokens.nextToken()));\r
372 }\r
373\r
374 ///\r
375 /// compose the final file content\r
376 ///\r
377 StringBuffer cleanedLines = new StringBuffer(40960);\r
378 Iterator<String> it = lineSet.iterator();\r
379 while (it.hasNext()) {\r
380 String filePath = it.next();\r
381 cleanedLines.append(filePath);\r
382 cleanedLines.append("\n");\r
383 }\r
384 ///\r
385 /// overwrite old dep file with new content\r
386 ///\r
387 FileWriter fileWriter = null;\r
388 fileWriter = new FileWriter(df);\r
389 fileWriter.write(cleanedLines.toString());\r
390 fileWriter.close();\r
391 } catch (IOException e) {\r
392 log (e.getMessage());\r
393 }\r
394\r
395 return true;\r
396 }\r
397\r
398 /**\r
399 Check if the dependency list file should be (re-)generated or not.\r
400\r
401 @returns true The dependency list file is uptodate. No re-generation is needed.\r
402 @returns false The dependency list file is outofdate. Re-generation is needed.\r
403 **/\r
404 private boolean isUptodate() {\r
405 File df = new File(depsFile);\r
406 if (!df.exists()) {\r
407 return false;\r
408 }\r
409\r
410 ///\r
411 /// If the source file(s) is newer than dependency list file, we need to\r
412 /// re-generate the dependency list file\r
413 ///\r
414 long depsFileTimeStamp = df.lastModified();\r
415 Iterator iterator = inputFileList.iterator();\r
416 while (iterator.hasNext()) {\r
417 Input inputFile = (Input)iterator.next();\r
418 File sf = new File(inputFile.getFile());\r
419 if (sf.lastModified() > depsFileTimeStamp) {\r
420 return false;\r
421 }\r
422 }\r
423\r
424 ///\r
425 /// If the source files haven't been changed since last time the dependency\r
426 /// list file was generated, we need to check each file in the file list to\r
427 /// see if any of them is changed or not. If anyone of them is newer than\r
428 /// the dependency list file, MakeDeps.exe is needed to run again.\r
429 ///\r
430 LineNumberReader lineReader = null;\r
431 FileReader fileReader = null;\r
432 boolean ret = true;\r
433 try {\r
434 fileReader = new FileReader(df);\r
435 lineReader = new LineNumberReader(fileReader);\r
436\r
437 String line = null;\r
438 while ((line = lineReader.readLine()) != null) {\r
439 File sourceFile = new File(line);\r
440 if (sourceFile.lastModified() > depsFileTimeStamp) {\r
441 ret = false;\r
442 break;\r
443 }\r
444 }\r
445 lineReader.close();\r
446 fileReader.close();\r
447 } catch (IOException e) {\r
448 log (e.getMessage());\r
449 }\r
450\r
451 return ret;\r
452 }\r
453}\r
454\r