Ahmed Kamal
Ahmed Kamal

Reputation: 136

Can I call a user defined function outside of a target

I want to know the following:

DOT_ENV_FILE ?= $(CURDIR)/.env

define importEnvFile
    VARS = $(shell sed -ne 's/ *\#.*$$//; /./ s/=/?=/ p' ${1})
    $(foreach v,${VARS},$(eval export ${v}))
endef

$(call importEnvFile,$(DOT_ENV_FILE))

Upvotes: 0

Views: 330

Answers (1)

Beta
Beta

Reputation: 99094

A1: Yes, if you use the eval function:

define defThings
 SKY := blue
endef

$(eval $(call defThings))

$(info sky is $(SKY))

A2: Yes:

  • makefile:
    define defThings
     export SKY := blue
    endef
    
    $(eval $(call defThings))
    
    delegate:
        $(MAKE) -f Makefile.other
  • Makefile.other:
    report:
        @echo sub-make reports sky is $(SKY)

The other complications of reading and parsing a file and iterating over a list can wait until you have this much working. When you implement a new functionality, keep it as simple as possible until you have it working.

Upvotes: 1

Related Questions