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