Search code examples
pythonwaf

Tell waf to wait for task to finish before staring next task


I have two Tasks in waf and they need to be exectued in the correct order, and the second task, has to wait until the first task has finished.

To show that it's behaving the way I expected it to work, I wait inside the tasks, in the first task (t_1) 4 seconds and in the second task (t_2) 1 second. And the second task finishes first. This can be seen, as the folders I create after waiting have timestamps t_2 < t_1.

In one Question: How can I tell waf, that t_2 is exectued after t_1 finished successfully?


  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 start t_1 && python -c "import time; time.sleep(4)" && echo end t_1 && mkdir t_1'
        color = 'RED'
    
    
    @TaskGen.feature('t_1')
    @TaskGen.after('apply_link')
    @TaskGen.before('t_2')
    def add_t_1_task(self):
        self.create_task('t_1')
    
    
    class t_2(Task.Task):
        always_run = True
        run_str = 'echo start t_2 && python -c "import time; time.sleep(1)" && echo end t_2 && mkdir t_2'
        color = 'RED'
    
    
    @TaskGen.feature('t_2')
    @TaskGen.after('apply_link', 't_1')
    def add_t_2_task(self):
        self.create_task('t_2')
    

Solution

  • You have to specify the task which has to run before (in that example t_1) in after attribute of the task which should "wait" for the the other task to finish (in that example (t_2). It is specified like this: after = ['t_1']

    The documentation to this can be found here in section build order constraints in the waf book.

    The documentation that explains generic task generator is §9.3 in the current waf book (waf 2.0), especially §9.3.4 explains the task generator execution order.


    The complete example MWE looks then like this:

    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 = 'date && echo 1 && echo start t_1 && python -c "import time; time.sleep(4)" && echo end t_1 && mkdir t_1 && date && echo 1'
        color = 'RED'
    
    
    @TaskGen.feature('t_1')
    @TaskGen.after('apply_link')
    @TaskGen.before('t_2')
    def add_t_1_task(self):
        self.create_task('t_1')
    
    
    class t_2(Task.Task):
        always_run = True
        after = ['t_1'] # <---------------------------- **The problems solution**
        run_str = 'date && echo 1 && echo start t_2 && python -c "import time; time.sleep(1)" && echo end t_2 && mkdir t_2 && date && echo 2'
        color = 'RED'
    
    
    @TaskGen.feature('t_2')
    @TaskGen.after('apply_link', 't_1')
    def add_t_2_task(self):
        self.create_task('t_2')