niklauzg
niklauzg

Reputation: 571

Set the default target(s) of a Makefile (GNU Make) via Environment Variable

I'd like to set the default target(s) of a Makefile to the space-deliminted value of an Environment Variable.

For this solution to work correctly, it must be possible for me to set an environment variable, then run make and have the target, (or optionally space-delimited targets), contained in the environment variable be used as though they were passed as targets.

DEFAULT_TARGETS="target target2" make
# ... should produce the same result as ...
make target1 target2

Thanks for the help!

Upvotes: 0

Views: 692

Answers (2)

niklauzg
niklauzg

Reputation: 571

Following @Andreas lead I've landed on this ...

.PHONY: DEFAULT_TARGET
DEFAULT_TARGETS ?= intro
DEFAULT_TARGET: $(DEFAULT_TARGETS)

This use the "First Target as Default" paradigm, then expands the contents of the DEFAULT_TARGETS env-var to add requisites. It also has the advantage that if the DEFAULT_TARGETS environment variable is not set, the makefile will use the fall-back value, in this case that's intro.

In the shell this would look like, DEFAULT_TARGETS="target target2" make

Upvotes: 0

MadScientist
MadScientist

Reputation: 100781

You can do it by adding something like this to your makefile:

ifneq ($(DEFAULT_TARGETS),)
__default_targets: $(DEFAULT_TARGETS)
.DEFAULT_GOAL = __default_targets
endif

Upvotes: 2

Related Questions