| 1 | /** @file FrameworkBuildTask.java\r |
| 2 | \r |
| 3 | The file is ANT task to find MSA or FPD file and build them. \r |
| 4 | \r |
| 5 | Copyright (c) 2006, Intel Corporation\r |
| 6 | All rights reserved. This program and the accompanying materials\r |
| 7 | are licensed and made available under the terms and conditions of the BSD License\r |
| 8 | which accompanies this distribution. The full text of the license may be found at\r |
| 9 | http://opensource.org/licenses/bsd-license.php\r |
| 10 | \r |
| 11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
| 12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
| 13 | **/\r |
| 14 | package org.tianocore.build;\r |
| 15 | \r |
| 16 | import java.io.BufferedReader;\r |
| 17 | import java.io.File;\r |
| 18 | import java.io.InputStreamReader;\r |
| 19 | import java.util.Iterator;\r |
| 20 | import java.util.LinkedHashSet;\r |
| 21 | import java.util.Map;\r |
| 22 | import java.util.Set;\r |
| 23 | \r |
| 24 | import org.apache.tools.ant.BuildException;\r |
| 25 | import org.apache.tools.ant.Task;\r |
| 26 | import org.tianocore.build.fpd.FpdParserTask;\r |
| 27 | import org.tianocore.build.global.GlobalData;\r |
| 28 | import org.tianocore.build.toolchain.ConfigReader;\r |
| 29 | import org.tianocore.build.toolchain.ToolChainInfo;\r |
| 30 | import org.tianocore.common.definitions.ToolDefinitions;\r |
| 31 | \r |
| 32 | /**\r |
| 33 | <p>\r |
| 34 | <code>FrameworkBuildTask</code> is an Ant task. The main function is finding\r |
| 35 | and processing a FPD or MSA file, then building a platform or stand-alone \r |
| 36 | module. \r |
| 37 | \r |
| 38 | <p>\r |
| 39 | The task search current directory and find out all MSA and FPD files by file\r |
| 40 | extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a\r |
| 41 | stand-alone module. The ACTIVE_PLATFORM policy is: \r |
| 42 | \r |
| 43 | <pre>\r |
| 44 | 1. More than one MSA files, report error; \r |
| 45 | 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;\r |
| 46 | 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;\r |
| 47 | 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;\r |
| 48 | 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;\r |
| 49 | 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;\r |
| 50 | 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform\r |
| 51 | and let user choose one. \r |
| 52 | </pre>\r |
| 53 | \r |
| 54 | <p>\r |
| 55 | Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].\r |
| 56 | And load all system environment variables to Ant properties. \r |
| 57 | \r |
| 58 | <p>\r |
| 59 | The usage for this task is : \r |
| 60 | \r |
| 61 | <pre>\r |
| 62 | <FrameworkBuild type="cleanall" />\r |
| 63 | </pre>\r |
| 64 | \r |
| 65 | @since GenBuild 1.0\r |
| 66 | **/\r |
| 67 | public class FrameworkBuildTask extends Task{\r |
| 68 | \r |
| 69 | private Set<File> buildFiles = new LinkedHashSet<File>();\r |
| 70 | \r |
| 71 | private Set<File> fpdFiles = new LinkedHashSet<File>();\r |
| 72 | \r |
| 73 | private Set<File> msaFiles = new LinkedHashSet<File>();\r |
| 74 | \r |
| 75 | String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;\r |
| 76 | \r |
| 77 | String targetFilename = ToolDefinitions.TARGET_FILE_PATH;\r |
| 78 | \r |
| 79 | String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;\r |
| 80 | \r |
| 81 | String activePlatform = null;\r |
| 82 | \r |
| 83 | ///\r |
| 84 | /// there are three type: all (build), clean and cleanall\r |
| 85 | ///\r |
| 86 | private String type = "all";\r |
| 87 | \r |
| 88 | public void execute() throws BuildException {\r |
| 89 | //\r |
| 90 | // Seach build.xml -> .FPD -> .MSA file\r |
| 91 | //\r |
| 92 | try {\r |
| 93 | //\r |
| 94 | // Gen Current Working Directory\r |
| 95 | //\r |
| 96 | File dummyFile = new File(".");\r |
| 97 | File cwd = dummyFile.getCanonicalFile();\r |
| 98 | File[] files = cwd.listFiles();\r |
| 99 | for (int i = 0; i < files.length; i++) {\r |
| 100 | if (files[i].isFile()) {\r |
| 101 | if (files[i].getName().equalsIgnoreCase("build.xml")) {\r |
| 102 | //\r |
| 103 | // First, search build.xml, if found, ANT call it\r |
| 104 | //\r |
| 105 | buildFiles.add(files[i]);\r |
| 106 | \r |
| 107 | } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r |
| 108 | //\r |
| 109 | // Second, search FPD file, if found, build it\r |
| 110 | //\r |
| 111 | fpdFiles.add(files[i]);\r |
| 112 | } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r |
| 113 | //\r |
| 114 | // Third, search MSA file, if found, build it\r |
| 115 | //\r |
| 116 | msaFiles.add(files[i]);\r |
| 117 | }\r |
| 118 | }\r |
| 119 | }\r |
| 120 | } catch (Exception e) {\r |
| 121 | throw new BuildException(e.getMessage());\r |
| 122 | }\r |
| 123 | \r |
| 124 | //\r |
| 125 | // Deal with all environment variable (Add them to properties)\r |
| 126 | //\r |
| 127 | backupSystemProperties();\r |
| 128 | \r |
| 129 | //\r |
| 130 | // Read target.txt file\r |
| 131 | //\r |
| 132 | readTargetFile();\r |
| 133 | \r |
| 134 | //\r |
| 135 | // Global Data initialization\r |
| 136 | //\r |
| 137 | File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r |
| 138 | getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r |
| 139 | GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r |
| 140 | \r |
| 141 | //\r |
| 142 | // If find MSA file and ACTIVE_PLATFORM is set, build the module; \r |
| 143 | // else fail build. \r |
| 144 | // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM. \r |
| 145 | // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform; \r |
| 146 | // If find more than one FPD files, let user select one. \r |
| 147 | //\r |
| 148 | File buildFile = null;\r |
| 149 | if (msaFiles.size() > 1) {\r |
| 150 | throw new BuildException("Having more than one MSA file in a directory is not allowed!");\r |
| 151 | } else if (msaFiles.size() == 1 && activePlatform == null) {\r |
| 152 | throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");\r |
| 153 | } else if (msaFiles.size() == 1 && activePlatform != null) {\r |
| 154 | //\r |
| 155 | // Build the single module\r |
| 156 | //\r |
| 157 | buildFile = msaFiles.toArray(new File[1])[0];\r |
| 158 | } else if (activePlatform != null) {\r |
| 159 | buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r |
| 160 | } else if (fpdFiles.size() == 1) {\r |
| 161 | buildFile = fpdFiles.toArray(new File[1])[0];\r |
| 162 | } else if (fpdFiles.size() > 1) {\r |
| 163 | buildFile = intercommuniteWithUser();\r |
| 164 | }\r |
| 165 | //\r |
| 166 | // If there is no build files or FPD files or MSA files, stop build\r |
| 167 | //\r |
| 168 | else {\r |
| 169 | throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r |
| 170 | }\r |
| 171 | \r |
| 172 | //\r |
| 173 | // Build every FPD files (PLATFORM build)\r |
| 174 | //\r |
| 175 | if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r |
| 176 | System.out.println("Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r |
| 177 | FpdParserTask fpdParserTask = new FpdParserTask();\r |
| 178 | fpdParserTask.setType(type);\r |
| 179 | fpdParserTask.setProject(getProject());\r |
| 180 | fpdParserTask.setFpdFile(buildFile);\r |
| 181 | fpdParserTask.execute();\r |
| 182 | }\r |
| 183 | \r |
| 184 | //\r |
| 185 | // Build every MSA files (SINGLE MODULE BUILD)\r |
| 186 | //\r |
| 187 | else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r |
| 188 | File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r |
| 189 | System.out.println("Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r |
| 190 | System.out.println("Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r |
| 191 | GenBuildTask genBuildTask = new GenBuildTask();\r |
| 192 | genBuildTask.setSingleModuleBuild(true);\r |
| 193 | genBuildTask.setType(type);\r |
| 194 | getProject().setProperty("PLATFORM_FILE", activePlatform);\r |
| 195 | genBuildTask.setProject(getProject());\r |
| 196 | genBuildTask.setMsaFile(buildFile);\r |
| 197 | genBuildTask.execute();\r |
| 198 | }\r |
| 199 | }\r |
| 200 | \r |
| 201 | /**\r |
| 202 | Transfer system environment variables to ANT properties. If system variable \r |
| 203 | already exiests in ANT properties, skip it.\r |
| 204 | \r |
| 205 | **/\r |
| 206 | private void backupSystemProperties() {\r |
| 207 | Map<String, String> sysProperties = System.getenv();\r |
| 208 | Set<String> keys = sysProperties.keySet();\r |
| 209 | Iterator<String> iter = keys.iterator();\r |
| 210 | while (iter.hasNext()) {\r |
| 211 | String name = iter.next();\r |
| 212 | \r |
| 213 | //\r |
| 214 | // If system environment variable is not in ANT properties, add it\r |
| 215 | //\r |
| 216 | if (getProject().getProperty(name) == null) {\r |
| 217 | getProject().setProperty(name, sysProperties.get(name));\r |
| 218 | }\r |
| 219 | }\r |
| 220 | }\r |
| 221 | \r |
| 222 | private File intercommuniteWithUser(){\r |
| 223 | File file = null;\r |
| 224 | if (fpdFiles.size() > 1) {\r |
| 225 | File[] allFiles = new File[fpdFiles.size()];\r |
| 226 | int index = 0;\r |
| 227 | Iterator<File> iter = fpdFiles.iterator();\r |
| 228 | while (iter.hasNext()) {\r |
| 229 | allFiles[index] = iter.next();\r |
| 230 | index++;\r |
| 231 | }\r |
| 232 | \r |
| 233 | System.out.println("Finding " + allFiles.length + " FPD files: ");\r |
| 234 | for (int i = 0; i < allFiles.length; i++) {\r |
| 235 | System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r |
| 236 | }\r |
| 237 | \r |
| 238 | boolean flag = true;\r |
| 239 | System.out.print("Please select one of the following FPD files to build:[1] ");\r |
| 240 | do{\r |
| 241 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r |
| 242 | try {\r |
| 243 | String str = br.readLine();\r |
| 244 | if (str.trim().length() == 0) {\r |
| 245 | file = allFiles[0];\r |
| 246 | flag = false;\r |
| 247 | continue ;\r |
| 248 | }\r |
| 249 | int indexSelect = Integer.parseInt(str);\r |
| 250 | if (indexSelect <=0 || indexSelect > allFiles.length) {\r |
| 251 | System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");\r |
| 252 | continue ;\r |
| 253 | } else {\r |
| 254 | file = allFiles[indexSelect - 1];\r |
| 255 | flag = false;\r |
| 256 | continue ;\r |
| 257 | }\r |
| 258 | } catch (Exception e) {\r |
| 259 | System.out.print("Please enter a valid number:[1] ");\r |
| 260 | flag = true;\r |
| 261 | }\r |
| 262 | } while (flag);\r |
| 263 | } else if (fpdFiles.size() == 1) {\r |
| 264 | file = fpdFiles.toArray(new File[1])[0];\r |
| 265 | }\r |
| 266 | return file;\r |
| 267 | }\r |
| 268 | \r |
| 269 | \r |
| 270 | public void setType(String type) {\r |
| 271 | if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r |
| 272 | this.type = type.toLowerCase();\r |
| 273 | } else {\r |
| 274 | this.type = "all";\r |
| 275 | }\r |
| 276 | }\r |
| 277 | \r |
| 278 | private void readTargetFile(){\r |
| 279 | try {\r |
| 280 | String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r |
| 281 | \r |
| 282 | String[][] targetFileInfo = ConfigReader.parse(targetFile);\r |
| 283 | \r |
| 284 | //\r |
| 285 | // Get ToolChain Info from target.txt\r |
| 286 | //\r |
| 287 | ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r |
| 288 | String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r |
| 289 | if (str == null || str.trim().equals("")) {\r |
| 290 | envToolChainInfo.addTargets("*");\r |
| 291 | } else {\r |
| 292 | envToolChainInfo.addTargets(str);\r |
| 293 | }\r |
| 294 | str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r |
| 295 | if (str == null || str.trim().equals("")) {\r |
| 296 | envToolChainInfo.addTagnames("*");\r |
| 297 | } else {\r |
| 298 | envToolChainInfo.addTagnames(str);\r |
| 299 | }\r |
| 300 | str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r |
| 301 | if (str == null || str.trim().equals("")) {\r |
| 302 | envToolChainInfo.addArchs("*");\r |
| 303 | } else {\r |
| 304 | envToolChainInfo.addArchs(str);\r |
| 305 | }\r |
| 306 | GlobalData.setToolChainEnvInfo(envToolChainInfo);\r |
| 307 | \r |
| 308 | str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r |
| 309 | if (str != null && str.trim().length() > 0) {\r |
| 310 | toolsDefFilename = str;\r |
| 311 | }\r |
| 312 | \r |
| 313 | str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r |
| 314 | if (str != null && ! str.trim().equals("")) {\r |
| 315 | if ( ! str.endsWith(".fpd")) {\r |
| 316 | throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r |
| 317 | }\r |
| 318 | activePlatform = str;\r |
| 319 | }\r |
| 320 | }\r |
| 321 | catch (Exception ex) {\r |
| 322 | throw new BuildException(ex.getMessage());\r |
| 323 | }\r |
| 324 | }\r |
| 325 | \r |
| 326 | private String getValue(String key, String[][] map) {\r |
| 327 | for (int i = 0; i < map[0].length; i++){\r |
| 328 | if (key.equalsIgnoreCase(map[0][i])) {\r |
| 329 | return map[1][i];\r |
| 330 | }\r |
| 331 | }\r |
| 332 | return null;\r |
| 333 | }\r |
| 334 | }\r |