Chapter 9. Advanced scenarios

Table of Contents

Simple file transformations
Same targets, different configurations (variants)
Building the compiler first
Writing the output of a program into a header
A compiler producing source files with names unknown in advance
A task without any file dependency

This chapter provides examples for advanced scenarios to demonstrate the practical use of the Waf library.

Simple file transformations

The Waf tool misc contains various routines to ease file manipulations such as substituting parameters or executing custom compilers. The objective of this section is to illustrate the principles of the current apis.

File substitutions

The following example illustrates how to produce a pkg-config file from a template:

def configure(conf):
	conf.check_tool('misc')

def build(bld):
	obj = bld.new_task_gen('subst')
	obj.source = 'test.pc.in'
	obj.target = 'test.pc'
	obj.dict   = {'LIBS': '-lm', 'CFLAGS': '-Wall', 'VERSION': '1.0'}

	# any kind of map will work, for example
	# obj.dict = obj.env
				

The variables must be declared in the template file by enclosing the names between @ characters (m4 syntax).

Name: test
Description: @CFLAGS@
Version: @VERSION@
Libs: -L${libdir} @LIBS@
Cflags: -I${includedir} @CFLAGS@
				

The default substitution function may be replaced by changing the attribute func of the task generator.

Compiler output

The command-output system enables execution of system commands without requiring a shell. The following example illustrates how to execute a script present in the source directory each time it is executed.

import time
import misc 1
def build(bld):
	out1 = bld.new_task_gen('command-output')
	out1.stdout = 'test1' 2
	out1.stdin = 'wscript_build' 3
	#out1.command_is_external = False
	out1.command = 'some-script' 4
	out1.argv = 5 ['--output-md5', misc.output_file('test1.md5')6]
	out1.vars = 'COINCOIN'
	out1.env['COINCOIN'] = time.time() 7
				

1

misc is the Waf module providing the command-output features

2

The standard output will be redirected to the file test1

3

The file 'wscript_build' will be passed to the standard input

4

The program to execute is assumed to be present in the directory of the wscript file, unless command_is_external is set to True

5

Additional parameters are provided in the argv attribute, as a list.

6

Parameters representing files must be added through the special classes input_file, output_file, input_dir and output_dir to resolve the paths properly

7

An additional dependency is added on the time, the task will be executed whenever the time changes