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 from __future__
import absolute_import
19 import Common
.LongFilePathOs
as os
20 from . import EdkLogger
21 from . import DataType
22 from .BuildToolError
import *
23 from . import GlobalData
24 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
26 gDefaultTargetTxtFile
= "target.txt"
28 ## TargetTxtClassObject
30 # This class defined content used in file target.txt
32 # @param object: Inherited from object class
33 # @param Filename: Input value for full path of target.txt
35 # @var TargetTxtDictionary: To store keys and values defined in target.txt
37 class TargetTxtClassObject(object):
38 def __init__(self
, Filename
= None):
39 self
.TargetTxtDictionary
= {
40 DataType
.TAB_TAT_DEFINES_ACTIVE_PLATFORM
: '',
41 DataType
.TAB_TAT_DEFINES_ACTIVE_MODULE
: '',
42 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
: '',
43 DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
: '',
44 DataType
.TAB_TAT_DEFINES_TARGET
: [],
45 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_TAG
: [],
46 DataType
.TAB_TAT_DEFINES_TARGET_ARCH
: [],
47 DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
: '',
49 self
.ConfDirectoryPath
= ""
50 if Filename
is not None:
51 self
.LoadTargetTxtFile(Filename
)
55 # Load target.txt file and parse it, return a set structure to store keys and values
57 # @param Filename: Input value for full path of target.txt
59 # @retval set() A set structure to store keys and values
60 # @retval 1 Error happenes in parsing
62 def LoadTargetTxtFile(self
, Filename
):
63 if os
.path
.exists(Filename
) and os
.path
.isfile(Filename
):
64 return self
.ConvertTextFileToDict(Filename
, '#', '=')
66 EdkLogger
.error("Target.txt Parser", FILE_NOT_FOUND
, ExtraData
=Filename
)
69 ## ConvertTextFileToDict
71 # Convert a text file to a dictionary of (name:value) pairs.
72 # The data is saved to self.TargetTxtDictionary
74 # @param FileName: Text filename
75 # @param CommentCharacter: Comment char, be used to ignore comment content
76 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
78 # @retval 0 Convert successfully
79 # @retval 1 Open file failed
81 def ConvertTextFileToDict(self
, FileName
, CommentCharacter
, KeySplitCharacter
):
84 F
= open(FileName
, 'r')
85 self
.ConfDirectoryPath
= os
.path
.dirname(FileName
)
87 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=FileName
)
93 if Line
.startswith(CommentCharacter
) or Line
== '':
96 LineList
= Line
.split(KeySplitCharacter
, 1)
97 Key
= LineList
[0].strip()
98 if len(LineList
) == 2:
99 Value
= LineList
[1].strip()
103 if Key
in [DataType
.TAB_TAT_DEFINES_ACTIVE_PLATFORM
, DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
, \
104 DataType
.TAB_TAT_DEFINES_ACTIVE_MODULE
, DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
]:
105 self
.TargetTxtDictionary
[Key
] = Value
.replace('\\', '/')
106 if Key
== DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_CONF
and self
.TargetTxtDictionary
[Key
]:
107 if self
.TargetTxtDictionary
[Key
].startswith("Conf/"):
108 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
109 if not os
.path
.exists(Tools_Def
) or not os
.path
.isfile(Tools_Def
):
110 # If Conf/Conf does not exist, try just the Conf/ directory
111 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].replace("Conf/", "", 1).strip())
113 # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory
114 Tools_Def
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
115 self
.TargetTxtDictionary
[Key
] = Tools_Def
116 if Key
== DataType
.TAB_TAT_DEFINES_BUILD_RULE_CONF
and self
.TargetTxtDictionary
[Key
]:
117 if self
.TargetTxtDictionary
[Key
].startswith("Conf/"):
118 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
119 if not os
.path
.exists(Build_Rule
) or not os
.path
.isfile(Build_Rule
):
120 # If Conf/Conf does not exist, try just the Conf/ directory
121 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].replace("Conf/", "", 1).strip())
123 # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory
124 Build_Rule
= os
.path
.join(self
.ConfDirectoryPath
, self
.TargetTxtDictionary
[Key
].strip())
125 self
.TargetTxtDictionary
[Key
] = Build_Rule
126 elif Key
in [DataType
.TAB_TAT_DEFINES_TARGET
, DataType
.TAB_TAT_DEFINES_TARGET_ARCH
, \
127 DataType
.TAB_TAT_DEFINES_TOOL_CHAIN_TAG
]:
128 self
.TargetTxtDictionary
[Key
] = Value
.split()
129 elif Key
== DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
:
133 EdkLogger
.error("build", FORMAT_INVALID
, "Invalid number of [%s]: %s." % (Key
, Value
),
135 self
.TargetTxtDictionary
[Key
] = Value
136 #elif Key not in GlobalData.gGlobalDefines:
137 # GlobalData.gGlobalDefines[Key] = Value
144 # Load target.txt in input Conf dir
146 # @param ConfDir: Conf dir
148 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
150 def TargetTxtDict(ConfDir
):
151 Target
= TargetTxtClassObject()
152 Target
.LoadTargetTxtFile(os
.path
.normpath(os
.path
.join(ConfDir
, gDefaultTargetTxtFile
)))
157 # This acts like the main() function for the script, unless it is 'import'ed into another
160 if __name__
== '__main__':
162 Target
= TargetTxtDict(os
.getenv("WORKSPACE"))
163 print(Target
.TargetTxtDictionary
[DataType
.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER
])
164 print(Target
.TargetTxtDictionary
[DataType
.TAB_TAT_DEFINES_TARGET
])
165 print(Target
.TargetTxtDictionary
)