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