Dmitry Sokolov
Dmitry Sokolov

Reputation: 3190

Optional depencency on target in Makefile

Is it possible to create an optional dependency on a target in Makefile (GNU Make)?

help:
    @echo Usage:

clean:
    @echo Cleaning...

build: clean?
    @echo Building...

The expected output:

$ make
Usage:

$ make clean
Cleaning...

$ make build
Building...

$ make build clean
Cleaning...
Building...

$ make clean build
Cleaning...
Building...

The set of targets is fixed and can not be extended, i.e.

rebuild: clean build

.PHONY: rebuild

is not acceptable.

Upvotes: 0

Views: 52

Answers (1)

MadScientist
MadScientist

Reputation: 101111

Yes, you can do this:

build: $(filter clean,$(MAKECMDGOALS))
        @echo Building...

See: https://www.gnu.org/software/make/manual/html_node/Goals.html and https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-filter

Upvotes: 1

Related Questions