Cabrera
Cabrera

Reputation: 1980

Conditional error in makefile

I have the following commands in my MakeFile:

.PHONY: lint
lint:
    eslint src --ext ts

.PHONY: component
component:
ifndef name
$(error name is not set. try 'make component name=')
endif

The issue is when I run make lint, or any other command, make exits with the error found in component.

How would I stop this from happening?

Upvotes: 2

Views: 464

Answers (1)

Make expands the ifndef … block as part of parsing the makefile. It could expand to targets or commands. You wrote “if name is not defined, generate an error” so Make does what you instructed.

It seems that you meant to write a rule for the component target. If so, you need to change the parse-time error into a run-time error.

component:
ifdef name
        command_to_generate_component $(name)
else
        false "Error: name is not set. Try 'make component name=…'"
endif

Upvotes: 3

Related Questions