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
;
17 import java
.io
.BufferedReader
;
18 import java
.io
.BufferedWriter
;
20 import java
.io
.FileReader
;
21 import java
.io
.FileWriter
;
22 import java
.io
.IOException
;
23 import java
.io
.LineNumberReader
;
24 import java
.util
.HashMap
;
25 import java
.util
.HashSet
;
26 import java
.util
.Iterator
;
27 import java
.util
.LinkedHashSet
;
28 import java
.util
.List
;
30 import java
.util
.Stack
;
31 import java
.util
.regex
.Matcher
;
32 import java
.util
.regex
.Pattern
;
34 import org
.apache
.tools
.ant
.BuildException
;
35 import org
.apache
.tools
.ant
.Project
;
36 import org
.apache
.tools
.ant
.Task
;
37 import org
.apache
.tools
.ant
.taskdefs
.Execute
;
38 import org
.apache
.tools
.ant
.taskdefs
.LogStreamHandler
;
39 import org
.apache
.tools
.ant
.types
.Commandline
;
40 import org
.tianocore
.common
.cache
.FileTimeStamp
;
41 import org
.tianocore
.common
.logger
.EdkLog
;
44 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
46 public class MakeDeps
extends Task
{
49 // private members, use set/get to access them
51 private String depsFilePath
= "";
52 private IncludePath includePathList
= new IncludePath();
53 private Input inputFileList
= new Input();
55 // cache the including files to speed up dependency check
57 private static HashMap
<String
, Set
<String
>> includesCache
= new HashMap
<String
, Set
<String
>>();
59 // regular expression for "#include ..." directive
61 private static final Pattern incPattern
= Pattern
.compile("[\n\r \t]*#[ \t]*include[ \t\"<]+([^\n\r\"<>]+)");
68 The Standard execute method for ANT task. It will check if it's necessary
69 to generate the dependency list file. If no file is found or the dependency
70 is changed, it will compose the command line and call MakeDeps.exe to
71 generate the dependency list file.
73 @throws BuildException
75 public void execute() throws BuildException
{
77 // check if the dependency list file is uptodate or not
84 // if no include path is specified, try locally
86 if (includePathList
.isEmpty()) {
87 includePathList
.insPath(".");
90 Set
<String
> depFiles
= getDependencies(inputFileList
.toArray());
92 File depsFile
= new File(depsFilePath
);
93 FileWriter fileWriter
= null;
94 BufferedWriter bufWriter
= null;
97 fileWriter
= new FileWriter(depsFile
);
98 bufWriter
= new BufferedWriter(fileWriter
);
101 for (Iterator it
= depFiles
.iterator(); it
.hasNext();) {
102 String depFile
= (String
)it
.next();
103 bufWriter
.write(depFile
, 0, depFile
.length());
104 bufWriter
.write("\n", 0, 1);
107 // put a "tab" at the end of file as file ending flag
109 bufWriter
.write("\t", 0, 1);
110 } catch (Exception e
) {
111 throw new BuildException(e
.getMessage());
114 if (bufWriter
!= null) {
117 if (fileWriter
!= null) {
120 } catch (Exception e
) {
121 throw new BuildException(e
.getMessage());
126 // update time stamp of dependency file
128 FileTimeStamp
.update(depsFilePath
, depsFile
.lastModified());
132 Set method for "DepsFile" attribute
134 @param name The name of dependency list file
136 public void setDepsFile(String name
) {
141 Get method for "DepsFile" attribute
143 @returns The name of dependency list file
145 public String
getDepsFile() {
150 Add method for "IncludePath" nested element
152 @param path The IncludePath object from nested IncludePath type of element
154 public void addConfiguredIncludepath(IncludePath path
) {
155 includePathList
.insert(path
);
159 Add method for "Input" nested element
161 @param input The Input object from nested Input type of element
163 public void addConfiguredInput(Input inputFile
) {
164 inputFileList
.insert(inputFile
);
168 Check if the dependency list file should be (re-)generated or not.
170 @returns true The dependency list file is uptodate. No re-generation is needed.
171 @returns false The dependency list file is outofdate. Re-generation is needed.
173 private boolean isUptodate() {
174 File df
= new File(depsFilePath
);
176 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, depsFilePath
+ " doesn't exist!");
181 // If the source file(s) is newer than dependency list file, we need to
182 // re-generate the dependency list file
184 long depsFileTimeStamp
= FileTimeStamp
.get(depsFilePath
);
185 List
<String
> fileList
= inputFileList
.getNameList();
186 for (int i
= 0, length
= fileList
.size(); i
< length
; ++i
) {
187 String sf
= fileList
.get(i
);
188 if (FileTimeStamp
.get(sf
) > depsFileTimeStamp
) {
189 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, sf
+ " has been changed since last build!");
195 // If the source files haven't been changed since last time the dependency
196 // list file was generated, we need to check each file in the file list to
197 // see if any of them is changed or not. If anyone of them is newer than
198 // the dependency list file, MakeDeps.exe is needed to run again.
200 LineNumberReader lineReader
= null;
201 FileReader fileReader
= null;
204 fileReader
= new FileReader(df
);
205 lineReader
= new LineNumberReader(fileReader
);
209 while ((line
= lineReader
.readLine()) != null) {
211 // check file end flag "\t" to see if the .dep was generated correctly
213 if (line
.equals("\t")) {
221 if (line
.length() == 0) {
227 // If a file cannot be found (moved or removed) or newer, regenerate the dep file
229 File sourceFile
= new File(line
);
230 if ((!sourceFile
.exists()) || (FileTimeStamp
.get(line
) > depsFileTimeStamp
)) {
231 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, sourceFile
.getPath() + " has been (re)moved or changed since last build!");
238 // check if the .dep file is empty
241 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, depsFilePath
+ " is empty!");
244 } catch (IOException e
) {
245 throw new BuildException(e
.getMessage());
248 if (lineReader
!= null) {
251 if (fileReader
!= null) {
254 } catch (Exception e
) {
255 throw new BuildException(e
.getMessage());
263 // get dependent files list by parsing "#include" directive
265 private synchronized Set
<String
> getDependencies(String
[] sourceFiles
) {
266 Set
<String
> dependencies
= new LinkedHashSet
<String
>();
267 String
[] searchPathList
= includePathList
.toArray();
269 Stack
<String
> pendingFiles
= new Stack
<String
>();
270 for (int i
= 0; i
< sourceFiles
.length
; ++i
) {
271 File srcFile
= new File(sourceFiles
[i
]);
272 if (srcFile
.exists()) {
274 // a file must depend itself
276 dependencies
.add(srcFile
.getAbsolutePath());
277 pendingFiles
.push(sourceFiles
[i
]);
281 while (!pendingFiles
.empty()) {
282 String src
= pendingFiles
.pop();
283 File srcFile
= new File(src
);
284 if (!srcFile
.exists()) {
291 Set
<String
> incFiles
= includesCache
.get(src
);
292 if (incFiles
== null) {
293 incFiles
= new HashSet
<String
>();
294 FileReader fileReader
= null;
295 BufferedReader bufReader
= null;
296 String fileContent
= "";
297 int fileLength
= (int)srcFile
.length();
300 fileReader
= new FileReader(srcFile
);
301 bufReader
= new BufferedReader(fileReader
);
302 char[] buf
= new char[fileLength
];
304 bufReader
.read(buf
, 0, fileLength
);
305 fileContent
= new String(buf
);
306 } catch (IOException e
) {
307 throw new BuildException(e
.getMessage());
310 if (bufReader
!= null) {
313 if (fileReader
!= null) {
316 } catch (Exception e
) {
317 throw new BuildException(e
.getMessage());
322 // find out all "#include" lines
324 Matcher matcher
= incPattern
.matcher(fileContent
);
325 while (matcher
.find()) {
326 String incFilePath
= fileContent
.substring(matcher
.start(1), matcher
.end(1));
327 incFiles
.add(incFilePath
);
331 // put the includes in cache to avoid re-parsing
333 includesCache
.put(src
, incFiles
);
337 // try each include search path to see if the include file exists or not
339 for (Iterator
<String
> it
= incFiles
.iterator(); it
.hasNext();) {
340 String depFilePath
= it
.next();
342 for (int i
= 0; i
< searchPathList
.length
; ++i
) {
343 File depFile
= new File(searchPathList
[i
] + File
.separator
+ depFilePath
);
344 String filePath
= depFile
.getAbsolutePath();
346 // following check is a must. it can prevent dead loop if two
347 // files include each other
349 if (depFile
.exists() && !dependencies
.contains(filePath
)) {
350 dependencies
.add(filePath
);
351 pendingFiles
.push(filePath
);