878ddf1f |
1 | /** @file\r |
2 | This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate\r |
3 | dependency files for source code.\r |
4 | \r |
5 | Copyright (c) 2006, Intel Corporation\r |
6 | All rights reserved. This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r |
9 | http://opensource.org/licenses/bsd-license.php\r |
10 | \r |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
13 | \r |
14 | **/\r |
15 | package org.tianocore.framework.tasks;\r |
16 | \r |
878ddf1f |
17 | import java.io.File;\r |
18 | import java.io.FileReader;\r |
878ddf1f |
19 | import java.io.IOException;\r |
20 | import java.io.LineNumberReader;\r |
21 | import java.util.ArrayList;\r |
22 | import java.util.Iterator;\r |
23 | import java.util.List;\r |
878ddf1f |
24 | \r |
196ad8d7 |
25 | import org.apache.tools.ant.BuildException;\r |
26 | import org.apache.tools.ant.Project;\r |
27 | import org.apache.tools.ant.Task;\r |
28 | import org.apache.tools.ant.taskdefs.Execute;\r |
29 | import org.apache.tools.ant.taskdefs.LogStreamHandler;\r |
30 | import org.apache.tools.ant.types.Commandline;\r |
31 | import org.apache.tools.ant.types.Path;\r |
ff225cbb |
32 | \r |
33 | import org.tianocore.common.logger.EdkLog;\r |
196ad8d7 |
34 | \r |
878ddf1f |
35 | /**\r |
36 | Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.\r |
37 | **/\r |
38 | public class MakeDeps extends Task {\r |
39 | \r |
40 | //\r |
41 | // private members, use set/get to access them\r |
42 | //\r |
43 | private static final String cmdName = "MakeDeps";\r |
878ddf1f |
44 | private String depsFile = null;\r |
45 | private String subDir = null;\r |
46 | private boolean quietMode = true;\r |
47 | private boolean ignoreError = true;\r |
48 | private String extraDeps = "";\r |
49 | private List<IncludePath> includePathList = new ArrayList<IncludePath>();\r |
50 | private List<Input> inputFileList = new ArrayList<Input>();\r |
51 | \r |
52 | public MakeDeps() {\r |
53 | \r |
54 | }\r |
55 | \r |
56 | /**\r |
57 | The Standard execute method for ANT task. It will check if it's necessary\r |
58 | to generate the dependency list file. If no file is found or the dependency\r |
59 | is changed, it will compose the command line and call MakeDeps.exe to\r |
60 | generate the dependency list file.\r |
61 | \r |
62 | @throws BuildException\r |
63 | **/\r |
64 | public void execute() throws BuildException {\r |
65 | ///\r |
66 | /// check if the dependency list file is uptodate or not\r |
67 | ///\r |
68 | if (isUptodate()) {\r |
69 | return;\r |
70 | }\r |
71 | \r |
72 | Project prj = this.getOwningTarget().getProject();\r |
2da8968b |
73 | String toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r |
219e2247 |
74 | \r |
878ddf1f |
75 | ///\r |
76 | /// compose full tool path\r |
77 | ///\r |
78 | if (toolPath == null || toolPath.length() == 0) {\r |
82810f3b |
79 | toolPath = cmdName;\r |
878ddf1f |
80 | } else {\r |
81 | if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {\r |
82 | toolPath = toolPath + cmdName;\r |
83 | } else {\r |
82810f3b |
84 | toolPath = toolPath + File.separator + cmdName;\r |
878ddf1f |
85 | }\r |
86 | }\r |
87 | \r |
88 | ///\r |
89 | /// compose tool arguments\r |
90 | ///\r |
91 | StringBuffer args = new StringBuffer(4096);\r |
92 | if (ignoreError) {\r |
82810f3b |
93 | args.append(" -ignorenotfound ");\r |
878ddf1f |
94 | }\r |
95 | if (quietMode) {\r |
82810f3b |
96 | args.append(" -q ");\r |
878ddf1f |
97 | }\r |
98 | if (subDir != null && subDir.length() > 0) {\r |
99 | args.append(" -s ");\r |
100 | args.append(subDir);\r |
101 | }\r |
102 | \r |
103 | ///\r |
104 | /// if there's no source files, we can do nothing about dependency\r |
ff225cbb |
105 | ///\r |
878ddf1f |
106 | if (inputFileList.size() == 0) {\r |
107 | throw new BuildException("No source files specified to scan");\r |
108 | }\r |
109 | \r |
110 | ///\r |
111 | /// compose source file arguments\r |
112 | ///\r |
82810f3b |
113 | for (int i = 0, listLength = inputFileList.size(); i < listLength; ++i) {\r |
114 | args.append(inputFileList.get(i).toString());\r |
878ddf1f |
115 | }\r |
116 | \r |
82810f3b |
117 | for (int i = 0, listLength = includePathList.size(); i < listLength; ++i) {\r |
118 | args.append(includePathList.get(i).toString());\r |
878ddf1f |
119 | }\r |
878ddf1f |
120 | \r |
121 | ///\r |
122 | /// We don't need a real target. So just a "dummy" is given\r |
123 | ///\r |
124 | args.append(" -target dummy");\r |
125 | args.append(" -o ");\r |
82810f3b |
126 | args.append(depsFile);\r |
878ddf1f |
127 | \r |
128 | ///\r |
129 | /// prepare to execute the tool\r |
130 | ///\r |
131 | Commandline cmd = new Commandline();\r |
132 | cmd.setExecutable(toolPath);\r |
133 | cmd.createArgument().setLine(args.toString());\r |
134 | \r |
135 | LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);\r |
136 | Execute runner = new Execute(streamHandler, null);\r |
137 | \r |
138 | runner.setAntRun(prj);\r |
139 | runner.setCommandline(cmd.getCommandline());\r |
140 | \r |
91f7d582 |
141 | EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmd.getCommandline()));\r |
219e2247 |
142 | \r |
878ddf1f |
143 | int result = 0;\r |
144 | try {\r |
145 | result = runner.execute();\r |
146 | } catch (IOException e) {\r |
147 | throw new BuildException(e.getMessage());\r |
148 | }\r |
149 | \r |
150 | if (result != 0) {\r |
91f7d582 |
151 | EdkLog.log(this, EdkLog.EDK_INFO, "MakeDeps failed!");\r |
82810f3b |
152 | throw new BuildException("MakeDeps: failed to generate dependency file!");\r |
878ddf1f |
153 | }\r |
878ddf1f |
154 | }\r |
155 | \r |
156 | ///\r |
157 | /// Remove any duplicated path separator or inconsistent path separator\r |
158 | ///\r |
159 | private String cleanupPathName(String path) {\r |
52cbbdbc |
160 | String separator = "\\" + File.separator;\r |
161 | String duplicateSeparator = separator + "{2}";\r |
162 | path = Path.translateFile(path);\r |
163 | path = path.replaceAll(duplicateSeparator, separator);\r |
878ddf1f |
164 | return path;\r |
165 | }\r |
166 | \r |
167 | /**\r |
168 | Set method for "DepsFile" attribute\r |
169 | \r |
170 | @param name The name of dependency list file\r |
171 | **/\r |
172 | public void setDepsFile(String name) {\r |
173 | depsFile = cleanupPathName(name);\r |
174 | }\r |
175 | \r |
176 | /**\r |
177 | Get method for "DepsFile" attribute\r |
178 | \r |
179 | @returns The name of dependency list file\r |
180 | **/\r |
181 | public String getDepsFile() {\r |
182 | return depsFile;\r |
183 | }\r |
184 | \r |
185 | /**\r |
186 | Set method for "IgnoreError" attribute\r |
187 | \r |
188 | @param ignore flag to control error handling (true/false)\r |
189 | **/\r |
190 | public void setIgnoreError(boolean ignore) {\r |
191 | ignoreError = ignore;\r |
192 | }\r |
193 | \r |
194 | /**\r |
195 | Get method for "IgnoreError" attribute\r |
196 | \r |
197 | @returns The value of current IgnoreError flag\r |
198 | **/\r |
199 | public boolean getIgnoreError() {\r |
200 | return ignoreError;\r |
201 | }\r |
202 | \r |
203 | /**\r |
204 | Set method for "QuietMode" attribute\r |
205 | \r |
206 | @param quiet flag to control the output information (true/false)\r |
207 | **/\r |
208 | public void setQuietMode(boolean quiet) {\r |
209 | quietMode = quiet;\r |
210 | }\r |
211 | \r |
212 | /**\r |
213 | Get method for "QuietMode" attribute\r |
214 | \r |
215 | @returns value of current QuietMode flag\r |
216 | **/\r |
217 | public boolean getQuietMode() {\r |
218 | return quietMode;\r |
219 | }\r |
220 | \r |
221 | /**\r |
222 | Set method for "SubDir" attribute\r |
223 | \r |
224 | @param dir The name of sub-directory in which source files will be scanned\r |
225 | **/\r |
226 | public void setSubDir(String dir) {\r |
82810f3b |
227 | subDir = cleanupPathName(dir);\r |
878ddf1f |
228 | }\r |
229 | \r |
230 | /**\r |
231 | Get method for "SubDir" attribute\r |
232 | \r |
233 | @returns The name of sub-directory\r |
234 | **/\r |
235 | public String getSubDir() {\r |
236 | return subDir;\r |
237 | }\r |
238 | \r |
878ddf1f |
239 | /**\r |
240 | Set method for "ExtraDeps" attribute\r |
241 | \r |
242 | @param deps The name of dependency file specified separately\r |
243 | **/\r |
244 | public void setExtraDeps(String deps) {\r |
82810f3b |
245 | extraDeps = cleanupPathName(deps);\r |
878ddf1f |
246 | }\r |
247 | \r |
248 | /**\r |
249 | Get method for "ExtraDeps" attribute\r |
250 | \r |
251 | @returns The name of dependency file specified separately\r |
252 | **/\r |
253 | public String getExtraDeps () {\r |
254 | return extraDeps;\r |
255 | }\r |
256 | \r |
257 | /**\r |
258 | Add method for "IncludePath" nested element\r |
259 | \r |
260 | @param path The IncludePath object from nested IncludePath type of element\r |
261 | **/\r |
262 | public void addIncludepath(IncludePath path) {\r |
263 | includePathList.add(path);\r |
264 | }\r |
265 | \r |
266 | /**\r |
267 | Add method for "Input" nested element\r |
268 | \r |
269 | @param input The Input object from nested Input type of element\r |
270 | **/\r |
271 | public void addInput(Input inputFile) {\r |
272 | inputFileList.add(inputFile);\r |
273 | }\r |
274 | \r |
878ddf1f |
275 | /**\r |
276 | Check if the dependency list file should be (re-)generated or not.\r |
277 | \r |
278 | @returns true The dependency list file is uptodate. No re-generation is needed.\r |
279 | @returns false The dependency list file is outofdate. Re-generation is needed.\r |
280 | **/\r |
281 | private boolean isUptodate() {\r |
282 | File df = new File(depsFile);\r |
283 | if (!df.exists()) {\r |
284 | return false;\r |
285 | }\r |
286 | \r |
82810f3b |
287 | //\r |
288 | // If the source file(s) is newer than dependency list file, we need to\r |
289 | // re-generate the dependency list file\r |
290 | //\r |
878ddf1f |
291 | long depsFileTimeStamp = df.lastModified();\r |
82810f3b |
292 | Iterator<Input> iterator = (Iterator<Input>)inputFileList.iterator();\r |
878ddf1f |
293 | while (iterator.hasNext()) {\r |
82810f3b |
294 | Input inputFile = iterator.next();\r |
295 | List<String> fileList = inputFile.getNameList();\r |
296 | for (int i = 0, length = fileList.size(); i < length; ++i) {\r |
297 | File sf = new File(fileList.get(i));\r |
298 | if (sf.lastModified() > depsFileTimeStamp) {\r |
299 | return false;\r |
300 | }\r |
878ddf1f |
301 | }\r |
302 | }\r |
303 | \r |
82810f3b |
304 | //\r |
305 | // If the source files haven't been changed since last time the dependency\r |
306 | // list file was generated, we need to check each file in the file list to\r |
307 | // see if any of them is changed or not. If anyone of them is newer than\r |
308 | // the dependency list file, MakeDeps.exe is needed to run again.\r |
309 | //\r |
878ddf1f |
310 | LineNumberReader lineReader = null;\r |
311 | FileReader fileReader = null;\r |
312 | boolean ret = true;\r |
313 | try {\r |
314 | fileReader = new FileReader(df);\r |
315 | lineReader = new LineNumberReader(fileReader);\r |
316 | \r |
317 | String line = null;\r |
318 | while ((line = lineReader.readLine()) != null) {\r |
319 | File sourceFile = new File(line);\r |
1f08e795 |
320 | //\r |
321 | // If a file cannot be found (moved or removed) or newer, regenerate the dep file\r |
322 | // \r |
323 | if ((!sourceFile.exists()) || (sourceFile.lastModified() > depsFileTimeStamp)) {\r |
878ddf1f |
324 | ret = false;\r |
325 | break;\r |
326 | }\r |
327 | }\r |
328 | lineReader.close();\r |
329 | fileReader.close();\r |
330 | } catch (IOException e) {\r |
331 | log (e.getMessage());\r |
332 | }\r |
333 | \r |
334 | return ret;\r |
335 | }\r |
336 | }\r |
337 | \r |