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