2010-05-04 12:08:43 +04:00
# based on playground/evil in the waf svn tree
2015-10-27 22:46:46 +03:00
import os , datetime , fnmatch
2018-01-31 12:48:43 +03:00
from waflib import Scripting , Utils , Options , Logs , Errors
2018-10-28 00:32:35 +03:00
from waflib import ConfigSet , Context
2015-10-27 22:46:46 +03:00
from samba_utils import LOCAL_CACHE , os_path_relpath
2010-05-04 12:08:43 +04:00
def run_task ( t , k ) :
2012-02-09 16:08:31 +04:00
''' run a single build task '''
ret = t . run ( )
if ret :
2018-01-31 12:48:43 +03:00
raise Errors . WafError ( " Failed to build %s : %u " % ( k , ret ) )
2010-05-04 12:08:43 +04:00
def run_named_build_task ( cmd ) :
2012-02-09 16:08:31 +04:00
''' run a named build task, matching the cmd name using fnmatch
wildcards against inputs and outputs of all build tasks '''
bld = fake_build_environment ( info = False )
found = False
cwd_node = bld . root . find_dir ( os . getcwd ( ) )
top_node = bld . root . find_dir ( bld . srcnode . abspath ( ) )
2010-11-03 02:49:08 +03:00
2012-02-09 16:08:31 +04:00
cmd = os . path . normpath ( cmd )
2010-11-03 02:49:08 +03:00
2012-02-09 16:08:31 +04:00
# cope with builds of bin/*/*
if os . path . islink ( cmd ) :
cmd = os_path_relpath ( os . readlink ( cmd ) , os . getcwd ( ) )
2010-11-03 02:49:08 +03:00
2012-02-09 16:08:31 +04:00
if cmd [ 0 : 12 ] == " bin/default/ " :
cmd = cmd [ 12 : ]
2010-11-03 02:49:08 +03:00
2012-02-09 16:08:31 +04:00
for g in bld . task_manager . groups :
for attr in [ ' outputs ' , ' inputs ' ] :
for t in g . tasks :
s = getattr ( t , attr , [ ] )
for k in s :
relpath1 = k . relpath_gen ( cwd_node )
relpath2 = k . relpath_gen ( top_node )
if ( fnmatch . fnmatch ( relpath1 , cmd ) or
fnmatch . fnmatch ( relpath2 , cmd ) ) :
t . position = [ 0 , 0 ]
print ( t . display ( ) )
run_task ( t , k )
found = True
2010-11-03 00:18:05 +03:00
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
if not found :
2018-01-31 12:48:43 +03:00
raise Errors . WafError ( " Unable to find build target matching %s " % cmd )
2010-05-04 12:08:43 +04:00
2011-06-10 05:32:27 +04:00
def rewrite_compile_targets ( ) :
2012-02-09 16:08:31 +04:00
''' cope with the bin/ form of compile target '''
if not Options . options . compile_targets :
return
2011-06-10 05:32:27 +04:00
2012-02-09 16:08:31 +04:00
bld = fake_build_environment ( info = False )
targets = LOCAL_CACHE ( bld , ' TARGET_TYPE ' )
tlist = [ ]
2011-06-10 05:32:27 +04:00
2012-02-09 16:08:31 +04:00
for t in Options . options . compile_targets . split ( ' , ' ) :
if not os . path . islink ( t ) :
tlist . append ( t )
continue
link = os . readlink ( t )
list = link . split ( ' / ' )
for name in [ list [ - 1 ] , ' / ' . join ( list [ - 2 : ] ) ] :
if name in targets :
tlist . append ( name )
continue
Options . options . compile_targets = " , " . join ( tlist )
2011-06-10 05:32:27 +04:00
2010-05-04 12:08:43 +04:00
def wildcard_main ( missing_cmd_fn ) :
2012-02-09 16:08:31 +04:00
''' this replaces main from Scripting, allowing us to override the
behaviour for unknown commands
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
If a unknown command is found , then missing_cmd_fn ( ) is called with
the name of the requested command
'''
Scripting . commands = Options . arg_line [ : ]
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
# rewrite the compile targets to cope with the bin/xx form
rewrite_compile_targets ( )
2011-06-10 05:32:27 +04:00
2012-02-09 16:08:31 +04:00
while Scripting . commands :
x = Scripting . commands . pop ( 0 )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
ini = datetime . datetime . now ( )
if x == ' configure ' :
fun = Scripting . configure
elif x == ' build ' :
fun = Scripting . build
else :
fun = getattr ( Utils . g_module , x , None )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
# this is the new addition on top of main from Scripting.py
if not fun :
missing_cmd_fn ( x )
break
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
ctx = getattr ( Utils . g_module , x + ' _context ' , Utils . Context ) ( )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
if x in [ ' init ' , ' shutdown ' , ' dist ' , ' distclean ' , ' distcheck ' ] :
try :
fun ( ctx )
except TypeError :
fun ( )
else :
fun ( ctx )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
ela = ' '
if not Options . options . progress_bar :
ela = ' ( %s ) ' % Utils . get_elapsed_time ( ini )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
if x != ' init ' and x != ' shutdown ' :
Logs . info ( ' %r finished successfully %s ' % ( x , ela ) )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
if not Scripting . commands and x != ' shutdown ' :
Scripting . commands . append ( ' shutdown ' )
2010-05-04 12:08:43 +04:00
2011-06-10 05:32:27 +04:00
def fake_build_environment ( info = True , flush = False ) :
2012-02-09 16:08:31 +04:00
""" create all the tasks for the project, but do not run the build
return the build context in use """
2018-01-31 12:48:43 +03:00
bld = getattr ( Context . g_module , ' build_context ' , Utils . Context ) ( )
2012-02-09 16:08:31 +04:00
bld = Scripting . check_configured ( bld )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
Options . commands [ ' install ' ] = False
Options . commands [ ' uninstall ' ] = False
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
bld . is_install = 0 # False
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
try :
2018-01-31 12:48:43 +03:00
proj = ConfigSet . ConfigSet ( Options . lockfile )
2012-02-09 16:08:31 +04:00
except IOError :
2018-01-31 12:48:43 +03:00
raise Errors . WafError ( " Project not configured (run ' waf configure ' first) " )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
bld . load_envs ( )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
if info :
Logs . info ( " Waf: Entering directory ` %s ' " % bld . bldnode . abspath ( ) )
2018-01-31 12:48:43 +03:00
bld . add_subdirs ( [ os . path . split ( Context . g_module . root_path ) [ 0 ] ] )
2010-05-04 12:08:43 +04:00
2012-02-09 16:08:31 +04:00
bld . pre_build ( )
if flush :
bld . flush ( )
return bld
2010-05-04 12:08:43 +04:00