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