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