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