]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/TargetTxtClassObject.py
89e393708625a7eb4a019db1a0ecdc8f6a8edc08
[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 - 2014, 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 Common.LongFilePathOs as os
18 import EdkLogger
19 import DataType
20 from BuildToolError import *
21 import GlobalData
22 from Common.LongFilePathSupport import OpenLongFilePath as open
23
24 gDefaultTargetTxtFile = "Conf/target.txt"
25
26 ## TargetTxtClassObject
27 #
28 # This class defined content used in file target.txt
29 #
30 # @param object: Inherited from object class
31 # @param Filename: Input value for full path of target.txt
32 #
33 # @var TargetTxtDictionary: To store keys and values defined in target.txt
34 #
35 class TargetTxtClassObject(object):
36 def __init__(self, Filename = None):
37 self.TargetTxtDictionary = {
38 DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',
39 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',
40 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',
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_MAX_CONCURRENT_THREAD_NUMBER:
106 try:
107 V = int(Value, 0)
108 except:
109 EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),
110 File=FileName)
111 self.TargetTxtDictionary[Key] = Value
112 #elif Key not in GlobalData.gGlobalDefines:
113 # GlobalData.gGlobalDefines[Key] = Value
114
115 F.close()
116 return 0
117
118 ## Print the dictionary
119 #
120 # Print all items of dictionary one by one
121 #
122 # @param Dict: The dictionary to be printed
123 #
124 def printDict(Dict):
125 if Dict != None:
126 KeyList = Dict.keys()
127 for Key in KeyList:
128 if Dict[Key] != '':
129 print Key + ' = ' + str(Dict[Key])
130
131 ## Print the dictionary
132 #
133 # Print the items of dictionary which matched with input key
134 #
135 # @param list: The dictionary to be printed
136 # @param key: The key of the item to be printed
137 #
138 def printList(Key, List):
139 if type(List) == type([]):
140 if len(List) > 0:
141 if Key.find(TAB_SPLIT) != -1:
142 print "\n" + Key
143 for Item in List:
144 print Item
145 ## TargetTxtDict
146 #
147 # Load target.txt in input workspace dir
148 #
149 # @param WorkSpace: Workspace dir
150 #
151 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
152 #
153 def TargetTxtDict(WorkSpace):
154 Target = TargetTxtClassObject()
155 Target.LoadTargetTxtFile(os.path.normpath(os.path.join(WorkSpace, gDefaultTargetTxtFile)))
156 return Target
157
158 ##
159 #
160 # This acts like the main() function for the script, unless it is 'import'ed into another
161 # script.
162 #
163 if __name__ == '__main__':
164 pass
165 Target = TargetTxtDict(os.getenv("WORKSPACE"))
166 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
167 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
168 print Target.TargetTxtDictionary