summaryrefslogtreecommitdiff
path: root/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/MakeDeps.java
blob: bd305fa0794c6c57a9071ab223f9757206d1d84e (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/** @file
This file is to wrap MakeDeps.exe tool as ANT task, which is used to generate
dependency files for source code.

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.framework.tasks;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;

import org.tianocore.common.logger.EdkLog;

/**
 Class MakeDeps is used to wrap MakeDeps.exe as an ANT task.
 **/
public class MakeDeps extends Task {

    //
    // private members, use set/get to access them
    //
    private static final String toolName = "MakeDeps";
    private FileArg              depsFile = new FileArg();
    private ToolArg              subDir = new ToolArg();
    private ToolArg              quietMode = new ToolArg(" -", "q");
    private ToolArg              ignoreError = new ToolArg(" -", "ignorenotfound");
    private IncludePath          includePathList = new IncludePath();
    private Input                inputFileList = new Input();
    private ToolArg              target = new FileArg(" -target ", "dummy");

    public MakeDeps() {

    }

    /**
     The Standard execute method for ANT task. It will check if it's necessary
     to generate the dependency list file. If no file is found or the dependency
     is changed, it will compose the command line and call MakeDeps.exe to
     generate the dependency list file.

     @throws    BuildException
     **/
    public void execute() throws BuildException {
        ///
        /// check if the dependency list file is uptodate or not
        ///
        if (isUptodate()) {
            return;
        }

        Project prj  = this.getOwningTarget().getProject();
        String  toolPath = prj.getProperty("env.FRAMEWORK_TOOLS_PATH");

        ///
        /// compose full tool path
        ///
        if (toolPath == null || toolPath.length() == 0) {
            toolPath = toolName;
        } else {
            if (toolPath.endsWith("/") || toolPath.endsWith("\\")) {
                toolPath = toolPath + toolName;
            } else {
                toolPath = toolPath + File.separator + toolName;
            }
        }

        ///
        /// compose tool arguments
        ///
        String argument = "" + inputFileList + includePathList + subDir
                             + quietMode + ignoreError + target + depsFile;

        ///
        /// prepare to execute the tool
        ///
        Commandline cmd = new Commandline();
        cmd.setExecutable(toolPath);
        cmd.createArgument().setLine(argument);

        LogStreamHandler streamHandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
        Execute runner = new Execute(streamHandler, null);

        runner.setAntRun(prj);
        runner.setCommandline(cmd.getCommandline());

        EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmd.getCommandline()));

        int result = 0;
        try {
            result = runner.execute();
        } catch (IOException e) {
            throw new BuildException(e.getMessage());
        }

        if (result != 0) {
            EdkLog.log(this, EdkLog.EDK_INFO, toolName + " failed!");
            throw new BuildException(toolName + ": failed to generate dependency file!");
        } else {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, toolName + " succeeded!");
        }
    }

    /**
     Set method for "DepsFile" attribute

     @param     name    The name of dependency list file
     **/
    public void setDepsFile(String name) {
        depsFile.setArg(" -o ", name);
    }

    /**
     Get method for "DepsFile" attribute

     @returns   The name of dependency list file
     **/
    public String getDepsFile() {
        return depsFile.getValue();
    }

    /**
     Set method for "IgnoreError" attribute

     @param     ignore    flag to control error handling (true/false)
     **/
    public void setIgnoreError(boolean ignore) {
        if (!ignore) {
            ignoreError.setArg(" ", " ");
        }
    }

    /**
     Get method for "IgnoreError" attribute

     @returns   The value of current IgnoreError flag
     **/
    public boolean getIgnoreError() {
        return ignoreError.getValue().length() > 0;
    }

    /**
     Set method for "QuietMode" attribute

     @param     quiet   flag to control the output information (true/false)
     **/
    public void setQuietMode(boolean quiet) {
        if (!quiet) {
            quietMode.setArg(" ", " ");
        }
    }

    /**
     Get method for "QuietMode" attribute

     @returns   value of current QuietMode flag
     **/
    public boolean getQuietMode() {
        return quietMode.getValue().length() > 0;
    }

    /**
     Set method for "SubDir" attribute

     @param     dir     The name of sub-directory in which source files will be scanned
     **/
    public void setSubDir(String dir) {
        subDir.setArg(" -s ", dir);
    }

    /**
     Get method for "SubDir" attribute

     @returns   The name of sub-directory
     **/
    public String getSubDir() {
        return subDir.getValue();
    }

    /**
     Add method for "IncludePath" nested element

     @param     path    The IncludePath object from nested IncludePath type of element
     **/
    public void addConfiguredIncludepath(IncludePath path) {
        includePathList.insert(path);
    }

    /**
     Add method for "Input" nested element

     @param     input   The Input object from nested Input type of element
     **/
    public void addConfiguredInput(Input inputFile) {
        inputFileList.insert(inputFile);
    }

    /**
     Check if the dependency list file should be (re-)generated or not.

     @returns   true    The dependency list file is uptodate. No re-generation is needed.
     @returns   false   The dependency list file is outofdate. Re-generation is needed.
     **/
    private boolean isUptodate() {
        String dfName = depsFile.getValue();
        File df = new File(dfName);
        if (!df.exists()) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, dfName + " doesn't exist!");
            return false;
        }

        //
        // If the source file(s) is newer than dependency list file, we need to
        // re-generate the dependency list file
        //
        long depsFileTimeStamp = df.lastModified();
        List<String> fileList = inputFileList.getNameList();
        for (int i = 0, length = fileList.size(); i < length; ++i) {
            File sf = new File(fileList.get(i));
            if (sf.lastModified() > depsFileTimeStamp) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, sf.getPath() + " has been changed since last build!");
                return false;
            }
        }

        //
        // If the source files haven't been changed since last time the dependency
        // list file was generated, we need to check each file in the file list to
        // see if any of them is changed or not. If anyone of them is newer than
        // the dependency list file, MakeDeps.exe is needed to run again.
        //
        LineNumberReader    lineReader = null;
        FileReader          fileReader = null;
        boolean             ret = false;
        try {
            fileReader = new FileReader(df);
            lineReader = new LineNumberReader(fileReader);

            String line = null;
            int lines = 0;
            while ((line = lineReader.readLine()) != null) {
                //
                // check file end flag "\t" to see if the .dep was generated correctly
                // 
                if (line.equals("\t")) {
                    ret = true;
                    continue;
                }
                line = line.trim();
                //
                // skip empty line
                // 
                if (line.length() == 0) {
                    continue;
                }
                ++lines;

                //
                // If a file cannot be found (moved or removed) or newer, regenerate the dep file
                // 
                File sourceFile = new File(line);
                if ((!sourceFile.exists()) || (sourceFile.lastModified() > depsFileTimeStamp)) {
                    EdkLog.log(this, EdkLog.EDK_VERBOSE, sourceFile.getPath() + " has been (re)moved or changed since last build!");
                    ret = false;
                    break;
                }
            }

            //
            // check if the .dep file is empty
            // 
            if (lines == 0) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, dfName + " is empty!");
                ret = false;
            }

            lineReader.close();
            fileReader.close();
        } catch (IOException e) {
            throw new BuildException(e.getMessage());
        }

        return ret;
    }
}