summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/global/OnDependency.java
blob: 678bfb869bf63cb111f1d46482a89c30f2cc6272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/** @file
This file is to define OnDependency class.

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

--*/
package org.tianocore.build.global;

import java.io.File;
import java.util.Iterator;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Sequential;
import org.tianocore.common.logger.EdkLog;
import org.tianocore.common.cache.FileTimeStamp;

/**
 Class OnDepdendency is used to check the timestamp between source files and
 target files, which can be used to determine if the target files are needed to
 be re-generated from source files.
 **/
public class OnDependency extends Task {
    //
    // source files list
    //
    private DpFileList sources = null;

    //
    // target files list
    //
    private DpFileList targets = null;

    //
    // tasks to be performed to generate target files
    //
    private Sequential  task = null;

    /**
     An empty constructor for an ANT task can avoid some potential issues
     **/
    public OnDependency(){
    }

    /**
     Standard execute method of ANT task
     **/
    public void execute() throws BuildException {
        if (isOutOfDate() && task != null) {
            task.perform();
        }

        //
        // Update the time stamp of target files since they are just re-generated
        // 
        for (Iterator dstIt = targets.nameList.iterator(); dstIt.hasNext();) {
            FileTimeStamp.update((String)dstIt.next());
        }
    }

    //
    // check if the target files are outofdate
    //
    private boolean isOutOfDate() {
        ///
        /// if no source files specified, take it as a fresh start
        ///
        if (sources.nameList.size() == 0) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, "No source file spcified!");
            return true;
        }

        if (targets.nameList.size() == 0) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, "No target file found!");
            return true;
        }

        Iterator dstIt = targets.nameList.iterator();
        while (dstIt.hasNext()) {
            String dstFileName = (String)dstIt.next();
            File dstFile = new File(dstFileName);
            if (!dstFile.exists()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, "Target file [" + dstFileName + "] doesn't exist!");
                return true;
            }

            long dstTimeStamp = FileTimeStamp.get(dstFileName);
            Iterator srcIt = sources.nameList.iterator();
            while (srcIt.hasNext()) {
                String srcFileName = (String)srcIt.next();
                long srcTimeStamp = FileTimeStamp.get(srcFileName);

                if (srcTimeStamp == 0) {
                    //
                    // time stamp 0 means that the file doesn't exist
                    // 
                    throw new BuildException("Source File name: " + srcFileName + " doesn't exist!!!");
                }

                if (dstTimeStamp < srcTimeStamp) {
                    EdkLog.log(this, EdkLog.EDK_VERBOSE, "Source file [" + srcFileName + "] has been changed since last build!");
                    return true;
                }
            }
        }

        EdkLog.log(this, EdkLog.EDK_VERBOSE, "Target files are up-to-date!");
        return false;
    }

    /**
     Add method of ANT task for nested element with Sequential type

     @param     task    Sequential object which contains tasks for generating target files
     **/
    public void addSequential(Sequential task) {
        this.task = task;
    }

    /**
     Add method of ANT task for nested element with DpFileList type

     @param     sources DpFileList object which contains the list of source files
     **/
    public void addSourcefiles(DpFileList sources) {
        this.sources = sources;
    }

    /**
     Add method of ANT task for nested element with DpFileList type

     @param     targets DpFileList object which contains the list of target files
     **/
    public void addTargetfiles(DpFileList targets) {
        this.targets = targets;
    }
}