Makefile 规则依赖项列表中的修饰符

发布于 2024-09-01 03:13:20 字数 205 浏览 5 评论 0原文

问题很简单。我正在尝试编写一个规则,给定所需文件的名称将能够定制其依赖项。

假设我有两个程序:calc_foo 和 calc_bar,它们生成一个文件,其输出取决于参数。我的目标名称为“target_*_*”;例如,“target_foo_1”将通过运行“./calc_foo 1”生成。

问题是,如何编写一个 makefile 来生成两个程序的一系列参数的输出?

The problem is fairly simple. I am trying to write a rule, that given the name of the required file will be able to tailor its dependencies.

Let's say I have two programs: calc_foo and calc_bar and they generate a file with output dependent on the parameter. My target would have a name 'target_*_*'; for example, 'target_foo_1' would be generated by running './calc_foo 1'.

The question is, how to write a makefile that would generate outputs of the two programs for a range of parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無心 2024-09-08 03:13:20

如果只有几个程序,您可以为每个程序制定一个规则:

target_foo_%:
    ./calc_foo $*

如果您想运行一个带有参数列表的程序:

foo_parameter_list = 1 2 green Thursday 23 bismuth

foo_targets = $(addprefix target_foo_,$(foo_parameter_list))

all: $(foo_targets)

如果您想为每个程序使用一组不同的参数,但有一些共同点,您可以将常见的分开:

common_parameter_list = 1 2 green Thursday

foo_parameter_list = $(common_parameters) 23 bismuth

bar_parameter_list = $(common_parameters) 46 111

如果事实证明您的程序比您想象的要多,但仍然想使用此方法,您只想将其自动化:

# add programs here
PROGRAMS = foo bar baz

# You still have to tailor the parameter lists by hand
foo_parameter_list = 1 2 green Thursday 23 bismuth

# everything from here on can be left alone

define PROGRAM_template

$(1)_targets = $(addprefix target_$(1)_,$($(1)_parameter_list))

target_$(1)_%:
    ./calc_$(1) $*

all: $(1)_targets

endef

$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))

If there are just a few programs, you can have a rule for each one:

target_foo_%:
    ./calc_foo $*

If you want to run a program with a list of parameters:

foo_parameter_list = 1 2 green Thursday 23 bismuth

foo_targets = $(addprefix target_foo_,$(foo_parameter_list))

all: $(foo_targets)

If you want a different set of parameters for each program, but with some in common, you can separate the common ones:

common_parameter_list = 1 2 green Thursday

foo_parameter_list = $(common_parameters) 23 bismuth

bar_parameter_list = $(common_parameters) 46 111

If it turns out you have more programs than you thought, but still want to use this method, you just want to automate it:

# add programs here
PROGRAMS = foo bar baz

# You still have to tailor the parameter lists by hand
foo_parameter_list = 1 2 green Thursday 23 bismuth

# everything from here on can be left alone

define PROGRAM_template

$(1)_targets = $(addprefix target_$(1)_,$($(1)_parameter_list))

target_$(1)_%:
    ./calc_$(1) $*

all: $(1)_targets

endef

$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
挽你眉间 2024-09-08 03:13:20

这似乎或多或少地满足了您的要求 - 假设您正在使用 GNU Make。

Makefile

BAR_out  = target_bar_
BAR_list = 1 2 3 4 5 6 7 8 9
BAR_targets = $(addprefix ${BAR_out},${BAR_list})

FOO_out  = target_foo_
FOO_list = 11 12 13 14 15 16 17 18 19
FOO_targets = $(addprefix ${FOO_out},${FOO_list})

all: ${BAR_targets} ${FOO_targets}

${BAR_targets}:
    calc_bar $(subst ${BAR_out},,$@)

${FOO_targets}:
    calc_foo $(subst ${FOO_out},,$@)

它可能可以被清理,但我用以下命令测试了它:

calc_bar

echo "BAR $@" | tee target_bar_$@
sleep $@

calc_foo

echo "FOO $@" | tee target_foo_$@
sleep $@

显然,如果您想要不同的列表,您可以在命令行上指定:

make -j4 FOO_LIST="1 2 3 4 5 6 33"

This seems to do more or less what you are requesting - assuming you are using GNU Make.

Makefile

BAR_out  = target_bar_
BAR_list = 1 2 3 4 5 6 7 8 9
BAR_targets = $(addprefix ${BAR_out},${BAR_list})

FOO_out  = target_foo_
FOO_list = 11 12 13 14 15 16 17 18 19
FOO_targets = $(addprefix ${FOO_out},${FOO_list})

all: ${BAR_targets} ${FOO_targets}

${BAR_targets}:
    calc_bar $(subst ${BAR_out},,$@)

${FOO_targets}:
    calc_foo $(subst ${FOO_out},,$@)

It can probably be cleaned up, but I tested it with the commands:

calc_bar

echo "BAR $@" | tee target_bar_$@
sleep $@

calc_foo

echo "FOO $@" | tee target_foo_$@
sleep $@

Clearly, if you want a different list, you can specify that on the command line:

make -j4 FOO_LIST="1 2 3 4 5 6 33"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文