在Makefile中自定义SHELL(设置SHELL = my_shell.sh) [英] Customize SHELL in Makefile (set SHELL=my_shell.sh)

查看:243
本文介绍了在Makefile中自定义SHELL(设置SHELL = my_shell.sh)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Makefile自定义外壳,但是我遇到了麻烦.这是MWE.我有一个Makefile,

I would like to customize the shell for a Makefile, but I am running into trouble. Here is a MWE. I have a Makefile,

SHELL=./my_shell.sh

all: abc def

abc:
        touch abc

def:
        touch def

clean:
        rm -f abc def

和一个简单的自定义脚本my_shell.sh.

and a simple custom script, my_shell.sh.

#!/bin/bash
eval $*

我确保事先运行chmod +x my_shell.sh.然后,当我键入make时,我得到了一个错误.

I made sure to run chmod +x my_shell.sh beforehand. Then, when I typed make, I got an error.

make[3]: Entering directory `<CONFIDENTIAL>'
touch abc
./my_shell.sh: line 2: eval: -c: invalid option
eval: usage: eval [arg ...]
make[3]: *** [abc] Error 2
make[3]: Leaving directory `<CONFIDENTIAL>`

我一直在努力摆脱-c选项.设置.SHELLFLAGS=''似乎无效.

I have been struggling to get rid of the -c option. Setting .SHELLFLAGS='' doesn't seem to work.

推荐答案

首先,如果要将make变量设置为空使用

First, if you want to set a make variable to empty use

.SHELLFLAGS =

通过添加引号,您实际上已将变量设置为文字字符串''(make不是shell,也不会进行shell引号剥离).

By adding the quotes you've actually set the variable to the literal string '' (make is not the shell and does not do shell quote stripping).

第二,.SHELLFLAGS变量是在GNU make 3.82中添加的,因此,如果您的版本比该版本旧,则它将不起作用.如果您使用的是3.82或更高版本,那么它确实可以工作(我刚刚尝试过).

Second, the .SHELLFLAGS variable was added in GNU make 3.82 so if your version is older than that, it won't work. If you have 3.82 or newer then it does work (I just tried it).

最后,正如让·弗朗索瓦(Jean-François)指出的那样,如果您的命令转义了空格,您将遇到问题.但是,他使用"$@"的解决方案无法按原样工作,因为这样,整个命令就被视为一个字符串.

Lastly, as Jean-François points out you will problems if your command has escaped spaces. However, his solution of using "$@" cannot work as-is, because then the entire command is seen as a single string.

该脚本的更可靠实现是:

A more reliable implementation of the script would be:

#!/bin/bash
exec /bin/bash "$@"

然后不要修改.SHELLFLAGS.否则,请将.SHELLFLAGS设置为空并创建脚本:

And then do not modify .SHELLFLAGS. Or else, do set .SHELLFLAGS to empty and make your script:

#!/bin/bash
exec /bin/bash -c "$@"

这篇关于在Makefile中自定义SHELL(设置SHELL = my_shell.sh)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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