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!");
188 /// Remove any duplicated path separator or inconsistent path separator
190 private String
cleanupPathName(String path
) {
191 String separator
= "\\" + File
.separator
;
192 String duplicateSeparator
= separator
+ "{2}";
193 path
= Path
.translateFile(path
);
194 path
= path
.replaceAll(duplicateSeparator
, separator
);
199 Set method for "DepsFile" attribute
201 @param name The name of dependency list file
203 public void setDepsFile(String name
) {
204 depsFile
= cleanupPathName(name
);
208 Get method for "DepsFile" attribute
210 @returns The name of dependency list file
212 public String
getDepsFile() {
217 Set method for "IgnoreError" attribute
219 @param ignore flag to control error handling (true/false)
221 public void setIgnoreError(boolean ignore
) {
222 ignoreError
= ignore
;
226 Get method for "IgnoreError" attribute
228 @returns The value of current IgnoreError flag
230 public boolean getIgnoreError() {
235 Set method for "QuietMode" attribute
237 @param quiet flag to control the output information (true/false)
239 public void setQuietMode(boolean quiet
) {
244 Get method for "QuietMode" attribute
246 @returns value of current QuietMode flag
248 public boolean getQuietMode() {
253 Set method for "SubDir" attribute
255 @param dir The name of sub-directory in which source files will be scanned
257 public void setSubDir(String dir
) {
262 Get method for "SubDir" attribute
264 @returns The name of sub-directory
266 public String
getSubDir() {
271 Set method for "IncludePath" attribute
273 @param path The name of include path
275 public void setIncludePath(String path
) {
276 includePath
= cleanupPathName(path
);
280 Get method for "IncludePath" attribute
282 @returns The name of include path
284 public String
getIncludePath() {
289 Set method for "ExtraDeps" attribute
291 @param deps The name of dependency file specified separately
293 public void setExtraDeps(String deps
) {
298 Get method for "ExtraDeps" attribute
300 @returns The name of dependency file specified separately
302 public String
getExtraDeps () {
307 Add method for "IncludePath" nested element
309 @param path The IncludePath object from nested IncludePath type of element
311 public void addIncludepath(IncludePath path
) {
312 includePathList
.add(path
);
316 Add method for "Input" nested element
318 @param input The Input object from nested Input type of element
320 public void addInput(Input inputFile
) {
321 inputFileList
.add(inputFile
);
325 Check if the dependency list file should be (re-)generated or not.
327 @returns true The dependency list file is uptodate. No re-generation is needed.
328 @returns false The dependency list file is outofdate. Re-generation is needed.
330 private boolean isUptodate() {
331 File df
= new File(depsFile
);
337 /// If the source file(s) is newer than dependency list file, we need to
338 /// re-generate the dependency list file
340 long depsFileTimeStamp
= df
.lastModified();
341 Iterator iterator
= inputFileList
.iterator();
342 while (iterator
.hasNext()) {
343 Input inputFile
= (Input
)iterator
.next();
344 File sf
= new File(inputFile
.getFile());
345 if (sf
.lastModified() > depsFileTimeStamp
) {
351 /// If the source files haven't been changed since last time the dependency
352 /// list file was generated, we need to check each file in the file list to
353 /// see if any of them is changed or not. If anyone of them is newer than
354 /// the dependency list file, MakeDeps.exe is needed to run again.
356 LineNumberReader lineReader
= null;
357 FileReader fileReader
= null;
360 fileReader
= new FileReader(df
);
361 lineReader
= new LineNumberReader(fileReader
);
364 while ((line
= lineReader
.readLine()) != null) {
365 File sourceFile
= new File(line
);
366 if (sourceFile
.lastModified() > depsFileTimeStamp
) {
373 } catch (IOException e
) {
374 log (e
.getMessage());