GNU make - 如何为每个目标运行前和后配方 [英] GNU make - how to run pre- and post-recipes for every target

查看:96
本文介绍了GNU make - 如何为每个目标运行前和后配方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

make 中,是否可以为每个目标定义前和后配方?



我想(隐含地)在显式配方的第一行之上插入预配方,然后(隐式地)在显式配方中的最后一行之后插入后配方。


$ b $

解决方案

使用正则表达式插入行很容易, p>您可以创建一个特殊的帮助程序shell,在其输入脚本之前和之后执行所需的前置和后置动作,并告诉 make 使用该shell来执行配方请使用 SHELL )。



此外,如果您使用多行配方,您必须启用< a href =https://www.gnu.org/software/make/manual/html_node/One-Shell.html#One-Shell =nofollow> .ONESHELL 模式。



一个)不会失败
规则,所以你必须加入&& 的命令,或者附加
||退出1 到每个命令的末尾,或者使用 -e
选项运行实际shell。


示例:



预处理后

 #!/ bin / bash 

preaction()
{
echo action
}

postaction()
{
echoPost-action
}

preaction&& ; / bin / bash$ @&&& postaction

makefile

  SHELL =。/ pre-post-shell 

all:Hello Bye

.ONESHELL:

Hello:
@echo Hello
echo您好再次

Bye:
@echo Bye

输出:

  $ make 
b $ b Hello
您好再次
后操作
预执行
Bye
后操作


In make, is it possible to define a pre- and post-recipe for every target?

I want to (implicitly) insert the pre-recipe just above the first line of the explicit recipe and then (implicitly) insert the post-recipe after the last line in the explicit recipe.

It would be pretty easy to do it using regular expressions to insert lines but implicit ones would be so much cleaner.

解决方案

You can create a special helper shell that executes the desired pre- and post- actions before and after its input script and tell make to use that shell for executing the recipes (use the SHELL variable to that end).

Besides, if you are using multiline recipes, you will have to enable the .ONESHELL mode.

Caveat: in this mode a failed command (except the last one) doesn't fail the rule, so you either have to join the commands with &&, or append || exit 1 to the end of each command, or run the real shell with the -e option.

Example:

pre-post-shell

#!/bin/bash

preaction()
{
    echo "Pre-action"
}

postaction()
{
    echo "Post-action"
}

preaction && /bin/bash "$@" && postaction

makefile

SHELL=./pre-post-shell

all: Hello Bye

.ONESHELL:

Hello:
    @echo Hello
    echo Hello again

Bye:
    @echo Bye

Output:

$ make
Pre-action
Hello
Hello again
Post-action
Pre-action
Bye
Post-action

这篇关于GNU make - 如何为每个目标运行前和后配方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆