0

What I want to do ?

Load an env file in "lazy" loading like this:

$ cat env.int
X=file
$ make toto
X=file
$ X=env make toto
X=env

What I got ?

I've got a env-file env.int like this:

X=file

And a Makefile like this:

MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-builtin-variables
MAKEFLAGS += --no-print-directory

SHELL := /bin/bash

.ONESHELL:
.SHELLFLAGS: -c
.SILENT:

define task_header
    $(info ###################### $(1) ######################)
endef


ENV_FILE?=$(CURDIR)/env.int

define setup_env_file
    $(eval include $(1))
    $(eval export $(shell [ ! -n "$(1)" ] || cat $(1) | grep -v -E '^('$$(env | sed 's/=.*//'g | tr '\n' '|')')\=') )
endef

.PHONY: toto # sample target
toto:
    $(call setup_env_file,$(ENV_FILE))
    env | sort

What It does ?

$ make toto
X=file
$ X=value make toto
######################  ######################

X=file
$

Obviously it doesn't work !

Any Idea how can I do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.