]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/global/OnDependency.java
1) Add FileTimeStamp class to centralize the cache mechanism for file time stamp...
[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 import org.tianocore.common.cache.FileTimeStamp;
26
27 /**
28 Class OnDepdendency is used to check the timestamp between source files and
29 target files, which can be used to determine if the target files are needed to
30 be re-generated from source files.
31 **/
32 public class OnDependency extends Task {
33 //
34 // source files list
35 //
36 private DpFileList sources = null;
37
38 //
39 // target files list
40 //
41 private DpFileList targets = null;
42
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() throws BuildException {
58 if (isOutOfDate() && task != null) {
59 task.perform();
60 }
61
62 //
63 // Update the time stamp of target files since they are just re-generated
64 //
65 for (Iterator dstIt = targets.nameList.iterator(); dstIt.hasNext();) {
66 FileTimeStamp.update((String)dstIt.next());
67 }
68 }
69
70 //
71 // check if the target files are outofdate
72 //
73 private boolean isOutOfDate() {
74 ///
75 /// if no source files specified, take it as a fresh start
76 ///
77 if (sources.nameList.size() == 0) {
78 EdkLog.log(this, EdkLog.EDK_VERBOSE, "No source file spcified!");
79 return true;
80 }
81
82 if (targets.nameList.size() == 0) {
83 EdkLog.log(this, EdkLog.EDK_VERBOSE, "No target file found!");
84 return true;
85 }
86
87 Iterator dstIt = targets.nameList.iterator();
88 while (dstIt.hasNext()) {
89 String dstFileName = (String)dstIt.next();
90 File dstFile = new File(dstFileName);
91 if (!dstFile.exists()) {
92 EdkLog.log(this, EdkLog.EDK_VERBOSE, "Target file [" + dstFileName + "] doesn't exist!");
93 return true;
94 }
95
96 long dstTimeStamp = FileTimeStamp.get(dstFileName);
97 Iterator srcIt = sources.nameList.iterator();
98 while (srcIt.hasNext()) {
99 String srcFileName = (String)srcIt.next();
100 long srcTimeStamp = FileTimeStamp.get(srcFileName);
101
102 if (srcTimeStamp == 0) {
103 //
104 // time stamp 0 means that the file doesn't exist
105 //
106 throw new BuildException("Source File name: " + srcFileName + " doesn't exist!!!");
107 }
108
109 if (dstTimeStamp < srcTimeStamp) {
110 EdkLog.log(this, EdkLog.EDK_VERBOSE, "Source file [" + srcFileName + "] has been changed since last build!");
111 return true;
112 }
113 }
114 }
115
116 EdkLog.log(this, EdkLog.EDK_VERBOSE, "Target files are up-to-date!");
117 return false;
118 }
119
120 /**
121 Add method of ANT task for nested element with Sequential type
122
123 @param task Sequential object which contains tasks for generating target files
124 **/
125 public void addSequential(Sequential task) {
126 this.task = task;
127 }
128
129 /**
130 Add method of ANT task for nested element with DpFileList type
131
132 @param sources DpFileList object which contains the list of source files
133 **/
134 public void addSourcefiles(DpFileList sources) {
135 this.sources = sources;
136 }
137
138 /**
139 Add method of ANT task for nested element with DpFileList type
140
141 @param targets DpFileList object which contains the list of target files
142 **/
143 public void addTargetfiles(DpFileList targets) {
144 this.targets = targets;
145 }
146 }
147