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