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