]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/TargetTxtClassObject.py
BaseTools: use set instead of list for a variable to be used with in
[mirror_edk2.git] / BaseTools / Source / Python / Common / TargetTxtClassObject.py
CommitLineData
30fdf114
LG
1## @file\r
2# This file is used to define each component of Target.txt file\r
3#\r
1be2ed90 4# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5# This program and the accompanying materials\r
30fdf114
LG
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14##\r
15# Import Modules\r
16#\r
1be2ed90 17import Common.LongFilePathOs as os\r
30fdf114
LG
18import EdkLogger\r
19import DataType\r
20from BuildToolError import *\r
21import GlobalData\r
1be2ed90 22from Common.LongFilePathSupport import OpenLongFilePath as open\r
30fdf114 23\r
97fa0ee9 24gDefaultTargetTxtFile = "target.txt"\r
30fdf114
LG
25\r
26## TargetTxtClassObject\r
27#\r
28# This class defined content used in file target.txt\r
29#\r
30# @param object: Inherited from object class\r
31# @param Filename: Input value for full path of target.txt\r
32#\r
33# @var TargetTxtDictionary: To store keys and values defined in target.txt\r
34#\r
35class TargetTxtClassObject(object):\r
36 def __init__(self, Filename = None):\r
37 self.TargetTxtDictionary = {\r
38 DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',\r
39 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',\r
40 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',\r
30fdf114
LG
41 DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',\r
42 DataType.TAB_TAT_DEFINES_TARGET : [],\r
43 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],\r
44 DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],\r
45 DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',\r
46 }\r
97fa0ee9 47 self.ConfDirectoryPath = ""\r
4231a819 48 if Filename is not None:\r
30fdf114
LG
49 self.LoadTargetTxtFile(Filename)\r
50\r
51 ## LoadTargetTxtFile\r
52 #\r
53 # Load target.txt file and parse it, return a set structure to store keys and values\r
54 #\r
55 # @param Filename: Input value for full path of target.txt\r
56 #\r
57 # @retval set() A set structure to store keys and values\r
58 # @retval 1 Error happenes in parsing\r
59 #\r
60 def LoadTargetTxtFile(self, Filename):\r
61 if os.path.exists(Filename) and os.path.isfile(Filename):\r
62 return self.ConvertTextFileToDict(Filename, '#', '=')\r
63 else:\r
64 EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)\r
65 return 1\r
66\r
67 ## ConvertTextFileToDict\r
68 #\r
69 # Convert a text file to a dictionary of (name:value) pairs.\r
70 # The data is saved to self.TargetTxtDictionary\r
71 #\r
72 # @param FileName: Text filename\r
73 # @param CommentCharacter: Comment char, be used to ignore comment content\r
74 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r
75 #\r
76 # @retval 0 Convert successfully\r
77 # @retval 1 Open file failed\r
78 #\r
79 def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):\r
80 F = None\r
81 try:\r
97fa0ee9
YL
82 F = open(FileName, 'r')\r
83 self.ConfDirectoryPath = os.path.dirname(FileName)\r
30fdf114
LG
84 except:\r
85 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)\r
4231a819 86 if F is not None:\r
30fdf114
LG
87 F.close()\r
88\r
89 for Line in F:\r
90 Line = Line.strip()\r
91 if Line.startswith(CommentCharacter) or Line == '':\r
92 continue\r
93\r
94 LineList = Line.split(KeySplitCharacter, 1)\r
95 Key = LineList[0].strip()\r
96 if len(LineList) == 2:\r
97 Value = LineList[1].strip()\r
98 else:\r
99 Value = ""\r
100\r
101 if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \\r
102 DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:\r
103 self.TargetTxtDictionary[Key] = Value.replace('\\', '/')\r
97fa0ee9
YL
104 if Key == DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.TargetTxtDictionary[Key]:\r
105 if self.TargetTxtDictionary[Key].startswith("Conf/"):\r
106 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r
107 if not os.path.exists(Tools_Def) or not os.path.isfile(Tools_Def):\r
108 # If Conf/Conf does not exist, try just the Conf/ directory\r
109 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())\r
110 else:\r
111 # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory\r
112 Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r
113 self.TargetTxtDictionary[Key] = Tools_Def\r
114 if Key == DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF and self.TargetTxtDictionary[Key]:\r
115 if self.TargetTxtDictionary[Key].startswith("Conf/"):\r
116 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r
117 if not os.path.exists(Build_Rule) or not os.path.isfile(Build_Rule):\r
118 # If Conf/Conf does not exist, try just the Conf/ directory\r
119 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())\r
120 else:\r
121 # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory\r
122 Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())\r
123 self.TargetTxtDictionary[Key] = Build_Rule\r
30fdf114
LG
124 elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \\r
125 DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:\r
126 self.TargetTxtDictionary[Key] = Value.split()\r
30fdf114
LG
127 elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:\r
128 try:\r
129 V = int(Value, 0)\r
130 except:\r
131 EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),\r
132 File=FileName)\r
133 self.TargetTxtDictionary[Key] = Value\r
134 #elif Key not in GlobalData.gGlobalDefines:\r
135 # GlobalData.gGlobalDefines[Key] = Value\r
136\r
137 F.close()\r
138 return 0\r
139\r
30fdf114
LG
140## TargetTxtDict\r
141#\r
97fa0ee9 142# Load target.txt in input Conf dir\r
30fdf114 143#\r
97fa0ee9 144# @param ConfDir: Conf dir\r
30fdf114
LG
145#\r
146# @retval Target An instance of TargetTxtClassObject() with loaded target.txt\r
147#\r
97fa0ee9 148def TargetTxtDict(ConfDir):\r
30fdf114 149 Target = TargetTxtClassObject()\r
97fa0ee9 150 Target.LoadTargetTxtFile(os.path.normpath(os.path.join(ConfDir, gDefaultTargetTxtFile)))\r
30fdf114
LG
151 return Target\r
152\r
153##\r
154#\r
155# This acts like the main() function for the script, unless it is 'import'ed into another\r
156# script.\r
157#\r
158if __name__ == '__main__':\r
159 pass\r
160 Target = TargetTxtDict(os.getenv("WORKSPACE"))\r
161 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]\r
162 print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]\r
163 print Target.TargetTxtDictionary\r