]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/MakeDeps.java
27e0ed7be5b7476b9dc4349e117395c77faac8d1
[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.BufferedReader;
18 import java.io.BufferedWriter;
19 import java.io.File;
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;
29 import java.util.Set;
30 import java.util.Stack;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
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;
42
43 /**
44 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
45 **/
46 public class MakeDeps extends Task {
47
48 //
49 // private members, use set/get to access them
50 //
51 private String depsFilePath = "";
52 private IncludePath includePathList = new IncludePath();
53 private Input inputFileList = new Input();
54 //
55 // cache the including files to speed up dependency check
56 //
57 private static HashMap<String, Set<String>> includesCache = new HashMap<String, Set<String>>();
58 //
59 // regular expression for "#include ..." directive
60 //
61 private static final Pattern incPattern = Pattern.compile("[\n\r \t]*#[ \t]*include[ \t\"<]+([^\n\r\"<>]+)");
62
63 public MakeDeps() {
64
65 }
66
67 /**
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.
72
73 @throws BuildException
74 **/
75 public void execute() throws BuildException {
76 //
77 // check if the dependency list file is uptodate or not
78 //
79 if (isUptodate()) {
80 return;
81 }
82
83 //
84 // if no include path is specified, try locally
85 //
86 if (includePathList.isEmpty()) {
87 includePathList.insPath(".");
88 }
89
90 Set<String> depFiles = getDependencies(inputFileList.toArray());
91
92 File depsFile = new File(depsFilePath);
93 FileWriter fileWriter = null;
94 BufferedWriter bufWriter = null;
95
96 try {
97 fileWriter = new FileWriter(depsFile);
98 bufWriter = new BufferedWriter(fileWriter);
99
100
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);
105 }
106 //
107 // put a "tab" at the end of file as file ending flag
108 //
109 bufWriter.write("\t", 0, 1);
110 } catch (Exception e) {
111 throw new BuildException(e.getMessage());
112 } finally {
113 try {
114 if (bufWriter != null) {
115 bufWriter.close();
116 }
117 if (fileWriter != null) {
118 fileWriter.close();
119 }
120 } catch (Exception e) {
121 throw new BuildException(e.getMessage());
122 }
123 }
124
125 //
126 // update time stamp of dependency file
127 //
128 FileTimeStamp.update(depsFilePath, depsFile.lastModified());
129 }
130
131 /**
132 Set method for "DepsFile" attribute
133
134 @param name The name of dependency list file
135 **/
136 public void setDepsFile(String name) {
137 depsFilePath = name;
138 }
139
140 /**
141 Get method for "DepsFile" attribute
142
143 @returns The name of dependency list file
144 **/
145 public String getDepsFile() {
146 return depsFilePath;
147 }
148
149 /**
150 Add method for "IncludePath" nested element
151
152 @param path The IncludePath object from nested IncludePath type of element
153 **/
154 public void addConfiguredIncludepath(IncludePath path) {
155 includePathList.insert(path);
156 }
157
158 /**
159 Add method for "Input" nested element
160
161 @param input The Input object from nested Input type of element
162 **/
163 public void addConfiguredInput(Input inputFile) {
164 inputFileList.insert(inputFile);
165 }
166
167 /**
168 Check if the dependency list file should be (re-)generated or not.
169
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.
172 **/
173 private boolean isUptodate() {
174 File df = new File(depsFilePath);
175 if (!df.exists()) {
176 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " doesn't exist!");
177 return false;
178 }
179
180 //
181 // If the source file(s) is newer than dependency list file, we need to
182 // re-generate the dependency list file
183 //
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!");
190 return false;
191 }
192 }
193
194 //
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.
199 //
200 LineNumberReader lineReader = null;
201 FileReader fileReader = null;
202 boolean ret = false;
203 try {
204 fileReader = new FileReader(df);
205 lineReader = new LineNumberReader(fileReader);
206
207 String line = null;
208 int lines = 0;
209 while ((line = lineReader.readLine()) != null) {
210 //
211 // check file end flag "\t" to see if the .dep was generated correctly
212 //
213 if (line.equals("\t")) {
214 ret = true;
215 continue;
216 }
217 line = line.trim();
218 //
219 // skip empty line
220 //
221 if (line.length() == 0) {
222 continue;
223 }
224 ++lines;
225
226 //
227 // If a file cannot be found (moved or removed) or newer, regenerate the dep file
228 //
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!");
232 ret = false;
233 break;
234 }
235 }
236
237 //
238 // check if the .dep file is empty
239 //
240 if (lines == 0) {
241 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " is empty!");
242 ret = false;
243 }
244 } catch (IOException e) {
245 throw new BuildException(e.getMessage());
246 } finally {
247 try {
248 if (lineReader != null) {
249 lineReader.close();
250 }
251 if (fileReader != null) {
252 fileReader.close();
253 }
254 } catch (Exception e) {
255 throw new BuildException(e.getMessage());
256 }
257 }
258
259 return ret;
260 }
261
262 //
263 // get dependent files list by parsing "#include" directive
264 //
265 private synchronized Set<String> getDependencies(String[] sourceFiles) {
266 Set<String> dependencies = new LinkedHashSet<String>();
267 String[] searchPathList = includePathList.toArray();
268
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()) {
273 //
274 // a file must depend itself
275 //
276 dependencies.add(srcFile.getAbsolutePath());
277 pendingFiles.push(sourceFiles[i]);
278 }
279 }
280
281 while (!pendingFiles.empty()) {
282 String src = pendingFiles.pop();
283 File srcFile = new File(src);
284 if (!srcFile.exists()) {
285 continue;
286 }
287
288 //
289 // try cache first
290 //
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();
298
299 try {
300 fileReader = new FileReader(srcFile);
301 bufReader = new BufferedReader(fileReader);
302 char[] buf = new char[fileLength];
303
304 bufReader.read(buf, 0, fileLength);
305 fileContent = new String(buf);
306 } catch (IOException e) {
307 throw new BuildException(e.getMessage());
308 } finally {
309 try {
310 if (bufReader != null) {
311 bufReader.close();
312 }
313 if (fileReader != null) {
314 fileReader.close();
315 }
316 } catch (Exception e) {
317 throw new BuildException(e.getMessage());
318 }
319 }
320
321 //
322 // find out all "#include" lines
323 //
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);
328 }
329
330 //
331 // put the includes in cache to avoid re-parsing
332 //
333 includesCache.put(src, incFiles);
334 }
335
336 //
337 // try each include search path to see if the include file exists or not
338 //
339 for (Iterator<String> it = incFiles.iterator(); it.hasNext();) {
340 String depFilePath = it.next();
341
342 for (int i = 0; i < searchPathList.length; ++i) {
343 File depFile = new File(searchPathList[i] + File.separator + depFilePath);
344 String filePath = depFile.getAbsolutePath();
345 //
346 // following check is a must. it can prevent dead loop if two
347 // files include each other
348 //
349 if (depFile.exists() && !dependencies.contains(filePath)) {
350 dependencies.add(filePath);
351 pendingFiles.push(filePath);
352 break;
353 }
354 }
355 }
356 }
357
358 return dependencies;
359 }
360 }
361