2 # This file is used to define each component of Target.txt file
4 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
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
20 gDefaultTargetTxtFile
= "target.txt"
22 ## TargetTxtClassObject
24 # This class defined content used in file target.txt
26 # @param object: Inherited from object class
27 # @param Filename: Input value for full path of target.txt
29 # @var TargetTxtDictionary: To store keys and values defined in target.txt
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
: '',
43 self
.ConfDirectoryPath
= ""
44 if Filename
is not None:
45 self
.LoadTargetTxtFile(Filename
)
49 # Load target.txt file and parse it, return a set structure to store keys and values
51 # @param Filename: Input value for full path of target.txt
53 # @retval set() A set structure to store keys and values
54 # @retval 1 Error happenes in parsing
56 def LoadTargetTxtFile(self
, Filename
):
57 if os
.path
.exists(Filename
) and os
.path
.isfile(Filename
):
58 return self
.ConvertTextFileToDict(Filename
, '#', '=')
60 EdkLogger
.error("Target.txt Parser", FILE_NOT_FOUND
, ExtraData
=Filename
)
63 ## ConvertTextFileToDict
65 # Convert a text file to a dictionary of (name:value) pairs.
66 # The data is saved to self.TargetTxtDictionary
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
72 # @retval 0 Convert successfully
73 # @retval 1 Open file failed
75 def ConvertTextFileToDict(self
, FileName
, CommentCharacter
, KeySplitCharacter
):
78 F
= open(FileName
, 'r')
79 self
.ConfDirectoryPath
= os
.path
.dirname(FileName
)
81 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=FileName
)
87 if Line
.startswith(CommentCharacter
) or Line
== '':
90 LineList
= Line
.split(KeySplitCharacter
, 1)
91 Key
= LineList
[0].strip()
92 if len(LineList
) == 2:
93 Value
= LineList
[1].strip()
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())
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())
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
:
127 EdkLogger
.error("build", FORMAT_INVALID
, "Invalid number of [%s]: %s." % (Key
, Value
),
129 self
.TargetTxtDictionary
[Key
] = Value
130 #elif Key not in GlobalData.gGlobalDefines:
131 # GlobalData.gGlobalDefines[Key] = Value
138 # Load target.txt in input Conf dir
140 # @param ConfDir: Conf dir
142 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
144 def TargetTxtDict(ConfDir
):
145 Target
= TargetTxtClassObject()
146 Target
.LoadTargetTxtFile(os
.path
.normpath(os
.path
.join(ConfDir
, gDefaultTargetTxtFile
)))
151 # This acts like the main() function for the script, unless it is 'import'ed into another
154 if __name__
== '__main__':
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
)