Fixed EDKT102;
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / OnDependency.java
CommitLineData
878ddf1f 1/** @file\r
2This file is to define OnDependency class.\r
3\r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13--*/\r
14package org.tianocore.build.global;\r
15\r
196ad8d7 16import java.io.File;\r
17import java.util.HashMap;\r
18import java.util.Iterator;\r
19import java.util.Map;\r
20\r
878ddf1f 21import org.apache.tools.ant.BuildException;\r
22import org.apache.tools.ant.Task;\r
23import org.apache.tools.ant.taskdefs.Sequential;\r
24\r
878ddf1f 25/**\r
26 Class OnDepdendency is used to check the timestamp between source files and\r
27 target files, which can be used to determine if the target files are needed to\r
28 be re-generated from source files.\r
29 **/\r
30public class OnDependency extends Task {\r
196ad8d7 31 ///\r
32 /// cache the modified timestamp of files accessed, to speed up the depencey check\r
33 /// \r
34 private static Map<String, Long> timeStampCache = new HashMap<String, Long>();\r
878ddf1f 35 ///\r
36 /// source files list\r
37 ///\r
38 private DpFileList sources = null;\r
39 ///\r
40 /// target files list\r
41 ///\r
42 private DpFileList targets = null;\r
43 ///\r
44 /// tasks to be performed to generate target files\r
45 ///\r
46 private Sequential task = null;\r
47\r
48 ///\r
49 /// An empty constructor for an ANT task can avoid some potential issues\r
50 ///\r
51 public OnDependency(){\r
52 }\r
53\r
54 /**\r
55 Standard execute method of ANT task\r
56 **/\r
57 public void execute() {\r
58 if (isOutOfDate() && task != null) {\r
59 task.perform();\r
60 }\r
61 }\r
62\r
63 ///\r
64 /// check if the target files are outofdate\r
65 ///\r
66 private boolean isOutOfDate() {\r
67 ///\r
68 /// if no source files specified, take it as a fresh start\r
69 ///\r
70 if (sources.nameList.size() == 0) {\r
71 return true;\r
72 }\r
73\r
74 Iterator dstIt = targets.nameList.iterator();\r
75 while (dstIt.hasNext()) {\r
76 String dstFileName = (String)dstIt.next();\r
77 File dstFile = new File(dstFileName);\r
78 if (!dstFile.exists()) {\r
79 return true;\r
80 }\r
81\r
82 long dstTimeStamp = dstFile.lastModified();\r
83 Iterator srcIt = sources.nameList.iterator();\r
84 while (srcIt.hasNext()) {\r
85 String srcFileName = (String)srcIt.next();\r
196ad8d7 86 long srcTimeStamp;\r
87\r
88 if (timeStampCache.containsKey(srcFileName)) {\r
89 srcTimeStamp = ((Long)timeStampCache.get(srcFileName)).longValue();\r
90 } else {\r
91 File srcFile = new File(srcFileName);\r
92 if (!srcFile.exists()) {\r
93 throw new BuildException(srcFileName + " doesn't exist !!!");\r
94 }\r
95 srcTimeStamp = srcFile.lastModified();\r
96 timeStampCache.put(srcFileName, new Long(srcTimeStamp));\r
878ddf1f 97 }\r
98\r
196ad8d7 99 if (dstTimeStamp < srcTimeStamp) {\r
878ddf1f 100 return true;\r
101 }\r
102 }\r
103 }\r
104\r
105 return false;\r
106 }\r
107\r
108 /**\r
109 Add method of ANT task for nested element with Sequential type\r
110\r
111 @param task Sequential object which contains tasks for generating target files\r
112 **/\r
113 public void addSequential(Sequential task) {\r
114 this.task = task;\r
115 }\r
116\r
117 /**\r
118 Add method of ANT task for nested element with DpFileList type\r
119\r
120 @param sources DpFileList object which contains the list of source files\r
121 **/\r
122 public void addSourcefiles(DpFileList sources) {\r
123 this.sources = sources;\r
124 }\r
125\r
126 /**\r
127 Add method of ANT task for nested element with DpFileList type\r
128\r
129 @param targets DpFileList object which contains the list of target files\r
130 **/\r
131 public void addTargetfiles(DpFileList targets) {\r
132 this.targets = targets;\r
133 }\r
134}\r
135\r