]>
Commit | Line | Data |
---|---|---|
1 | ## @file\r | |
2 | # This file is used to define each component of Target.txt file\r | |
3 | #\r | |
4 | # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
5 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | #\r | |
7 | \r | |
8 | ##\r | |
9 | # Import Modules\r | |
10 | #\r | |
11 | from __future__ import print_function\r | |
12 | from __future__ import absolute_import\r | |
13 | \r | |
14 | import Common.GlobalData as GlobalData\r | |
15 | import Common.LongFilePathOs as os\r | |
16 | from . import EdkLogger\r | |
17 | from . import DataType\r | |
18 | from .BuildToolError import *\r | |
19 | \r | |
20 | from Common.LongFilePathSupport import OpenLongFilePath as open\r | |
21 | from Common.MultipleWorkspace import MultipleWorkspace as mws\r | |
22 | \r | |
23 | gDefaultTargetTxtFile = "target.txt"\r | |
24 | \r | |
25 | ## TargetTxtClassObject\r | |
26 | #\r | |
27 | # This class defined content used in file target.txt\r | |
28 | #\r | |
29 | # @param object: Inherited from object class\r | |
30 | # @param Filename: Input value for full path of target.txt\r | |
31 | #\r | |
32 | # @var TargetTxtDictionary: To store keys and values defined in target.txt\r | |
33 | #\r | |
34 | class TargetTxtClassObject(object):\r | |
35 | def __init__(self, Filename = None):\r | |
36 | self.TargetTxtDictionary = {\r | |
37 | DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',\r | |
38 | DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',\r | |
39 | DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',\r | |
40 | DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',\r | |
41 | DataType.TAB_TAT_DEFINES_TARGET : [],\r | |
42 | DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],\r | |
43 | DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],\r | |
44 | DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',\r | |
45 | }\r | |
46 | self.ConfDirectoryPath = ""\r | |
47 | if Filename is not None:\r | |
48 | self.LoadTargetTxtFile(Filename)\r | |
49 | \r | |
50 | ## LoadTargetTxtFile\r | |
51 | #\r | |
52 | # Load target.txt file and parse it, return a set structure to store keys and values\r | |
53 | #\r | |
54 | # @param Filename: Input value for full path of target.txt\r | |
55 | #\r | |
56 | # @retval set() A set structure to store keys and values\r | |
57 | # @retval 1 Error happenes in parsing\r | |
58 | #\r | |
59 | def LoadTargetTxtFile(self, Filename):\r | |
60 | if os.path.exists(Filename) and os.path.isfile(Filename):\r | |
61 | return self.ConvertTextFileToDict(Filename, '#', '=')\r | |
62 | else:\r | |
63 | EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)\r | |
64 | return 1\r | |
65 | \r | |
66 | ## ConvertTextFileToDict\r | |
67 | #\r | |
68 | # Convert a text file to a dictionary of (name:value) pairs.\r | |
69 | # The data is saved to self.TargetTxtDictionary\r | |
70 | #\r | |
71 | # @param FileName: Text filename\r | |
72 | # @param CommentCharacter: Comment char, be used to ignore comment content\r | |
73 | # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r | |
74 | #\r | |
75 | # @retval 0 Convert successfully\r | |
76 | # @retval 1 Open file failed\r | |
77 | #\r | |
78 | def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):\r | |
79 | F = None\r | |
80 | try:\r | |
81 | F = open(FileName, 'r')\r | |
82 | self.ConfDirectoryPath = os.path.dirname(FileName)\r | |
83 | except:\r | |
84 | EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)\r | |
85 | if F is not None:\r | |
86 | F.close()\r | |
87 | \r | |
88 | for Line in F:\r | |
89 | Line = Line.strip()\r | |
90 | if Line.startswith(CommentCharacter) or Line == '':\r | |
91 | continue\r | |
92 | \r | |
93 | LineList = Line.split(KeySplitCharacter, 1)\r | |
94 | Key = LineList[0].strip()\r | |
95 | if len(LineList) == 2:\r | |
96 | Value = LineList[1].strip()\r | |
97 | else:\r | |
98 | Value = ""\r | |
99 | \r | |
100 | if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \\r | |
101 | DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:\r | |
102 | self.TargetTxtDictionary[Key] = Value.replace('\\', '/')\r | |
103 | if Key == DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.TargetTxtDictionary[Key]:\r | |
104 | if self.TargetTxtDictionary[Key].startswith("Conf/"):\r | |
105 | Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r | |
106 | if not os.path.exists(Tools_Def) or not os.path.isfile(Tools_Def):\r | |
107 | # If Conf/Conf does not exist, try just the Conf/ directory\r | |
108 | Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())\r | |
109 | else:\r | |
110 | # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory\r | |
111 | Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r | |
112 | self.TargetTxtDictionary[Key] = Tools_Def\r | |
113 | if Key == DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF and self.TargetTxtDictionary[Key]:\r | |
114 | if self.TargetTxtDictionary[Key].startswith("Conf/"):\r | |
115 | Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r | |
116 | if not os.path.exists(Build_Rule) or not os.path.isfile(Build_Rule):\r | |
117 | # If Conf/Conf does not exist, try just the Conf/ directory\r | |
118 | Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())\r | |
119 | else:\r | |
120 | # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory\r | |
121 | Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r | |
122 | self.TargetTxtDictionary[Key] = Build_Rule\r | |
123 | elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \\r | |
124 | DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:\r | |
125 | self.TargetTxtDictionary[Key] = Value.split()\r | |
126 | elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:\r | |
127 | try:\r | |
128 | V = int(Value, 0)\r | |
129 | except:\r | |
130 | EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),\r | |
131 | File=FileName)\r | |
132 | self.TargetTxtDictionary[Key] = Value\r | |
133 | #elif Key not in GlobalData.gGlobalDefines:\r | |
134 | # GlobalData.gGlobalDefines[Key] = Value\r | |
135 | \r | |
136 | F.close()\r | |
137 | return 0\r | |
138 | \r | |
139 | ## TargetTxtDict\r | |
140 | #\r | |
141 | # Load target.txt in input Conf dir\r | |
142 | #\r | |
143 | # @param ConfDir: Conf dir\r | |
144 | #\r | |
145 | # @retval Target An instance of TargetTxtClassObject() with loaded target.txt\r | |
146 | #\r | |
147 | \r | |
148 | class TargetTxtDict():\r | |
149 | \r | |
150 | def __new__(cls, *args, **kw):\r | |
151 | if not hasattr(cls, '_instance'):\r | |
152 | orig = super(TargetTxtDict, cls)\r | |
153 | cls._instance = orig.__new__(cls, *args, **kw)\r | |
154 | return cls._instance\r | |
155 | \r | |
156 | def __init__(self):\r | |
157 | if not hasattr(self, 'Target'):\r | |
158 | self.TxtTarget = None\r | |
159 | \r | |
160 | @property\r | |
161 | def Target(self):\r | |
162 | if not self.TxtTarget:\r | |
163 | self._GetTarget()\r | |
164 | return self.TxtTarget\r | |
165 | \r | |
166 | def _GetTarget(self):\r | |
167 | Target = TargetTxtClassObject()\r | |
168 | ConfDirectory = GlobalData.gCmdConfDir\r | |
169 | if ConfDirectory:\r | |
170 | # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r | |
171 | ConfDirectoryPath = os.path.normpath(ConfDirectory)\r | |
172 | \r | |
173 | if not os.path.isabs(ConfDirectoryPath):\r | |
174 | # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE\r | |
175 | # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r | |
176 | ConfDirectoryPath = mws.join(os.environ["WORKSPACE"], ConfDirectoryPath)\r | |
177 | else:\r | |
178 | if "CONF_PATH" in os.environ:\r | |
179 | ConfDirectoryPath = os.path.normcase(os.path.normpath(os.environ["CONF_PATH"]))\r | |
180 | else:\r | |
181 | # Get standard WORKSPACE/Conf use the absolute path to the WORKSPACE/Conf\r | |
182 | ConfDirectoryPath = mws.join(os.environ["WORKSPACE"], 'Conf')\r | |
183 | GlobalData.gConfDirectory = ConfDirectoryPath\r | |
184 | targettxt = os.path.normpath(os.path.join(ConfDirectoryPath, gDefaultTargetTxtFile))\r | |
185 | if os.path.exists(targettxt):\r | |
186 | Target.LoadTargetTxtFile(targettxt)\r | |
187 | self.TxtTarget = Target\r | |
188 | \r | |
189 | ##\r | |
190 | #\r | |
191 | # This acts like the main() function for the script, unless it is 'import'ed into another\r | |
192 | # script.\r | |
193 | #\r | |
194 | if __name__ == '__main__':\r | |
195 | pass\r | |
196 | Target = TargetTxtDict(os.getenv("WORKSPACE"))\r | |
197 | print(Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER])\r | |
198 | print(Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET])\r | |
199 | print(Target.TargetTxtDictionary)\r |