Corrected the regular expression because it will skip many includes.
[mirror_edk2.git] / Tools / Java / Source / FrameworkTasks / org / tianocore / framework / tasks / MakeDeps.java
1 /** @file
2 This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate
3 dependency files for source code.
4
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
10
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.
13
14 **/
15 package org.tianocore.framework.tasks;
16
17 import java.io.BufferedInputStream;
18 import java.io.BufferedReader;
19 import java.io.BufferedWriter;
20 import java.io.File;
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;
31 import java.util.Set;
32 import java.util.Stack;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
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;
40
41 /**
42 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
43 **/
44 public class MakeDeps extends Task {
45
46 //
47 // private members, use set/get to access them
48 //
49 private String targetFile = "";
50 private String depsFilePath = "";
51 private IncludePath includePathList = new IncludePath();
52 private Input inputFileList = new Input();
53 //
54 // cache the including files to speed up dependency check
55 //
56 private static HashMap<String, Set<String>> includesCache = new HashMap<String, Set<String>>();
57 //
58 // regular expression for "#include ..." directive
59 //
60 private static final Pattern incPattern = Pattern.compile("[\n\r \t#]*include[ \t]+[\"<]*([^\n\r\"<>]+)[>\" \t]*");
61
62 public MakeDeps() {
63
64 }
65
66 /**
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.
71
72 @throws BuildException
73 **/
74 public void execute() throws BuildException {
75 //
76 // if target file is specified and it hasn't been generated, don't generate
77 // dep file
78 //
79 if (targetFile.length() != 0 && (new File(targetFile)).exists() == false) {
80 return;
81 }
82 //
83 // check if the dependency list file is uptodate or not
84 //
85 if (isUptodate()) {
86 return;
87 }
88
89 //
90 // if no include path is specified, try locally
91 //
92 if (includePathList.isEmpty()) {
93 includePathList.insPath(".");
94 }
95
96 Set<String> depFiles = getDependencies(inputFileList.toArray());
97
98 File depsFile = new File(depsFilePath);
99 FileWriter fileWriter = null;
100 BufferedWriter bufWriter = null;
101
102 try {
103 fileWriter = new FileWriter(depsFile);
104 bufWriter = new BufferedWriter(fileWriter);
105
106
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);
111 }
112 //
113 // put a "tab" at the end of file as file ending flag
114 //
115 bufWriter.write("\t", 0, 1);
116 } catch (Exception e) {
117 throw new BuildException(e.getMessage());
118 } finally {
119 try {
120 if (bufWriter != null) {
121 bufWriter.close();
122 }
123 if (fileWriter != null) {
124 fileWriter.close();
125 }
126 } catch (Exception e) {
127 throw new BuildException(e.getMessage());
128 }
129 }
130
131 //
132 // update time stamp of dependency file
133 //
134 FileTimeStamp.update(depsFilePath, depsFile.lastModified());
135 }
136
137 public void setTargetFile(String name) {
138 targetFile = name;
139 }
140
141 public String getTargetFile() {
142 return targetFile;
143 }
144
145 /**
146 Set method for "DepsFile" attribute
147
148 @param name The name of dependency list file
149 **/
150 public void setDepsFile(String name) {
151 depsFilePath = name;
152 }
153
154 /**
155 Get method for "DepsFile" attribute
156
157 @returns The name of dependency list file
158 **/
159 public String getDepsFile() {
160 return depsFilePath;
161 }
162
163 /**
164 Add method for "IncludePath" nested element
165
166 @param path The IncludePath object from nested IncludePath type of element
167 **/
168 public void addConfiguredIncludepath(IncludePath path) {
169 includePathList.insert(path);
170 }
171
172 /**
173 Add method for "Input" nested element
174
175 @param input The Input object from nested Input type of element
176 **/
177 public void addConfiguredInput(Input inputFile) {
178 inputFileList.insert(inputFile);
179 }
180
181 /**
182 Check if the dependency list file should be (re-)generated or not.
183
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.
186 **/
187 private boolean isUptodate() {
188 File df = new File(depsFilePath);
189 if (!df.exists()) {
190 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " doesn't exist!");
191 return false;
192 }
193
194 //
195 // If the source file(s) is newer than dependency list file, we need to
196 // re-generate the dependency list file
197 //
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!");
204 return false;
205 }
206 }
207
208 //
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.
213 //
214 LineNumberReader lineReader = null;
215 FileReader fileReader = null;
216 boolean ret = false;
217 try {
218 fileReader = new FileReader(df);
219 lineReader = new LineNumberReader(fileReader);
220
221 String line = null;
222 int lines = 0;
223 while ((line = lineReader.readLine()) != null) {
224 //
225 // check file end flag "\t" to see if the .dep was generated correctly
226 //
227 if (line.equals("\t")) {
228 ret = true;
229 continue;
230 }
231 line = line.trim();
232 //
233 // skip empty line
234 //
235 if (line.length() == 0) {
236 continue;
237 }
238 ++lines;
239
240 //
241 // If a file cannot be found (moved or removed) or newer, regenerate the dep file
242 //
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!");
246 ret = false;
247 break;
248 }
249 }
250
251 //
252 // check if the .dep file is empty
253 //
254 if (lines == 0) {
255 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " is empty!");
256 ret = false;
257 }
258 } catch (IOException e) {
259 throw new BuildException(e.getMessage());
260 } finally {
261 try {
262 if (lineReader != null) {
263 lineReader.close();
264 }
265 if (fileReader != null) {
266 fileReader.close();
267 }
268 } catch (Exception e) {
269 throw new BuildException(e.getMessage());
270 }
271 }
272
273 return ret;
274 }
275
276 //
277 // get dependent files list by parsing "#include" directive
278 //
279 private synchronized Set<String> getDependencies(String[] sourceFiles) {
280 Set<String> dependencies = new LinkedHashSet<String>();
281 String[] searchPathList = includePathList.toArray();
282
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()) {
287 //
288 // a file must depend itself
289 //
290 dependencies.add(srcFile.getAbsolutePath());
291 pendingFiles.push(sourceFiles[i]);
292 }
293 }
294
295 while (!pendingFiles.empty()) {
296 String src = pendingFiles.pop();
297 File srcFile = new File(src);
298 if (!srcFile.exists()) {
299 continue;
300 }
301 //
302 // try cache first
303 //
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();
311
312 try {
313 fileReader = new FileInputStream(srcFile);
314 bufReader = new BufferedInputStream(fileReader);
315 byte[] buf = new byte[fileLength];
316
317 bufReader.read(buf, 0, fileLength);
318 //
319 // check if the file is utf-16 encoded
320 //
321 if (buf[0] == (byte)0xff || buf[0] == (byte)0xfe) {
322 fileContent = new String(buf, "UTF-16");
323 buf = fileContent.getBytes("UTF-8");
324 }
325 fileContent = new String(buf);
326 } catch (IOException e) {
327 throw new BuildException(e.getMessage());
328 } finally {
329 try {
330 if (bufReader != null) {
331 bufReader.close();
332 }
333 if (fileReader != null) {
334 fileReader.close();
335 }
336 } catch (Exception e) {
337 throw new BuildException(e.getMessage());
338 }
339 }
340
341 //
342 // find out all "#include" lines
343 //
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);
348 }
349
350 //
351 // put the includes in cache to avoid re-parsing
352 //
353 includesCache.put(src, incFiles);
354 }
355
356 //
357 // try each include search path to see if the include file exists or not
358 //
359 for (Iterator<String> it = incFiles.iterator(); it.hasNext();) {
360 String depFilePath = it.next();
361
362 for (int i = 0; i < searchPathList.length; ++i) {
363 File depFile = new File(searchPathList[i] + File.separator + depFilePath);
364 String filePath = depFile.getAbsolutePath();
365 //
366 // following check is a must. it can prevent dead loop if two
367 // files include each other
368 //
369 if (depFile.exists() && !dependencies.contains(filePath)) {
370 dependencies.add(filePath);
371 pendingFiles.push(filePath);
372 break;
373 }
374 }
375 }
376 }
377
378 return dependencies;
379 }
380 }
381