\r
import java.io.BufferedReader;\r
import java.io.File;\r
+import java.io.IOException;\r
import java.io.InputStreamReader;\r
+import java.util.Hashtable;\r
import java.util.Iterator;\r
import java.util.LinkedHashSet;\r
import java.util.Map;\r
\r
import org.apache.tools.ant.BuildException;\r
import org.apache.tools.ant.Task;\r
+import org.tianocore.build.exception.AutoGenException;\r
+import org.tianocore.build.exception.GenBuildException;\r
+import org.tianocore.build.exception.PcdAutogenException;\r
+import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;\r
+import org.tianocore.build.fpd.FpdParserForThread;\r
import org.tianocore.build.fpd.FpdParserTask;\r
+import org.tianocore.build.global.GenBuildLogger;\r
import org.tianocore.build.global.GlobalData;\r
import org.tianocore.build.toolchain.ConfigReader;\r
import org.tianocore.build.toolchain.ToolChainInfo;\r
+import org.tianocore.common.definitions.ToolDefinitions;\r
+import org.tianocore.common.exception.EdkException;\r
+import org.tianocore.common.logger.EdkLog;\r
\r
+/**\r
+ <p>\r
+ <code>FrameworkBuildTask</code> is an Ant task. The main function is finding\r
+ and processing a FPD or MSA file, then building a platform or stand-alone \r
+ module. \r
+ \r
+ <p>\r
+ The task search current directory and find out all MSA and FPD files by file\r
+ extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a\r
+ stand-alone module. The ACTIVE_PLATFORM policy is: \r
+ \r
+ <pre>\r
+ 1. More than one MSA files, report error; \r
+ 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;\r
+ 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;\r
+ 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;\r
+ 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;\r
+ 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;\r
+ 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform\r
+ and let user choose one. \r
+ </pre>\r
+ \r
+ <p>\r
+ Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].\r
+ And load all system environment variables to Ant properties. \r
+ \r
+ <p>\r
+ The usage for this task is : \r
+ \r
+ <pre>\r
+ <FrameworkBuild type="cleanall" />\r
+ </pre>\r
+ \r
+ @since GenBuild 1.0\r
+**/\r
public class FrameworkBuildTask extends Task{\r
\r
private Set<File> buildFiles = new LinkedHashSet<File>();\r
\r
private Set<File> msaFiles = new LinkedHashSet<File>();\r
\r
- String toolsDefFilename = "Tools" + File.separatorChar + "Conf" + File.separatorChar + "tools_def.txt";\r
+ //\r
+ // This is only for none-multi-thread build to reduce overriding message\r
+ //\r
+ public static Hashtable<String, String> originalProperties = new Hashtable<String, String>();\r
\r
- String targetFilename = "target.txt";\r
+ String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;\r
\r
- String activePlatform = null;\r
+ String targetFilename = ToolDefinitions.TARGET_FILE_PATH;\r
+ \r
+ String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;\r
\r
+ String activePlatform = null;\r
+\r
+ ///\r
+ /// The flag to present current is multi-thread enabled\r
+ ///\r
+ public static boolean multithread = false;\r
+\r
+ ///\r
+ /// The concurrent thread number\r
+ ///\r
+ public static int MAX_CONCURRENT_THREAD_NUMBER = 2;\r
+\r
///\r
/// there are three type: all (build), clean and cleanall\r
///\r
private String type = "all";\r
\r
public void execute() throws BuildException {\r
+ //\r
+ // set Logger\r
+ //\r
+ GenBuildLogger logger = new GenBuildLogger(getProject());\r
+ EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));\r
+ EdkLog.setLogger(logger);\r
+ \r
+ try {\r
+ processFrameworkBuild();\r
+ } catch (PcdAutogenException e) {\r
+ //\r
+ // Add more logic process here\r
+ //\r
+ throw new BuildException(e.getMessage());\r
+ } catch (AutoGenException e) {\r
+ //\r
+ // Add more logic process here\r
+ //\r
+ throw new BuildException(e.getMessage());\r
+ } catch (PlatformPcdPreprocessBuildException e) {\r
+ //\r
+ // Add more logic process here\r
+ //\r
+ throw new BuildException(e.getMessage());\r
+ } catch (GenBuildException e) {\r
+ //\r
+ // Add more logic process here\r
+ //\r
+ throw new BuildException(e.getMessage());\r
+ } catch (EdkException e) {\r
+ //\r
+ // Add more logic process here\r
+ //\r
+ throw new BuildException(e.getMessage());\r
+ }\r
+ }\r
+ \r
+ private void processFrameworkBuild() throws EdkException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {\r
//\r
// Seach build.xml -> .FPD -> .MSA file\r
//\r
//\r
buildFiles.add(files[i]);\r
\r
- } else if (files[i].getName().endsWith(".fpd")) {\r
+ } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
//\r
// Second, search FPD file, if found, build it\r
//\r
fpdFiles.add(files[i]);\r
- } else if (files[i].getName().endsWith(".msa")) {\r
+ } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
//\r
// Third, search MSA file, if found, build it\r
//\r
}\r
}\r
}\r
- } catch (Exception e) {\r
- throw new BuildException(e.getMessage());\r
+ } catch (IOException ex) {\r
+ BuildException buildException = new BuildException("Scanning current directory error. \n" + ex.getMessage());\r
+ buildException.setStackTrace(ex.getStackTrace());\r
+ throw buildException;\r
}\r
\r
//\r
//\r
// Global Data initialization\r
//\r
- GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",\r
- getProject().getProperty("WORKSPACE_DIR"), toolsDefFilename);\r
- \r
-\r
+ File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r
+ getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r
+ GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r
\r
//\r
// If find MSA file and ACTIVE_PLATFORM is set, build the module; \r
//\r
File buildFile = null;\r
if (msaFiles.size() > 1) {\r
- throw new BuildException("More than one MSA file under current directory. It is not allowd. ");\r
- }\r
- else if (msaFiles.size() == 1 && activePlatform == null) {\r
- throw new BuildException("If try to build a single module, please set ACTIVE_PLATFORM in file [Tool/Conf/target.txt]. ");\r
- }\r
- else if (msaFiles.size() == 1 && activePlatform != null) {\r
+ throw new BuildException("Having more than one MSA file in a directory is not allowed!");\r
+ } else if (msaFiles.size() == 1 && activePlatform == null) {\r
+ throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");\r
+ } else if (msaFiles.size() == 1 && activePlatform != null) {\r
//\r
// Build the single module\r
//\r
buildFile = msaFiles.toArray(new File[1])[0];\r
- }\r
- else if (activePlatform != null) {\r
+ } else if (activePlatform != null) {\r
buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
- }\r
- else if (fpdFiles.size() == 1) {\r
+ } else if (fpdFiles.size() == 1) {\r
buildFile = fpdFiles.toArray(new File[1])[0];\r
- }\r
- else if (fpdFiles.size() > 1) {\r
+ } else if (fpdFiles.size() > 1) {\r
buildFile = intercommuniteWithUser();\r
}\r
//\r
// If there is no build files or FPD files or MSA files, stop build\r
//\r
else {\r
- throw new BuildException("Can't find any FPD files or MSA files in current directory. ");\r
+ throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r
}\r
\r
//\r
// Build every FPD files (PLATFORM build)\r
//\r
- if (buildFile.getName().endsWith(".fpd")) {\r
- System.out.println("Start to build FPD file [" + buildFile.getPath() + "] ..>> ");\r
+ if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
+ EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r
+ //\r
+ // Iff for platform build will enable the multi-thread if set in target.txt\r
+ //\r
+ if (multithread && type.equalsIgnoreCase("all")) {\r
+ EdkLog.log(this, "Multi-thread build is enabled. ");\r
+ FpdParserForThread fpdParserForThread = new FpdParserForThread();\r
+ fpdParserForThread.setType(type);\r
+ fpdParserForThread.setProject(getProject());\r
+ fpdParserForThread.setFpdFile(buildFile);\r
+ fpdParserForThread.perform();\r
+ return ;\r
+ }\r
+ \r
FpdParserTask fpdParserTask = new FpdParserTask();\r
fpdParserTask.setType(type);\r
fpdParserTask.setProject(getProject());\r
fpdParserTask.setFpdFile(buildFile);\r
- fpdParserTask.execute();\r
+ fpdParserTask.perform();\r
+ \r
+ //\r
+ // If cleanall delete the Platform_build.xml\r
+ //\r
+ if (type.compareTo("cleanall") == 0) {\r
+ File platformBuildFile = \r
+ new File(getProject().getProperty("BUILD_DIR") \r
+ + File.separatorChar \r
+ + getProject().getProperty("PLATFORM") \r
+ + "_build.xml");\r
+ platformBuildFile.deleteOnExit();\r
+ }\r
}\r
\r
//\r
// Build every MSA files (SINGLE MODULE BUILD)\r
//\r
- else if (buildFile.getName().endsWith(".msa")) {\r
- System.out.println("Start to build MSA file [" + buildFile.getPath() + "] ..>> ");\r
+ else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
+ if (multithread) {\r
+ EdkLog.log(this, EdkLog.EDK_WARNING, "Multi-Thead do not take effect on Stand-Alone (Single) module build. ");\r
+ multithread = false;\r
+ }\r
+ File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
+ EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r
+ EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r
GenBuildTask genBuildTask = new GenBuildTask();\r
genBuildTask.setSingleModuleBuild(true);\r
genBuildTask.setType(type);\r
getProject().setProperty("PLATFORM_FILE", activePlatform);\r
+ if( !multithread) {\r
+ originalProperties.put("PLATFORM_FILE", activePlatform);\r
+ }\r
genBuildTask.setProject(getProject());\r
genBuildTask.setMsaFile(buildFile);\r
- genBuildTask.execute();\r
+ genBuildTask.perform();\r
}\r
}\r
\r
**/\r
private void backupSystemProperties() {\r
Map<String, String> sysProperties = System.getenv();\r
- Set<String> keys = sysProperties.keySet();\r
- Iterator<String> iter = keys.iterator();\r
+ Iterator<String> iter = sysProperties.keySet().iterator();\r
while (iter.hasNext()) {\r
String name = iter.next();\r
\r
getProject().setProperty(name, sysProperties.get(name));\r
}\r
}\r
+ \r
+ Hashtable allProperties = getProject().getProperties();\r
+ Iterator piter = allProperties.keySet().iterator();\r
+ while (piter.hasNext()) {\r
+ String name = (String)piter.next();\r
+ originalProperties.put(new String(name), new String((String)allProperties.get(name)));\r
+ }\r
}\r
\r
private File intercommuniteWithUser(){\r
File file = null;\r
- if (fpdFiles.size() + msaFiles.size() > 1) {\r
- File[] allFiles = new File[fpdFiles.size() + msaFiles.size()];\r
+ if (fpdFiles.size() > 1) {\r
+ File[] allFiles = new File[fpdFiles.size()];\r
int index = 0;\r
Iterator<File> iter = fpdFiles.iterator();\r
while (iter.hasNext()) {\r
allFiles[index] = iter.next();\r
index++;\r
}\r
- iter = msaFiles.iterator();\r
- while (iter.hasNext()) {\r
- allFiles[index] = iter.next();\r
- index++;\r
- }\r
- System.out.println("Find " + allFiles.length + " FPD and MSA files: ");\r
+\r
+ EdkLog.log(this, "Finding " + allFiles.length + " FPD files: ");\r
for (int i = 0; i < allFiles.length; i++) {\r
System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
}\r
\r
boolean flag = true;\r
- System.out.print("Please select one file to build:[1] ");\r
+ EdkLog.log(this, "Please select one of the following FPD files to build:[1] ");\r
do{\r
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
try {\r
}\r
int indexSelect = Integer.parseInt(str);\r
if (indexSelect <=0 || indexSelect > allFiles.length) {\r
- System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
+ EdkLog.log(this, "Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
continue ;\r
} else {\r
file = allFiles[indexSelect - 1];\r
continue ;\r
}\r
} catch (Exception e) {\r
- System.out.print("Please enter a valid number:[1] ");\r
+ EdkLog.log(this, "Please enter a valid number:[1] ");\r
flag = true;\r
}\r
} while (flag);\r
- }\r
- else if (fpdFiles.size() == 1) {\r
+ } else if (fpdFiles.size() == 1) {\r
file = fpdFiles.toArray(new File[1])[0];\r
}\r
- else if (msaFiles.size() == 1) {\r
- file = msaFiles.toArray(new File[1])[0];\r
- }\r
return file;\r
}\r
\r
public void setType(String type) {\r
if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
this.type = type.toLowerCase();\r
- }\r
- else {\r
+ } else {\r
this.type = "all";\r
}\r
}\r
\r
- private void readTargetFile(){\r
- try {\r
- String[][] targetFileInfo = ConfigReader.parse(getProject().getProperty("WORKSPACE_DIR"), "Tools" + File.separatorChar + "Conf" + File.separatorChar + targetFilename);\r
- \r
- //\r
- // Get ToolChain Info from target.txt\r
- //\r
- ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
- String str = getValue("TARGET", targetFileInfo);\r
- if (str == null || str.trim().equals("")) {\r
- envToolChainInfo.addTargets("*");\r
- }\r
- else {\r
- envToolChainInfo.addTargets(str);\r
- }\r
- str = getValue("TOOL_CHAIN_TAG", targetFileInfo);\r
- if (str == null || str.trim().equals("")) {\r
- envToolChainInfo.addTagnames("*");\r
- }\r
- else {\r
- envToolChainInfo.addTagnames(str);\r
- }\r
- str = getValue("TARGET_ARCH", targetFileInfo);\r
- if (str == null || str.trim().equals("")) {\r
- envToolChainInfo.addArchs("*");\r
- }\r
- else {\r
- envToolChainInfo.addArchs(str);\r
- }\r
- GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
- \r
- str = getValue("TOOL_CHAIN_CONF", targetFileInfo);\r
- if (str != null) {\r
- toolsDefFilename = str;\r
+ private void readTargetFile() throws EdkException{\r
+ String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r
+ \r
+ String[][] targetFileInfo = ConfigReader.parse(targetFile);\r
+ \r
+ //\r
+ // Get ToolChain Info from target.txt\r
+ //\r
+ ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
+ String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r
+ if (str == null || str.trim().equals("")) {\r
+ envToolChainInfo.addTargets("*");\r
+ } else {\r
+ envToolChainInfo.addTargets(str);\r
+ }\r
+ str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r
+ if (str == null || str.trim().equals("")) {\r
+ envToolChainInfo.addTagnames("*");\r
+ } else {\r
+ envToolChainInfo.addTagnames(str);\r
+ }\r
+ str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r
+ if (str == null || str.trim().equals("")) {\r
+ envToolChainInfo.addArchs("*");\r
+ } else {\r
+ envToolChainInfo.addArchs(str);\r
+ }\r
+ GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
+ \r
+ str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r
+ if (str != null && str.trim().length() > 0) {\r
+ toolsDefFilename = str;\r
+ }\r
+ \r
+ str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r
+ if (str != null && ! str.trim().equals("")) {\r
+ if ( ! str.endsWith(".fpd")) {\r
+ throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r
}\r
- \r
- str = getValue("ACTIVE_PLATFORM", targetFileInfo);\r
- if (str != null && ! str.trim().equals("")) {\r
- if ( ! str.endsWith(".fpd")) {\r
- throw new BuildException("FPD file's file extension must be \".fpd\"");\r
+ activePlatform = str;\r
+ }\r
+ \r
+ str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);\r
+ if (str != null && str.trim().equalsIgnoreCase("Enable")) {\r
+ multithread = true;\r
+ }\r
+\r
+ str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);\r
+ if (str != null ) {\r
+ try {\r
+ int threadNum = Integer.parseInt(str);\r
+ if (threadNum > 0) {\r
+ MAX_CONCURRENT_THREAD_NUMBER = threadNum;\r
}\r
- activePlatform = str;\r
+ } catch (Exception ex) {\r
}\r
}\r
- catch (Exception ex) {\r
- throw new BuildException(ex.getMessage());\r
- }\r
}\r
\r
private String getValue(String key, String[][] map) {\r