]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/TargetTxtClassObject.py
BaseTools: Clear build versions to sync with buildtools/BaseTools
[mirror_edk2.git] / BaseTools / Source / Python / Common / TargetTxtClassObject.py
1 ## @file
2 # This file is used to define each component of Target.txt file
3 #
4 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ##
15 # Import Modules
16 #
17 import os
18 import EdkLogger
19 import DataType
20 from BuildToolError import *
21 import GlobalData
22
23 gDefaultTargetTxtFile = "Conf/target.txt"
24
25 ## TargetTxtClassObject
26 #
27 # This class defined content used in file target.txt
28 #
29 # @param object: Inherited from object class
30 # @param Filename: Input value for full path of target.txt
31 #
32 # @var TargetTxtDictionary: To store keys and values defined in target.txt
33 #
34 class TargetTxtClassObject(object):
35 def __init__(self, Filename = None):
36 self.TargetTxtDictionary = {
37 DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',
38 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',
39 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',
40 DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',
41 DataType.TAB_TAT_DEFINES_TARGET : [],
42 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],
43 DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],
44 DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',
45 }
46 if Filename != None:
47 self.LoadTargetTxtFile(Filename)
48
49 ## LoadTargetTxtFile
50 #
51 # Load target.txt file and parse it, return a set structure to store keys and values
52 #
53 # @param Filename: Input value for full path of target.txt
54 #
55 # @retval set() A set structure to store keys and values
56 # @retval 1 Error happenes in parsing
57 #
58 def LoadTargetTxtFile(self, Filename):
59 if os.path.exists(Filename) and os.path.isfile(Filename):
60 return self.ConvertTextFileToDict(Filename, '#', '=')
61 else:
62 EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)
63 return 1
64
65 ## ConvertTextFileToDict
66 #
67 # Convert a text file to a dictionary of (name:value) pairs.
68 # The data is saved to self.TargetTxtDictionary
69 #
70 # @param FileName: Text filename
71 # @param CommentCharacter: Comment char, be used to ignore comment content
72 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
73 #
74 # @retval 0 Convert successfully
75 # @retval 1 Open file failed
76 #
77 def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):
78 F = None
79 try:
80 F = open(FileName,'r')
81 except:
82 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)
83 if F != None:
84 F.close()
85
86 for Line in F:
87 Line = Line.strip()
88 if Line.startswith(CommentCharacter) or Line == '':
89 continue
90
91 LineList = Line.split(KeySplitCharacter, 1)
92 Key = LineList[0].strip()
93 if len(LineList) == 2:
94 Value = LineList[1].strip()
95 else:
96 Value = ""
97
98 if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \
99 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:
100 self.TargetTxtDictionary[Key] = Value.replace('\\', '/')
101 elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \
102 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:
103 self.TargetTxtDictionary[Key] = Value.split()
104 elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:
105 try:
106 V = int(Value, 0)
107 except:
108 EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),
109 File=FileName)
110 self.TargetTxtDictionary[Key] = Value
111 #elif Key not in GlobalData.gGlobalDefines:
112 # GlobalData.gGlobalDefines[Key] = Value
113
114 F.close()
115 return 0
116
117 ## Print the dictionary
118 #
119 # Print all items of dictionary one by one
120 #
121 # @param Dict: The dictionary to be printed
122 #
123 def printDict(Dict):
124 if Dict != None:
125 KeyList = Dict.keys()
126 for Key in KeyList:
127 if Dict[Key] != '':
128 print Key + ' = ' + str(Dict[Key])
129
130 ## Print the dictionary
131 #
132 # Print the items of dictionary which matched with input key
133 #
134 # @param list: The dictionary to be printed
135 # @param key: The key of the item to be printed
136 #
137 def printList(Key, List):
138 if type(List) == type([]):
139 if len(List) > 0:
140 if Key.find(TAB_SPLIT) != -1:
141 print "\n" + Key
142 for Item in List:
143 print Item
144 ## TargetTxtDict
145 #
146 # Load target.txt in input workspace dir
147 #
148 # @param WorkSpace: Workspace dir
149 #
150 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
151 #
152 def TargetTxtDict(WorkSpace):
153 Target = TargetTxtClassObject()
154 Target.LoadTargetTxtFile(os.path.normpath(os.path.join(WorkSpace, gDefaultTargetTxtFile)))
155 return Target
156
157 ##
158 #
159 # This acts like the main() function for the script, unless it is 'import'ed into another
160 # script.
161 #
162 if __name__ == '__main__':
163 pass
164 Target = TargetTxtDict(os.getenv("WORKSPACE"))
165 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
166 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
167 print Target.TargetTxtDictionary