Fixed EDKT111
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / GenBuildTask.java
CommitLineData
878ddf1f 1/** @file\r
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
5 ANT script build.xml, then call the the ANT script to build the module.\r
6 \r
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
a29c47e0 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
a29c47e0 25import java.util.Stack;\r
878ddf1f 26import java.util.Vector;\r
27import java.util.regex.Matcher;\r
28import java.util.regex.Pattern;\r
29\r
878ddf1f 30import org.apache.tools.ant.BuildException;\r
878ddf1f 31import org.apache.tools.ant.taskdefs.Ant;\r
a29c47e0 32import org.apache.tools.ant.taskdefs.Property;\r
878ddf1f 33import org.apache.xmlbeans.XmlObject;\r
878ddf1f 34\r
136adffc 35import org.tianocore.build.autogen.AutoGen;\r
878ddf1f 36import org.tianocore.build.fpd.FpdParserTask;\r
136adffc 37import org.tianocore.build.global.GenBuildLogger;\r
878ddf1f 38import org.tianocore.build.global.GlobalData;\r
39import org.tianocore.build.global.OutputManager;\r
40import org.tianocore.build.global.SurfaceAreaQuery;\r
a29c47e0 41import org.tianocore.build.id.FpdModuleIdentification;\r
42import org.tianocore.build.id.ModuleIdentification;\r
43import org.tianocore.build.id.PackageIdentification;\r
44import org.tianocore.build.id.PlatformIdentification;\r
45import org.tianocore.build.tools.ModuleItem;\r
136adffc 46import org.tianocore.exception.EdkException;\r
47import org.tianocore.logger.EdkLog;\r
878ddf1f 48\r
49/**\r
50 <p>\r
51 <code>GenBuildTask</code> is an ANT task that can be used in ANT build\r
52 system. The main function of this task is to parse module's surface area,\r
53 then generate the corresponding <em>BaseName_build.xml</em> (the real ANT\r
a29c47e0 54 build script) and call this to build the module. The whole process including:\r
55 1. generate AutoGen.c and AutoGen.h; 2. build all dependent library instances; \r
56 3. build all source files inlcude AutoGen.c; 4. generate sections;\r
57 5. generate FFS file if it is driver module while LIB file if it is Library module. \r
878ddf1f 58 </p>\r
59 \r
60 <p>\r
61 The usage is (take module <em>HelloWorld</em> for example):\r
62 </p>\r
63 \r
64 <pre>\r
a29c47e0 65 &lt;GenBuild \r
66 msaFilename=&quot;HelloWorld.msa&quot;/&gt; \r
67 processTo=&quot;ALL&quot;/&gt;\r
878ddf1f 68 </pre>\r
69 \r
a29c47e0 70 <p><code>processTo</code> provides a way to customize the whole build process. \r
71 processTo can be one value of ALL, AUTOGEN, FILES, LIBRARYINSTANCES, SECTIONS, NONE. \r
72 Default is ALL, means whole \r
73 </p>\r
74 \r
878ddf1f 75 <p>\r
76 This task calls <code>AutoGen</code> to generate <em>AutoGen.c</em> and\r
77 <em>AutoGen.h</em>. The task also parses the development environment\r
78 configuration files, such as collecting package information, setting compiler\r
79 flags and so on.\r
80 </p>\r
81 \r
82 \r
83 @since GenBuild 1.0\r
84**/\r
a29c47e0 85public class GenBuildTask extends Ant {\r
86 \r
878ddf1f 87 ///\r
88 /// Module surface area file.\r
89 ///\r
a29c47e0 90 File msaFile;\r
878ddf1f 91\r
92 ///\r
878ddf1f 93 /// \r
878ddf1f 94 ///\r
a29c47e0 95 private String type = "all"; // = "build";\r
96 \r
878ddf1f 97 ///\r
a29c47e0 98 /// Module's Identification.\r
878ddf1f 99 ///\r
a29c47e0 100 private ModuleIdentification moduleId;\r
878ddf1f 101\r
a29c47e0 102 private Vector<Property> properties = new Vector<Property>();\r
de4bb9f6 103\r
a29c47e0 104 private static Stack<Hashtable> backupPropertiesStack = new Stack<Hashtable>();\r
105 \r
de4bb9f6 106 private boolean isSingleModuleBuild = false;\r
a29c47e0 107 \r
878ddf1f 108 /**\r
109 Public construct method. It is necessary for ANT task.\r
110 **/\r
111 public GenBuildTask() {\r
112 }\r
113\r
114 /**\r
a29c47e0 115 \r
878ddf1f 116 @throws BuildException\r
117 From module build, exception from module surface area invalid.\r
118 **/\r
119 public void execute() throws BuildException {\r
136adffc 120 //\r
121 // set Logger\r
122 //\r
123 GenBuildLogger logger = new GenBuildLogger(getProject());\r
124 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));\r
125 EdkLog.setLogger(logger);\r
126 // remove !!\r
127 try {\r
a29c47e0 128 pushProperties();\r
878ddf1f 129 //\r
a29c47e0 130 // Enable all specified properties\r
878ddf1f 131 //\r
a29c47e0 132 Iterator<Property> iter = properties.iterator();\r
133 while (iter.hasNext()) {\r
134 Property item = iter.next();\r
135 getProject().setProperty(item.getName(), item.getValue());\r
878ddf1f 136 }\r
a29c47e0 137 \r
878ddf1f 138 //\r
a29c47e0 139 // GenBuild should specify either msaFile or moduleGuid & packageGuid\r
878ddf1f 140 //\r
a29c47e0 141 if (msaFile == null ) {\r
142 String moduleGuid = getProject().getProperty("MODULE_GUID");\r
143 String moduleVersion = getProject().getProperty("MODULE_VERSION");\r
144 String packageGuid = getProject().getProperty("PACKAGE_GUID");\r
145 String packageVersion = getProject().getProperty("PACKAGE_VERSION");\r
146 if (moduleGuid == null || packageGuid == null) {\r
147 throw new BuildException("GenBuild parameters error. ");\r
878ddf1f 148 }\r
a29c47e0 149 PackageIdentification packageId = new PackageIdentification(packageGuid, packageVersion);\r
150 moduleId = new ModuleIdentification(moduleGuid, moduleVersion);\r
151 moduleId.setPackage(packageId);\r
152 Map<String, XmlObject> doc = GlobalData.getNativeMsa(moduleId);\r
153 SurfaceAreaQuery.setDoc(doc);\r
154 moduleId = SurfaceAreaQuery.getMsaHeader();\r
878ddf1f 155 }\r
a29c47e0 156 else {\r
157 Map<String, XmlObject> doc = GlobalData.getNativeMsa(msaFile);\r
158 SurfaceAreaQuery.setDoc(doc);\r
159 moduleId = SurfaceAreaQuery.getMsaHeader();\r
878ddf1f 160 }\r
42b78757 161 String[] producedLibraryClasses = SurfaceAreaQuery.getLibraryClasses("ALWAYS_PRODUCED",null);\r
a29c47e0 162 if (producedLibraryClasses.length == 0) {\r
163 moduleId.setLibrary(false);\r
878ddf1f 164 }\r
a29c47e0 165 else {\r
166 moduleId.setLibrary(true);\r
878ddf1f 167 }\r
a29c47e0 168 \r
878ddf1f 169 //\r
a29c47e0 170 // Judge whether it is single module build or not\r
878ddf1f 171 //\r
de4bb9f6 172 if (isSingleModuleBuild) {\r
878ddf1f 173 //\r
a29c47e0 174 // Single Module build\r
878ddf1f 175 //\r
a29c47e0 176 prepareSingleModuleBuild();\r
177 }\r
178 else {\r
179 //\r
180 // Platform build. Restore the platform related info\r
181 //\r
de4bb9f6 182 String filename = getProject().getProperty("PLATFORM_FILE");\r
183 PlatformIdentification platformId = GlobalData.getPlatform(filename);\r
a29c47e0 184 getProject().setProperty("PLATFORM_DIR", platformId.getFpdFile().getParent().replaceAll("(\\\\)", "/"));\r
185 getProject().setProperty("PLATFORM_RELATIVE_DIR", platformId.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));\r
878ddf1f 186 \r
a29c47e0 187 String packageGuid = getProject().getProperty("PACKAGE_GUID");\r
188 String packageVersion = getProject().getProperty("PACKAGE_VERSION");\r
189 PackageIdentification packageId = new PackageIdentification(packageGuid, packageVersion);\r
190 moduleId.setPackage(packageId);\r
878ddf1f 191 }\r
a29c47e0 192 \r
193 //\r
194 // If single module : intersection MSA supported ARCHs and tools def!!\r
195 // else, get arch from pass down\r
196 //\r
68542af1 197 String[] archList = GlobalData.getToolChainInfo().getArchs(); \r
a29c47e0 198 \r
199 //\r
200 // Judge if arch is all supported by current module. If not, throw Exception.\r
201 //\r
202 List moduleSupportedArchs = SurfaceAreaQuery.getModuleSupportedArchs();\r
203 if (moduleSupportedArchs != null) {\r
204 for (int k = 0; k < archList.length; k++) {\r
205 if ( ! moduleSupportedArchs.contains(archList[k])) {\r
206 throw new BuildException("ARCH [" + archList[k] + "] is not supported by " + moduleId + ". " + moduleId + " only supports [" + moduleSupportedArchs + "].");\r
207 }\r
208 }\r
878ddf1f 209 }\r
a29c47e0 210 \r
211 for (int k = 0; k < archList.length; k++) {\r
212 getProject().setProperty("ARCH", archList[k]);\r
213 \r
214 FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, archList[k]);\r
215 \r
878ddf1f 216 //\r
a29c47e0 217 // Whether the module is built before\r
878ddf1f 218 //\r
a29c47e0 219 if (GlobalData.isModuleBuilt(fpdModuleId)) {\r
220 return ;\r
221 }\r
222 else {\r
223 GlobalData.registerBuiltModule(fpdModuleId);\r
224 }\r
225 \r
878ddf1f 226 //\r
a29c47e0 227 // For Every TOOLCHAIN, TARGET\r
228 //\r
229 String[] targetList = GlobalData.getToolChainInfo().getTargets();\r
230 for (int i = 0; i < targetList.length; i ++){\r
231 //\r
232 // Prepare for target related common properties\r
233 // TARGET\r
234 //\r
235 getProject().setProperty("TARGET", targetList[i]);\r
236 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();\r
237 for(int j = 0; j < toolchainList.length; j ++){\r
c773bec0 238 //\r
239 // check if any tool is defined for current target + toolchain + arch\r
240 // don't do anything if no tools found\r
241 // \r
242 if (GlobalData.isCommandSet(targetList[i], toolchainList[j], archList[k]) == false) {\r
196ad8d7 243 System.out.println("Warning: No build issued. No tools found for [target=" + targetList[i] + " toolchain=" + toolchainList[j] + " arch=" + archList[k] + "]\n");\r
c773bec0 244 continue;\r
245 }\r
246\r
a29c47e0 247 //\r
248 // Prepare for toolchain related common properties\r
249 // TOOLCHAIN\r
250 //\r
251 getProject().setProperty("TOOLCHAIN", toolchainList[j]);\r
252\r
253 System.out.println("Build " + moduleId + " start >>>");\r
254 System.out.println("Target: " + targetList[i] + " Tagname: " + toolchainList[j] + " Arch: " + archList[k]);\r
255 SurfaceAreaQuery.setDoc(GlobalData.getDoc(fpdModuleId));\r
256 \r
257 //\r
258 // Prepare for all other common properties\r
259 // PACKAGE, PACKAGE_GUID, PACKAGE_VERSION, PACKAGE_DIR, PACKAGE_RELATIVE_DIR\r
260 // MODULE or BASE_NAME, GUID or FILE_GUID, VERSION, MODULE_TYPE\r
261 // MODULE_DIR, MODULE_RELATIVE_DIR\r
262 // SUBSYSTEM, ENTRYPOINT, EBC_TOOL_LIB_PATH\r
263 // LIBS, OBJECTS, SDB_FILES\r
264 //\r
265 setModuleCommonProperties(archList[k]);\r
266 \r
267 //\r
268 // OutputManage prepare for \r
269 // BIN_DIR, DEST_DIR_DEBUG, DEST_DIR_OUTPUT, BUILD_DIR, FV_DIR\r
270 //\r
271 OutputManager.getInstance().update(getProject());\r
272 \r
273 if (type.equalsIgnoreCase("all") || type.equalsIgnoreCase("build")) {\r
274 applyBuild(targetList[i], toolchainList[j], fpdModuleId);\r
275 }\r
276 else if (type.equalsIgnoreCase("clean")) {\r
277 applyClean(fpdModuleId);\r
278 }\r
279 else if (type.equalsIgnoreCase("cleanall")) {\r
280 applyCleanall(fpdModuleId);\r
281 }\r
282 }\r
283 }\r
878ddf1f 284 }\r
a29c47e0 285 popProperties();\r
286 }catch (Exception e){\r
a29c47e0 287 throw new BuildException(e.getMessage());\r
878ddf1f 288 }\r
289 }\r
290\r
291 /**\r
a29c47e0 292 This method is used to prepare Platform-related information. \r
878ddf1f 293 \r
a29c47e0 294 <p>In Single Module Build mode, platform-related information is not ready.\r
295 The method read the system environment variable <code>ACTIVE_PLATFORM</code> \r
296 and search in the Framework Database. Note that platform name in the Framework\r
297 Database must be unique. </p>\r
298 \r
878ddf1f 299 **/\r
a29c47e0 300 private void prepareSingleModuleBuild(){\r
878ddf1f 301 //\r
a29c47e0 302 // Find out the package which the module belongs to\r
303 // TBD: Enhance it!!!!\r
878ddf1f 304 //\r
a29c47e0 305 PackageIdentification packageId = GlobalData.getPackageForModule(moduleId);\r
306 \r
307 moduleId.setPackage(packageId);\r
308 \r
878ddf1f 309 //\r
a29c47e0 310 // Read ACTIVE_PLATFORM's FPD file (Call FpdParserTask's method)\r
878ddf1f 311 //\r
de4bb9f6 312 String filename = getProject().getProperty("PLATFORM_FILE");\r
a29c47e0 313 \r
de4bb9f6 314 if (filename == null){\r
a29c47e0 315 throw new BuildException("Plese set ACTIVE_PLATFORM if you want to build a single module. ");\r
878ddf1f 316 }\r
a29c47e0 317 \r
de4bb9f6 318 PlatformIdentification platformId = GlobalData.getPlatform(filename);\r
a29c47e0 319 \r
878ddf1f 320 //\r
a29c47e0 321 // Read FPD file\r
878ddf1f 322 //\r
a29c47e0 323 FpdParserTask fpdParser = new FpdParserTask();\r
324 fpdParser.setProject(getProject());\r
325 fpdParser.parseFpdFile(platformId.getFpdFile());\r
326 \r
878ddf1f 327 //\r
a29c47e0 328 // Prepare for Platform related common properties\r
329 // PLATFORM, PLATFORM_DIR, PLATFORM_RELATIVE_DIR\r
878ddf1f 330 //\r
de4bb9f6 331 getProject().setProperty("PLATFORM", platformId.getName());\r
a29c47e0 332 getProject().setProperty("PLATFORM_DIR", platformId.getFpdFile().getParent().replaceAll("(\\\\)", "/"));\r
333 getProject().setProperty("PLATFORM_RELATIVE_DIR", platformId.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));\r
878ddf1f 334 }\r
335\r
878ddf1f 336\r
337 /**\r
a29c47e0 338 Set Module-Related information to properties.\r
878ddf1f 339 **/\r
a29c47e0 340 private void setModuleCommonProperties(String arch) {\r
878ddf1f 341 //\r
a29c47e0 342 // Prepare for all other common properties\r
343 // PACKAGE, PACKAGE_GUID, PACKAGE_VERSION, PACKAGE_DIR, PACKAGE_RELATIVE_DIR\r
878ddf1f 344 //\r
a29c47e0 345 PackageIdentification packageId = moduleId.getPackage();\r
346 getProject().setProperty("PACKAGE", packageId.getName());\r
347 getProject().setProperty("PACKAGE_GUID", packageId.getGuid());\r
348 getProject().setProperty("PACKAGE_VERSION", packageId.getVersion());\r
349 getProject().setProperty("PACKAGE_DIR", packageId.getPackageDir().replaceAll("(\\\\)", "/"));\r
350 getProject().setProperty("PACKAGE_RELATIVE_DIR", packageId.getPackageRelativeDir().replaceAll("(\\\\)", "/"));\r
351 \r
352 //\r
353 // MODULE or BASE_NAME, GUID or FILE_GUID, VERSION, MODULE_TYPE\r
354 // MODULE_DIR, MODULE_RELATIVE_DIR\r
355 //\r
356 getProject().setProperty("MODULE", moduleId.getName());\r
357 String baseName = SurfaceAreaQuery.getModuleOutputFileBasename();\r
358 if (baseName == null) {\r
359 getProject().setProperty("BASE_NAME", moduleId.getName());\r
360 }\r
878ddf1f 361 else {\r
a29c47e0 362 getProject().setProperty("BASE_NAME", baseName);\r
363 }\r
364 getProject().setProperty("GUID", moduleId.getGuid());\r
365 getProject().setProperty("FILE_GUID", moduleId.getGuid());\r
366 getProject().setProperty("VERSION", moduleId.getVersion());\r
367 getProject().setProperty("MODULE_TYPE", moduleId.getModuleType());\r
368 getProject().setProperty("MODULE_DIR", moduleId.getMsaFile().getParent().replaceAll("(\\\\)", "/"));\r
369 getProject().setProperty("MODULE_RELATIVE_DIR", moduleId.getModuleRelativePath().replaceAll("(\\\\)", "/"));\r
370 \r
371 //\r
372 // SUBSYSTEM\r
373 //\r
374 String[][] subsystemMap = { { "BASE", "EFI_BOOT_SERVICE_DRIVER"},\r
375 { "SEC", "EFI_BOOT_SERVICE_DRIVER" }, \r
376 { "PEI_CORE", "EFI_BOOT_SERVICE_DRIVER" }, \r
377 { "PEIM", "EFI_BOOT_SERVICE_DRIVER" }, \r
378 { "DXE_CORE", "EFI_BOOT_SERVICE_DRIVER" },\r
379 { "DXE_DRIVER", "EFI_BOOT_SERVICE_DRIVER" }, \r
380 { "DXE_RUNTIME_DRIVER", "EFI_RUNTIME_DRIVER" }, \r
381 { "DXE_SAL_DRIVER", "EFI_BOOT_SERVICE_DRIVER" }, \r
382 { "DXE_SMM_DRIVER", "EFI_BOOT_SERVICE_DRIVER" }, \r
383 { "TOOL", "EFI_BOOT_SERVICE_DRIVER" }, \r
384 { "UEFI_DRIVER", "EFI_BOOT_SERVICE_DRIVER" },\r
385 { "UEFI_APPLICATION", "EFI_APPLICATION" }, \r
386 { "USER_DEFINED", "EFI_BOOT_SERVICE_DRIVER"} }; \r
387 \r
388 String subsystem = "EFI_BOOT_SERVICE_DRIVER";\r
389 for (int i = 0; i < subsystemMap.length; i++) {\r
390 if (moduleId.getModuleType().equalsIgnoreCase(subsystemMap[i][0])) {\r
391 subsystem = subsystemMap[i][1];\r
392 break ;\r
878ddf1f 393 }\r
394 }\r
a29c47e0 395 getProject().setProperty("SUBSYSTEM", subsystem);\r
396 \r
397 //\r
398 // ENTRYPOINT\r
399 //\r
400 if (arch.equalsIgnoreCase("EBC")) {\r
401 getProject().setProperty("ENTRYPOINT", "EfiStart");\r
878ddf1f 402 }\r
a29c47e0 403 else {\r
404 getProject().setProperty("ENTRYPOINT", "_ModuleEntryPoint");\r
878ddf1f 405 }\r
a29c47e0 406 \r
878ddf1f 407 //\r
a29c47e0 408 // LIBS, OBJECTS, SDB_FILES\r
878ddf1f 409 //\r
a29c47e0 410 getProject().setProperty("OBJECTS", "");\r
411 getProject().setProperty("SDB_FILES", "");\r
412 getProject().setProperty("LIBS", "");\r
878ddf1f 413 }\r
414\r
a29c47e0 415 private void getCompilerFlags(String target, String toolchain, FpdModuleIdentification fpdModuleId) throws EdkException {\r
416 String[] cmd = GlobalData.getToolChainInfo().getCommands();\r
417 for ( int m = 0; m < cmd.length; m++) {\r
878ddf1f 418 //\r
a29c47e0 419 // Set cmd, like CC, DLINK\r
878ddf1f 420 //\r
a29c47e0 421 String[] key = new String[]{target, toolchain, fpdModuleId.getArch(), cmd[m], null};\r
422 key[4] = "PATH";\r
423 String cmdPath = GlobalData.getCommandSetting(key, fpdModuleId);\r
424 key[4] = "NAME";\r
425 String cmdName = GlobalData.getCommandSetting(key, fpdModuleId);\r
426 File cmdFile = new File(cmdPath + File.separatorChar + cmdName);\r
a29c47e0 427 getProject().setProperty(cmd[m], cmdFile.getPath().replaceAll("(\\\\)", "/"));\r
428 \r
878ddf1f 429 //\r
a29c47e0 430 // set CC_FLAGS\r
878ddf1f 431 //\r
a29c47e0 432 key[4] = "FLAGS";\r
433 String cmdFlags = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 434 Set<String> addset = new LinkedHashSet<String>();\r
435 Set<String> subset = new LinkedHashSet<String>();\r
436 putFlagsToSet(addset, cmdFlags);\r
437 getProject().setProperty(cmd[m] + "_FLAGS", getProject().replaceProperties(getFlags(addset, subset)));\r
438 \r
878ddf1f 439 //\r
a29c47e0 440 // Set CC_EXT\r
878ddf1f 441 //\r
a29c47e0 442 key[4] = "EXT";\r
443 String extName = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 444 if ( extName != null && ! extName.equalsIgnoreCase("")) {\r
445 getProject().setProperty(cmd[m] + "_EXT", extName);\r
878ddf1f 446 }\r
a29c47e0 447 else {\r
448 getProject().setProperty(cmd[m] + "_EXT", "");\r
878ddf1f 449 }\r
a29c47e0 450 \r
878ddf1f 451 //\r
a29c47e0 452 // set CC_FAMILY\r
878ddf1f 453 //\r
a29c47e0 454 key[4] = "FAMILY";\r
455 String toolChainFamily = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 456 if (toolChainFamily != null) {\r
457 getProject().setProperty(cmd[m] + "_FAMILY", toolChainFamily);\r
878ddf1f 458 }\r
a29c47e0 459 \r
878ddf1f 460 //\r
a29c47e0 461 // set CC_SPATH\r
878ddf1f 462 //\r
a29c47e0 463 key[4] = "SPATH";\r
464 String spath = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 465 if (spath != null) {\r
466 getProject().setProperty(cmd[m] + "_SPATH", spath.replaceAll("(\\\\)", "/"));\r
878ddf1f 467 }\r
a29c47e0 468 else {\r
469 getProject().setProperty(cmd[m] + "_SPATH", "");\r
878ddf1f 470 }\r
a29c47e0 471 \r
878ddf1f 472 //\r
a29c47e0 473 // set CC_DPATH\r
878ddf1f 474 //\r
a29c47e0 475 key[4] = "DPATH";\r
476 String dpath = GlobalData.getCommandSetting(key, fpdModuleId);\r
a29c47e0 477 if (dpath != null) {\r
478 getProject().setProperty(cmd[m] + "_DPATH", dpath.replaceAll("(\\\\)", "/"));\r
878ddf1f 479 }\r
a29c47e0 480 else {\r
481 getProject().setProperty(cmd[m] + "_DPATH", "");\r
878ddf1f 482 }\r
483 }\r
a29c47e0 484 }\r
485 \r
486 public void setMsaFile(File msaFile) {\r
487 this.msaFile = msaFile;\r
878ddf1f 488 }\r
489\r
490 /**\r
a29c47e0 491 Method is for ANT to initialize MSA file. \r
492 \r
493 @param msaFilename MSA file name\r
878ddf1f 494 **/\r
a29c47e0 495 public void setMsaFile(String msaFilename) {\r
496 String moduleDir = getProject().getProperty("MODULE_DIR");\r
878ddf1f 497 \r
a29c47e0 498 //\r
499 // If is Single Module Build, then use the Base Dir defined in build.xml\r
500 //\r
501 if (moduleDir == null) {\r
502 moduleDir = getProject().getBaseDir().getPath();\r
878ddf1f 503 }\r
a29c47e0 504 msaFile = new File(moduleDir + File.separatorChar + msaFilename);\r
505 }\r
506 \r
507 public void addConfiguredModuleItem(ModuleItem moduleItem) {\r
508 PackageIdentification packageId = new PackageIdentification(moduleItem.getPackageGuid(), moduleItem.getPackageVersion());\r
509 ModuleIdentification moduleId = new ModuleIdentification(moduleItem.getModuleGuid(), moduleItem.getModuleVersion());\r
510 moduleId.setPackage(packageId);\r
511 this.moduleId = moduleId;\r
512 }\r
513 \r
514 /**\r
515 Add a property. \r
516 \r
517 @param p property\r
518 **/\r
519 public void addProperty(Property p) {\r
520 properties.addElement(p);\r
521 }\r
522\r
523 public void setType(String type) {\r
524 this.type = type;\r
525 }\r
526 \r
527 private void applyBuild(String buildTarget, String buildTagname, FpdModuleIdentification fpdModuleId) throws EdkException{\r
528 //\r
529 // AutoGen\r
530 //\r
136adffc 531 \r
73b4e31a 532 AutoGen autogen = new AutoGen(getProject().getProperty("FV_DIR"), getProject().getProperty("DEST_DIR_DEBUG"), fpdModuleId.getModule(),fpdModuleId.getArch());\r
136adffc 533 autogen.genAutogen();\r
a29c47e0 534 \r
878ddf1f 535 \r
a29c47e0 536 //\r
537 // Get compiler flags\r
538 //\r
539 getCompilerFlags(buildTarget, buildTagname, fpdModuleId);\r
540 \r
541 //\r
542 // Prepare LIBS\r
543 //\r
544 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());\r
545 String propertyLibs = "";\r
546 for (int i = 0; i < libinstances.length; i++) {\r
547 propertyLibs += " " + getProject().getProperty("BIN_DIR") + File.separatorChar + libinstances[i].getName() + ".lib";\r
878ddf1f 548 }\r
a29c47e0 549 getProject().setProperty("LIBS", propertyLibs.replaceAll("(\\\\)", "/"));\r
550 \r
551 //\r
552 // if it is CUSTOM_BUILD\r
553 // then call the exist BaseName_build.xml directly.\r
554 //\r
555 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
556 GlobalData.log.info("Call user-defined " + moduleId.getName() + "_build.xml");\r
557 Ant ant = new Ant();\r
558 ant.setProject(getProject());\r
559 ant.setAntfile(getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml");\r
560 ant.setInheritAll(true);\r
561 ant.init();\r
562 ant.execute();\r
563 return ;\r
878ddf1f 564 }\r
a29c47e0 565 \r
878ddf1f 566 //\r
a29c47e0 567 // Generate ${BASE_NAME}_build.xml\r
568 // TBD\r
878ddf1f 569 //\r
a29c47e0 570 String ffsKeyword = SurfaceAreaQuery.getModuleFfsKeyword();\r
571 ModuleBuildFileGenerator fileGenerator = new ModuleBuildFileGenerator(getProject(), ffsKeyword, fpdModuleId);\r
572 String buildFilename = getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml";\r
573 fileGenerator.genBuildFile(buildFilename);\r
574 \r
575 //\r
576 // Ant call ${BASE_NAME}_build.xml\r
577 //\r
578 Ant ant = new Ant();\r
579 ant.setProject(getProject());\r
580 ant.setAntfile(getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml");\r
581 ant.setInheritAll(true);\r
582 ant.init();\r
583 ant.execute();\r
584 }\r
585 \r
586 private void applyClean(FpdModuleIdentification fpdModuleId){\r
587 //\r
588 // if it is CUSTOM_BUILD\r
589 // then call the exist BaseName_build.xml directly.\r
590 //\r
591 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
592 GlobalData.log.info("Call user-defined " + moduleId.getName() + "_build.xml");\r
593 Ant ant = new Ant();\r
594 ant.setProject(getProject());\r
595 ant.setAntfile(getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml");\r
596 ant.setTarget("clean");\r
597 ant.setInheritAll(true);\r
598 ant.init();\r
599 ant.execute();\r
600 return ;\r
878ddf1f 601 }\r
a29c47e0 602 \r
603 Ant ant = new Ant();\r
604 ant.setProject(getProject());\r
605 ant.setAntfile(getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml");\r
606 ant.setTarget("clean");\r
607 ant.setInheritAll(true);\r
608 ant.init();\r
609 ant.execute();\r
610 \r
611 //\r
612 // Delete current module's DEST_DIR_OUTPUT\r
613 // TBD\r
614 }\r
615 \r
616 private void applyCleanall(FpdModuleIdentification fpdModuleId){\r
617 //\r
618 // if it is CUSTOM_BUILD\r
619 // then call the exist BaseName_build.xml directly.\r
620 //\r
621 if (moduleId.getModuleType().equalsIgnoreCase("USER_DEFINED")) {\r
622 GlobalData.log.info("Call user-defined " + moduleId.getName() + "_build.xml");\r
623 Ant ant = new Ant();\r
624 ant.setProject(getProject());\r
625 ant.setAntfile(getProject().getProperty("MODULE_DIR") + File.separatorChar + moduleId.getName() + "_build.xml");\r
626 ant.setTarget("cleanall");\r
627 ant.setInheritAll(true);\r
628 ant.init();\r
629 ant.execute();\r
630 return ;\r
878ddf1f 631 }\r
a29c47e0 632 \r
633 Ant ant = new Ant();\r
634 ant.setProject(getProject());\r
635 ant.setAntfile(getProject().getProperty("DEST_DIR_OUTPUT") + File.separatorChar + moduleId.getName() + "_build.xml");\r
636 ant.setTarget("cleanall");\r
637 ant.setInheritAll(true);\r
638 ant.init();\r
639 ant.execute();\r
640 \r
641 //\r
642 // Delete current module's DEST_DIR_OUTPUT\r
643 // TBD\r
878ddf1f 644 }\r
645\r
a29c47e0 646\r
647\r
648\r
878ddf1f 649 /**\r
650 Separate the string and instore in set.\r
651 \r
652 <p> String is separated by Java Regulation Expression \r
653 "[^\\\\]?(\".*?[^\\\\]\")[ \t,]+". </p>\r
654 \r
655 <p>For example: </p>\r
656 \r
657 <pre>\r
658 "/nologo", "/W3", "/WX"\r
659 "/C", "/DSTRING_DEFINES_FILE=\"BdsStrDefs.h\""\r
660 </pre>\r
661 \r
662 @param set store the separated string\r
663 @param str string to separate\r
664 **/\r
665 private void putFlagsToSet(Set<String> set, String str) {\r
a29c47e0 666 if (str == null || str.length() == 0) {\r
667 return;\r
668 }\r
669\r
878ddf1f 670 Pattern myPattern = Pattern.compile("[^\\\\]?(\".*?[^\\\\]\")[ \t,]+");\r
671 Matcher matcher = myPattern.matcher(str + " ");\r
672 while (matcher.find()) {\r
673 String item = str.substring(matcher.start(1), matcher.end(1));\r
a29c47e0 674 set.add(item);\r
878ddf1f 675 }\r
676 }\r
677 \r
678 /**\r
679 Generate the final flags string will be used by compile command. \r
680 \r
681 @param add the add flags set\r
682 @param sub the sub flags set\r
683 @return final flags after add set substract sub set\r
684 **/\r
685 private String getFlags(Set<String> add, Set<String> sub) {\r
686 String result = "";\r
687 add.removeAll(sub);\r
688 Iterator iter = add.iterator();\r
689 while (iter.hasNext()) {\r
a29c47e0 690 String str = (String) iter.next();\r
878ddf1f 691 result += str.substring(1, str.length() - 1) + " ";\r
692 }\r
693 return result;\r
694 }\r
695\r
696 /**\r
697 Generate the flags string with original format. The format is defined by \r
698 Java Regulation Expression "[^\\\\]?(\".*?[^\\\\]\")[ \t,]+". </p>\r
699 \r
700 <p>For example: </p>\r
701 \r
702 <pre>\r
703 "/nologo", "/W3", "/WX"\r
704 "/C", "/DSTRING_DEFINES_FILE=\"BdsStrDefs.h\""\r
705 </pre>\r
706 \r
707 @param add the add flags set\r
708 @param sub the sub flags set\r
709 @return flags with original format\r
710 **/\r
711 private String getRawFlags(Set<String> add, Set<String> sub) {\r
a29c47e0 712 String result = null;\r
878ddf1f 713 add.removeAll(sub);\r
714 Iterator iter = add.iterator();\r
715 while (iter.hasNext()) {\r
a29c47e0 716 String str = (String) iter.next();\r
878ddf1f 717 result += "\"" + str.substring(1, str.length() - 1) + "\", ";\r
718 }\r
719 return result;\r
720 }\r
721\r
a29c47e0 722 private String parseOptionString(String optionString, Set<String> addSet, Set<String> subSet) {\r
723 boolean overrideOption = false;\r
724 Pattern pattern = Pattern.compile("ADD\\.\\[(.+)\\]");\r
725 Matcher matcher = pattern.matcher(optionString);\r
726\r
727 while (matcher.find()) {\r
728 overrideOption = true;\r
729 String addOption = optionString.substring(matcher.start(1), matcher.end(1)).trim();\r
730 putFlagsToSet(addSet, addOption);\r
731 \r
732 }\r
878ddf1f 733\r
a29c47e0 734 pattern = Pattern.compile("SUB\\.\\[(.+)\\]");\r
735 matcher = pattern.matcher(optionString);\r
736\r
737 while (matcher.find()) {\r
738 overrideOption = true;\r
739 String subOption = optionString.substring(matcher.start(1), matcher.end(1)).trim();\r
740 putFlagsToSet(subSet, subOption);\r
741 }\r
742\r
743 if (overrideOption == true) {\r
744 return null;\r
745 }\r
746\r
747 return optionString;\r
748 }\r
749 \r
750 private void pushProperties() {\r
751 backupPropertiesStack.push(getProject().getProperties());\r
752 }\r
753 \r
754 private void popProperties() {\r
755 Hashtable backupProperties = backupPropertiesStack.pop();\r
756 Set keys = backupProperties.keySet();\r
757 Iterator iter = keys.iterator();\r
758 while (iter.hasNext()) {\r
759 String item = (String)iter.next();\r
760 getProject().setProperty(item, (String)backupProperties.get(item));\r
761 }\r
762 }\r
de4bb9f6 763\r
764 public void setSingleModuleBuild(boolean isSingleModuleBuild) {\r
765 this.isSingleModuleBuild = isSingleModuleBuild;\r
766 }\r
878ddf1f 767}\r