Reputation: 311
I'm trying to connect to some API using python script. that API requires an env variable to be available. to get that env I have another command. I combined both commands in one single Makefile command
# Current Makefile
cmd_1:
$(eval MY_ENV := $somevalue)
MY_ENV=$(MY_ENV) python myscript.py cmd_1
cmd_2:
$(eval MY_ENV := $somevalue)
MY_ENV=$(MY_ENV) python myscript.py cmd_2
Now as you can see some part(1st line) is same in both above commands. I want to get rid of that duplicacy. I want something like the below so that I can reuse it even if in future I have more than 2 mgmt commands.
# Desired Makefile (Using DRY)?
get_env:
$(eval MY_ENV := $somevalue)
cmd_1:
MY_ENV=$(MY_ENV) python myscript.py cmd_1
cmd_2:
MY_ENV=$(MY_ENV) python myscript.py cmd_2
I tried to wrap these two commands and create another Make command, like below. but no luck.
mycmd:
@$(MAKE) get_env
@$(MAKE) cmd_1
So I'm wondering if there is a better way to do this?
Upvotes: 1
Views: 942
Reputation: 6218
Let me suggest this:
get_env ::
$(eval MY_ENV := $somevalue)
cmd_1 cmd_2 :: get_env
MY_ENV=$(MY_ENV) python myscript.py $@
Some explanations:
::
makes the targets phony, so you won't have to add a line .PHONY: get_env cmd_1 cmd_2
in the Makefile;$@
is the current targetThe key point is to add a dependency to get_env
in the rule, so that MY_ENV
is kept.
For a simpler trial, run make bar
or make baz
with this toy Makefile:
foo ::
$(eval qux := `pwd`)
bar baz :: foo
@echo "qux = $(qux) and target = $@"
Upvotes: 2