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 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 from __future__
import print_function
18 import Common
.LongFilePathOs
as os
21 from BuildToolError
import *
23 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
25 gDefaultTargetTxtFile
= "target.txt"
27 ## TargetTxtClassObject
29 # This class defined content used in file target.txt
31 # @param object: Inherited from object class
32 # @param Filename: Input value for full path of target.txt
34 # @var TargetTxtDictionary: To store keys and values defined in target.txt
36 class TargetTxtClassObject(object):
37 def __init__(self
, Filename
= None):
38 self
.TargetTxtDictionary
= {
39 DataType
.TAB_TAT_DEFINES_ACTIVE_PLATFORM
: '',
40 DataType
.TAB_TAT_DEFINES_ACTIVE_MODULE
: '',
41 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
: '',
42 DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
: '',
43 DataType
.TAB_TAT_DEFINES_TARGET
: [],
44 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_TAG
: [],
45 DataType
.TAB_TAT_DEFINES_TARGET_ARCH
: [],
46 DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
: '',
48 self
.ConfDirectoryPath
= ""
49 if Filename
is not None:
50 self
.LoadTargetTxtFile(Filename
)
54 # Load target.txt file and parse it, return a set structure to store keys and values
56 # @param Filename: Input value for full path of target.txt
58 # @retval set() A set structure to store keys and values
59 # @retval 1 Error happenes in parsing
61 def LoadTargetTxtFile(self
, Filename
):
62 if os
.path
.exists(Filename
) and os
.path
.isfile(Filename
):
63 return self
.ConvertTextFileToDict(Filename
, '#', '=')
65 EdkLogger
.error("Target.txt Parser", FILE_NOT_FOUND
, ExtraData
=Filename
)
68 ## ConvertTextFileToDict
70 # Convert a text file to a dictionary of (name:value) pairs.
71 # The data is saved to self.TargetTxtDictionary
73 # @param FileName: Text filename
74 # @param CommentCharacter: Comment char, be used to ignore comment content
75 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
77 # @retval 0 Convert successfully
78 # @retval 1 Open file failed
80 def ConvertTextFileToDict(self
, FileName
, CommentCharacter
, KeySplitCharacter
):
83 F
= open(FileName
, 'r')
84 self
.ConfDirectoryPath
= os
.path
.dirname(FileName
)
86 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=FileName
)
92 if Line
.startswith(CommentCharacter
) or Line
== '':
95 LineList
= Line
.split(KeySplitCharacter
, 1)
96 Key
= LineList
[0].strip()
97 if len(LineList
) == 2:
98 Value
= LineList
[1].strip()
102 if Key
in [DataType
.TAB_TAT_DEFINES_ACTIVE_PLATFORM
, DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
, \
103 DataType
.TAB_TAT_DEFINES_ACTIVE_MODULE
, DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
]:
104 self
.TargetTxtDictionary
[Key
] = Value
.replace('\\', '/')
105 if Key
== DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
and self
.TargetTxtDictionary
[Key
]:
106 if self
.TargetTxtDictionary
[Key
].startswith("Conf/"):
107 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
108 if not os
.path
.exists(Tools_Def
) or not os
.path
.isfile(Tools_Def
):
109 # If Conf/Conf does not exist, try just the Conf/ directory
110 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].replace("Conf/", "", 1).strip())
112 # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory
113 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
114 self
.TargetTxtDictionary
[Key
] = Tools_Def
115 if Key
== DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
and self
.TargetTxtDictionary
[Key
]:
116 if self
.TargetTxtDictionary
[Key
].startswith("Conf/"):
117 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
118 if not os
.path
.exists(Build_Rule
) or not os
.path
.isfile(Build_Rule
):
119 # If Conf/Conf does not exist, try just the Conf/ directory
120 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].replace("Conf/", "", 1).strip())
122 # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory
123 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
124 self
.TargetTxtDictionary
[Key
] = Build_Rule
125 elif Key
in [DataType
.TAB_TAT_DEFINES_TARGET
, DataType
.TAB_TAT_DEFINES_TARGET_ARCH
, \
126 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_TAG
]:
127 self
.TargetTxtDictionary
[Key
] = Value
.split()
128 elif Key
== DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
:
132 EdkLogger
.error("build", FORMAT_INVALID
, "Invalid number of [%s]: %s." % (Key
, Value
),
134 self
.TargetTxtDictionary
[Key
] = Value
135 #elif Key not in GlobalData.gGlobalDefines:
136 # GlobalData.gGlobalDefines[Key] = Value
143 # Load target.txt in input Conf dir
145 # @param ConfDir: Conf dir
147 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
149 def TargetTxtDict(ConfDir
):
150 Target
= TargetTxtClassObject()
151 Target
.LoadTargetTxtFile(os
.path
.normpath(os
.path
.join(ConfDir
, gDefaultTargetTxtFile
)))
156 # This acts like the main() function for the script, unless it is 'import'ed into another
159 if __name__
== '__main__':
161 Target
= TargetTxtDict(os
.getenv("WORKSPACE"))
162 print(Target
.TargetTxtDictionary
[DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
])
163 print(Target
.TargetTxtDictionary
[DataType
.TAB_TAT_DEFINES_TARGET
])
164 print(Target
.TargetTxtDictionary
)