In many builds, it is necessary to build the same targets for different purposes: debugging, profiling, optimizations. A system is provided for duplicating the targets easily.
The following code demonstrates how to create a configuration for a new variant named 'debug'
def configure(conf): # default variant conf.env['foo'] = 'bar' # create a new environment for the variant 'debug' env_variant2 = conf.env.copy() conf.set_env_name('debug', env_variant2) env_variant2.set_variant('debug') conf.setenv('debug') conf.env['foo'] = 'bar2'
After running waf configure, the build folders corresponding to the default and to the debug variants are produced:
build/ |-- c4che | |-- build.config.py | |-- debug.cache.py | `-- default.cache.py |-- config.log |-- debug `-- default
To use the debug variant, the environment must be given directly, by default, a copy of the environment named default is set:
def build(bld): prog = bld.new_task_gen() prog.features = 'cxx cprogram' prog.source = 'src1.c' prog.includes = '.' prog.target = 'myprog' prog.env = bld.env_of_name('debug')
Task generators may be copied for different variants easily using the clone method. The parameter is the name of the environment to use (not the variant). The named environments are created during the configuration, as shown in the previous subsection.
def build(bld): prog_in_default = bld.new_task_gen( features = 'cxx cprogram' source = 'src1.c' target = 'myprog') prog_in_debug = prog_in_default.clone('debug')
It is also possible to clone all task generators from a project using a code similar to the following:
for obj in [] + bld.all_task_gen: new_obj = obj.clone('debug')
The variant system is meant for automating the duplication of targets, which are supposed to run in in isolation. As a consequence, it is not specified how linking a program against a library created in a different variant should be processed. The general recommendation is to avoid optimizations and to duplicate the targets as necessary. The following bug tracker entry provides more details http://code.google.com/p/waf/issues/detail?id=171