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