Search code examples
taskgoogletestwaf

waf using recurse and keep tasks in order


I'm using waf to build and run a gtest.

If I put everything in my root wscript like the following it works.

def build( bld ):
    # build hello world lib
    bld.recurse("src/cpp/hw")

    # build hello world gtest app
    cppUnitTest = bld.program(
      target = 'test_run',
      source = [ 'test/unit/test_run.cpp' ],
      use = [ 'HW_static', 'GTEST_static' ],
      lib = [ 'pthread' ],
      includes = [ bld.env.LIBGTEST_INCLUDES ],
    )

    # run c++ unit test
    bld(
      dependsOn = [cppUnitTest],
      name = 'c++ unit test',
      rule = './test_run',
      cwd = bld.path.find_dir( 'build' )
    )

It builds a lib called HW (Hello World), it builds an application called test_run and than it will run that test application.

But if I want the application 'test_run' to be build using recurse I don't get it to work. This is one of my attempts so far.

def build( bld ):
    global cppUnitTest

    # build hello world lib
    bld.recurse("src/cpp/hw")

    # build hello world gtest app
    bld.recurse("tests/unit")

    # run c++ unit test
    bld(
      dependsOn = [cppUnitTest],
      name = 'c++ unit test',
      rule = './test_run',
      cwd = bld.path.find_dir( 'build' )
    )

and the wscript_build file from the subfolder looks like that

cppUnitTest = bld.program(
  target = 'test_run',
  source = [ 'test_run.cpp' ],
  use = [ 'HW_static', 'GTEST_static' ],
  lib = [ 'pthread' ],
  includes = [ bld.env.LIBGTEST_INCLUDES ],
)

EDIT: As you can see below I found a solution especially for test applications which is fine. But I would be interested in a more general solution how to make sure certain build steps are running in the right order waiting for each other. e.g. if I had an application to build which I than use to generate some code which is used to build an other application and so on.


Solution

  • So for the sole purpose of running a unit test I found a very nice and simple solution. I used the build in waf_unit_test module. Simply adding the import like this ...

    from waflib.Tools import waf_unit_test
    

    ... than adding the attribute "features" with the value "test" to all my testing programs like that ...

    bld.program(
      features = 'test'
      target = 'test_run',
      source = [ 'test_run.cpp' ],
      use = [ 'HW_static', 'GTEST_static' ],
      lib = [ 'pthread' ],
      includes = [ bld.env.LIBGTEST_INCLUDES ],
    )
    

    ... and remove the dependsOn and cppUnitTest instructions. Unfortunately like this the output of my testing applications got suppressed but to get a feedback if the tests where successful one can add the following callback within the build function.

    bld.add_post_fun(waf_unit_test.summary)
    

    So that is ok for gtest applications but I need a more general solution to keep tasks in order. See edited question.