2014-08-28 13:53:34 +00:00
## @file
# Target Tool Parser
2009-07-17 09:10:31 +00:00
#
2021-04-13 20:11:48 -07:00
# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
2009-07-17 09:10:31 +00:00
#
2019-04-03 16:03:11 -07:00
# SPDX-License-Identifier: BSD-2-Clause-Patent
2009-07-17 09:10:31 +00:00
#
2018-10-15 08:27:53 +08:00
from __future__ import print_function
2014-08-15 03:06:48 +00:00
import Common.LongFilePathOs as os
2009-07-17 09:10:31 +00:00
import sys
import traceback
from optparse import OptionParser
import Common.EdkLogger as EdkLogger
import Common.BuildToolError as BuildToolError
from Common.DataType import *
2011-09-18 12:17:25 +00:00
from Common.BuildVersion import gBUILD_VERSION
2014-08-15 03:06:48 +00:00
from Common.LongFilePathSupport import OpenLongFilePath as open
2021-09-23 16:59:03 +08:00
from Common.TargetTxtClassObject import gDefaultTargetTxtFile
2009-07-17 09:10:31 +00:00
# To Do 1.set clean, 2. add item, if the line is disabled.
class TargetTool ():
def __init__ ( self , opt , args ):
self . WorkSpace = os . path . normpath ( os . getenv ( 'WORKSPACE' ))
self . Opt = opt
self . Arg = args [ 0 ]
2021-09-23 16:59:03 +08:00
self . FileName = os . path . normpath ( os . path . join ( self . WorkSpace , 'Conf' , gDefaultTargetTxtFile ))
2009-07-17 09:10:31 +00:00
if os . path . isfile ( self . FileName ) == False :
2018-06-25 18:31:26 +08:00
print ( " %s does not exist." % self . FileName )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
self . TargetTxtDictionary = {
TAB_TAT_DEFINES_ACTIVE_PLATFORM : None ,
TAB_TAT_DEFINES_TOOL_CHAIN_CONF : None ,
TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : None ,
TAB_TAT_DEFINES_TARGET : None ,
TAB_TAT_DEFINES_TOOL_CHAIN_TAG : None ,
TAB_TAT_DEFINES_TARGET_ARCH : None ,
TAB_TAT_DEFINES_BUILD_RULE_CONF : None ,
}
self . LoadTargetTxtFile ( self . FileName )
def LoadTargetTxtFile ( self , filename ):
if os . path . exists ( filename ) and os . path . isfile ( filename ):
2010-10-11 06:26:52 +00:00
return self . ConvertTextFileToDict ( filename , '#' , '=' )
2009-07-17 09:10:31 +00:00
else :
raise ParseError ( 'LoadTargetTxtFile() : No Target.txt file exists.' )
return 1
#
# Convert a text file to a dictionary
#
def ConvertTextFileToDict ( self , FileName , CommentCharacter , KeySplitCharacter ):
"""Convert a text file to a dictionary of (name:value) pairs."""
try :
2018-06-25 18:31:33 +08:00
f = open ( FileName , 'r' )
2009-07-17 09:10:31 +00:00
for Line in f :
if Line . startswith ( CommentCharacter ) or Line . strip () == '' :
continue
2018-06-25 18:31:33 +08:00
LineList = Line . split ( KeySplitCharacter , 1 )
2009-07-17 09:10:31 +00:00
if len ( LineList ) >= 2 :
Key = LineList [ 0 ] . strip ()
2018-04-17 22:40:15 +08:00
if Key . startswith ( CommentCharacter ) == False and Key in self . TargetTxtDictionary :
2009-07-17 09:10:31 +00:00
if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM or Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF \
2010-10-11 06:26:52 +00:00
or Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER \
2009-07-17 09:10:31 +00:00
or Key == TAB_TAT_DEFINES_ACTIVE_MODULE :
self . TargetTxtDictionary [ Key ] = LineList [ 1 ] . replace ( ' \\ ' , '/' ) . strip ()
elif Key == TAB_TAT_DEFINES_TARGET or Key == TAB_TAT_DEFINES_TARGET_ARCH \
or Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG or Key == TAB_TAT_DEFINES_BUILD_RULE_CONF :
self . TargetTxtDictionary [ Key ] = LineList [ 1 ] . split ()
f . close ()
return 0
except :
last_type , last_value , last_tb = sys . exc_info ()
traceback . print_exception ( last_type , last_value , last_tb )
def Print ( self ):
errMsg = ''
2018-04-06 07:14:02 +08:00
for Key in self . TargetTxtDictionary :
2018-06-25 18:31:35 +08:00
if isinstance ( self . TargetTxtDictionary [ Key ], type ([])):
2018-06-25 18:31:26 +08:00
print ( " %-30s = %s " % ( Key , '' . join ( elem + ' ' for elem in self . TargetTxtDictionary [ Key ])))
2018-03-27 04:25:43 +08:00
elif self . TargetTxtDictionary [ Key ] is None :
2018-07-05 17:40:04 +08:00
errMsg += " Missing %s configuration information, please use TargetTool to set value!" % Key + os . linesep
2009-07-17 09:10:31 +00:00
else :
2018-06-25 18:31:26 +08:00
print ( " %-30s = %s " % ( Key , self . TargetTxtDictionary [ Key ]))
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
if errMsg != '' :
2018-06-25 18:31:26 +08:00
print ( os . linesep + 'Warning:' + os . linesep + errMsg )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
def RWFile ( self , CommentCharacter , KeySplitCharacter , Num ):
try :
fr = open ( self . FileName , 'r' )
fw = open ( os . path . normpath ( os . path . join ( self . WorkSpace , 'Conf \\ targetnew.txt' )), 'w' )
existKeys = []
for Line in fr :
if Line . startswith ( CommentCharacter ) or Line . strip () == '' :
fw . write ( Line )
else :
2018-06-25 18:31:33 +08:00
LineList = Line . split ( KeySplitCharacter , 1 )
2009-07-17 09:10:31 +00:00
if len ( LineList ) >= 2 :
Key = LineList [ 0 ] . strip ()
2018-04-17 22:40:15 +08:00
if Key . startswith ( CommentCharacter ) == False and Key in self . TargetTxtDictionary :
2009-07-17 09:10:31 +00:00
if Key not in existKeys :
existKeys . append ( Key )
else :
2018-06-25 18:31:26 +08:00
print ( "Warning: Found duplicate key item in original configuration files!" )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
if Num == 0 :
Line = " %-30s = \n " % Key
else :
ret = GetConfigureKeyValue ( self , Key )
2018-03-27 04:25:43 +08:00
if ret is not None :
2009-07-17 09:10:31 +00:00
Line = ret
fw . write ( Line )
2018-04-17 22:40:15 +08:00
for key in self . TargetTxtDictionary :
2009-07-17 09:10:31 +00:00
if key not in existKeys :
2018-06-25 18:31:26 +08:00
print ( "Warning: %s does not exist in original configuration file" % key )
2009-07-17 09:10:31 +00:00
Line = GetConfigureKeyValue ( self , key )
2018-03-27 04:25:43 +08:00
if Line is None :
2009-07-17 09:10:31 +00:00
Line = " %-30s = " % key
fw . write ( Line )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
fr . close ()
fw . close ()
os . remove ( self . FileName )
os . rename ( os . path . normpath ( os . path . join ( self . WorkSpace , 'Conf \\ targetnew.txt' )), self . FileName )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
except :
last_type , last_value , last_tb = sys . exc_info ()
traceback . print_exception ( last_type , last_value , last_tb )
def GetConfigureKeyValue ( self , Key ):
Line = None
2018-03-27 04:25:43 +08:00
if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM and self . Opt . DSCFILE is not None :
2009-07-17 09:10:31 +00:00
dscFullPath = os . path . join ( self . WorkSpace , self . Opt . DSCFILE )
if os . path . exists ( dscFullPath ):
Line = " %-30s = %s \n " % ( Key , self . Opt . DSCFILE )
else :
2019-02-06 15:44:39 +08:00
EdkLogger . error ( "TargetTool" , BuildToolError . FILE_NOT_FOUND ,
2009-07-17 09:10:31 +00:00
"DSC file %s does not exist!" % self . Opt . DSCFILE , RaiseError = False )
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self . Opt . TOOL_DEFINITION_FILE is not None :
2009-07-17 09:10:31 +00:00
tooldefFullPath = os . path . join ( self . WorkSpace , self . Opt . TOOL_DEFINITION_FILE )
if os . path . exists ( tooldefFullPath ):
Line = " %-30s = %s \n " % ( Key , self . Opt . TOOL_DEFINITION_FILE )
else :
2019-02-06 15:44:39 +08:00
EdkLogger . error ( "TargetTool" , BuildToolError . FILE_NOT_FOUND ,
2009-07-17 09:10:31 +00:00
"Tooldef file %s does not exist!" % self . Opt . TOOL_DEFINITION_FILE , RaiseError = False )
2010-10-11 06:26:52 +00:00
elif self . Opt . NUM >= 2 :
Line = " %-30s = %s \n " % ( Key , 'Enable' )
elif self . Opt . NUM <= 1 :
2018-07-05 17:40:04 +08:00
Line = " %-30s = %s \n " % ( Key , 'Disable' )
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER and self . Opt . NUM is not None :
2009-07-17 09:10:31 +00:00
Line = " %-30s = %s \n " % ( Key , str ( self . Opt . NUM ))
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_TARGET and self . Opt . TARGET is not None :
2009-07-17 09:10:31 +00:00
Line = " %-30s = %s \n " % ( Key , '' . join ( elem + ' ' for elem in self . Opt . TARGET ))
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_TARGET_ARCH and self . Opt . TARGET_ARCH is not None :
2009-07-17 09:10:31 +00:00
Line = " %-30s = %s \n " % ( Key , '' . join ( elem + ' ' for elem in self . Opt . TARGET_ARCH ))
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG and self . Opt . TOOL_CHAIN_TAG is not None :
2009-07-17 09:10:31 +00:00
Line = " %-30s = %s \n " % ( Key , self . Opt . TOOL_CHAIN_TAG )
2018-03-27 04:25:43 +08:00
elif Key == TAB_TAT_DEFINES_BUILD_RULE_CONF and self . Opt . BUILD_RULE_FILE is not None :
2009-07-17 09:10:31 +00:00
buildruleFullPath = os . path . join ( self . WorkSpace , self . Opt . BUILD_RULE_FILE )
if os . path . exists ( buildruleFullPath ):
Line = " %-30s = %s \n " % ( Key , self . Opt . BUILD_RULE_FILE )
else :
2018-07-05 17:40:04 +08:00
EdkLogger . error ( "TagetTool" , BuildToolError . FILE_NOT_FOUND ,
2009-07-17 09:10:31 +00:00
"Build rule file %s does not exist!" % self . Opt . BUILD_RULE_FILE , RaiseError = False )
return Line
2011-09-18 12:17:25 +00:00
VersionNumber = ( "0.01" + " " + gBUILD_VERSION )
2009-07-17 09:10:31 +00:00
__version__ = "%prog Version " + VersionNumber
2018-07-05 17:40:04 +08:00
__copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation All rights reserved."
2009-07-17 09:10:31 +00:00
__usage__ = "%prog [options] {args} \
\n Args: \
\n Clean clean the all default configuration of target.txt. \
\n Print print the all default configuration of target.txt. \
\n Set replace the default configuration with expected value specified by option."
gParamCheck = []
def SingleCheckCallback ( option , opt_str , value , parser ):
if option not in gParamCheck :
setattr ( parser . values , option . dest , value )
gParamCheck . append ( option )
else :
parser . error ( "Option %s only allows one instance in command line!" % option )
def RangeCheckCallback ( option , opt_str , value , parser ):
if option not in gParamCheck :
gParamCheck . append ( option )
if value < 1 or value > 8 :
parser . error ( "The count of multi-thread is not in valid range of 1 ~ 8." )
else :
setattr ( parser . values , option . dest , value )
else :
parser . error ( "Option %s only allows one instance in command line!" % option )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
def MyOptionParser ():
2018-06-25 18:31:33 +08:00
parser = OptionParser ( version = __version__ , prog = "TargetTool.exe" , usage = __usage__ , description = __copyright__ )
2021-04-13 20:11:48 -07:00
parser . add_option ( "-a" , "--arch" , action = "append" , dest = "TARGET_ARCH" ,
2019-01-09 14:44:36 +08:00
help = "ARCHS is one of list: IA32, X64, ARM, AARCH64 or EBC, which replaces target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option. 0 will clear this setting in target.txt and can't combine with other value." )
2009-07-17 09:10:31 +00:00
parser . add_option ( "-p" , "--platform" , action = "callback" , type = "string" , dest = "DSCFILE" , callback = SingleCheckCallback ,
help = "Specify a DSC file, which replace target.txt's ACTIVE_PLATFORM definition. 0 will clear this setting in target.txt and can't combine with other value." )
parser . add_option ( "-c" , "--tooldef" , action = "callback" , type = "string" , dest = "TOOL_DEFINITION_FILE" , callback = SingleCheckCallback ,
help = "Specify the WORKSPACE relative path of tool_def.txt file, which replace target.txt's TOOL_CHAIN_CONF definition. 0 will clear this setting in target.txt and can't combine with other value." )
2018-06-25 18:31:33 +08:00
parser . add_option ( "-t" , "--target" , action = "append" , type = "choice" , choices = [ 'DEBUG' , 'RELEASE' , '0' ], dest = "TARGET" ,
2009-07-17 09:10:31 +00:00
help = "TARGET is one of list: DEBUG, RELEASE, which replaces target.txt's TARGET definition. To specify more TARGET, please repeat this option. 0 will clear this setting in target.txt and can't combine with other value." )
parser . add_option ( "-n" , "--tagname" , action = "callback" , type = "string" , dest = "TOOL_CHAIN_TAG" , callback = SingleCheckCallback ,
help = "Specify the Tool Chain Tagname, which replaces target.txt's TOOL_CHAIN_TAG definition. 0 will clear this setting in target.txt and can't combine with other value." )
parser . add_option ( "-r" , "--buildrule" , action = "callback" , type = "string" , dest = "BUILD_RULE_FILE" , callback = SingleCheckCallback ,
help = "Specify the build rule configure file, which replaces target.txt's BUILD_RULE_CONF definition. If not specified, the default value Conf/build_rule.txt will be set." )
parser . add_option ( "-m" , "--multithreadnum" , action = "callback" , type = "int" , dest = "NUM" , callback = RangeCheckCallback ,
help = "Specify the multi-thread number which replace target.txt's MAX_CONCURRENT_THREAD_NUMBER. If the value is less than 2, MULTIPLE_THREAD will be disabled. If the value is larger than 1, MULTIPLE_THREAD will be enabled." )
( opt , args ) = parser . parse_args ()
return ( opt , args )
if __name__ == '__main__' :
EdkLogger . Initialize ()
EdkLogger . SetLevel ( EdkLogger . QUIET )
2018-03-27 04:25:43 +08:00
if os . getenv ( 'WORKSPACE' ) is None :
2018-06-25 18:31:26 +08:00
print ( "ERROR: WORKSPACE should be specified or edksetup script should be executed before run TargetTool" )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
2018-07-05 17:40:04 +08:00
2009-07-17 09:10:31 +00:00
( opt , args ) = MyOptionParser ()
if len ( args ) != 1 or ( args [ 0 ] . lower () != 'print' and args [ 0 ] . lower () != 'clean' and args [ 0 ] . lower () != 'set' ):
2018-06-25 18:31:26 +08:00
print ( "The number of args isn't 1 or the value of args is invalid." )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
2018-03-27 04:25:43 +08:00
if opt . NUM is not None and opt . NUM < 1 :
2018-06-25 18:31:26 +08:00
print ( "The MAX_CONCURRENT_THREAD_NUMBER must be larger than 0." )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
2018-03-27 04:25:43 +08:00
if opt . TARGET is not None and len ( opt . TARGET ) > 1 :
2009-07-17 09:10:31 +00:00
for elem in opt . TARGET :
if elem == '0' :
2018-06-25 18:31:26 +08:00
print ( "0 will clear the TARGET setting in target.txt and can't combine with other value." )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
2018-03-27 04:25:43 +08:00
if opt . TARGET_ARCH is not None and len ( opt . TARGET_ARCH ) > 1 :
2009-07-17 09:10:31 +00:00
for elem in opt . TARGET_ARCH :
if elem == '0' :
2018-06-25 18:31:26 +08:00
print ( "0 will clear the TARGET_ARCH setting in target.txt and can't combine with other value." )
2009-07-17 09:10:31 +00:00
sys . exit ( 1 )
try :
FileHandle = TargetTool ( opt , args )
if FileHandle . Arg . lower () == 'print' :
FileHandle . Print ()
sys . exit ( 0 )
elif FileHandle . Arg . lower () == 'clean' :
FileHandle . RWFile ( '#' , '=' , 0 )
else :
FileHandle . RWFile ( '#' , '=' , 1 )
2018-06-25 18:31:25 +08:00
except Exception as e :
2009-07-17 09:10:31 +00:00
last_type , last_value , last_tb = sys . exc_info ()
traceback . print_exception ( last_type , last_value , last_tb )