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