我有一个相当大而且冗长的基于行的配置文件部分。我想使用此部分作为模板(假设我要预先配置此部分,测试它然后用$(make)
$(macros)
替换实际值),替换关键参数(很少,真的)有效地“克隆”这个“模板”,几个自定义参数到工作配置文件。 make
可以在描述的情况下为我做的工作吗?
请耐心等待我,我真的是make
外行,甚至不确定在这种情况下它是否是正确的工具。
我正在预配置和测试类似的内容:
<section0>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with trailing0
</section0>
我想知道如果将上面标有尾随零的转换标记转换为宏:
<$(section)>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with $(trailing)
</$(section)>
...想知道我可以利用make
生成预制配置的克隆,这些配置可以用我的数据代替宏来定制:
<section42>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with trailing42
</section42>
<foo>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with bar
</foo>
假设"section42"
,"foo"
和"trailing42"
,"bar"
分别替代$(section)
,$(trailing)
宏。
答案 0 :(得分:1)
您可以在makefile中使用m4
预处理器来完成以下操作:在模板文件中展开宏:
M4可以称为“模板语言”,“宏语言”或“预处理器语言”。名称“m4”也指以这种语言处理文本的程序:这个“预处理器”或“宏处理器”将m4模板作为输入,并在对任何嵌入式指令(称为宏)执行操作后将其发送到输出。 / p>
创建名为section.m4
的文件:
$ cat section.m4
<section0>
contains a lot of settings
which were tested and should
be exactly the same in every copy
except marked with trailing0
</section0>
在makefile中有一条规则来扩展该模板中的宏以生成section.cfg
:
section.cfg : section.m4
m4 -Dsection0=foo -Dtrailing0=bar $< >$@