2010-10-14 09:24:50 +04:00
# encoding: utf-8
# Thomas Nagy, 2006-2010 (ita)
"""
Add a pre - build hook to remove all build files
which do not have a corresponding target
This can be used for example to remove the targets
that have changed name without performing
a full ' waf clean '
Of course , it will only work if there are no dynamically generated
nodes / tasks , in which case the method will have to be modified
to exclude some folders for example .
"""
2018-07-03 13:06:40 +03:00
from waflib import Logs , Build , Options , Utils , Errors
import os
from wafsamba import samba_utils
2010-10-14 09:24:50 +04:00
from Runner import Parallel
old_refill_task_list = Parallel . refill_task_list
def replace_refill_task_list ( self ) :
''' replacement for refill_task_list() that deletes stale files '''
iit = old_refill_task_list ( self )
bld = self . bld
if not getattr ( bld , ' new_rules ' , False ) :
# we only need to check for stale files if the build rules changed
return iit
if Options . options . compile_targets :
# not safe when --target is used
return iit
# execute only once
if getattr ( self , ' cleanup_done ' , False ) :
return iit
self . cleanup_done = True
def group_name ( g ) :
tm = self . bld . task_manager
return [ x for x in tm . groups_names if id ( tm . groups_names [ x ] ) == id ( g ) ] [ 0 ]
bin_base = bld . bldnode . abspath ( )
bin_base_len = len ( bin_base )
# paranoia
if bin_base [ - 4 : ] != ' /bin ' :
2018-01-31 12:48:43 +03:00
raise Errors . WafError ( " Invalid bin base: %s " % bin_base )
2012-09-27 20:30:47 +04:00
2010-10-14 09:24:50 +04:00
# obtain the expected list of files
expected = [ ]
for i in range ( len ( bld . task_manager . groups ) ) :
g = bld . task_manager . groups [ i ]
tasks = g . tasks_gen
for x in tasks :
try :
if getattr ( x , ' target ' ) :
tlist = samba_utils . TO_LIST ( getattr ( x , ' target ' ) )
2011-10-19 09:34:32 +04:00
ttype = getattr ( x , ' samba_type ' , None )
task_list = getattr ( x , ' compiled_tasks ' , [ ] )
if task_list :
# this gets all of the .o files, including the task
# ids, so foo.c maps to foo_3.o for idx=3
for tsk in task_list :
for output in tsk . outputs :
objpath = os . path . normpath ( output . abspath ( bld . env ) )
expected . append ( objpath )
2010-10-14 09:24:50 +04:00
for t in tlist :
2021-08-21 00:05:57 +03:00
if ttype in [ ' LIBRARY ' , ' PLUGIN ' , ' MODULE ' ] :
2011-10-19 09:34:32 +04:00
t = samba_utils . apply_pattern ( t , bld . env . shlib_PATTERN )
if ttype == ' PYTHON ' :
t = samba_utils . apply_pattern ( t , bld . env . pyext_PATTERN )
2010-10-14 09:24:50 +04:00
p = os . path . join ( x . path . abspath ( bld . env ) , t )
p = os . path . normpath ( p )
expected . append ( p )
for n in x . allnodes :
p = n . abspath ( bld . env )
if p [ 0 : bin_base_len ] == bin_base :
expected . append ( p )
except :
pass
for root , dirs , files in os . walk ( bin_base ) :
for f in files :
p = root + ' / ' + f
if os . path . islink ( p ) :
2011-02-28 10:53:36 +03:00
link = os . readlink ( p )
if link [ 0 : bin_base_len ] == bin_base :
p = link
2010-10-14 09:24:50 +04:00
if f in [ ' config.h ' ] :
continue
2011-10-19 09:34:32 +04:00
( froot , fext ) = os . path . splitext ( f )
if fext not in [ ' .c ' , ' .h ' , ' .so ' , ' .o ' ] :
2010-10-14 09:24:50 +04:00
continue
if f [ - 7 : ] == ' .inst.h ' :
continue
if p . find ( " /.conf " ) != - 1 :
continue
2011-10-19 09:34:32 +04:00
if not p in expected and os . path . exists ( p ) :
2010-10-14 09:24:50 +04:00
Logs . warn ( " Removing stale file: %s " % p )
os . unlink ( p )
return iit
def AUTOCLEANUP_STALE_FILES ( bld ) :
""" automatically clean up any files in bin that shouldn ' t be there """
2023-08-29 11:48:14 +03:00
global old_refill_task_list
2010-10-14 09:24:50 +04:00
old_refill_task_list = Parallel . refill_task_list
Parallel . refill_task_list = replace_refill_task_list
Parallel . bld = bld
Build . BuildContext . AUTOCLEANUP_STALE_FILES = AUTOCLEANUP_STALE_FILES