在其他waf任务中使用waf Task的输出

时间:2017-12-04 10:10:59

标签: python waf

我想在另一个waf任务中使用waf任务的目标作为,但这不能按预期工作。

一个简单但完整的例子: 我添加了两个任务(t_1t_2)我想添加为功能,并在beforeafter之后添加了约束。

  • 在任务t_1中,我使用src=link_task.outputs[0]作为src,将link_task.outputs[0].change_ext('.txt')作为tgt作为任务。

  • 但在t_2中,我想使用t_1的输出/目标作为输入。我以为我可以通过引用self.t_1.ouputs来获取它,但这似乎是错误的。为什么这适用于t_1任务的apply_link,而t_1则不适用?

  1. MWE: wscript

    from waflib import Context, Options
    from waflib import Task, TaskGen
    from waflib.Tools.compiler_c import c_compiler
    
    def options(opt):
            opt.load('compiler_c')
    
    def configure(cnf):
            cnf.load('compiler_c')
    
    def build(bld):
            bld.program(features=['t_1', 't_2'], source='main.c', target="abc")
    
    class t_1(Task.Task):
        always_run = True
        run_str = 'echo ${SRC} && echo hello t_1 > ${TGT}'
        color = 'RED'
    
    
    @TaskGen.feature('t_1')
    @TaskGen.after('apply_link')
    @TaskGen.before('t_2')
    def add_t_1_task(self):
        try:
            link_task = self.link_task
        except AttributeError as err:
            print err
            return
        self.create_task('t_1', src=link_task.outputs[0], tgt=link_task.outputs[0].change_ext('.txt'))
    
    
    class t_2(Task.Task):
        always_run = True
        run_str = 'echo ${SRC}'
        color = 'RED'
    
    
    @TaskGen.feature('t_2')
    @TaskGen.after('apply_link', 't_1')
    def add_t_2_task(self):
        try:
            t_1 = self.t_1
        except AttributeError as err:
            print err
            return
        self.create_task('t_2', src=t_1.outputs[0])
    
    1. 正在运行python waf configure build会导致:

      user@laptop /cygdrive/c/work
      $ python waf-1.9.13 configure clean build
      Setting top to                           : /cygdrive/c/work
      Setting out to                           : /cygdrive/c/work/build
      Checking for 'gcc' (C compiler)          : /usr/bin/gcc
      'configure' finished successfully (0.150s)
      'clean' finished successfully (0.010s)
      Waf: Entering directory `/cygdrive/c/work/build'
      'task_gen' object has no attribute 't_1' <===================== How to get this working
      [1/3] Compiling main.c
      [2/3] Linking build/abc.exe
      [3/3] Compiling build/abc.exe
      abc.exe
      
      Waf: Leaving directory `/cygdrive/c/work/build'
      'build' finished successfully (0.270s)
      

1 个答案:

答案 0 :(得分:2)

beforeafter装饰器适用于任务生成器方法,而非任务。你应该:

@TaskGen.feature('t_1')
@TaskGen.after('apply_link')
# correction: no need to use @TaskGen.before('add_t_2_task')
def add_t_1_task(self):

    # correction: you have to define self.t_1

    self.t_1 = self.create_task(
        't_1',
        self.link_task.outputs[0], 
        self.link_task.outputs[0].change_ext('.txt')
    )

@TaskGen.feature('t_2')
@TaskGen.after('apply_link', 'add_t_1_task') # correction: use method name
def add_t_2_task(self):

    # add_t-2_task is executed after add_t_1_task, so using self.t_1 is ok

    print "t_1", self.t_1

顺便说一下,不要使用always_run=True因为它击败了waf主要功能之一,即只构建你需要构建的东西:)