wafwafwaf
wafwafwaf

Reputation: 191

Waf Task should use variant directory

I am creating files in a Task, the example code looks as follows:

from waflib import Task, TaskGen

def build(bld):
    bld(features='write_file')

    class xyz(Task.Task):
        def run(self):
            self.generator.path.get_bld().make_node(self.outputs[0].relpath())

    @TaskGen.feature('write_file')
    def make_tasks(self):
        for x in range(20):
            src = bld.path.find_node('wscript')
            tgt = src.change_ext('.'+str(x))
            tsk = self.create_task('xyz', src=src, tgt=tgt)

Now all files get placed inside the build directory, but I want them to be placed in build\abc. How do I do that? For normal builds, I can use a BuildContext and specify a variant:

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

But I can't get the BuildContext working on that example, and setting variant on a Task.Task does not work.


Update

I update the example based on neuros answer:

A minimal working example with this code looks like this:

from waflib import Task, TaskGen, Configure

Configure.autoconfig = True

def configure(cnf):
    cnf.path.get_src().make_node('a/wscript').write('')

def build(bld):
    bld(features='write_file')

    class xyz(Task.Task):
        def run(self):
            self.generator.path.get_bld().find_or_declare(self.outputs[0].abspath()).write('')

    @TaskGen.feature('write_file')
    def make_tasks(self):
        srcs = bld.path.ant_glob('**/wscript', excl='build')
        for src in srcs:
            build_dir_of_src = src.get_bld().parent
            my_sub_node = build_dir_of_src.make_node('xyz')
            my_sub_node.mkdir()

            tgt_basename = src.name
            tgt = my_sub_node.make_node(tgt_basename)
            tsk = self.create_task('xyz', src=src, tgt=tgt)

The problem is that this creates the following:

build\xyz\wscript
build\a\xyz\wscript

But I want this:

build\xyz\wscript
build\xyz\a\wscript

So I just to create the folder xyz between build and what ever the tgt is. So exactly the behavior of variant in a BuildContext.

Upvotes: 1

Views: 183

Answers (1)

neuro
neuro

Reputation: 15180

When tasks execute, you are already in a variant build dir. To control the outputs of a task you have to use the waflib.Node class API. In your example, change_ext get the source build directory equivalent and change the extension. To insert a subdir:

# [...]

build_dir_of_src = src.get_bld().parent
my_sub_node = build_dir_of_src.make_node("my_sub_dir")
my_sub_node.mkdir()

tgt_basename = src.change_ext('.' + str(x)).name
tgt = my_sub_node.make_node(tgt_basename)

# [...]

If you want to insert a "variant style" directory, you can use bld.bldnode (untested but you see the point, use bld.bldnode):

def get_my_bld(bld, src_node):

     variant_like_dirname = "xyz"
     my_bld_node = bld.bldnode.make_node(variant_like_dirname)
     my_bld_node.mkdir()

     rp = src_node.get_bld().relpath(bld.bldnode)
     my_bld_target = my_bld_node.make_node(rp)

     return my_bld_tgt

 # [...]

 tgt = get_my_bld(bld, src)

 # [...]

Upvotes: 1

Related Questions