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
.BufferedInputStream
;
18 import java
.io
.BufferedReader
;
19 import java
.io
.BufferedWriter
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.FileReader
;
23 import java
.io
.FileWriter
;
24 import java
.io
.IOException
;
25 import java
.io
.LineNumberReader
;
26 import java
.util
.HashMap
;
27 import java
.util
.HashSet
;
28 import java
.util
.Iterator
;
29 import java
.util
.LinkedHashSet
;
30 import java
.util
.List
;
32 import java
.util
.Stack
;
33 import java
.util
.regex
.Matcher
;
34 import java
.util
.regex
.Pattern
;
36 import org
.apache
.tools
.ant
.BuildException
;
37 import org
.apache
.tools
.ant
.Task
;
38 import org
.tianocore
.common
.cache
.FileTimeStamp
;
39 import org
.tianocore
.common
.logger
.EdkLog
;
42 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
44 public class MakeDeps
extends Task
{
47 // private members, use set/get to access them
49 private String targetFile
= "";
50 private String depsFilePath
= "";
51 private IncludePath includePathList
= new IncludePath();
52 private Input inputFileList
= new Input();
54 // cache the including files to speed up dependency check
56 private static HashMap
<String
, Set
<String
>> includesCache
= new HashMap
<String
, Set
<String
>>();
58 // regular expression for "#include ..." directive
60 private static final Pattern incPattern
= Pattern
.compile("[\n\r]+[ \t#]*[ \t]*include[ \t]+[\"<]*([^\n\r\"<>]+)[>\"]*[\n\r]+");
67 The Standard execute method for ANT task. It will check if it's necessary
68 to generate the dependency list file. If no file is found or the dependency
69 is changed, it will compose the command line and call MakeDeps.exe to
70 generate the dependency list file.
72 @throws BuildException
74 public void execute() throws BuildException
{
76 // if target file is specified and it hasn't been generated, don't generate
79 if (targetFile
.length() != 0 && (new File(targetFile
)).exists() == false) {
83 // check if the dependency list file is uptodate or not
90 // if no include path is specified, try locally
92 if (includePathList
.isEmpty()) {
93 includePathList
.insPath(".");
96 Set
<String
> depFiles
= getDependencies(inputFileList
.toArray());
98 File depsFile
= new File(depsFilePath
);
99 FileWriter fileWriter
= null;
100 BufferedWriter bufWriter
= null;
103 fileWriter
= new FileWriter(depsFile
);
104 bufWriter
= new BufferedWriter(fileWriter
);
107 for (Iterator it
= depFiles
.iterator(); it
.hasNext();) {
108 String depFile
= (String
)it
.next();
109 bufWriter
.write(depFile
, 0, depFile
.length());
110 bufWriter
.write("\n", 0, 1);
113 // put a "tab" at the end of file as file ending flag
115 bufWriter
.write("\t", 0, 1);
116 } catch (Exception e
) {
117 throw new BuildException(e
.getMessage());
120 if (bufWriter
!= null) {
123 if (fileWriter
!= null) {
126 } catch (Exception e
) {
127 throw new BuildException(e
.getMessage());
132 // update time stamp of dependency file
134 FileTimeStamp
.update(depsFilePath
, depsFile
.lastModified());
137 public void setTargetFile(String name
) {
141 public String
getTargetFile() {
146 Set method for "DepsFile" attribute
148 @param name The name of dependency list file
150 public void setDepsFile(String name
) {
155 Get method for "DepsFile" attribute
157 @returns The name of dependency list file
159 public String
getDepsFile() {
164 Add method for "IncludePath" nested element
166 @param path The IncludePath object from nested IncludePath type of element
168 public void addConfiguredIncludepath(IncludePath path
) {
169 includePathList
.insert(path
);
173 Add method for "Input" nested element
175 @param input The Input object from nested Input type of element
177 public void addConfiguredInput(Input inputFile
) {
178 inputFileList
.insert(inputFile
);
182 Check if the dependency list file should be (re-)generated or not.
184 @returns true The dependency list file is uptodate. No re-generation is needed.
185 @returns false The dependency list file is outofdate. Re-generation is needed.
187 private boolean isUptodate() {
188 File df
= new File(depsFilePath
);
190 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, depsFilePath
+ " doesn't exist!");
195 // If the source file(s) is newer than dependency list file, we need to
196 // re-generate the dependency list file
198 long depsFileTimeStamp
= FileTimeStamp
.get(depsFilePath
);
199 List
<String
> fileList
= inputFileList
.getNameList();
200 for (int i
= 0, length
= fileList
.size(); i
< length
; ++i
) {
201 String sf
= fileList
.get(i
);
202 if (FileTimeStamp
.get(sf
) > depsFileTimeStamp
) {
203 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, sf
+ " has been changed since last build!");
209 // If the source files haven't been changed since last time the dependency
210 // list file was generated, we need to check each file in the file list to
211 // see if any of them is changed or not. If anyone of them is newer than
212 // the dependency list file, MakeDeps.exe is needed to run again.
214 LineNumberReader lineReader
= null;
215 FileReader fileReader
= null;
218 fileReader
= new FileReader(df
);
219 lineReader
= new LineNumberReader(fileReader
);
223 while ((line
= lineReader
.readLine()) != null) {
225 // check file end flag "\t" to see if the .dep was generated correctly
227 if (line
.equals("\t")) {
235 if (line
.length() == 0) {
241 // If a file cannot be found (moved or removed) or newer, regenerate the dep file
243 File sourceFile
= new File(line
);
244 if ((!sourceFile
.exists()) || (FileTimeStamp
.get(line
) > depsFileTimeStamp
)) {
245 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, sourceFile
.getPath() + " has been (re)moved or changed since last build!");
252 // check if the .dep file is empty
255 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, depsFilePath
+ " is empty!");
258 } catch (IOException e
) {
259 throw new BuildException(e
.getMessage());
262 if (lineReader
!= null) {
265 if (fileReader
!= null) {
268 } catch (Exception e
) {
269 throw new BuildException(e
.getMessage());
277 // get dependent files list by parsing "#include" directive
279 private synchronized Set
<String
> getDependencies(String
[] sourceFiles
) {
280 Set
<String
> dependencies
= new LinkedHashSet
<String
>();
281 String
[] searchPathList
= includePathList
.toArray();
283 Stack
<String
> pendingFiles
= new Stack
<String
>();
284 for (int i
= 0; i
< sourceFiles
.length
; ++i
) {
285 File srcFile
= new File(sourceFiles
[i
]);
286 if (srcFile
.exists()) {
288 // a file must depend itself
290 dependencies
.add(srcFile
.getAbsolutePath());
291 pendingFiles
.push(sourceFiles
[i
]);
295 while (!pendingFiles
.empty()) {
296 String src
= pendingFiles
.pop();
297 File srcFile
= new File(src
);
298 if (!srcFile
.exists()) {
304 Set
<String
> incFiles
= includesCache
.get(src
);
305 if (incFiles
== null) {
306 incFiles
= new HashSet
<String
>();
307 FileInputStream fileReader
= null;
308 BufferedInputStream bufReader
= null;
309 String fileContent
= "";
310 int fileLength
= (int)srcFile
.length();
313 fileReader
= new FileInputStream(srcFile
);
314 bufReader
= new BufferedInputStream(fileReader
);
315 byte[] buf
= new byte[fileLength
];
317 bufReader
.read(buf
, 0, fileLength
);
319 // check if the file is utf-16 encoded
321 if (buf
[0] == (byte)0xff || buf
[0] == (byte)0xfe) {
322 fileContent
= new String(buf
, "UTF-16");
323 buf
= fileContent
.getBytes("UTF-8");
325 fileContent
= new String(buf
);
326 } catch (IOException e
) {
327 throw new BuildException(e
.getMessage());
330 if (bufReader
!= null) {
333 if (fileReader
!= null) {
336 } catch (Exception e
) {
337 throw new BuildException(e
.getMessage());
342 // find out all "#include" lines
344 Matcher matcher
= incPattern
.matcher(fileContent
);
345 while (matcher
.find()) {
346 String incFilePath
= fileContent
.substring(matcher
.start(1), matcher
.end(1));
347 incFiles
.add(incFilePath
);
351 // put the includes in cache to avoid re-parsing
353 includesCache
.put(src
, incFiles
);
357 // try each include search path to see if the include file exists or not
359 for (Iterator
<String
> it
= incFiles
.iterator(); it
.hasNext();) {
360 String depFilePath
= it
.next();
362 for (int i
= 0; i
< searchPathList
.length
; ++i
) {
363 File depFile
= new File(searchPathList
[i
] + File
.separator
+ depFilePath
);
364 String filePath
= depFile
.getAbsolutePath();
366 // following check is a must. it can prevent dead loop if two
367 // files include each other
369 if (depFile
.exists() && !dependencies
.contains(filePath
)) {
370 dependencies
.add(filePath
);
371 pendingFiles
.push(filePath
);