Executing specific routines before or after the build

User functions may be bound to be executed at two key moments during the build command (callbacks):

Here is how to execute a test after the build is finished:

srcdir = '.'
blddir = 'build'

def set_options(ctx):
	ctx.add_option('--exe', action='store_true', default=False,
		help='execute the program after it is built')

def configure(ctx):
	pass

def pre(ctx): 1
	print("before the build is started")

def post(ctx):
	import Options, Utils
	if Options.options.exe: 2
		Utils.exec_command('/sbin/ldconfig') 3

def build(ctx):
	ctx.add_pre_fun(pre) 4
	ctx.add_post_fun(post)
			

1

The callbacks take the build context as unique parameter ctx

2

Access to the command-line options

3

A common scenario is to call ldconfig after the files are installed.

4

Scheduling the functions for later execution. Remember that in Python, functions are objects too.

Upon execution, the following output will be produced:

$  waf distclean configure build --exe
'distclean' finished successfully (0.002s)
'configure' finished successfully (0.001s)
Waf: Entering directory `/tmp/smallproject/build'
before the build is started
Waf: Leaving directory `/tmp/smallproject/build'
hello
'build' finished successfully (0.008s)