How to assign the output of a command to a Makefile variable

后端 未结 7 1122
生来不讨喜
生来不讨喜 2020-12-02 05:11

I need to execute some make rules conditionally, only if the Python installed is greater than a certain version (say 2.5).

I thought I could do something like execut

相关标签:
7条回答
  • 2020-12-02 05:47

    I'm writing an answer to increase visibility to the actual syntax that solves the problem. Unfortunately, what someone might see as trivial can become a very significant headache to someone looking for a simple answer to a reasonable question.

    Put the following into the file "Makefile".

    MY_VAR := $(shell python -c 'import sys; print int(sys.version_info >= (2,5))')
    
    all:
        @echo MY_VAR IS $(MY_VAR)
    

    The behavior you would like to see is the following (assuming you have recent python installed).

    make
    MY_VAR IS 1
    

    If you copy and paste the above text into the Makefile, will you get this? Probably not. You will probably get an error like what is reported here:

    makefile:4: *** missing separator. Stop

    Why: Because although I personally used a genuine tab, Stack Overflow (attempting to be helpful) converts my tab into a number of spaces. You, frustrated internet citizen, now copy this, thinking that you now have the same text that I used. The make command, now reads the spaces and finds that the "all" command is incorrectly formatted. So copy the above text, paste it, and then convert the whitespace before "@echo" to a tab, and this example should, at last, hopefully, work for you.

    0 讨论(0)
提交回复
热议问题