]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/TargetTxtClassObject.py
Sync EDKII BaseTools to BaseTools project r1971
[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, 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_MULTIPLE_THREAD : '',
41 DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',
42 DataType.TAB_TAT_DEFINES_TARGET : [],
43 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],
44 DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],
45 DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',
46 }
47 if Filename != None:
48 self.LoadTargetTxtFile(Filename)
49
50 ## LoadTargetTxtFile
51 #
52 # Load target.txt file and parse it, return a set structure to store keys and values
53 #
54 # @param Filename: Input value for full path of target.txt
55 #
56 # @retval set() A set structure to store keys and values
57 # @retval 1 Error happenes in parsing
58 #
59 def LoadTargetTxtFile(self, Filename):
60 if os.path.exists(Filename) and os.path.isfile(Filename):
61 return self.ConvertTextFileToDict(Filename, '#', '=')
62 else:
63 EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)
64 return 1
65
66 ## ConvertTextFileToDict
67 #
68 # Convert a text file to a dictionary of (name:value) pairs.
69 # The data is saved to self.TargetTxtDictionary
70 #
71 # @param FileName: Text filename
72 # @param CommentCharacter: Comment char, be used to ignore comment content
73 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
74 #
75 # @retval 0 Convert successfully
76 # @retval 1 Open file failed
77 #
78 def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):
79 F = None
80 try:
81 F = open(FileName,'r')
82 except:
83 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)
84 if F != None:
85 F.close()
86
87 for Line in F:
88 Line = Line.strip()
89 if Line.startswith(CommentCharacter) or Line == '':
90 continue
91
92 LineList = Line.split(KeySplitCharacter, 1)
93 Key = LineList[0].strip()
94 if len(LineList) == 2:
95 Value = LineList[1].strip()
96 else:
97 Value = ""
98
99 if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \
100 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:
101 self.TargetTxtDictionary[Key] = Value.replace('\\', '/')
102 elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \
103 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:
104 self.TargetTxtDictionary[Key] = Value.split()
105 elif Key == DataType.TAB_TAT_DEFINES_MULTIPLE_THREAD:
106 if Value not in ["Enable", "Disable"]:
107 EdkLogger.error("build", FORMAT_INVALID, "Invalid setting of [%s]: %s." % (Key, Value),
108 ExtraData="\tSetting must be one of [Enable, Disable]",
109 File=FileName)
110 self.TargetTxtDictionary[Key] = Value
111 elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:
112 try:
113 V = int(Value, 0)
114 except:
115 EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),
116 File=FileName)
117 self.TargetTxtDictionary[Key] = Value
118 #elif Key not in GlobalData.gGlobalDefines:
119 # GlobalData.gGlobalDefines[Key] = Value
120
121 F.close()
122 return 0
123
124 ## Print the dictionary
125 #
126 # Print all items of dictionary one by one
127 #
128 # @param Dict: The dictionary to be printed
129 #
130 def printDict(Dict):
131 if Dict != None:
132 KeyList = Dict.keys()
133 for Key in KeyList:
134 if Dict[Key] != '':
135 print Key + ' = ' + str(Dict[Key])
136
137 ## Print the dictionary
138 #
139 # Print the items of dictionary which matched with input key
140 #
141 # @param list: The dictionary to be printed
142 # @param key: The key of the item to be printed
143 #
144 def printList(Key, List):
145 if type(List) == type([]):
146 if len(List) > 0:
147 if Key.find(TAB_SPLIT) != -1:
148 print "\n" + Key
149 for Item in List:
150 print Item
151 ## TargetTxtDict
152 #
153 # Load target.txt in input workspace dir
154 #
155 # @param WorkSpace: Workspace dir
156 #
157 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
158 #
159 def TargetTxtDict(WorkSpace):
160 Target = TargetTxtClassObject()
161 Target.LoadTargetTxtFile(os.path.normpath(os.path.join(WorkSpace, gDefaultTargetTxtFile)))
162 return Target
163
164 ##
165 #
166 # This acts like the main() function for the script, unless it is 'import'ed into another
167 # script.
168 #
169 if __name__ == '__main__':
170 pass
171 Target = TargetTxtDict(os.getenv("WORKSPACE"))
172 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
173 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
174 print Target.TargetTxtDictionary