build - 如何声明测试脚本对构建程序的WAF依赖?

标签 build dependencies waf

我正在努力寻找定义测试脚本依赖关系的正确方法 在 waf 在同一构建过程中构建的二进制程序上。 以下是 wscript 的最小示例:

from waflib.Tools import waf_unit_test

def options(opt):
    opt.load("compiler_cxx python waf_unit_test")

def configure(cnf):
    cnf.load("compiler_cxx python waf_unit_test")


def build(bld):
    bld.add_post_fun(waf_unit_test.summary)
    bld.options.clear_failed_tests= True

    bld(features= "cxx cxxprogram",
        target= "foo",
        source= "foo.cpp")

    tg= bld(features= "test_scripts",
        test_scripts_source= "fooTest.py",
        test_scripts_template= "${PYTHON} ${SRC}")
    tg.post()
    tg.tasks[0].inputs.append(bld.path.find_or_declare("foo"))

我想表达waf应该构建程序foo。 然后,如果程序 foo 自上次构建运行以来发生了更改,脚本 fooTest.py 将检查该程序的执行情况。上面的 wscript 有效:

Waf: Entering directory `/home/x/tmp/exe_depend/build'
[1/3] Compiling foo.cpp
[2/3] Linking build/foo
[3/3] Processing utest: fooTest.py build/foo
Waf: Leaving directory `/home/x/tmp/exe_depend/build'
execution summary 
  tests that pass 1/1 
    /home/x/tmp/exe_depend/fooTest.py 
  tests that fail 0/1 
'build' finished successfully (0.054s)

原则上,上面的wscript满足了我的需求,但它看起来像黑客一样丑陋,而且摆弄任务生成器tg似乎是错误的。你们有人知道一个顺利的解决方案吗?

最佳答案

嗯,waf_unit_test.py 中没有太多钩子(Hook)。最好的方法是通过制作自己的工具并将两行代码放入其中来扩展它。类似这样的事情:

# in file my_unit_test.py

def options(opt):
    opt.load("waf_unit_test") # we extend it, no need to recode

def configure(conf):
    conf.load("waf_unit_test") # we extend it, no need to recode

@feature('test_scripts')
@after_method('make_interpreted_test') # we execute after task creation
def process_test_scripts_dependencies(self):
    try:
        self.test_scripts_deps
    except AttributeError:
        return

    for task in self.tasks: # there can be many scripts
        task.set_inputs(self.to_nodes(self.test_scripts_deps))

使用它:

def options(opt):
    opt.load("compiler_cxx python my_unit_test")

def configure(cnf):
    cnf.load("compiler_cxx python my_unit_test")


def build(bld):
    bld.add_post_fun(waf_unit_test.summary)
    bld.options.clear_failed_tests= True

    myprogram = "foo"

    bld(
        features = "cxx cxxprogram",
        target = myprogram,
        source = "foo.cpp",
    )

    bld(
        features = "test_scripts",
        test_scripts_deps = myprogram,
        test_scripts_source = "fooTest.py",
        test_scripts_template = "${PYTHON} ${SRC}",
    )

当然,您可以更进一步,重写一些适合您需要的东西:)

关于build - 如何声明测试脚本对构建程序的WAF依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61479068/

相关文章:

java - 如何更改 Sublime 中 Java 的路径?

java - 从 ant 构建文件中以编程方式检索某些任务

Android/Gradle : Where is this dependency embedded?

c++ - 通过使用特定于平台的项目文件或使用项目生成器来构建自动化?

translation - waf - 更新并生成翻译 pot 文件

c# - 在 .net core 3.1 类库项目中的构建时设置具有某个值的类私有(private)字符串参数

c++ - 将 3rd 方头文件放在源代码树中的什么位置?

.net - 在 C# dll 中缓存数据的最佳方法是什么?

visual-studio - MS C++ 2010 和 mspdb100.dll

c++ - 将 NS3 返回/退出代码(通过 waf)捕获为 Bash 脚本中的变量