2 This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate
3 dependency files for source code.
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
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.
15 package org
.tianocore
.framework
.tasks
;
18 import java
.io
.FileReader
;
19 import java
.io
.FileWriter
;
20 import java
.io
.IOException
;
21 import java
.io
.LineNumberReader
;
22 import java
.util
.ArrayList
;
23 import java
.util
.HashSet
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import java
.util
.StringTokenizer
;
29 import org
.apache
.tools
.ant
.BuildException
;
30 import org
.apache
.tools
.ant
.Project
;
31 import org
.apache
.tools
.ant
.Task
;
32 import org
.apache
.tools
.ant
.taskdefs
.Execute
;
33 import org
.apache
.tools
.ant
.taskdefs
.LogStreamHandler
;
34 import org
.apache
.tools
.ant
.types
.Commandline
;
35 import org
.apache
.tools
.ant
.types
.Path
;
37 import org
.tianocore
.common
.logger
.EdkLog
;
40 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
42 public class MakeDeps
extends Task
{
45 // private members, use set/get to access them
47 private static final String cmdName
= "MakeDeps";
48 private String includePath
= null;
49 private String depsFile
= null;
50 private String subDir
= null;
51 private boolean quietMode
= true;
52 private boolean ignoreError
= true;
53 private String extraDeps
= "";
54 private List
<IncludePath
> includePathList
= new ArrayList
<IncludePath
>();
55 private List
<Input
> inputFileList
= new ArrayList
<Input
>();
62 The Standard execute method for ANT task. It will check if it's necessary
63 to generate the dependency list file. If no file is found or the dependency
64 is changed, it will compose the command line and call MakeDeps.exe to
65 generate the dependency list file.
67 @throws BuildException
69 public void execute() throws BuildException
{
71 /// check if the dependency list file is uptodate or not
77 Project prj
= this.getOwningTarget().getProject();
78 String toolPath
= prj
.getProperty("env.FRAMEWORK_TOOLS_PATH");
79 FrameworkLogger logger
= new FrameworkLogger(prj
, "makedeps");
80 EdkLog
.setLogLevel(prj
.getProperty("env.LOGLEVEL"));
81 EdkLog
.setLogger(logger
);
84 /// compose full tool path
86 if (toolPath
== null || toolPath
.length() == 0) {
87 toolPath
= "./" + cmdName
;
89 if (toolPath
.endsWith("/") || toolPath
.endsWith("\\")) {
90 toolPath
= toolPath
+ cmdName
;
92 toolPath
= toolPath
+ "/" + cmdName
;
97 /// compose tool arguments
99 StringBuffer args
= new StringBuffer(4096);
101 args
.append(" -ignorenotfound");
106 if (subDir
!= null && subDir
.length() > 0) {
112 /// if there's no source files, we can do nothing about dependency
114 if (inputFileList
.size() == 0) {
115 throw new BuildException("No source files specified to scan");
119 /// compose source file arguments
121 Iterator iterator
= inputFileList
.iterator();
122 while (iterator
.hasNext()) {
123 Input inputFile
= (Input
)iterator
.next();
124 String inputFileString
= cleanupPathName(inputFile
.getFile());
126 args
.append(inputFileString
);
130 /// compose search pathes argument
132 StringBuffer includePathArg
= new StringBuffer(4096);
133 if (includePath
!= null && includePath
.length() > 0) {
134 StringTokenizer pathTokens
= new StringTokenizer(includePath
, ";");
135 while (pathTokens
.hasMoreTokens()) {
136 String tmpPath
= pathTokens
.nextToken().trim();
137 if (tmpPath
.length() == 0) {
141 includePathArg
.append(" -i ");
142 includePathArg
.append(cleanupPathName(tmpPath
));
145 iterator
= includePathList
.iterator();
146 while (iterator
.hasNext()) {
147 IncludePath path
= (IncludePath
)iterator
.next();
148 includePathArg
.append(cleanupPathName(path
.getPath()));
150 args
.append(includePathArg
);
153 /// We don't need a real target. So just a "dummy" is given
155 args
.append(" -target dummy");
157 args
.append(cleanupPathName(depsFile
));
160 /// prepare to execute the tool
162 Commandline cmd
= new Commandline();
163 cmd
.setExecutable(toolPath
);
164 cmd
.createArgument().setLine(args
.toString());
166 LogStreamHandler streamHandler
= new LogStreamHandler(this, Project
.MSG_INFO
, Project
.MSG_WARN
);
167 Execute runner
= new Execute(streamHandler
, null);
169 runner
.setAntRun(prj
);
170 runner
.setCommandline(cmd
.getCommandline());
172 EdkLog
.log(EdkLog
.EDK_VERBOSE
, Commandline
.toString(cmd
.getCommandline()));
176 result
= runner
.execute();
177 } catch (IOException e
) {
178 throw new BuildException(e
.getMessage());
182 EdkLog
.log(EdkLog
.EDK_INFO
, "MakeDeps failed!");
186 // change the old DEP file format (makefile compatible) to just file list
188 throw new BuildException(depsFile
+ " was not generated!");
193 /// Remove any duplicated path separator or inconsistent path separator
195 private String
cleanupPathName(String path
) {
196 String separator
= "\\" + File
.separator
;
197 String duplicateSeparator
= separator
+ "{2}";
198 path
= Path
.translateFile(path
);
199 path
= path
.replaceAll(duplicateSeparator
, separator
);
204 Set method for "DepsFile" attribute
206 @param name The name of dependency list file
208 public void setDepsFile(String name
) {
209 depsFile
= cleanupPathName(name
);
213 Get method for "DepsFile" attribute
215 @returns The name of dependency list file
217 public String
getDepsFile() {
222 Set method for "IgnoreError" attribute
224 @param ignore flag to control error handling (true/false)
226 public void setIgnoreError(boolean ignore
) {
227 ignoreError
= ignore
;
231 Get method for "IgnoreError" attribute
233 @returns The value of current IgnoreError flag
235 public boolean getIgnoreError() {
240 Set method for "QuietMode" attribute
242 @param quiet flag to control the output information (true/false)
244 public void setQuietMode(boolean quiet
) {
249 Get method for "QuietMode" attribute
251 @returns value of current QuietMode flag
253 public boolean getQuietMode() {
258 Set method for "SubDir" attribute
260 @param dir The name of sub-directory in which source files will be scanned
262 public void setSubDir(String dir
) {
267 Get method for "SubDir" attribute
269 @returns The name of sub-directory
271 public String
getSubDir() {
276 Set method for "IncludePath" attribute
278 @param path The name of include path
280 public void setIncludePath(String path
) {
281 includePath
= cleanupPathName(path
);
285 Get method for "IncludePath" attribute
287 @returns The name of include path
289 public String
getIncludePath() {
294 Set method for "ExtraDeps" attribute
296 @param deps The name of dependency file specified separately
298 public void setExtraDeps(String deps
) {
303 Get method for "ExtraDeps" attribute
305 @returns The name of dependency file specified separately
307 public String
getExtraDeps () {
312 Add method for "IncludePath" nested element
314 @param path The IncludePath object from nested IncludePath type of element
316 public void addIncludepath(IncludePath path
) {
317 includePathList
.add(path
);
321 Add method for "Input" nested element
323 @param input The Input object from nested Input type of element
325 public void addInput(Input inputFile
) {
326 inputFileList
.add(inputFile
);
330 The original file generated by MakeDeps.exe is for makefile uses. The target
331 part (before :) is not useful for ANT. This method will do the removal.
333 @returns true if cleaned files is saved successfully
334 @returns false if error occurs in file I/O system
336 private boolean cleanup() {
337 File df
= new File(depsFile
);
343 LineNumberReader lineReader
= null;
344 FileReader fileReader
= null;
345 Set
<String
> lineSet
= new HashSet
<String
>(100); // used to remove duplicated lines
347 fileReader
= new FileReader(df
);
348 lineReader
= new LineNumberReader(fileReader
);
351 /// clean-up each line in deps file
354 while ((line
= lineReader
.readLine()) != null) {
355 String
[] filePath
= line
.split(" : ");
356 if (filePath
.length
== 2) {
358 /// keep the file name after ":"
360 lineSet
.add(cleanupPathName(filePath
[1]));
367 /// we may have explicitly specified dependency files
369 StringTokenizer fileTokens
= new StringTokenizer(extraDeps
, ";");
370 while (fileTokens
.hasMoreTokens()) {
371 lineSet
.add(cleanupPathName(fileTokens
.nextToken()));
375 /// compose the final file content
377 StringBuffer cleanedLines
= new StringBuffer(40960);
378 Iterator
<String
> it
= lineSet
.iterator();
379 while (it
.hasNext()) {
380 String filePath
= it
.next();
381 cleanedLines
.append(filePath
);
382 cleanedLines
.append("\n");
385 /// overwrite old dep file with new content
387 FileWriter fileWriter
= null;
388 fileWriter
= new FileWriter(df
);
389 fileWriter
.write(cleanedLines
.toString());
391 } catch (IOException e
) {
392 log (e
.getMessage());
399 Check if the dependency list file should be (re-)generated or not.
401 @returns true The dependency list file is uptodate. No re-generation is needed.
402 @returns false The dependency list file is outofdate. Re-generation is needed.
404 private boolean isUptodate() {
405 File df
= new File(depsFile
);
411 /// If the source file(s) is newer than dependency list file, we need to
412 /// re-generate the dependency list file
414 long depsFileTimeStamp
= df
.lastModified();
415 Iterator iterator
= inputFileList
.iterator();
416 while (iterator
.hasNext()) {
417 Input inputFile
= (Input
)iterator
.next();
418 File sf
= new File(inputFile
.getFile());
419 if (sf
.lastModified() > depsFileTimeStamp
) {
425 /// If the source files haven't been changed since last time the dependency
426 /// list file was generated, we need to check each file in the file list to
427 /// see if any of them is changed or not. If anyone of them is newer than
428 /// the dependency list file, MakeDeps.exe is needed to run again.
430 LineNumberReader lineReader
= null;
431 FileReader fileReader
= null;
434 fileReader
= new FileReader(df
);
435 lineReader
= new LineNumberReader(fileReader
);
438 while ((line
= lineReader
.readLine()) != null) {
439 File sourceFile
= new File(line
);
440 if (sourceFile
.lastModified() > depsFileTimeStamp
) {
447 } catch (IOException e
) {
448 log (e
.getMessage());