Makefile $(command) not working but `command` did

本小妞迷上赌 提交于 2019-12-01 16:44:43

问题


The thing is when I was writing a Makefile for my project, when I needed to detect the current branch name, in a make rule I did this :

check_branch:
    if [ "$(git rev-parse --abbrev-ref HEAD)" == "master" ]; then \
    echo "In master"
    else \
    echo "Not in master"; \
    fi

When I called make check_branch, the "$(git rev-parse --abbrev-ref HEAD)" didn't work, it returned "" empty string. But instead when I changed $() to ` `, it worked perfectly.

check_branch:
    if [ "`git rev-parse --abbrev-ref HEAD`" == "master" ]; then \
    echo "In master"
    else \
    echo "Not in master"; \
    fi

Why is $() not working but `` did? Only for the "git" command.

Note that in my Makefile, I used $() normally in many rules.

Thanks :)


回答1:


Inside recipes you have to escape dollar signs in order to get them interpreted by the shell. Otherwise, Make treats $(git ...) as a built-in command or a variable and tries to evaluate it (of course, unsuccessfully).

check_branch:
    if [ "$$(git rev-parse --abbrev-ref HEAD)" == "master" ]; then \
    ...



回答2:


In shell lines, you write shell commands as you would in a shell script. That's why the second operation works.

If you want Make to evaluate git command outside of the shell, you can enclose the operation in a shell function, as in:

$(shell git rev-parse --abbrev-ref HEAD)

And you ought to be good to go, though I often implement this kind of thing as:

branch := $(shell git rev-parse --abbrev-ref HEAD)
target:dep
     mkdir -p build/$(branch)

Or something along those lines.



来源:https://stackoverflow.com/questions/15729491/makefile-command-not-working-but-command-did

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!