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