基于特征的一个输入的多个输出

Multiple outputs from one input based on features

我想基于相同的输入构建多个输出,例如来自精灵的十六进制和二进制。 我将在 wscript 的不同位置多次执行此操作,因此我想将其包装在一个功能中。

理想情况下是这样的:

bld(features="hex", source="output.elf")
bld(features="bin", source="output.elf")

我将如何实施它?

如果您的 elf 文件总是具有相同的扩展名,您可以简单地使用:

# untested, naive code

from waflib import TaskGen

@TaskGen.extension('.elf')
def process_elf(self, node): # <- self = task gen, node is the current input node
    if "bin" in self.features:
        bin_node = node.change_ext('.bin')
        self.create_task('make_bin_task', node, bin_node)

    if "hex" in self.features:
        hex_node = node.change_ext('.hex')
        self.create_task('make_hex_task', node, hex_node)

如果没有,您必须像这样定义您想要的功能:

from waflib import TaskGen

@Taskgen.feature("hex", "bin") # <- attach method features hex AND bin
@TaskGen.before('process_source')

def transform_source(self):  # <- here self = task generator
    self.inputs = self.to_nodes(getattr(self, 'source', []))
    self.meths.remove('process_source') # <- to disable the standard process_source

@Taskgen.feature("hex") # <- attach method to feature hex
@TaskGen.after('transform_source')
def process_hex(self):
    for i in self.inputs:
        self.create_task("make_hex_task", i, i.change_ext(".hex"))

@Taskgen.feature("bin") # <- attach method to feature bin
@TaskGen.after('transform_source')
def process_hex(self):
    for i in self.inputs:
        self.create_task("make_bin_task", i, i.change_ext(".bin"))

您必须编写 make_elf_taskmake_bin_task 这两个任务。你应该把所有这些放在一个单独的 python 文件中并制作一个 "plugin".

你也可以定义一个"shortcut"来调用:

def build(bld):
    bld.make_bin(source = "output.elf")
    bld.make_hex(source = "output.elf")

    bld(features = "hex bin", source = "output.elf") # when both needed in the same place

像那样:

from waflib.Configure import conf

@conf
def make_bin(self, *k, **kw): # <- here self = build context

    kw["features"] = "bin" # <- you can add bin to existing features kw
    return self(*k, **kw)

@conf
def make_hex(self, *k, **kw):

    kw["features"] = "hex"
    return self(*k, **kw)