Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute shell function on a command in make variable

Tags:

shell

makefile

In my makefile, I am trying to capture the output from a shell function call on a make variable containing the command string without success. When I run the shell function on the string directly, it works. I don't understand what the difference is between running the shell function on the command string versus running the shell function on a make variable containing the command string.

PG_CONFIG = "/usr/pgsql-9.4/bin/pg_config/"

PG_INCLUDE1 = $(shell $$PG_CONFIG)
PG_INCLUDE2 = $(shell /usr/pgsql-9.4/bin/pg_config --includedir-server)

.PHONY: print

print:
    @echo "PG_CONFIG="$(PG_CONFIG)
    @echo "PG_INCLUDE1="$(PG_INCLUDE1)
    @echo "PG_INCLUDE2="$(PG_INCLUDE2)

The output is:

$make -f Makefile.test print
PG_CONFIG=/usr/pgsql-9.4/bin/pg_config/
PG_INCLUDE1=
PG_INCLUDE2=/usr/pgsql-9.4/include/server
like image 441
cbAndrews Avatar asked Jun 26 '26 19:06

cbAndrews


1 Answers

As it stands in GNU Make Manual:

Variable and function references in recipes have identical syntax and semantics to references elsewhere in the makefile. They also have the same quoting rules: if you want a dollar sign to appear in your recipe, you must double it (‘$$’). For shells like the default shell, that use dollar signs to introduce variables, it’s important to keep clear in your mind whether the variable you want to reference is a make variable (use a single dollar sign) or a shell variable (use two dollar signs).

The $$ is replaced with a single dollar. In your example:

PG_INCLUDE1 = $(shell $$PG_CONFIG)

becomes:

PG_INCLUDE1 = $(shell $PG_CONFIG)

Next, $PG_CONFIG is executed in shell just as if you typed it in the terminal. And as there is no PG_CONFIG variable defined in the current subshell $PG_CONFIG is replaced with nothing. However, if you defined PG_CONFIG environment variable before running make it would work as you wished with $$ in Makefile:

$ export PG_CONFIG="aoeuidhtn"
$ make
PG_CONFIG=/usr/pgsql-9.4/bin/pg_config/
PG_INCLUDE1=aoeuidhtn
like image 174
Arkadiusz Drabczyk Avatar answered Jun 28 '26 15:06

Arkadiusz Drabczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!