Disp dependencies in error in case of circular dependency
[mirror_edk2.git] / Tools / Java / Source / FrameworkTasks / org / tianocore / framework / tasks / MakeDeps.java
CommitLineData
878ddf1f 1/** @file\r
2This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate\r
3dependency files for source code.\r
4\r
5Copyright (c) 2006, Intel Corporation\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15package org.tianocore.framework.tasks;\r
16\r
5a4c4342 17import java.io.BufferedReader;\r
18import java.io.BufferedWriter;\r
878ddf1f 19import java.io.File;\r
20import java.io.FileReader;\r
5a4c4342 21import java.io.FileWriter;\r
878ddf1f 22import java.io.IOException;\r
23import java.io.LineNumberReader;\r
5a4c4342 24import java.util.HashMap;\r
25import java.util.HashSet;\r
26import java.util.Iterator;\r
27import java.util.LinkedHashSet;\r
878ddf1f 28import java.util.List;\r
5a4c4342 29import java.util.Set;\r
30import java.util.Stack;\r
31import java.util.regex.Matcher;\r
32import java.util.regex.Pattern;\r
878ddf1f 33\r
196ad8d7 34import org.apache.tools.ant.BuildException;\r
196ad8d7 35import org.apache.tools.ant.Task;\r
4b134847 36import org.tianocore.common.cache.FileTimeStamp;\r
5a4c4342 37import org.tianocore.common.logger.EdkLog;\r
196ad8d7 38\r
878ddf1f 39/**\r
40 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.\r
41 **/\r
42public class MakeDeps extends Task {\r
43\r
44 //\r
45 // private members, use set/get to access them\r
46 //\r
5a4c4342 47 private String depsFilePath = "";\r
48 private IncludePath includePathList = new IncludePath();\r
49 private Input inputFileList = new Input();\r
50 //\r
51 // cache the including files to speed up dependency check\r
52 // \r
53 private static HashMap<String, Set<String>> includesCache = new HashMap<String, Set<String>>();\r
54 //\r
55 // regular expression for "#include ..." directive\r
56 // \r
57 private static final Pattern incPattern = Pattern.compile("[\n\r \t]*#[ \t]*include[ \t\"<]+([^\n\r\"<>]+)");\r
878ddf1f 58\r
59 public MakeDeps() {\r
60\r
61 }\r
62\r
63 /**\r
64 The Standard execute method for ANT task. It will check if it's necessary\r
65 to generate the dependency list file. If no file is found or the dependency\r
66 is changed, it will compose the command line and call MakeDeps.exe to\r
67 generate the dependency list file.\r
68\r
69 @throws BuildException\r
70 **/\r
71 public void execute() throws BuildException {\r
5a4c4342 72 //\r
73 // check if the dependency list file is uptodate or not\r
74 //\r
878ddf1f 75 if (isUptodate()) {\r
76 return;\r
77 }\r
78\r
5a4c4342 79 //\r
80 // if no include path is specified, try locally\r
81 // \r
82 if (includePathList.isEmpty()) {\r
83 includePathList.insPath(".");\r
878ddf1f 84 }\r
85\r
5a4c4342 86 Set<String> depFiles = getDependencies(inputFileList.toArray());\r
878ddf1f 87\r
5a4c4342 88 File depsFile = new File(depsFilePath);\r
89 FileWriter fileWriter = null;\r
90 BufferedWriter bufWriter = null;\r
878ddf1f 91\r
5a4c4342 92 try {\r
93 fileWriter = new FileWriter(depsFile);\r
94 bufWriter = new BufferedWriter(fileWriter);\r
878ddf1f 95\r
219e2247 96\r
5a4c4342 97 for (Iterator it = depFiles.iterator(); it.hasNext();) {\r
98 String depFile = (String)it.next();\r
99 bufWriter.write(depFile, 0, depFile.length());\r
100 bufWriter.write("\n", 0, 1);\r
101 }\r
102 //\r
103 // put a "tab" at the end of file as file ending flag\r
104 // \r
105 bufWriter.write("\t", 0, 1);\r
106 } catch (Exception e) {\r
878ddf1f 107 throw new BuildException(e.getMessage());\r
5a4c4342 108 } finally {\r
109 try {\r
110 if (bufWriter != null) {\r
111 bufWriter.close();\r
112 }\r
113 if (fileWriter != null) {\r
114 fileWriter.close();\r
115 }\r
116 } catch (Exception e) {\r
117 throw new BuildException(e.getMessage());\r
118 }\r
878ddf1f 119 }\r
120\r
5a4c4342 121 //\r
122 // update time stamp of dependency file\r
123 // \r
124 FileTimeStamp.update(depsFilePath, depsFile.lastModified());\r
878ddf1f 125 }\r
126\r
878ddf1f 127 /**\r
128 Set method for "DepsFile" attribute\r
129\r
130 @param name The name of dependency list file\r
131 **/\r
132 public void setDepsFile(String name) {\r
5a4c4342 133 depsFilePath = name;\r
878ddf1f 134 }\r
135\r
136 /**\r
137 Get method for "DepsFile" attribute\r
138\r
139 @returns The name of dependency list file\r
140 **/\r
141 public String getDepsFile() {\r
5a4c4342 142 return depsFilePath;\r
878ddf1f 143 }\r
144\r
145 /**\r
146 Add method for "IncludePath" nested element\r
147\r
148 @param path The IncludePath object from nested IncludePath type of element\r
149 **/\r
0fdb42ac 150 public void addConfiguredIncludepath(IncludePath path) {\r
151 includePathList.insert(path);\r
878ddf1f 152 }\r
153\r
154 /**\r
155 Add method for "Input" nested element\r
156\r
157 @param input The Input object from nested Input type of element\r
158 **/\r
0fdb42ac 159 public void addConfiguredInput(Input inputFile) {\r
160 inputFileList.insert(inputFile);\r
878ddf1f 161 }\r
162\r
878ddf1f 163 /**\r
164 Check if the dependency list file should be (re-)generated or not.\r
165\r
166 @returns true The dependency list file is uptodate. No re-generation is needed.\r
167 @returns false The dependency list file is outofdate. Re-generation is needed.\r
168 **/\r
169 private boolean isUptodate() {\r
5a4c4342 170 File df = new File(depsFilePath);\r
878ddf1f 171 if (!df.exists()) {\r
5a4c4342 172 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " doesn't exist!");\r
878ddf1f 173 return false;\r
174 }\r
175\r
82810f3b 176 //\r
177 // If the source file(s) is newer than dependency list file, we need to\r
178 // re-generate the dependency list file\r
179 //\r
5a4c4342 180 long depsFileTimeStamp = FileTimeStamp.get(depsFilePath);\r
0fdb42ac 181 List<String> fileList = inputFileList.getNameList();\r
182 for (int i = 0, length = fileList.size(); i < length; ++i) {\r
4b134847 183 String sf = fileList.get(i);\r
184 if (FileTimeStamp.get(sf) > depsFileTimeStamp) {\r
185 EdkLog.log(this, EdkLog.EDK_VERBOSE, sf + " has been changed since last build!");\r
0fdb42ac 186 return false;\r
878ddf1f 187 }\r
188 }\r
189\r
82810f3b 190 //\r
191 // If the source files haven't been changed since last time the dependency\r
192 // list file was generated, we need to check each file in the file list to\r
193 // see if any of them is changed or not. If anyone of them is newer than\r
194 // the dependency list file, MakeDeps.exe is needed to run again.\r
195 //\r
878ddf1f 196 LineNumberReader lineReader = null;\r
197 FileReader fileReader = null;\r
93f5dd0a 198 boolean ret = false;\r
878ddf1f 199 try {\r
200 fileReader = new FileReader(df);\r
201 lineReader = new LineNumberReader(fileReader);\r
202\r
203 String line = null;\r
93f5dd0a 204 int lines = 0;\r
878ddf1f 205 while ((line = lineReader.readLine()) != null) {\r
93f5dd0a 206 //\r
207 // check file end flag "\t" to see if the .dep was generated correctly\r
208 // \r
209 if (line.equals("\t")) {\r
210 ret = true;\r
211 continue;\r
212 }\r
213 line = line.trim();\r
214 //\r
215 // skip empty line\r
216 // \r
217 if (line.length() == 0) {\r
218 continue;\r
219 }\r
220 ++lines;\r
221\r
1f08e795 222 //\r
223 // If a file cannot be found (moved or removed) or newer, regenerate the dep file\r
224 // \r
93f5dd0a 225 File sourceFile = new File(line);\r
4b134847 226 if ((!sourceFile.exists()) || (FileTimeStamp.get(line) > depsFileTimeStamp)) {\r
93f5dd0a 227 EdkLog.log(this, EdkLog.EDK_VERBOSE, sourceFile.getPath() + " has been (re)moved or changed since last build!");\r
878ddf1f 228 ret = false;\r
229 break;\r
230 }\r
231 }\r
93f5dd0a 232\r
233 //\r
234 // check if the .dep file is empty\r
235 // \r
236 if (lines == 0) {\r
5a4c4342 237 EdkLog.log(this, EdkLog.EDK_VERBOSE, depsFilePath + " is empty!");\r
93f5dd0a 238 ret = false;\r
239 }\r
878ddf1f 240 } catch (IOException e) {\r
93f5dd0a 241 throw new BuildException(e.getMessage());\r
5a4c4342 242 } finally {\r
243 try {\r
244 if (lineReader != null) {\r
245 lineReader.close();\r
246 }\r
247 if (fileReader != null) {\r
248 fileReader.close();\r
249 }\r
250 } catch (Exception e) {\r
251 throw new BuildException(e.getMessage());\r
252 }\r
878ddf1f 253 }\r
254\r
255 return ret;\r
256 }\r
5a4c4342 257\r
258 //\r
259 // get dependent files list by parsing "#include" directive\r
260 // \r
261 private synchronized Set<String> getDependencies(String[] sourceFiles) {\r
262 Set<String> dependencies = new LinkedHashSet<String>();\r
263 String[] searchPathList = includePathList.toArray();\r
264\r
265 Stack<String> pendingFiles = new Stack<String>();\r
266 for (int i = 0; i < sourceFiles.length; ++i) {\r
267 File srcFile = new File(sourceFiles[i]);\r
268 if (srcFile.exists()) {\r
269 //\r
270 // a file must depend itself\r
271 // \r
272 dependencies.add(srcFile.getAbsolutePath());\r
273 pendingFiles.push(sourceFiles[i]);\r
274 }\r
275 }\r
276\r
277 while (!pendingFiles.empty()) {\r
278 String src = pendingFiles.pop();\r
279 File srcFile = new File(src);\r
280 if (!srcFile.exists()) {\r
281 continue;\r
282 }\r
283\r
284 //\r
285 // try cache first\r
286 // \r
287 Set<String> incFiles = includesCache.get(src);\r
288 if (incFiles == null) {\r
289 incFiles = new HashSet<String>();\r
290 FileReader fileReader = null;\r
291 BufferedReader bufReader = null;\r
292 String fileContent = "";\r
293 int fileLength = (int)srcFile.length();\r
294\r
295 try {\r
296 fileReader = new FileReader(srcFile);\r
297 bufReader = new BufferedReader(fileReader);\r
298 char[] buf = new char[fileLength];\r
299\r
300 bufReader.read(buf, 0, fileLength);\r
301 fileContent = new String(buf);\r
302 } catch (IOException e) {\r
303 throw new BuildException(e.getMessage());\r
304 } finally {\r
305 try {\r
306 if (bufReader != null) {\r
307 bufReader.close();\r
308 }\r
309 if (fileReader != null) {\r
310 fileReader.close();\r
311 }\r
312 } catch (Exception e) {\r
313 throw new BuildException(e.getMessage());\r
314 }\r
315 }\r
316\r
317 //\r
318 // find out all "#include" lines\r
319 // \r
320 Matcher matcher = incPattern.matcher(fileContent);\r
321 while (matcher.find()) {\r
322 String incFilePath = fileContent.substring(matcher.start(1), matcher.end(1));\r
323 incFiles.add(incFilePath);\r
324 }\r
325\r
326 //\r
327 // put the includes in cache to avoid re-parsing\r
328 // \r
329 includesCache.put(src, incFiles);\r
330 }\r
331\r
332 //\r
333 // try each include search path to see if the include file exists or not\r
334 // \r
335 for (Iterator<String> it = incFiles.iterator(); it.hasNext();) {\r
336 String depFilePath = it.next();\r
337\r
338 for (int i = 0; i < searchPathList.length; ++i) {\r
339 File depFile = new File(searchPathList[i] + File.separator + depFilePath);\r
340 String filePath = depFile.getAbsolutePath();\r
341 //\r
342 // following check is a must. it can prevent dead loop if two\r
343 // files include each other\r
344 // \r
345 if (depFile.exists() && !dependencies.contains(filePath)) {\r
346 dependencies.add(filePath);\r
347 pendingFiles.push(filePath);\r
348 break;\r
349 }\r
350 }\r
351 }\r
352 }\r
353\r
354 return dependencies;\r
355 }\r
878ddf1f 356}\r
357\r