2 ToolChainFactory class.
4 ToolChainFactory class parse all config files and get STD_FLAGS, GLOBAL_FLAGS,
5 and also command path + name.
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
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.
17 package org
.tianocore
.build
.toolchain
;
19 import java
.util
.HashMap
;
20 import java
.util
.Iterator
;
23 import java
.util
.StringTokenizer
;
26 import org
.apache
.tools
.ant
.Project
;
30 This class parse all config files and get STD_FLAGS, GLOBAL_FLAGS, and also
35 public class ToolChainFactory
{
37 /// list of Arch: EBC, ARM, IA32, X64, IPF, PPC
39 public final static String
[] arch
= { "EBC", "ARM", "IA32", "X64", "IPF",
43 /// list of OS: Linux, Windows
45 public final static String
[] os
= { "WINDOWS", "LINUX" };
48 /// list of Command Type: CC, LIB, LINK, ASL, ASM, ASMLINK, PP
50 public final static String
[] commandType
= { "CC", "LIB", "LINK", "ASL",
51 "ASM", "ASMLINK", "PP" };
54 /// default command name for every command
56 public final static String
[][] defaultCmdName
= { { "CC", "cl" },
57 { "LIB", "lib" }, { "LINK", "link" }, { "ASL", "iasl" },
58 { "ASM", "ml" }, { "ASMLINK", "link" }, { "PP", "cl" } };
60 private String confPath
= ".";
62 private String toolChainName
= "MSFT";
64 private String sTargetFilename
= "target.txt";
66 private String sToolsdefFilename
= "tools_def.txt";
68 private String sWorkspaceTarget
= "WORKSPACE_TARGET";
70 private String sTargetArch
= "TARGET_ARCH";
72 private HashMap
<String
,String
[][]> filesMap
= new HashMap
<String
,String
[][]>();
74 private HashMap
<String
,String
> globalFlagsMap
= new HashMap
<String
,String
>();
76 private String
[][] globalFlagTable
;
78 private String currentTarget
= "RELEASE";
81 /// toolchain array list all results by parsing config files
83 public static String
[][] toolchain
= null;
86 Public construct method.
88 public ToolChainFactory () {
92 Public construct method.
94 @param project current ANT Project.
96 public ToolChainFactory (Project project
) {
97 this.confPath
= project
.replaceProperties("${WORKSPACE_DIR}" + File
.separatorChar
+ "Tools" + File
.separatorChar
+ "Conf");
101 Public construct method.
103 @param confPath the path of config files
104 @param toolChainName TOOL_CHAIN name
106 public ToolChainFactory (String confPath
, String toolChainName
) {
107 this.confPath
= confPath
;
109 // If set tool used the set one, otherwise use default one.
110 // toolChain used to define open tools define txt file.
112 if (toolChainName
!= null && toolChainName
.length() > 0){
113 this.toolChainName
= toolChainName
;
118 Parse all config files, following are the detail steps:
121 <li>Parse target.txt file. This file define the current build TARGET
122 and supported ARCH list. </li>
123 <li>Parse tools_def.txt file. This file define every command name, path
125 <li>For every supported ARCH and Command Type, find out STD_FLAGS,
126 GLOBAL_ADD_FLAGS, GLOBAL_SUB_FLAGS. </li>
129 <p>Note that this method will be called only once during the whole build
132 public void setupToolChain() {
133 if (toolchain
!= null) {
136 Map
<String
, String
> map
= new HashMap
<String
, String
>(40);
140 String
[][] target
= ConfigReader
.parse(confPath
, sTargetFilename
);
142 // get workspace_target and initialize global flags setting
144 currentTarget
= getValue(sWorkspaceTarget
, target
);
145 parseGlobalSetting(currentTarget
);
146 String
[] archList
= getArchs(getValue(sTargetArch
, target
));
149 // If user write the ${toolChain}_Tools_Def.txt use this one,
150 // otherwise used "tools_def.txt" file.
152 File tempFile
= new File (confPath
+ File
.separator
+ toolChainName
.toLowerCase() + "_tools_def.txt");
153 if (tempFile
.exists()){
154 sToolsdefFilename
= toolChainName
.toLowerCase() + "_tools_def.txt";
157 System
.out
.println("Tools definition file is: " + sToolsdefFilename
);
159 // parse tools_def.txt
161 String
[][] tools_def
= ConfigReader
.parse(confPath
, sToolsdefFilename
);
163 // for each arch find all command's path&name and flags
165 for (int i
= 0; i
< archList
.length
; i
++) {
166 for (int j
= 0; j
< commandType
.length
; j
++) {
170 map
.put(archList
[i
] + "_" + commandType
[j
], getAbsoluteCmdPath(
171 archList
[i
], commandType
[j
], tools_def
));
173 // Flags: CMD_STD_FLAGS + CMD_GLOBAL_FLAGS + CMD_PROJ_FLAGS
174 // ARCH_CMD_STD_FLAGS
176 map
.put(archList
[i
] + "_" + commandType
[j
] + "_STD_FLAGS",
177 getStdFlags(archList
[i
], commandType
[j
],
180 // Flags:ARCH_CMD_VENDOR or ARCH_VENDOR
182 map
.put(archList
[i
]+ "_"+commandType
[j
]+"_VENDOR", getVendorFlag(archList
[i
],
183 commandType
[j
], tools_def
));
185 // ARCH_CMD_GLOBAL_FLAGS
187 String
[] globalFlags
= getGlobalFlags(archList
[i
], commandType
[j
],
189 map
.put(archList
[i
] + "_" + commandType
[j
] + "_GLOBAL_ADD_FLAGS",
191 map
.put(archList
[i
] + "_" + commandType
[j
] + "_GLOBAL_SUB_FLAGS",
194 // ARCH_CMD_GLOBAL_FLAGS, default is "".
196 map
.put(archList
[i
] + "_" + commandType
[j
] + "_PROJ_FLAGS", "");
198 map
.put(archList
[i
]+"_VENDOR", getVendorFlag(archList
[i
], null, tools_def
));
200 Set keyset
= map
.keySet();
201 Iterator iter
= keyset
.iterator();
202 String
[][] result
= new String
[map
.size()][2];
204 while (iter
.hasNext()) {
205 String key
= (String
) iter
.next();
207 result
[i
++][1] = (String
) map
.get(key
);
213 Get the standard flags (STD_FLAGS) for specified arch and command type.
216 <li>Find out Vendor that cmd Command Type with arch ARCH used. The
217 search sequence is ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT". Here
218 we suppose default Vendor is MSFT.</li>
219 <li>Search ${Vendor}_tools.txt file, and get the corrsponding flags.
224 @param cmd the command type
225 @param map detail flags information of tools_def.txt
226 @return the standard flags of arch ARCH and cmd Command Type
228 private String
getStdFlags(String arch
, String cmd
, String
[][] map
) {
230 // first is to find out its Vendor in map
231 // ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT"
232 // Here we suppose default Vendor is MSFT.
234 String vendor
= "MSFT";
236 if ((str
= getValue(arch
+ "_" + cmd
+ "_VENDOR", map
)) != null) {
238 } else if ((str
= getValue(arch
+ "_VENDOR", map
)) != null) {
242 // change to low letter
244 vendor
= vendor
.toLowerCase();
246 // parse the corresponding file and get arch_cmd value
248 String filename
= vendor
+ "_tools.txt";
250 if (filesMap
.containsKey(filename
)) {
251 flagsMap
= (String
[][]) filesMap
.get(filename
);
254 // read file and store in filesMap
256 flagsMap
= ConfigReader
.parse(confPath
, vendor
+ "_tools.txt");
257 filesMap
.put(filename
, flagsMap
);
259 if ((str
= getValue(arch
+ "_" + cmd
, flagsMap
)) != null) {
266 Get the global flags (GLOBAL_ADD_FLAGS & GLOBAL_SUB_FLAGS) for specified
267 arch and command type.
270 <li>Find out Vendor that cmd Command Type with arch ARCH used. The
271 search sequence is ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT". Here
272 we suppose default Vendor is MSFT.</li>
273 <li>Search efi_flags_table.txt file, and get the corrsponding flags.
278 @param cmd the command type
279 @param map detail flags information of tools_def.txt
280 @return two values, first is GLOBAL_ADD_FLAGS and another value is
283 private String
[] getGlobalFlags(String arch
, String cmd
, String
[][] map
) {
287 // first is to find out its Vendor in map
288 // ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT"
289 // Here we suppose default Vendor is MSFT.
291 String vendor
= "MSFT";
293 if ((str
= getValue(arch
+ "_" + cmd
+ "_VENDOR", map
)) != null) {
295 } else if ((str
= getValue(arch
+ "_VENDOR", map
)) != null) {
299 // parse global flags table
301 if (globalFlagTable
== null) {
302 globalFlagTable
= ConfigReader
.parseTable(confPath
, "efi_flags_table.txt");
304 for (int i
=0; i
< globalFlagTable
.length
; i
++){
305 String
[] item
= globalFlagTable
[i
];
306 if (item
[2].equalsIgnoreCase(vendor
+ "_" + arch
+ "_" + cmd
)){
308 // if item[0] == item[1] is existed in globalFlagsMap
310 if (globalFlagsMap
.containsKey(item
[0])){
311 if( item
[1].equalsIgnoreCase((String
)globalFlagsMap
.get(item
[0]))){
312 addStr
+= item
[3] + " ";
313 subStr
+= item
[4] + " ";
319 return new String
[]{addStr
, subStr
};
323 Find out command path and command name.
326 Command path searching sequence in tools_def.txt file:
327 Path: ARCH_CMD_PATH -> ARCH_PATH -> Set to "".
329 Command name searching sequence in tools_def.txt file:
330 Name: ARCH_CMD_NAME -> CMD_NAME -> Default Value.
334 @param cmd the Command Type
335 @param map detail flags information of tools_def.txt
336 @return the absolute command path and name
338 private String
getAbsoluteCmdPath(String arch
, String cmd
, String
[][] map
) {
345 if ((str
= getValue(arch
+ "_" + cmd
+ "_PATH", map
)) != null) {
347 } else if ((str
= getValue(arch
+ "_PATH", map
)) != null) {
353 if ((str
= getValue(arch
+ "_" + cmd
+ "_NAME", map
)) != null) {
355 } else if ((str
= getValue(cmd
+ "_NAME", map
)) != null) {
358 name
= getValue(cmd
, defaultCmdName
);
360 if (path
.equalsIgnoreCase("")) {
363 return path
+ File
.separatorChar
+ name
;
367 Find out all global flags value, such as EFI_DEBUG equal YES or NO. Here
368 are three type files: global_efi_flags.txt, ${TARGET}_efi_flags.txt,
369 my_efi_flags.txt. global_efi_flags.txt with the highest priority while
370 my_efi_flags.txt with the lowest priority.
372 <p>All global flags value will store in <code>globalFlagsMap</code> for
373 getGlobalFlags using. </p>
375 @param target current build TARGET value
377 private void parseGlobalSetting(String target
){
379 // parse global_efi_flags -> ${TARGET}_efi_flags -> my_efi_flags
380 // parse global_efi_flags
382 String
[][] map
= ConfigReader
.parse(confPath
, "global_efi_flags.txt");
383 for (int i
= 0; i
< map
.length
; i
++){
384 if(globalFlagsMap
.containsKey(map
[i
][0])){
385 globalFlagsMap
.remove(map
[i
][0]);
387 globalFlagsMap
.put(map
[i
][0], map
[i
][1]);
390 // parse ${TARGET}_efi_flags
392 map
= ConfigReader
.parse(confPath
, target
.toLowerCase() + "_efi_flags.txt");
393 for (int i
= 0; i
< map
.length
; i
++){
394 if(globalFlagsMap
.containsKey(map
[i
][0])){
395 globalFlagsMap
.remove(map
[i
][0]);
397 globalFlagsMap
.put(map
[i
][0], map
[i
][1]);
400 // parse my_efi_flags.txt
402 map
= ConfigReader
.parse(confPath
, "my_efi_flags.txt");
403 for (int i
= 0; i
< map
.length
; i
++){
404 if(globalFlagsMap
.containsKey(map
[i
][0])){
405 globalFlagsMap
.remove(map
[i
][0]);
407 globalFlagsMap
.put(map
[i
][0], map
[i
][1]);
412 Find value with key from map. If not found, return null.
414 <p>Note that default is case-insensitive</p>
417 @param map mapping information
418 @return the related value of key
420 private String
getValue(String key
, String
[][] map
) {
421 return getValue(key
, map
, false);
425 Find value with key from map. If not found, return null.
428 @param map mapping information
429 @param caseSensitive whether case sesitive or not
430 @return the related value of key
432 private String
getValue(String key
, String
[][] map
, boolean caseSensitive
) {
433 for (int i
= 0; i
< map
.length
; i
++) {
435 if (key
.compareTo(map
[i
][0]) == 0) {
439 if (key
.compareToIgnoreCase(map
[i
][0]) == 0) {
448 Find value with key from <code>toolchain</code>. If not found, return null.
451 @return the related value of key
453 public static String
getValue(String key
){
454 for (int i
= 0; i
< toolchain
.length
; i
++) {
455 if (key
.compareToIgnoreCase(toolchain
[i
][0]) == 0) {
456 return toolchain
[i
][1];
463 Get Arch list from a string separated with comma.
467 If the arch string is "IA32, X64, EBC".
468 Then the result is {"IA32", "X64", "EBC"}.
471 @param arch string separated with comma
474 public String
[] getArchs(String arch
) {
476 return new String
[0];
478 StringTokenizer st
= new StringTokenizer(arch
, " \t,");
479 String
[] archs
= new String
[st
.countTokens()];
481 while (st
.hasMoreTokens()) {
482 archs
[i
++] = st
.nextToken().toUpperCase();
488 Get current target value.
490 @return current target value
492 public String
getCurrentTarget() {
493 return currentTarget
;
497 Find out Vendor that cmd Command Type with arch ARCH used. The
498 search sequence is ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT". Here
499 we suppose default Vendor is MSFT.
502 @param cmd the Command Type
503 @param map detail flags information of tools_def.txt
504 @return the related vendor name
506 public String
getVendorFlag (String arch
, String cmdType
, String
[][] map
){
508 // ARCH_CMD_VENDOR -> ARCH_VENDOR -> "MSFT"
509 // Here we suppose default Vendor is MSFT.
513 if (cmdType
!= null){
514 if ((str
= getValue(arch
+ "_" + cmdType
+ "_VENDOR", map
)) != null) {
519 }else if (arch
!= null){
520 if ((str
= getValue(arch
+ "_VENDOR", map
)) != null) {