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