]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/MakeDeps.java
c72b1f66728dc0e84db5e766f7320dc9abe1413b
[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.FileWriter;
20 import java.io.IOException;
21 import java.io.LineNumberReader;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.StringTokenizer;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.Task;
34 import org.apache.tools.ant.taskdefs.Execute;
35 import org.apache.tools.ant.taskdefs.LogStreamHandler;
36 import org.apache.tools.ant.types.Commandline;
37 import org.apache.tools.ant.types.Path;
38
39 /**
40 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
41 **/
42 public class MakeDeps extends Task {
43
44 //
45 // private members, use set/get to access them
46 //
47 private static final String cmdName = "MakeDeps";
48 private static final String target = "dummy";
49 private String includePath = null;
50 private String depsFile = null;
51 private String subDir = null;
52 private boolean quietMode = true;
53 private boolean ignoreError = true;
54 private String extraDeps = "";
55 private List<IncludePath> includePathList = new ArrayList<IncludePath>();
56 private List<Input> inputFileList = new ArrayList<Input>();
57
58 public MakeDeps() {
59
60 }
61
62 /**
63 The Standard execute method for ANT task. It will check if it's necessary
64 to generate the dependency list file. If no file is found or the dependency
65 is changed, it will compose the command line and call MakeDeps.exe to
66 generate the dependency list file.
67
68 @throws BuildException
69 **/
70 public void execute() throws BuildException {
71 ///
72 /// check if the dependency list file is uptodate or not
73 ///
74 if (isUptodate()) {
75 return;
76 }
77
78 Project prj = this.getOwningTarget().getProject();
79 String toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");
80 ///
81 /// compose full tool path
82 ///
83 if (toolPath == null || toolPath.length() == 0) {
84 toolPath = "./" + cmdName;
85 } else {
86 if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {
87 toolPath = toolPath + cmdName;
88 } else {
89 toolPath = toolPath + "/" + cmdName;
90 }
91 }
92
93 ///
94 /// compose tool arguments
95 ///
96 StringBuffer args = new StringBuffer(4096);
97 if (ignoreError) {
98 args.append(" -ignorenotfound");
99 }
100 if (quietMode) {
101 args.append(" -q");
102 }
103 if (subDir != null && subDir.length() > 0) {
104 args.append(" -s ");
105 args.append(subDir);
106 }
107
108 ///
109 /// if there's no source files, we can do nothing about dependency
110 ///
111 if (inputFileList.size() == 0) {
112 throw new BuildException("No source files specified to scan");
113 }
114
115 ///
116 /// compose source file arguments
117 ///
118 Iterator iterator = inputFileList.iterator();
119 while (iterator.hasNext()) {
120 Input inputFile = (Input)iterator.next();
121 args.append(" -f ");
122 args.append(cleanupPathName(inputFile.getFile()));
123 }
124
125 ///
126 /// compose search pathes argument
127 ///
128 StringBuffer includePathArg = new StringBuffer(4096);
129 if (includePath != null && includePath.length() > 0) {
130 StringTokenizer pathTokens = new StringTokenizer(includePath, ";");
131 while (pathTokens.hasMoreTokens()) {
132 String tmpPath = pathTokens.nextToken().trim();
133 if (tmpPath.length() == 0) {
134 continue;
135 }
136
137 includePathArg.append(" -i ");
138 includePathArg.append(cleanupPathName(tmpPath));
139 }
140 }
141 iterator = includePathList.iterator();
142 while (iterator.hasNext()) {
143 IncludePath path = (IncludePath)iterator.next();
144 includePathArg.append(cleanupPathName(path.getPath()));
145 }
146 args.append(includePathArg);
147
148 ///
149 /// We don't need a real target. So just a "dummy" is given
150 ///
151 args.append(" -target dummy");
152 args.append(" -o ");
153 args.append(cleanupPathName(depsFile));
154
155 ///
156 /// prepare to execute the tool
157 ///
158 Commandline cmd = new Commandline();
159 cmd.setExecutable(toolPath);
160 cmd.createArgument().setLine(args.toString());
161
162 LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
163 Execute runner = new Execute(streamHandler, null);
164
165 runner.setAntRun(prj);
166 runner.setCommandline(cmd.getCommandline());
167
168 int result = 0;
169 try {
170 result = runner.execute();
171 } catch (IOException e) {
172 throw new BuildException(e.getMessage());
173 }
174
175 if (result != 0) {
176 log ("MakeDeps failed");
177 return;
178 }
179
180 // change the old DEP file format (makefile compatible) to just file list
181 if (!cleanup()) {
182 throw new BuildException(depsFile + " was not generated");
183 }
184 }
185
186 ///
187 /// Remove any duplicated path separator or inconsistent path separator
188 ///
189 private String cleanupPathName(String path) {
190 try {
191 path = (new File(path)).getCanonicalPath();
192 } catch (IOException e) {
193 String separator = "\\" + File.separator;
194 String duplicateSeparator = separator + "{2}";
195 path = Path.translateFile(path);
196 path = path.replaceAll(duplicateSeparator, separator);
197 return path;
198 }
199
200 return path;
201 }
202
203 /**
204 Set method for "DepsFile" attribute
205
206 @param name The name of dependency list file
207 **/
208 public void setDepsFile(String name) {
209 depsFile = cleanupPathName(name);
210 }
211
212 /**
213 Get method for "DepsFile" attribute
214
215 @returns The name of dependency list file
216 **/
217 public String getDepsFile() {
218 return depsFile;
219 }
220
221 /**
222 Set method for "IgnoreError" attribute
223
224 @param ignore flag to control error handling (true/false)
225 **/
226 public void setIgnoreError(boolean ignore) {
227 ignoreError = ignore;
228 }
229
230 /**
231 Get method for "IgnoreError" attribute
232
233 @returns The value of current IgnoreError flag
234 **/
235 public boolean getIgnoreError() {
236 return ignoreError;
237 }
238
239 /**
240 Set method for "QuietMode" attribute
241
242 @param quiet flag to control the output information (true/false)
243 **/
244 public void setQuietMode(boolean quiet) {
245 quietMode = quiet;
246 }
247
248 /**
249 Get method for "QuietMode" attribute
250
251 @returns value of current QuietMode flag
252 **/
253 public boolean getQuietMode() {
254 return quietMode;
255 }
256
257 /**
258 Set method for "SubDir" attribute
259
260 @param dir The name of sub-directory in which source files will be scanned
261 **/
262 public void setSubDir(String dir) {
263 subDir = dir;
264 }
265
266 /**
267 Get method for "SubDir" attribute
268
269 @returns The name of sub-directory
270 **/
271 public String getSubDir() {
272 return subDir;
273 }
274
275 /**
276 Set method for "IncludePath" attribute
277
278 @param path The name of include path
279 **/
280 public void setIncludePath(String path) {
281 includePath = cleanupPathName(path);
282 }
283
284 /**
285 Get method for "IncludePath" attribute
286
287 @returns The name of include path
288 **/
289 public String getIncludePath() {
290 return includePath;
291 }
292
293 /**
294 Set method for "ExtraDeps" attribute
295
296 @param deps The name of dependency file specified separately
297 **/
298 public void setExtraDeps(String deps) {
299 extraDeps = deps;
300 }
301
302 /**
303 Get method for "ExtraDeps" attribute
304
305 @returns The name of dependency file specified separately
306 **/
307 public String getExtraDeps () {
308 return extraDeps;
309 }
310
311 /**
312 Add method for "IncludePath" nested element
313
314 @param path The IncludePath object from nested IncludePath type of element
315 **/
316 public void addIncludepath(IncludePath path) {
317 includePathList.add(path);
318 }
319
320 /**
321 Add method for "Input" nested element
322
323 @param input The Input object from nested Input type of element
324 **/
325 public void addInput(Input inputFile) {
326 inputFileList.add(inputFile);
327 }
328
329 /**
330 The original file generated by MakeDeps.exe is for makefile uses. The target
331 part (before :) is not useful for ANT. This method will do the removal.
332
333 @returns true if cleaned files is saved successfully
334 @returns false if error occurs in file I/O system
335 **/
336 private boolean cleanup() {
337 File df = new File(depsFile);
338
339 if (!df.exists()) {
340 return false;
341 }
342
343 LineNumberReader lineReader = null;
344 FileReader fileReader = null;
345 Set<String> lineSet = new HashSet<String>(100); // used to remove duplicated lines
346 try {
347 fileReader = new FileReader(df);
348 lineReader = new LineNumberReader(fileReader);
349
350 ///
351 /// clean-up each line in deps file
352 //
353 String line = null;
354 while ((line = lineReader.readLine()) != null) {
355 Pattern pattern = Pattern.compile(target + "[ ]*:[ ]*(.+)");
356 Matcher matcher = pattern.matcher(line);
357
358 while (matcher.find()) {
359 ///
360 /// keep the file name after ":"
361 ///
362 String filePath = line.substring(matcher.start(1), matcher.end(1));
363 filePath = cleanupPathName(filePath);
364 lineSet.add(filePath);
365 }
366 }
367 lineReader.close();
368 fileReader.close();
369
370 ///
371 /// we may have explicitly specified dependency files
372 ///
373 StringTokenizer fileTokens = new StringTokenizer(extraDeps, ";");
374 while (fileTokens.hasMoreTokens()) {
375 lineSet.add(cleanupPathName(fileTokens.nextToken()));
376 }
377
378 ///
379 /// compose the final file content
380 ///
381 StringBuffer cleanedLines = new StringBuffer(40960);
382 Iterator<String> it = lineSet.iterator();
383 while (it.hasNext()) {
384 String filePath = it.next();
385 cleanedLines.append(filePath);
386 cleanedLines.append("\n");
387 }
388 ///
389 /// overwrite old dep file with new content
390 ///
391 FileWriter fileWriter = null;
392 fileWriter = new FileWriter(df);
393 fileWriter.write(cleanedLines.toString());
394 fileWriter.close();
395 } catch (IOException e) {
396 log (e.getMessage());
397 }
398
399 return true;
400 }
401
402 /**
403 Check if the dependency list file should be (re-)generated or not.
404
405 @returns true The dependency list file is uptodate. No re-generation is needed.
406 @returns false The dependency list file is outofdate. Re-generation is needed.
407 **/
408 private boolean isUptodate() {
409 File df = new File(depsFile);
410 if (!df.exists()) {
411 return false;
412 }
413
414 ///
415 /// If the source file(s) is newer than dependency list file, we need to
416 /// re-generate the dependency list file
417 ///
418 long depsFileTimeStamp = df.lastModified();
419 Iterator iterator = inputFileList.iterator();
420 while (iterator.hasNext()) {
421 Input inputFile = (Input)iterator.next();
422 File sf = new File(inputFile.getFile());
423 if (sf.lastModified() > depsFileTimeStamp) {
424 return false;
425 }
426 }
427
428 ///
429 /// If the source files haven't been changed since last time the dependency
430 /// list file was generated, we need to check each file in the file list to
431 /// see if any of them is changed or not. If anyone of them is newer than
432 /// the dependency list file, MakeDeps.exe is needed to run again.
433 ///
434 LineNumberReader lineReader = null;
435 FileReader fileReader = null;
436 boolean ret = true;
437 try {
438 fileReader = new FileReader(df);
439 lineReader = new LineNumberReader(fileReader);
440
441 String line = null;
442 while ((line = lineReader.readLine()) != null) {
443 File sourceFile = new File(line);
444 if (sourceFile.lastModified() > depsFileTimeStamp) {
445 ret = false;
446 break;
447 }
448 }
449 lineReader.close();
450 fileReader.close();
451 } catch (IOException e) {
452 log (e.getMessage());
453 }
454
455 return ret;
456 }
457 }
458