Search code examples
pythonwaf

Use different source in waf build variant


Based on this it is possible to build different variants of a project in waf to different output directories 7.2.2. Changing the output directory / Configuration sets for variants (https://waf.io/book/#_custom_build_outputs)

But I do not understand how to include different files or directories based on the variant. I modified the example from the waf-book like this, but I am missing how to build a different sourcefile or include files from different directories.

def configure(ctx):
    pass

def build(ctx):
    if not ctx.variant:
        ctx.fatal('call "waf a" or "waf b", and try "waf --help"')
    # for variant "a" it should build "a.c" and fpr "b" it should build "b.c"
    # for a: bld.program(source='a.c', target='app', includes='.')
    # for b: bld.program(source='b.c', target='app', includes='.')

from waflib.Build import BuildContext
class a(BuildContext):
    cmd = 'a'
    variant = 'a'

from waflib.Build import BuildContext
class b(BuildContext):
    cmd = 'b'
    variant = 'b'

Solution

  • You run python waf configure to configure the project. After that a variant is build with the commands build_a and build_b

    def configure(ctx):
        load('compiler_c')
    
    def build(bld):
        if not bld.variant:
            bld.fatal('call "waf build_a" or "waf build_b", and try "waf --help"')
    
        if bld.variant == 'a':
            bld.program(source='a.c', target='app', includes='.')
        elif bld.variant == 'b':
            bld.program(source='b.c', target='app', includes='.')
        else:
            bld.fatal('"')
    
    # create build and clean commands for each build context
    from waflib.Build import BuildContext, CleanContext
    
    for x in 'a b'.split():
        for y in (BuildContext, CleanContext):
            name = y.__name__.replace('Context','').lower()
            class tmp(y):
                __doc__ = '''executes the {} of {}'''.format(name, x)
                cmd = name + '_' + x
                variant = x