Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

makefile

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 executing:

python -c 'import sys; print int(sys.version_info >= (2,5))' 

and then using the output ('1' if ok, '0' otherwise) in a ifeq make statement.

In a simple bash shell script it's just:

MY_VAR=`python -c 'import sys; print int(sys.version_info >= (2,5))'` 

but that doesn't work in a Makefile.

Any suggestions? I could use any other sensible workaround to achieve this.

like image 820
fortran Avatar asked Jan 07 '10 11:01

fortran


People also ask

How do you assign the output of a command to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...] arg1 arg2 ...'

What is := in makefile?

Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.

How do I print a variable in makefile?

To use it, just set the list of variables to print on the command line, and include the debug target: $ make V="USERNAME SHELL" debug makefile:2: USERNAME = Owner makefile:2: SHELL = /bin/sh.exe make: debug is up to date. Now you can print variables by simply listing them on the command line.


1 Answers

Use the Make shell builtin like in MY_VAR=$(shell echo whatever)

me@Zack:~$make MY_VAR IS whatever 

me@Zack:~$ cat Makefile  MY_VAR := $(shell echo whatever)  all:     @echo MY_VAR IS $(MY_VAR) 
like image 61
Arkaitz Jimenez Avatar answered Oct 17 '22 02:10

Arkaitz Jimenez