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