summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFvImageTask.java
blob: 0f25032f7f0bb624837455a9beb2f8611e0831ba (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
/** @file
 GenFvImageTask class.

 GenFvImageTask is to call GenFvImage.exe to generate FvImage.
 
 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 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 java.io.File;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.FileReader;

import org.tianocore.common.logger.EdkLog;

/**
  GenFvImageTask
  
  GenFvImageTask is to call GenFvImage.exe to generate the FvImage.
  
**/
public class GenFvImageTask extends Task implements EfiDefine{
    //
    // tool name
    //
    static final private String toolName = "GenFvImage";
    //
    // Pattern to match the section header (e.g. [options], [files])
    // 
    static final private Pattern sectionHeader = Pattern.compile("\\[([^\\[\\]]+)\\]");
    //
    // The name of input inf file
    //
    private FileArg infFile = new FileArg();
    //
    // Output directory
    //
    private String outputDir = ".";

    /**
      execute
      
      GenFvImageTask execute is to assemble tool command line & execute tool
      command line.
    **/
    public void execute() throws BuildException  {
        Project project = this.getOwningTarget().getProject();
        String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");

        if (isUptodate()) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, infFile.toFileList() + " is uptodate!");
            return;
        }

        String command;
        if (path == null) {
            command = toolName;
        } else {
            command = path + File.separator + toolName;
        }

        String argument = "" + infFile;
        //
        // lauch the program
        //
        int exitCode = 0;
        try {
            Commandline cmdline = new Commandline();
            cmdline.setExecutable(command);
            cmdline.createArgument().setLine(argument);

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

            runner.setAntRun(project);
            runner.setCommandline(cmdline.getCommandline());
            runner.setWorkingDirectory(new File(outputDir)); 
            //
            // log command line string.
            //
            EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));
            EdkLog.log(this, infFile.toFileList());

            exitCode = runner.execute();
            if (exitCode != 0) {
                EdkLog.log(this, "ERROR = " + Integer.toHexString(exitCode));
            } else {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, "GenFvImage succeeded!");
            }
        } catch (Exception e) {
            throw new BuildException(e.getMessage());
        } finally {
            if (exitCode != 0) {
                throw new BuildException("GenFvImage: failed to generate FV file!");
            }
        }
    }
    /**
      getInfFile
      
      This function is to get class member of infFile
      @return String    name of infFile
    **/
    public String getInfFile() {
        return infFile.getValue();
    }
    
    /**
      setInfFile
      
      This function is to set class member of infFile.
      
      @param infFile  name of infFile
    **/
    public void setInfFile(String infFile) {
        this.infFile.setArg(" -I ", infFile);
    }
    
    /**
      getOutputDir
      
      This function is to get output directory.
      
      @return                Path of output directory.
    **/
    public String getOutputDir() {
        return outputDir;
    }

    /**
      setOutputDir
      
      This function is to set output directory.
      
      @param outputDir        The output direcotry.
    **/
    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }

    //
    // dependency check
    // 
    private boolean isUptodate() {
        String infName = this.infFile.getValue();
        String fvName = "";
        List<String> ffsFiles = new LinkedList<String>();
        File inf = new File(infName);

        try {
            FileReader reader = new FileReader(inf);
            BufferedReader in = new BufferedReader(reader);
            String str;

            //
            // Read the inf file line by line
            // 
            boolean inFiles = false;
            boolean inOptions = false;
            while ((str = in.readLine()) != null) {
                str = str.trim();
                if (str.length() == 0) {
                    continue;
                }

                Matcher matcher = sectionHeader.matcher(str);
                if (matcher.find()) {
                    //
                    // We take care of only "options" and "files" section
                    // 
                    String sectionName = str.substring(matcher.start(1), matcher.end(1));
                    if (sectionName.equalsIgnoreCase("options")) {
                        inOptions = true;
                        inFiles = false;
                    } else if (sectionName.equalsIgnoreCase("files")) {
                        inFiles = true;
                        inOptions = false;
                    } else {
                        inFiles = false;
                        inOptions = false;
                    }
                    continue;
                }

                //
                // skip invalid line
                // 
                int equalMarkPos = str.indexOf("=");
                if (equalMarkPos < 0) {
                    continue;
                }

                //
                // we have only interest in EFI_FILE_NAME
                // 
                String fileNameFlag = str.substring(0, equalMarkPos).trim();
                String fileName = str.substring(equalMarkPos + 1).trim();
                if (!fileNameFlag.equalsIgnoreCase("EFI_FILE_NAME")
                    || fileName.length() == 0) {
                    continue;
                }

                if (inFiles) {
                    //
                    // files specified beneath the [files] section are source files
                    // 
                    ffsFiles.add(fileName);
                } else if (inOptions) {
                    //
                    // file specified beneath the [options] section is the target file
                    // 
                    fvName = outputDir + File.separator + fileName;
                }
            }
        } catch (Exception ex) {
            throw new BuildException(ex.getMessage());
        }

        //
        // if destionation file doesn't exist, we need to generate it.
        // 
        File fvFile = new File(fvName);
        if (!fvFile.exists()) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, fvName + " doesn't exist!");
            return false;
        }

        //
        // the inf file itself will be taken as source file, check its timestamp
        // against the target file
        // 
        long fvFileTimeStamp = fvFile.lastModified();
        if (inf.lastModified() > fvFileTimeStamp) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, infName + " has been changed since last build!");
            return false;
        }

        //
        // no change in the inf file, we need to check each source files in it
        // against the target file
        // 
        for (Iterator it = ffsFiles.iterator(); it.hasNext(); ) {
            String fileName = (String)it.next();
            File file = new File(fileName);
            if (file.lastModified() > fvFileTimeStamp) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, fileName + " has been changed since last build!");
                return false;
            }
        }

        return true;
    }
}