Changed the way of using precompiled header in the build process. Now the use of...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / GenBuildTask.java
CommitLineData
878ddf1f 1/** @file\r
ff225cbb 2 This file is ANT task GenBuild.\r
3\r
4 The file is used to parse a specified Module, and generate its build time\r
878ddf1f 5 ANT script build.xml, then call the the ANT script to build the module.\r
ff225cbb 6\r
878ddf1f 7Copyright (c) 2006, Intel Corporation\r
8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15**/\r
16package org.tianocore.build;\r
17\r
18import java.io.File;\r
aca6c736 19import java.util.Hashtable;\r
878ddf1f 20import java.util.Iterator;\r
21import java.util.LinkedHashSet;\r
22import java.util.List;\r
23import java.util.Map;\r
24import java.util.Set;\r
25import java.util.Vector;\r
26import java.util.regex.Matcher;\r
27import java.util.regex.Pattern;\r
28\r
878ddf1f 29import org.apache.tools.ant.BuildException;\r
aca6c736 30import org.apache.tools.ant.BuildListener;\r
31import org.apache.tools.ant.Project;\r
878ddf1f 32import org.apache.tools.ant.taskdefs.Ant;\r
a29c47e0 33import org.apache.tools.ant.taskdefs.Property;\r
878ddf1f 34import org.apache.xmlbeans.XmlObject;\r
878ddf1f 35\r
4a6a5026 36import org.tianocore.common.definitions.ToolDefinitions;\r
ff225cbb 37import org.tianocore.common.exception.EdkException;\r
d965d1f6 38import org.tianocore.common.logger.EdkLog;\r
136adffc 39import org.tianocore.build.autogen.AutoGen;\r
892b0e7a 40import org.tianocore.build.exception.AutoGenException;\r
41import org.tianocore.build.exception.GenBuildException;\r
42import org.tianocore.build.exception.PcdAutogenException;\r
43import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;\r
878ddf1f 44import org.tianocore.build.fpd.FpdParserTask;\r
45import org.tianocore.build.global.GlobalData;\r
46import org.tianocore.build.global.OutputManager;\r
47import org.tianocore.build.global.SurfaceAreaQuery;\r
a29c47e0 48import org.tianocore.build.id.FpdModuleIdentification;\r
49import org.tianocore.build.id.ModuleIdentification;\r
50import org.tianocore.build.id.PackageIdentification;\r
51import org.tianocore.build.id.PlatformIdentification;\r
52import org.tianocore.build.tools.ModuleItem;\r
878ddf1f 53\r
54/**\r
55 <p>\r
56 <code>GenBuildTask</code> is an ANT task that can be used in ANT build\r
4a6a5026 57 system. \r
58 \r
59 <p>The main function of this task is to parse module's surface area (MSA),\r
878ddf1f 60 then generate the corresponding <em>BaseName_build.xml</em> (the real ANT\r
a29c47e0 61 build script) and call this to build the module. The whole process including:\r
4b5f5549 62 \r
2d16dcec 63 <pre>\r
4a6a5026 64 1. generate AutoGen.c and AutoGen.h; \r
65 2. build all dependent library instances;\r
66 3. build all source files inlcude AutoGen.c; \r
67 4. generate sections;\r
ff225cbb 68 5. generate FFS file if it is driver module while LIB file if it is Library module.\r
2d16dcec 69 </pre>\r
70\r
ff225cbb 71\r
878ddf1f 72 <p>\r
73 The usage is (take module <em>HelloWorld</em> for example):\r
74 </p>\r
ff225cbb 75\r
878ddf1f 76 <pre>\r
4b5f5549 77 &lt;GenBuild \r
78 msaFile="${PACKAGE_DIR}/Application/HelloWorld/HelloWorld.msa"\r
79 type="cleanall" /&gt;\r
878ddf1f 80 </pre>\r
ff225cbb 81\r
878ddf1f 82 <p>\r
83 This task calls <code>AutoGen</code> to generate <em>AutoGen.c</em> and\r
4b5f5549 84 <em>AutoGen.h</em>. \r
878ddf1f 85 </p>\r
ff225cbb 86\r
4b5f5549 87 <p>\r
88 This task will also set properties for current module, such as PACKAGE, \r
89 PACKAGE_GUID, PACKAGE_VERSION, PACKAGE_DIR, PACKAGE_RELATIVE_DIR \r
90 (relative to Workspace), MODULE or BASE_NAME, GUID, VERSION, MODULE_DIR, \r
91 MODULE_RELATIVE_DIR (relative to Package), CONFIG_DIR, BIN_DIR, \r
92 DEST_DIR_DEBUG, DEST_DIR_OUTPUT, TARGET, ARCH, TOOLCHAIN, TOOLCHAIN_FAMILY, \r
93 SUBSYSTEM, ENTRYPOINT, EBC_TOOL_LIB_PATH, all compiler command related \r
94 properties (CC, CC_FLAGS, CC_DPATH, CC_SPATH, CC_FAMILY, CC_EXT). \r
95 </p>\r
96 \r
878ddf1f 97 @since GenBuild 1.0\r
98**/\r
a29c47e0 99public class GenBuildTask extends Ant {\r
ff225cbb 100\r
878ddf1f 101 ///\r
102 /// Module surface area file.\r
103 ///\r
a29c47e0 104 File msaFile;\r
19bf6b15 105 \r
106 public ModuleIdentification parentId;\r
107 \r
82516887 108 private String type = "all"; \r
109 \r
878ddf1f 110 ///\r
a29c47e0 111 /// Module's Identification.\r
878ddf1f 112 ///\r
a29c47e0 113 private ModuleIdentification moduleId;\r
878ddf1f 114\r
a29c47e0 115 private Vector<Property> properties = new Vector<Property>();\r
de4bb9f6 116\r
de4bb9f6 117 private boolean isSingleModuleBuild = false;\r
83fba802 118 \r
119 private SurfaceAreaQuery saq = null;\r
ff225cbb 120\r
878ddf1f 121 /**\r
122 Public construct method. It is necessary for ANT task.\r
123 **/\r
124 public GenBuildTask() {\r
125 }\r
126\r
127 /**\r
ff225cbb 128\r
878ddf1f 129 @throws BuildException\r
130 From module build, exception from module surface area invalid.\r
131 **/\r
132 public void execute() throws BuildException {\r
89e1408f 133 this.setTaskName("GenBuild");\r
892b0e7a 134 try {\r
135 processGenBuild();\r
136 } catch (PcdAutogenException e) {\r
137 //\r
138 // Add more logic process here\r
139 //\r
140 throw new BuildException(e.getMessage());\r
141 } catch (AutoGenException e) {\r
142 //\r
143 // Add more logic process here\r
144 //\r
145 throw new BuildException(e.getMessage());\r
146 } catch (PlatformPcdPreprocessBuildException e) {\r
147 //\r
148 // Add more logic process here\r
149 //\r
150 throw new BuildException(e.getMessage());\r
151 } catch (GenBuildException e) {\r
152 //\r
153 // Add more logic process here\r
154 //\r
155 throw new BuildException(e.getMessage());\r
156 } catch (EdkException e) {\r
157 //\r
158 // Add more logic process here\r
159 //\r
160 throw new BuildException(e.getMessage());\r
161 }\r
162 }\r
163\r
164 private void processGenBuild() throws EdkException, BuildException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {\r
aca6c736 165 if (!FrameworkBuildTask.multithread) {\r
166 cleanupProperties();\r
167 }\r
82516887 168\r
878ddf1f 169 //\r
a29c47e0 170 // Enable all specified properties\r
878ddf1f 171 //\r
a29c47e0 172 Iterator<Property> iter = properties.iterator();\r
173 while (iter.hasNext()) {\r
174 Property item = iter.next();\r
aca6c736 175 getProject().setProperty(item.getName(), item.getValue());\r
878ddf1f 176 }\r
ff225cbb 177\r
878ddf1f 178 //\r
a29c47e0 179 // GenBuild should specify either msaFile or moduleGuid & packageGuid\r
878ddf1f 180 //\r
a29c47e0 181 if (msaFile == null ) {\r
182 String moduleGuid = getProject().getProperty("MODULE_GUID");\r
183 String moduleVersion = getProject().getProperty("MODULE_VERSION");\r
184 String packageGuid = getProject().getProperty("PACKAGE_GUID");\r
185 String packageVersion = getProject().getProperty("PACKAGE_VERSION");\r
892b0e7a 186 //\r
187 // If one of module Guid or package Guid is not specified, report error\r
188 //\r
a29c47e0 189 if (moduleGuid == null || packageGuid == null) {\r
391dbbb1 190 throw new BuildException("GenBuild parameter error.");\r
878ddf1f 191 }\r
892b0e7a 192 \r
a29c47e0 193 PackageIdentification packageId = new PackageIdentification(packageGuid, packageVersion);\r
892b0e7a 194 GlobalData.refreshPackageIdentification(packageId);\r
a29c47e0 195 moduleId = new ModuleIdentification(moduleGuid, moduleVersion);\r
196 moduleId.setPackage(packageId);\r
892b0e7a 197 GlobalData.refreshModuleIdentification(moduleId);\r
a29c47e0 198 Map<String, XmlObject> doc = GlobalData.getNativeMsa(moduleId);\r
83fba802 199 saq = new SurfaceAreaQuery(doc);\r
82516887 200 } else {\r
a29c47e0 201 Map<String, XmlObject> doc = GlobalData.getNativeMsa(msaFile);\r
83fba802 202 saq = new SurfaceAreaQuery(doc);\r
203 moduleId = saq.getMsaHeader();\r
89e1408f 204 moduleId.setMsaFile(msaFile);\r
878ddf1f 205 }\r
892b0e7a 206 \r
83fba802 207 String[] producedLibraryClasses = saq.getLibraryClasses("ALWAYS_PRODUCED",null);\r
a29c47e0 208 if (producedLibraryClasses.length == 0) {\r
209 moduleId.setLibrary(false);\r
82516887 210 } else {\r
a29c47e0 211 moduleId.setLibrary(true);\r
878ddf1f 212 }\r
ff225cbb 213\r
878ddf1f 214 //\r
a29c47e0 215 // Judge whether it is single module build or not\r
878ddf1f 216 //\r
de4bb9f6 217 if (isSingleModuleBuild) {\r
878ddf1f 218 //\r
a29c47e0 219 // Single Module build\r
878ddf1f 220 //\r
a29c47e0 221 prepareSingleModuleBuild();\r
878ddf1f 222 }\r
ff225cbb 223\r
a29c47e0 224 //\r
82516887 225 // If single module : get arch from pass down, otherwise intersection MSA \r
226 // supported ARCHs and tools def\r
a29c47e0 227 //\r
ff225cbb 228 Set<String> archListSupByToolChain = new LinkedHashSet<String>();\r
229 String[] archs = GlobalData.getToolChainInfo().getArchs();\r
230\r
3c696250 231 for (int i = 0; i < archs.length; i ++) {\r
232 archListSupByToolChain.add(archs[i]);\r
233 }\r
ff225cbb 234\r
3c696250 235 Set<String> archSet = new LinkedHashSet<String>();\r
ff225cbb 236\r
3c696250 237 if ( getProject().getProperty("ARCH") != null) {\r
238 String[] fpdArchList = getProject().getProperty("ARCH").split(" ");\r
ff225cbb 239\r
3c696250 240 for (int i = 0; i < fpdArchList.length; i++) {\r
241 if (archListSupByToolChain.contains(fpdArchList[i])) {\r
242 archSet.add(fpdArchList[i]);\r
243 }\r
244 }\r
82516887 245 } else {\r
246 archSet = archListSupByToolChain; \r
3c696250 247 }\r
ff225cbb 248\r
3c696250 249 String[] archList = archSet.toArray(new String[archSet.size()]);\r
ff225cbb 250\r
a29c47e0 251 //\r
252 // Judge if arch is all supported by current module. If not, throw Exception.\r
253 //\r
83fba802 254 List moduleSupportedArchs = saq.getModuleSupportedArchs();\r
a29c47e0 255 if (moduleSupportedArchs != null) {\r
256 for (int k = 0; k < archList.length; k++) {\r
257 if ( ! moduleSupportedArchs.contains(archList[k])) {\r
391dbbb1 258 throw new BuildException("Specified architecture [" + archList[k] + "] is not supported by " + moduleId + ". The module " + moduleId + " only supports [" + moduleSupportedArchs + "] architectures.");\r
a29c47e0 259 }\r
260 }\r
878ddf1f 261 }\r
ff225cbb 262\r
a29c47e0 263 for (int k = 0; k < archList.length; k++) {\r
ff225cbb 264\r
aca6c736 265 getProject().setProperty("ARCH", archList[k]);\r
ff225cbb 266\r
a29c47e0 267 FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, archList[k]);\r
ff225cbb 268\r
878ddf1f 269 //\r
a29c47e0 270 // Whether the module is built before\r
878ddf1f 271 //\r
34281092 272 if (moduleId.isLibrary() == false && GlobalData.hasFpdModuleSA(fpdModuleId) == false) {\r
d965d1f6 273 EdkLog.log(this, EdkLog.EDK_WARNING, "Warning: " + moduleId + " for " + archList[k] + " was not found in current platform FPD file!\n");\r
34281092 274 continue;\r
275 } else if (GlobalData.isModuleBuilt(fpdModuleId)) {\r
1fa1cb75 276 break;\r
34281092 277 } else {\r
a29c47e0 278 GlobalData.registerBuiltModule(fpdModuleId);\r
279 }\r
ff225cbb 280\r
878ddf1f 281 //\r
a29c47e0 282 // For Every TOOLCHAIN, TARGET\r
283 //\r
284 String[] targetList = GlobalData.getToolChainInfo().getTargets();\r
285 for (int i = 0; i < targetList.length; i ++){\r
286 //\r
287 // Prepare for target related common properties\r
288 // TARGET\r
289 //\r
aca6c736 290 getProject().setProperty("TARGET", targetList[i]);\r
a29c47e0 291 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();\r
292 for(int j = 0; j < toolchainList.length; j ++){\r
c773bec0 293 //\r
294 // check if any tool is defined for current target + toolchain + arch\r
295 // don't do anything if no tools found\r
ff225cbb 296 //\r
c773bec0 297 if (GlobalData.isCommandSet(targetList[i], toolchainList[j], archList[k]) == false) {\r
d965d1f6 298 EdkLog.log(this, EdkLog.EDK_WARNING, "Warning: No build issued. No tools were found for [target=" + targetList[i] + " toolchain=" + toolchainList[j] + " arch=" + archList[k] + "]\n");\r
c773bec0 299 continue;\r
300 }\r
301\r
a29c47e0 302 //\r
303 // Prepare for toolchain related common properties\r
304 // TOOLCHAIN\r
305 //\r
aca6c736 306 getProject().setProperty("TOOLCHAIN", toolchainList[j]);\r
a29c47e0 307\r
d965d1f6 308 EdkLog.log(this, "Build " + moduleId + " start >>>");\r
309 EdkLog.log(this, "Target: " + targetList[i] + " Tagname: " + toolchainList[j] + " Arch: " + archList[k]);\r
83fba802 310 saq.push(GlobalData.getDoc(fpdModuleId));\r
ff225cbb 311\r
a29c47e0 312 //\r
313 // Prepare for all other common properties\r
314 // PACKAGE, PACKAGE_GUID, PACKAGE_VERSION, PACKAGE_DIR, PACKAGE_RELATIVE_DIR\r
315 // MODULE or BASE_NAME, GUID or FILE_GUID, VERSION, MODULE_TYPE\r
316 // MODULE_DIR, MODULE_RELATIVE_DIR\r
317 // SUBSYSTEM, ENTRYPOINT, EBC_TOOL_LIB_PATH\r
a29c47e0 318 //\r
319 setModuleCommonProperties(archList[k]);\r
ff225cbb 320\r
a29c47e0 321 //\r
ff225cbb 322 // OutputManage prepare for\r
a29c47e0 323 // BIN_DIR, DEST_DIR_DEBUG, DEST_DIR_OUTPUT, BUILD_DIR, FV_DIR\r
324 //\r
325 OutputManager.getInstance().update(getProject());\r
ff225cbb 326\r
a29c47e0 327 if (type.equalsIgnoreCase("all") || type.equalsIgnoreCase("build")) {\r
328 applyBuild(targetList[i], toolchainList[j], fpdModuleId);\r
82516887 329 } else if (type.equalsIgnoreCase("clean")) {\r
a29c47e0 330 applyClean(fpdModuleId);\r
82516887 331 } else if (type.equalsIgnoreCase("cleanall")) {\r
a29c47e0 332 applyCleanall(fpdModuleId);\r
333 }\r
334 }\r
335 }\r
878ddf1f 336 }\r
878ddf1f 337 }\r
338\r
339 /**\r
ff225cbb 340 This method is used to prepare Platform-related information.\r
341\r
a29c47e0 342 <p>In Single Module Build mode, platform-related information is not ready.\r
ff225cbb 343 The method read the system environment variable <code>ACTIVE_PLATFORM</code>\r
a29c47e0 344 and search in the Framework Database. Note that platform name in the Framework\r
345 Database must be unique. </p>\r
ff225cbb 346\r
878ddf1f 347 **/\r
892b0e7a 348 private void prepareSingleModuleBuild() throws EdkException {\r
878ddf1f 349 //\r
a29c47e0 350 // Find out the package which the module belongs to\r
878ddf1f 351 //\r
a29c47e0 352 PackageIdentification packageId = GlobalData.getPackageForModule(moduleId);\r
892b0e7a 353 GlobalData.refreshPackageIdentification(packageId);\r
a29c47e0 354 moduleId.setPackage(packageId);\r
892b0e7a 355 GlobalData.refreshModuleIdentification(moduleId);\r
ff225cbb 356\r
878ddf1f 357 //\r
82516887 358 // Read ACTIVE_PLATFORM's FPD file \r
878ddf1f 359 //\r
de4bb9f6 360 String filename = getProject().getProperty("PLATFORM_FILE");\r
ff225cbb 361\r
de4bb9f6 362 if (filename == null){\r
391dbbb1 363 throw new BuildException("Please set ACTIVE_PLATFORM in the file: Tools/Conf/target.txt if you want to build a single module!");\r
878ddf1f 364 }\r
ff225cbb 365\r
de4bb9f6 366 PlatformIdentification platformId = GlobalData.getPlatform(filename);\r
ff225cbb 367\r
878ddf1f 368 //\r
82516887 369 // Read FPD file (Call FpdParserTask's method)\r
878ddf1f 370 //\r
a29c47e0 371 FpdParserTask fpdParser = new FpdParserTask();\r
372 fpdParser.setProject(getProject());\r
07193171 373 fpdParser.parseFpdFile(platformId.getFpdFile());\r
aca6c736 374 getProject().setProperty("ARCH", fpdParser.getAllArchForModule(moduleId));\r
878ddf1f 375 }\r
376\r
aca6c736 377 private void cleanupProperties() {\r
378 Project newProject = new Project();\r
379\r
380 Hashtable<String, String> passdownProperties = FrameworkBuildTask.originalProperties;\r
381 Iterator<String> iter = passdownProperties.keySet().iterator();\r
382 while (iter.hasNext()) {\r
383 String item = iter.next();\r
384 newProject.setProperty(item, passdownProperties.get(item));\r
385 }\r
386\r
387 newProject.setInputHandler(getProject().getInputHandler());\r
388\r
389 Iterator listenerIter = getProject().getBuildListeners().iterator();\r
390 while (listenerIter.hasNext()) {\r
391 newProject.addBuildListener((BuildListener) listenerIter.next());\r
392 }\r
393\r
394 getProject().initSubProject(newProject);\r
395\r
396 setProject(newProject);\r
397 }\r
878ddf1f 398\r
399 /**\r
a29c47e0 400 Set Module-Related information to properties.\r
82516887 401 \r
402 @param arch current build ARCH\r
878ddf1f 403 **/\r
a29c47e0 404 private void setModuleCommonProperties(String arch) {\r
878ddf1f 405 //\r
a29c47e0 406 // Prepare for all other common properties\r
407 // PACKAGE, PACKAGE_GUID, PACKAGE_VERSION, PACKAGE_DIR, PACKAGE_RELATIVE_DIR\r
878ddf1f 408 //\r
a29c47e0 409 PackageIdentification packageId = moduleId.getPackage();\r
aca6c736 410 getProject().setProperty("PACKAGE", packageId.getName());\r
411 getProject().setProperty("PACKAGE_GUID", packageId.getGuid());\r
412 getProject().setProperty("PACKAGE_VERSION", packageId.getVersion());\r
413 getProject().setProperty("PACKAGE_DIR", packageId.getPackageDir().replaceAll("(\\\\)", "/"));\r
414 getProject().setProperty("PACKAGE_RELATIVE_DIR", packageId.getPackageRelativeDir().replaceAll("(\\\\)", "/"));\r
ff225cbb 415\r
a29c47e0 416 //\r
417 // MODULE or BASE_NAME, GUID or FILE_GUID, VERSION, MODULE_TYPE\r
418 // MODULE_DIR, MODULE_RELATIVE_DIR\r
419 //\r
aca6c736 420 getProject().setProperty("MODULE", moduleId.getName());\r
83fba802 421 String baseName = saq.getModuleOutputFileBasename();\r
a29c47e0 422 if (baseName == null) {\r
aca6c736 423 getProject().setProperty("BASE_NAME", moduleId.getName());\r
82516887 424 } else {\r
aca6c736 425 getProject().setProperty("BASE_NAME", baseName);\r
a29c47e0 426 }\r
aca6c736 427 getProject().setProperty("GUID", moduleId.getGuid());\r
428 getProject().setProperty("FILE_GUID", moduleId.getGuid());\r
429 getProject().setProperty("VERSION", moduleId.getVersion());\r
430 getProject().setProperty("MODULE_TYPE", moduleId.getModuleType());\r
431 getProject().setProperty("MODULE_DIR", moduleId.getMsaFile().getParent().replaceAll("(\\\\)", "/"));\r
432 getProject().setProperty("MODULE_RELATIVE_DIR", moduleId.getModuleRelativePath().replaceAll("(\\\\)", "/"));\r
ff225cbb 433\r
a29c47e0 434 //\r
435 // SUBSYSTEM\r
436 //\r
437 String[][] subsystemMap = { { "BASE", "EFI_BOOT_SERVICE_DRIVER"},\r
ff225cbb 438 { "SEC", "EFI_BOOT_SERVICE_DRIVER" },\r
439 { "PEI_CORE", "EFI_BOOT_SERVICE_DRIVER" },\r
440 { "PEIM", "EFI_BOOT_SERVICE_DRIVER" },\r
a29c47e0 441 { "DXE_CORE", "EFI_BOOT_SERVICE_DRIVER" },\r
ff225cbb 442 { "DXE_DRIVER", "EFI_BOOT_SERVICE_DRIVER" },\r
443 { "DXE_RUNTIME_DRIVER", "EFI_RUNTIME_DRIVER" },\r
444 { "DXE_SAL_DRIVER", "EFI_BOOT_SERVICE_DRIVER" },\r
445 { "DXE_SMM_DRIVER", "EFI_BOOT_SERVICE_DRIVER" },\r
446 { "TOOL", "EFI_BOOT_SERVICE_DRIVER" },\r
a29c47e0 447 { "UEFI_DRIVER", "EFI_BOOT_SERVICE_DRIVER" },\r
ff225cbb 448 { "UEFI_APPLICATION", "EFI_APPLICATION" },\r
449 { "USER_DEFINED", "EFI_BOOT_SERVICE_DRIVER"} };\r
450\r
a29c47e0 451 String subsystem = "EFI_BOOT_SERVICE_DRIVER";\r
452 for (int i = 0; i < subsystemMap.length; i++) {\r
453 if (moduleId.getModuleType().equalsIgnoreCase(subsystemMap[i][0])) {\r
454 subsystem = subsystemMap[i][1];\r
455 break ;\r
878ddf1f 456 }\r
457 }\r
aca6c736 458 getProject().setProperty("SUBSYSTEM", subsystem);\r
ff225cbb 459\r
a29c47e0 460 //\r
461 // ENTRYPOINT\r
462 //\r
463 if (arch.equalsIgnoreCase("EBC")) {\r
aca6c736 464 getProject().setProperty("ENTRYPOINT", "EfiStart");\r
82516887 465 } else {\r
aca6c736 466 getProject().setProperty("ENTRYPOINT", "_ModuleEntryPoint");\r
878ddf1f 467 }\r
ff225cbb 468\r
aca6c736 469 getProject().setProperty("OBJECTS", "");\r
878ddf1f 470 }\r
471\r
a29c47e0 472 private void getCompilerFlags(String target, String toolchain, FpdModuleIdentification fpdModuleId) throws EdkException {\r
473 String[] cmd = GlobalData.getToolChainInfo().getCommands();\r
474 for ( int m = 0; m < cmd.length; m++) {\r
878ddf1f 475 //\r
a29c47e0 476 // Set cmd, like CC, DLINK\r
878ddf1f 477 //\r
a29c47e0 478 String[] key = new String[]{target, toolchain, fpdModuleId.getArch(), cmd[m], null};\r
4a6a5026 479 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_PATH;\r
a29c47e0 480 String cmdPath = GlobalData.getCommandSetting(key, fpdModuleId);\r
4a6a5026 481 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_NAME;\r
a29c47e0 482 String cmdName = GlobalData.getCommandSetting(key, fpdModuleId);\r
1549f516 483 if (cmdName.length() == 0) {\r
484 continue;\r
485 }\r
a29c47e0 486 File cmdFile = new File(cmdPath + File.separatorChar + cmdName);\r
aca6c736 487 getProject().setProperty(cmd[m], cmdFile.getPath().replaceAll("(\\\\)", "/"));\r
ff225cbb 488\r
878ddf1f 489 //\r
a29c47e0 490 // set CC_FLAGS\r
878ddf1f 491 //\r
4a6a5026 492 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_FLAGS;\r
a29c47e0 493 String cmdFlags = GlobalData.getCommandSetting(key, fpdModuleId);\r
e04a8a9a 494 if (cmdFlags != null) \r
495 {\r
496// Set<String> addset = new LinkedHashSet<String>();\r
497// Set<String> subset = new LinkedHashSet<String>();\r
498// putFlagsToSet(addset, cmdFlags);\r
499// getProject().setProperty(cmd[m] + "_FLAGS", getProject().replaceProperties(getFlags(addset, subset)));\r
500 getProject().setProperty(cmd[m] + "_FLAGS", cmdFlags);\r
501 } \r
502 else \r
503 {\r
504 getProject().setProperty(cmd[m] + "_FLAGS", "");\r
505 }\r
506\r
ff225cbb 507\r
878ddf1f 508 //\r
a29c47e0 509 // Set CC_EXT\r
878ddf1f 510 //\r
4a6a5026 511 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_EXT;\r
a29c47e0 512 String extName = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 513 if ( extName != null && ! extName.equalsIgnoreCase("")) {\r
aca6c736 514 getProject().setProperty(cmd[m] + "_EXT", extName);\r
82516887 515 } else {\r
aca6c736 516 getProject().setProperty(cmd[m] + "_EXT", "");\r
878ddf1f 517 }\r
ff225cbb 518\r
878ddf1f 519 //\r
a29c47e0 520 // set CC_FAMILY\r
878ddf1f 521 //\r
4a6a5026 522 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_FAMILY;\r
a29c47e0 523 String toolChainFamily = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 524 if (toolChainFamily != null) {\r
aca6c736 525 getProject().setProperty(cmd[m] + "_FAMILY", toolChainFamily);\r
878ddf1f 526 }\r
ff225cbb 527\r
878ddf1f 528 //\r
a29c47e0 529 // set CC_SPATH\r
878ddf1f 530 //\r
4a6a5026 531 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_SPATH;\r
a29c47e0 532 String spath = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 533 if (spath != null) {\r
aca6c736 534 getProject().setProperty(cmd[m] + "_SPATH", spath.replaceAll("(\\\\)", "/"));\r
82516887 535 } else {\r
aca6c736 536 getProject().setProperty(cmd[m] + "_SPATH", "");\r
878ddf1f 537 }\r
ff225cbb 538\r
878ddf1f 539 //\r
a29c47e0 540 // set CC_DPATH\r
878ddf1f 541 //\r
4a6a5026 542 key[4] = ToolDefinitions.TOOLS_DEF_ATTRIBUTE_DPATH;\r
a29c47e0 543 String dpath = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 544 if (dpath != null) {\r
aca6c736 545 getProject().setProperty(cmd[m] + "_DPATH", dpath.replaceAll("(\\\\)", "/"));\r
82516887 546 } else {\r
aca6c736 547 getProject().setProperty(cmd[m] + "_DPATH", "");\r
878ddf1f 548 }\r
549 }\r
a29c47e0 550 }\r
ff225cbb 551\r
a29c47e0 552 public void setMsaFile(File msaFile) {\r
553 this.msaFile = msaFile;\r
878ddf1f 554 }\r
555\r
556 /**\r
ff225cbb 557 Method is for ANT to initialize MSA file.\r
558\r
a29c47e0 559 @param msaFilename MSA file name\r
878ddf1f 560 **/\r
a29c47e0 561 public void setMsaFile(String msaFilename) {\r
562 String moduleDir = getProject().getProperty("MODULE_DIR");\r
ff225cbb 563\r
a29c47e0 564 //\r
565 // If is Single Module Build, then use the Base Dir defined in build.xml\r
566 //\r
567 if (moduleDir == null) {\r
568 moduleDir = getProject().getBaseDir().getPath();\r
878ddf1f 569 }\r
a29c47e0 570 msaFile = new File(moduleDir + File.separatorChar + msaFilename);\r
571 }\r
ff225cbb 572\r
a29c47e0 573 public void addConfiguredModuleItem(ModuleItem moduleItem) {\r
574 PackageIdentification packageId = new PackageIdentification(moduleItem.getPackageGuid(), moduleItem.getPackageVersion());\r
575 ModuleIdentification moduleId = new ModuleIdentification(moduleItem.getModuleGuid(), moduleItem.getModuleVersion());\r
576 moduleId.setPackage(packageId);\r
577 this.moduleId = moduleId;\r
578 }\r
ff225cbb 579\r
a29c47e0 580 /**\r
ff225cbb 581 Add a property.\r
582\r
a29c47e0 583 @param p property\r
584 **/\r
585 public void addProperty(Property p) {\r
586 properties.addElement(p);\r
587 }\r
588\r
589 public void setType(String type) {\r
590 this.type = type;\r
591 }\r
ff225cbb 592\r
892b0e7a 593 private void applyBuild(String buildTarget, String buildTagname, FpdModuleIdentification fpdModuleId) throws EdkException {\r
a29c47e0 594 //\r
83fba802 595 // Call AutoGen to generate AutoGen.c and AutoGen.h\r
a29c47e0 596 //\r
2eb7d78d 597 AutoGen autogen = new AutoGen(getProject().getProperty("FV_DIR"), getProject().getProperty("DEST_DIR_DEBUG"), fpdModuleId.getModule(),fpdModuleId.getArch(), saq, parentId);\r
136adffc 598 autogen.genAutogen();\r
ff225cbb 599\r
a29c47e0 600 //\r
601 // Get compiler flags\r
602 //\r
82516887 603 try {\r
604 getCompilerFlags(buildTarget, buildTagname, fpdModuleId);\r
605 }\r
606 catch (EdkException ee) {\r
607 throw new BuildException(ee.getMessage());\r
608 }\r
609 \r
a29c47e0 610 //\r
611 // Prepare LIBS\r
612 //\r
83fba802 613 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());\r
a29c47e0 614 String propertyLibs = "";\r
615 for (int i = 0; i < libinstances.length; i++) {\r
498e9021 616 propertyLibs += getProject().getProperty("BIN_DIR") + File.separatorChar + libinstances[i].getName() + ".lib" + " ";\r
878ddf1f 617 }\r
aca6c736 618 getProject().setProperty("LIBS", propertyLibs.replaceAll("(\\\\)", "/"));\r
ff225cbb 619\r
bf3a7173 620 //\r
621 // Get all includepath and set to INCLUDE_PATHS\r
622 //\r
623 String[] includes = prepareIncludePaths(fpdModuleId);\r
624 \r
a29c47e0 625 //\r
626 // if it is CUSTOM_BUILD\r
627 // then call the exist BaseName_build.xml directly.\r
628 //\r
629 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
d965d1f6 630 EdkLog.log(this, "Call user-defined " + moduleId.getName() + "_build.xml");\r
82516887 631 \r
632 String antFilename = getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml";\r
633 antCall(antFilename, null);\r
634 \r
a29c47e0 635 return ;\r
878ddf1f 636 }\r
ff225cbb 637\r
878ddf1f 638 //\r
a29c47e0 639 // Generate ${BASE_NAME}_build.xml\r
640 // TBD\r
878ddf1f 641 //\r
83fba802 642 String ffsKeyword = saq.getModuleFfsKeyword();\r
643 ModuleBuildFileGenerator fileGenerator = new ModuleBuildFileGenerator(getProject(), ffsKeyword, fpdModuleId, includes, saq);\r
a29c47e0 644 String buildFilename = getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml";\r
645 fileGenerator.genBuildFile(buildFilename);\r
ff225cbb 646\r
a29c47e0 647 //\r
648 // Ant call ${BASE_NAME}_build.xml\r
649 //\r
82516887 650 String antFilename = getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml";\r
651 antCall(antFilename, null);\r
a29c47e0 652 }\r
ff225cbb 653\r
a29c47e0 654 private void applyClean(FpdModuleIdentification fpdModuleId){\r
655 //\r
656 // if it is CUSTOM_BUILD\r
657 // then call the exist BaseName_build.xml directly.\r
658 //\r
659 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
d965d1f6 660 EdkLog.log(this, "Calling user-defined " + moduleId.getName() + "_build.xml");\r
82516887 661 \r
662 String antFilename = getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml";\r
663 antCall(antFilename, "clean");\r
664 \r
a29c47e0 665 return ;\r
878ddf1f 666 }\r
ff225cbb 667\r
82516887 668 String antFilename = getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml";\r
669 antCall(antFilename, "clean");\r
a29c47e0 670 }\r
ff225cbb 671\r
a29c47e0 672 private void applyCleanall(FpdModuleIdentification fpdModuleId){\r
673 //\r
674 // if it is CUSTOM_BUILD\r
675 // then call the exist BaseName_build.xml directly.\r
676 //\r
677 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
d965d1f6 678 EdkLog.log(this, "Calling user-defined " + moduleId.getName() + "_build.xml");\r
82516887 679\r
680 String antFilename = getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml";\r
681 antCall(antFilename, "cleanall");\r
682 \r
a29c47e0 683 return ;\r
878ddf1f 684 }\r
82516887 685 \r
686 String antFilename = getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml";\r
687 antCall(antFilename, "cleanall");\r
688 }\r
ff225cbb 689\r
82516887 690 private void antCall(String antFilename, String target) {\r
a29c47e0 691 Ant ant = new Ant();\r
692 ant.setProject(getProject());\r
82516887 693 ant.setAntfile(antFilename);\r
694 if (target != null) {\r
695 ant.setTarget(target);\r
696 }\r
a29c47e0 697 ant.setInheritAll(true);\r
698 ant.init();\r
699 ant.execute();\r
878ddf1f 700 }\r
701\r
a29c47e0 702\r
878ddf1f 703 /**\r
704 Separate the string and instore in set.\r
ff225cbb 705\r
706 <p> String is separated by Java Regulation Expression\r
878ddf1f 707 "[^\\\\]?(\".*?[^\\\\]\")[ \t,]+". </p>\r
ff225cbb 708\r
878ddf1f 709 <p>For example: </p>\r
ff225cbb 710\r
878ddf1f 711 <pre>\r
712 "/nologo", "/W3", "/WX"\r
713 "/C", "/DSTRING_DEFINES_FILE=\"BdsStrDefs.h\""\r
714 </pre>\r
ff225cbb 715\r
878ddf1f 716 @param set store the separated string\r
717 @param str string to separate\r
718 **/\r
719 private void putFlagsToSet(Set<String> set, String str) {\r
a29c47e0 720 if (str == null || str.length() == 0) {\r
721 return;\r
722 }\r
723\r
878ddf1f 724 Pattern myPattern = Pattern.compile("[^\\\\]?(\".*?[^\\\\]\")[ \t,]+");\r
725 Matcher matcher = myPattern.matcher(str + " ");\r
726 while (matcher.find()) {\r
727 String item = str.substring(matcher.start(1), matcher.end(1));\r
a29c47e0 728 set.add(item);\r
878ddf1f 729 }\r
730 }\r
ff225cbb 731\r
878ddf1f 732 /**\r
ff225cbb 733 Generate the final flags string will be used by compile command.\r
734\r
878ddf1f 735 @param add the add flags set\r
736 @param sub the sub flags set\r
737 @return final flags after add set substract sub set\r
738 **/\r
739 private String getFlags(Set<String> add, Set<String> sub) {\r
740 String result = "";\r
741 add.removeAll(sub);\r
742 Iterator iter = add.iterator();\r
743 while (iter.hasNext()) {\r
a29c47e0 744 String str = (String) iter.next();\r
878ddf1f 745 result += str.substring(1, str.length() - 1) + " ";\r
746 }\r
747 return result;\r
748 }\r
749\r
de4bb9f6 750 public void setSingleModuleBuild(boolean isSingleModuleBuild) {\r
751 this.isSingleModuleBuild = isSingleModuleBuild;\r
752 }\r
bf3a7173 753 \r
892b0e7a 754 private String[] prepareIncludePaths(FpdModuleIdentification fpdModuleId) throws EdkException{\r
bf3a7173 755 //\r
756 // Prepare the includes: PackageDependencies and Output debug direactory\r
757 //\r
758 Set<String> includes = new LinkedHashSet<String>();\r
759 String arch = fpdModuleId.getArch();\r
760 \r
761 //\r
762 // WORKSPACE\r
763 //\r
764 includes.add("${WORKSPACE_DIR}" + File.separatorChar);\r
765 \r
766 //\r
767 // Module iteself\r
768 //\r
769 includes.add("${MODULE_DIR}");\r
770 includes.add("${MODULE_DIR}" + File.separatorChar + archDir(arch));\r
771 \r
772 //\r
773 // Packages in PackageDenpendencies\r
774 //\r
83fba802 775 PackageIdentification[] packageDependencies = saq.getDependencePkg(fpdModuleId.getArch());\r
bf3a7173 776 for (int i = 0; i < packageDependencies.length; i++) {\r
777 GlobalData.refreshPackageIdentification(packageDependencies[i]);\r
778 File packageFile = packageDependencies[i].getSpdFile();\r
779 includes.add(packageFile.getParent() + File.separatorChar + "Include");\r
780 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));\r
781 }\r
782\r
783 //\r
784 // All Dependency Library Instance's PackageDependencies\r
785 //\r
83fba802 786 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());\r
bf3a7173 787 for (int i = 0; i < libinstances.length; i++) {\r
83fba802 788 saq.push(GlobalData.getDoc(libinstances[i], fpdModuleId.getArch()));\r
789 PackageIdentification[] libraryPackageDependencies = saq.getDependencePkg(fpdModuleId.getArch());\r
bf3a7173 790 for (int j = 0; j < libraryPackageDependencies.length; j++) {\r
791 GlobalData.refreshPackageIdentification(libraryPackageDependencies[j]);\r
792 File packageFile = libraryPackageDependencies[j].getSpdFile();\r
793 includes.add(packageFile.getParent() + File.separatorChar + "Include");\r
794 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));\r
795 }\r
83fba802 796 saq.pop();\r
bf3a7173 797 }\r
798 \r
799 \r
800 //\r
801 // The package which the module belongs to\r
802 // TBD\r
803 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include");\r
804 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));\r
805\r
806 //\r
807 // Debug files output directory\r
808 //\r
809 includes.add("${DEST_DIR_DEBUG}");\r
810 \r
811 //\r
812 // set to INCLUDE_PATHS property\r
813 //\r
814 Iterator<String> iter = includes.iterator();\r
815 StringBuffer includePaths = new StringBuffer();\r
816 while (iter.hasNext()) {\r
817 includePaths.append(iter.next());\r
818 includePaths.append("; ");\r
819 }\r
aca6c736 820 getProject().setProperty("INCLUDE_PATHS", getProject().replaceProperties(includePaths.toString()).replaceAll("(\\\\)", "/"));\r
bf3a7173 821 \r
822 return includes.toArray(new String[includes.size()]);\r
823 }\r
824 \r
825 /**\r
826 Return the name of the directory that corresponds to the architecture.\r
827 This is a translation from the XML Schema tag to a directory that\r
828 corresponds to our directory name coding convention.\r
829 \r
830 **/\r
831 private String archDir(String arch) {\r
832 return arch.replaceFirst("X64", "x64")\r
833 .replaceFirst("IPF", "Ipf")\r
834 .replaceFirst("IA32", "Ia32")\r
835 .replaceFirst("ARM", "Arm")\r
836 .replaceFirst("EBC", "Ebc");\r
19bf6b15 837 }\r
838 \r
839 \r
840 public void setExternalProperties(Vector<Property> v) {\r
841 this.properties = v;\r
842 }\r
878ddf1f 843}\r