]>
Commit | Line | Data |
---|---|---|
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 | |
17 | import java.io.File;\r | |
18 | import java.io.FileReader;\r | |
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 | |
24 | \r | |
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 | |
32 | \r | |
33 | import org.tianocore.common.logger.EdkLog;\r | |
34 | \r | |
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 toolName = "MakeDeps";\r | |
44 | private FileArg depsFile = new FileArg();\r | |
45 | private ToolArg subDir = new ToolArg();\r | |
46 | private ToolArg quietMode = new ToolArg(" -", "q");\r | |
47 | private ToolArg ignoreError = new ToolArg(" -", "ignorenotfound");\r | |
48 | private IncludePath includePathList = new IncludePath();\r | |
49 | private Input inputFileList = new Input();\r | |
50 | private ToolArg target = new FileArg(" -target ", "dummy");\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 | |
73 | String toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r | |
74 | \r | |
75 | ///\r | |
76 | /// compose full tool path\r | |
77 | ///\r | |
78 | if (toolPath == null || toolPath.length() == 0) {\r | |
79 | toolPath = toolName;\r | |
80 | } else {\r | |
81 | if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {\r | |
82 | toolPath = toolPath + toolName;\r | |
83 | } else {\r | |
84 | toolPath = toolPath + File.separator + toolName;\r | |
85 | }\r | |
86 | }\r | |
87 | \r | |
88 | ///\r | |
89 | /// compose tool arguments\r | |
90 | ///\r | |
91 | String argument = "" + inputFileList + includePathList + subDir\r | |
92 | + quietMode + ignoreError + target + depsFile;\r | |
93 | \r | |
94 | ///\r | |
95 | /// prepare to execute the tool\r | |
96 | ///\r | |
97 | Commandline cmd = new Commandline();\r | |
98 | cmd.setExecutable(toolPath);\r | |
99 | cmd.createArgument().setLine(argument);\r | |
100 | \r | |
101 | LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);\r | |
102 | Execute runner = new Execute(streamHandler, null);\r | |
103 | \r | |
104 | runner.setAntRun(prj);\r | |
105 | runner.setCommandline(cmd.getCommandline());\r | |
106 | \r | |
107 | EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmd.getCommandline()));\r | |
108 | \r | |
109 | int result = 0;\r | |
110 | try {\r | |
111 | result = runner.execute();\r | |
112 | } catch (IOException e) {\r | |
113 | throw new BuildException(e.getMessage());\r | |
114 | }\r | |
115 | \r | |
116 | if (result != 0) {\r | |
117 | EdkLog.log(this, EdkLog.EDK_INFO, toolName + " failed!");\r | |
118 | throw new BuildException(toolName + ": failed to generate dependency file!");\r | |
119 | } else {\r | |
120 | EdkLog.log(this, EdkLog.EDK_VERBOSE, toolName + " succeeded!");\r | |
121 | }\r | |
122 | }\r | |
123 | \r | |
124 | /**\r | |
125 | Set method for "DepsFile" attribute\r | |
126 | \r | |
127 | @param name The name of dependency list file\r | |
128 | **/\r | |
129 | public void setDepsFile(String name) {\r | |
130 | depsFile.setArg(" -o ", name);\r | |
131 | }\r | |
132 | \r | |
133 | /**\r | |
134 | Get method for "DepsFile" attribute\r | |
135 | \r | |
136 | @returns The name of dependency list file\r | |
137 | **/\r | |
138 | public String getDepsFile() {\r | |
139 | return depsFile.getValue();\r | |
140 | }\r | |
141 | \r | |
142 | /**\r | |
143 | Set method for "IgnoreError" attribute\r | |
144 | \r | |
145 | @param ignore flag to control error handling (true/false)\r | |
146 | **/\r | |
147 | public void setIgnoreError(boolean ignore) {\r | |
148 | if (!ignore) {\r | |
149 | ignoreError.setArg(" ", " ");\r | |
150 | }\r | |
151 | }\r | |
152 | \r | |
153 | /**\r | |
154 | Get method for "IgnoreError" attribute\r | |
155 | \r | |
156 | @returns The value of current IgnoreError flag\r | |
157 | **/\r | |
158 | public boolean getIgnoreError() {\r | |
159 | return ignoreError.getValue().length() > 0;\r | |
160 | }\r | |
161 | \r | |
162 | /**\r | |
163 | Set method for "QuietMode" attribute\r | |
164 | \r | |
165 | @param quiet flag to control the output information (true/false)\r | |
166 | **/\r | |
167 | public void setQuietMode(boolean quiet) {\r | |
168 | if (!quiet) {\r | |
169 | quietMode.setArg(" ", " ");\r | |
170 | }\r | |
171 | }\r | |
172 | \r | |
173 | /**\r | |
174 | Get method for "QuietMode" attribute\r | |
175 | \r | |
176 | @returns value of current QuietMode flag\r | |
177 | **/\r | |
178 | public boolean getQuietMode() {\r | |
179 | return quietMode.getValue().length() > 0;\r | |
180 | }\r | |
181 | \r | |
182 | /**\r | |
183 | Set method for "SubDir" attribute\r | |
184 | \r | |
185 | @param dir The name of sub-directory in which source files will be scanned\r | |
186 | **/\r | |
187 | public void setSubDir(String dir) {\r | |
188 | subDir.setArg(" -s ", dir);\r | |
189 | }\r | |
190 | \r | |
191 | /**\r | |
192 | Get method for "SubDir" attribute\r | |
193 | \r | |
194 | @returns The name of sub-directory\r | |
195 | **/\r | |
196 | public String getSubDir() {\r | |
197 | return subDir.getValue();\r | |
198 | }\r | |
199 | \r | |
200 | /**\r | |
201 | Add method for "IncludePath" nested element\r | |
202 | \r | |
203 | @param path The IncludePath object from nested IncludePath type of element\r | |
204 | **/\r | |
205 | public void addConfiguredIncludepath(IncludePath path) {\r | |
206 | includePathList.insert(path);\r | |
207 | }\r | |
208 | \r | |
209 | /**\r | |
210 | Add method for "Input" nested element\r | |
211 | \r | |
212 | @param input The Input object from nested Input type of element\r | |
213 | **/\r | |
214 | public void addConfiguredInput(Input inputFile) {\r | |
215 | inputFileList.insert(inputFile);\r | |
216 | }\r | |
217 | \r | |
218 | /**\r | |
219 | Check if the dependency list file should be (re-)generated or not.\r | |
220 | \r | |
221 | @returns true The dependency list file is uptodate. No re-generation is needed.\r | |
222 | @returns false The dependency list file is outofdate. Re-generation is needed.\r | |
223 | **/\r | |
224 | private boolean isUptodate() {\r | |
225 | String dfName = depsFile.getValue();\r | |
226 | File df = new File(dfName);\r | |
227 | if (!df.exists()) {\r | |
228 | EdkLog.log(this, EdkLog.EDK_VERBOSE, dfName + " doesn't exist!");\r | |
229 | return false;\r | |
230 | }\r | |
231 | \r | |
232 | //\r | |
233 | // If the source file(s) is newer than dependency list file, we need to\r | |
234 | // re-generate the dependency list file\r | |
235 | //\r | |
236 | long depsFileTimeStamp = df.lastModified();\r | |
237 | List<String> fileList = inputFileList.getNameList();\r | |
238 | for (int i = 0, length = fileList.size(); i < length; ++i) {\r | |
239 | File sf = new File(fileList.get(i));\r | |
240 | if (sf.lastModified() > depsFileTimeStamp) {\r | |
241 | EdkLog.log(this, EdkLog.EDK_VERBOSE, sf.getPath() + " has been changed since last build!");\r | |
242 | return false;\r | |
243 | }\r | |
244 | }\r | |
245 | \r | |
246 | //\r | |
247 | // If the source files haven't been changed since last time the dependency\r | |
248 | // list file was generated, we need to check each file in the file list to\r | |
249 | // see if any of them is changed or not. If anyone of them is newer than\r | |
250 | // the dependency list file, MakeDeps.exe is needed to run again.\r | |
251 | //\r | |
252 | LineNumberReader lineReader = null;\r | |
253 | FileReader fileReader = null;\r | |
254 | boolean ret = false;\r | |
255 | try {\r | |
256 | fileReader = new FileReader(df);\r | |
257 | lineReader = new LineNumberReader(fileReader);\r | |
258 | \r | |
259 | String line = null;\r | |
260 | int lines = 0;\r | |
261 | while ((line = lineReader.readLine()) != null) {\r | |
262 | //\r | |
263 | // check file end flag "\t" to see if the .dep was generated correctly\r | |
264 | // \r | |
265 | if (line.equals("\t")) {\r | |
266 | ret = true;\r | |
267 | continue;\r | |
268 | }\r | |
269 | line = line.trim();\r | |
270 | //\r | |
271 | // skip empty line\r | |
272 | // \r | |
273 | if (line.length() == 0) {\r | |
274 | continue;\r | |
275 | }\r | |
276 | ++lines;\r | |
277 | \r | |
278 | //\r | |
279 | // If a file cannot be found (moved or removed) or newer, regenerate the dep file\r | |
280 | // \r | |
281 | File sourceFile = new File(line);\r | |
282 | if ((!sourceFile.exists()) || (sourceFile.lastModified() > depsFileTimeStamp)) {\r | |
283 | EdkLog.log(this, EdkLog.EDK_VERBOSE, sourceFile.getPath() + " has been (re)moved or changed since last build!");\r | |
284 | ret = false;\r | |
285 | break;\r | |
286 | }\r | |
287 | }\r | |
288 | \r | |
289 | //\r | |
290 | // check if the .dep file is empty\r | |
291 | // \r | |
292 | if (lines == 0) {\r | |
293 | EdkLog.log(this, EdkLog.EDK_VERBOSE, dfName + " is empty!");\r | |
294 | ret = false;\r | |
295 | }\r | |
296 | \r | |
297 | lineReader.close();\r | |
298 | fileReader.close();\r | |
299 | } catch (IOException e) {\r | |
300 | throw new BuildException(e.getMessage());\r | |
301 | }\r | |
302 | \r | |
303 | return ret;\r | |
304 | }\r | |
305 | }\r | |
306 | \r |