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