]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/TargetTxtClassObject.py
BaseTools: Replace StandardError with Expression
[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 from __future__ import print_function
18 import Common.LongFilePathOs as os
19 import EdkLogger
20 import DataType
21 from BuildToolError import *
22 import GlobalData
23 from Common.LongFilePathSupport import OpenLongFilePath as open
24
25 gDefaultTargetTxtFile = "target.txt"
26
27 ## TargetTxtClassObject
28 #
29 # This class defined content used in file target.txt
30 #
31 # @param object: Inherited from object class
32 # @param Filename: Input value for full path of target.txt
33 #
34 # @var TargetTxtDictionary: To store keys and values defined in target.txt
35 #
36 class TargetTxtClassObject(object):
37 def __init__(self, Filename = None):
38 self.TargetTxtDictionary = {
39 DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',
40 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',
41 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',
42 DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',
43 DataType.TAB_TAT_DEFINES_TARGET : [],
44 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],
45 DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],
46 DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',
47 }
48 self.ConfDirectoryPath = ""
49 if Filename is not None:
50 self.LoadTargetTxtFile(Filename)
51
52 ## LoadTargetTxtFile
53 #
54 # Load target.txt file and parse it, return a set structure to store keys and values
55 #
56 # @param Filename: Input value for full path of target.txt
57 #
58 # @retval set() A set structure to store keys and values
59 # @retval 1 Error happenes in parsing
60 #
61 def LoadTargetTxtFile(self, Filename):
62 if os.path.exists(Filename) and os.path.isfile(Filename):
63 return self.ConvertTextFileToDict(Filename, '#', '=')
64 else:
65 EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)
66 return 1
67
68 ## ConvertTextFileToDict
69 #
70 # Convert a text file to a dictionary of (name:value) pairs.
71 # The data is saved to self.TargetTxtDictionary
72 #
73 # @param FileName: Text filename
74 # @param CommentCharacter: Comment char, be used to ignore comment content
75 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
76 #
77 # @retval 0 Convert successfully
78 # @retval 1 Open file failed
79 #
80 def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):
81 F = None
82 try:
83 F = open(FileName, 'r')
84 self.ConfDirectoryPath = os.path.dirname(FileName)
85 except:
86 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)
87 if F is not None:
88 F.close()
89
90 for Line in F:
91 Line = Line.strip()
92 if Line.startswith(CommentCharacter) or Line == '':
93 continue
94
95 LineList = Line.split(KeySplitCharacter, 1)
96 Key = LineList[0].strip()
97 if len(LineList) == 2:
98 Value = LineList[1].strip()
99 else:
100 Value = ""
101
102 if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \
103 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:
104 self.TargetTxtDictionary[Key] = Value.replace('\\', '/')
105 if Key == DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.TargetTxtDictionary[Key]:
106 if self.TargetTxtDictionary[Key].startswith("Conf/"):
107 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
108 if not os.path.exists(Tools_Def) or not os.path.isfile(Tools_Def):
109 # If Conf/Conf does not exist, try just the Conf/ directory
110 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())
111 else:
112 # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory
113 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
114 self.TargetTxtDictionary[Key] = Tools_Def
115 if Key == DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF and self.TargetTxtDictionary[Key]:
116 if self.TargetTxtDictionary[Key].startswith("Conf/"):
117 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
118 if not os.path.exists(Build_Rule) or not os.path.isfile(Build_Rule):
119 # If Conf/Conf does not exist, try just the Conf/ directory
120 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())
121 else:
122 # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory
123 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
124 self.TargetTxtDictionary[Key] = Build_Rule
125 elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \
126 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:
127 self.TargetTxtDictionary[Key] = Value.split()
128 elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:
129 try:
130 V = int(Value, 0)
131 except:
132 EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),
133 File=FileName)
134 self.TargetTxtDictionary[Key] = Value
135 #elif Key not in GlobalData.gGlobalDefines:
136 # GlobalData.gGlobalDefines[Key] = Value
137
138 F.close()
139 return 0
140
141 ## TargetTxtDict
142 #
143 # Load target.txt in input Conf dir
144 #
145 # @param ConfDir: Conf dir
146 #
147 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
148 #
149 def TargetTxtDict(ConfDir):
150 Target = TargetTxtClassObject()
151 Target.LoadTargetTxtFile(os.path.normpath(os.path.join(ConfDir, gDefaultTargetTxtFile)))
152 return Target
153
154 ##
155 #
156 # This acts like the main() function for the script, unless it is 'import'ed into another
157 # script.
158 #
159 if __name__ == '__main__':
160 pass
161 Target = TargetTxtDict(os.getenv("WORKSPACE"))
162 print(Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER])
163 print(Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET])
164 print(Target.TargetTxtDictionary)