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