Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't environment variables work in $(shell) commands?

Tags:

makefile

export HELLO=Hello,world
all:
        @echo $(HELLO)
        @echo $(shell echo $$HELLO)
        @echo `echo $$HELLO`

outputs:

Hello,world

Hello,world

Why is there a difference between backtick and $(shell), and is there a way to pass the environment variables to $(shell) invocations?

I'm trying to use pkg-config in a cross-compilation environment, so I need to set $PKG_CONFIG_SYSROOT. I can use backticks, but it's executed once for every .o file. As per Computing Makefile variable on assignment, I need to use PKG_CFLAGS := $(shell pkg-config $(PACKAGES)), but I can't pass in the required environment variable to make that work properly.

Tested on GNU Make 4.0.

like image 763
Dan Merillat Avatar asked Oct 16 '25 19:10

Dan Merillat


1 Answers

Congratulations you have hit a make bug: $(shell) doesn't honor export but this is undocumented?

There's a comment in the code (that precede's the filing of the ticket and is quoted in the ticket) which indicates that there are complicated situations where this cannot work correctly and so it appears that it just isn't done.

I can think of two ways to get the $(shell) environment to have the variables you want to set manually available.

  1. Set them in the $(shell) context explicitly.

    PCVAR:=$(shell PKG_CONFIG_SYSROOT=$(make-level-variable-PKG_CONFIG_SYSROOT) pkg-config ...)
    
  2. Set them in the make processes environment so the $(shell) environment inherits them normally.

    $ PKG_CONFIG_SYSROOT=/some/path make
    
like image 119
Etan Reisner Avatar answered Oct 18 '25 23:10

Etan Reisner



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!