Fixed EDKT102;
[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
25 /**
26 Class OnDepdendency is used to check the timestamp between source files and
27 target files, which can be used to determine if the target files are needed to
28 be re-generated from source files.
29 **/
30 public class OnDependency extends Task {
31 ///
32 /// cache the modified timestamp of files accessed, to speed up the depencey check
33 ///
34 private static Map<String, Long> timeStampCache = new HashMap<String, Long>();
35 ///
36 /// source files list
37 ///
38 private DpFileList sources = null;
39 ///
40 /// target files list
41 ///
42 private DpFileList targets = null;
43 ///
44 /// tasks to be performed to generate target files
45 ///
46 private Sequential task = null;
47
48 ///
49 /// An empty constructor for an ANT task can avoid some potential issues
50 ///
51 public OnDependency(){
52 }
53
54 /**
55 Standard execute method of ANT task
56 **/
57 public void execute() {
58 if (isOutOfDate() && task != null) {
59 task.perform();
60 }
61 }
62
63 ///
64 /// check if the target files are outofdate
65 ///
66 private boolean isOutOfDate() {
67 ///
68 /// if no source files specified, take it as a fresh start
69 ///
70 if (sources.nameList.size() == 0) {
71 return true;
72 }
73
74 Iterator dstIt = targets.nameList.iterator();
75 while (dstIt.hasNext()) {
76 String dstFileName = (String)dstIt.next();
77 File dstFile = new File(dstFileName);
78 if (!dstFile.exists()) {
79 return true;
80 }
81
82 long dstTimeStamp = dstFile.lastModified();
83 Iterator srcIt = sources.nameList.iterator();
84 while (srcIt.hasNext()) {
85 String srcFileName = (String)srcIt.next();
86 long srcTimeStamp;
87
88 if (timeStampCache.containsKey(srcFileName)) {
89 srcTimeStamp = ((Long)timeStampCache.get(srcFileName)).longValue();
90 } else {
91 File srcFile = new File(srcFileName);
92 if (!srcFile.exists()) {
93 throw new BuildException(srcFileName + " doesn't exist !!!");
94 }
95 srcTimeStamp = srcFile.lastModified();
96 timeStampCache.put(srcFileName, new Long(srcTimeStamp));
97 }
98
99 if (dstTimeStamp < srcTimeStamp) {
100 return true;
101 }
102 }
103 }
104
105 return false;
106 }
107
108 /**
109 Add method of ANT task for nested element with Sequential type
110
111 @param task Sequential object which contains tasks for generating target files
112 **/
113 public void addSequential(Sequential task) {
114 this.task = task;
115 }
116
117 /**
118 Add method of ANT task for nested element with DpFileList type
119
120 @param sources DpFileList object which contains the list of source files
121 **/
122 public void addSourcefiles(DpFileList sources) {
123 this.sources = sources;
124 }
125
126 /**
127 Add method of ANT task for nested element with DpFileList type
128
129 @param targets DpFileList object which contains the list of target files
130 **/
131 public void addTargetfiles(DpFileList targets) {
132 this.targets = targets;
133 }
134 }
135