简体   繁体   中英

Cause makefile to error if an environment variable is not set?

I'd like my makefile to crash if an environment variable is not set. This is what I have so far:

ifneq ($(shell echo $${VIRTUAL_ENV:+True}),True)
$(error Looks like no virtualenv is active)
endif

and it works!

I'm wondering if there's a more elegant way to do this, perhaps with make directly instead of calling $(shell ...) .

Thanks for your help!

You can make use of the origin function...

ifeq ($(origin VIRTUAL_ENV),undefined)
$(error Looks like no virtualenv is active)
endif

This could be simplest option, but isn't as fine tuned as using origin .

ifndef VIRTUAL_ENV
$(error Looks like no virtualenv is active)
endif

One caveat here is that you can't distinguish if VIRTUAL_ENV defined in the Makefile or in the environment.

As @KurtisRader pointed out in a comment , this is possible because environment vars are implicitly "imported" into make's namespace https://www.gnu.org/software/make/manual/html_node/Environment.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM