2

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')
    
wafwafwaf
  • 191
  • 9

1 Answers1

2

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')
neuro
  • 14,948
  • 3
  • 36
  • 59
wafwafwaf
  • 191
  • 9
  • 1
    `@TaskGen.before('t_2')` is wrong. It shoul be `@TaskGen.before('add_t_2_task')`. Moreover it is not necessary. Tasks `t_1` and `t_2` can be created in any order in your example. What is important is the execution order. – neuro Jan 09 '18 at 08:00
  • Thanks for the comment. Where can I find the documentation, that I need to put the task generator methods not tasks as constraint? But my solution to the problem of using `after = ['t_1']` in `class t_2(Task.Task):` is correct? – wafwafwaf Jan 17 '18 at 07:18
  • @neuro sorry for asking again, but as you seem very experienced with waf, I would very much appriciate if you would tell me, where to find this in the documentation? – wafwafwaf Jan 19 '18 at 08:34
  • I added the reference for task method execution order, ie §9.3.4 in current waf book. §9.3.6 might be of interest too, it explains how to set abstract dependencies between taskgens. – neuro Jan 22 '18 at 10:21
  • Thanks for updating your already perfect answer - I have already read these section, but it seems I have missed something to fully understand, and therefore I will read it again! – wafwafwaf Jan 22 '18 at 10:39