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