Cloning task generators

A cloning scheme is provided for duplicating task generators for several variants easily. A general design pattern is to duplicate the task generators for the desired variants immediately before the build starts. Here is an example, also available in the folder demos/cxx of the Waf distribution.

srcdir = '.'
blddir = 'build'

import Options

def set_options(opt):
	opt.add_option('--build_kind', action='store', default='debug,release', help='build the selected variants') 1

def configure(conf): 2
	for x in ['debug', 'release']:
		env = conf.env.copy()
		env.set_variant(x)
		conf.set_env_name(x, env)

def build(bld):

	bld(rule='touch ${TGT}', target='foo.txt') 3

	for obj in bld.all_task_gen[:]: 4
		for x in ['debug', 'release']:
			cloned_obj = obj.clone(x) 5
			kind = Options.options.build_kind
			if kind.find(x) < 0:6
				cloned_obj.posted = True
		obj.posted = True 7
			

1

Add a command-line option for enabling or disabling the release and debug builds

2

The configuration will create the release and debug configuration sets, bound to a variant of the same names

3

Targets are declared normally for the default variant

4

A copy of the existing task generators is created to avoid the creation of an infinite loop (new task generator instances get added to that list)

5

Clone a task generator for the configuration set debug or release. Making task generator clones is a cheap operation compared to duplicating tasks.

6

Look at the command-line arguments, and disable the unwanted variant(s)

7

Disable the original task generator for the default configuration set (do not use it).

Some task generators are use indexed attributes to generate unique values which may cause unnecessary rebuilds if the scripts change. To avoid problems, it is a best practice to create the task generators for the default configuration set first. Also, the method clone is not a substitute for creating instances for lots of nearly identical task generators. In such a situation, it will be better to use one task generator to create lots of tasks. As a reminder, creating task generator clones for the same variant will lead to build errors.

[Warning]Warning
Do not create task generator clones for the same variant or for the same configuration set.