可以为我在外部文本文件中扩展几个宏吗?

时间:2014-02-09 14:10:48

标签: macros makefile

我有一个相当大而且冗长的基于行的配置文件部分。我想使用此部分作为模板(假设我要预先配置此部分,测试它然后用$(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)宏。

1 个答案:

答案 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 $< >$@