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