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