Use the error function:
ifndef MY_FLAG $(error MY_FLAG is not set) endif
Note that lines should not be indented. More precisely, no tabs should precede these lines.
Common decision
If you are going to test many variables, you should define a helper function for this:
And here is how to use it:
$(call check_defined, MY_FLAG) $(call check_defined, OUT_DIR, build directory) $(call check_defined, BIN_DIR, where to put binary artifacts) $(call check_defined, \ LIB_INCLUDE_DIR \ LIB_SOURCE_DIR, \ library path)
This will result in an error:
Makefile:17: *** Undefined OUT_DIR (build directory). Stop.
Verification for specific objects
You can also expand the solution so that you can require a variable only if a specific target is called.
$(call check_defined, ...) from inside the recipe
Just move the check to the recipe:
foo : @:$(call check_defined, BAR, baz value)
The leading @ sign disables the echo command, and : - a valid command, no-op stub shell.
Target Name Display
The check_defined function can be improved to display the target name (provided via the $@ variable):
check_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) __check_defined = \ $(if $(value $1),, \ $(error Undefined $1$(if $2, ($2))$(if $(value @), \ required by target `$@')))
So, now a failed check produces a well-formatted output:
Makefile:7: *** Undefined BAR (baz value) required by target `foo'. Stop.
check-defined-MY_FLAG special purpose
Personally, I would use the simple and direct solution above. However, for example, this answer suggests using a special purpose to perform the actual check. You can try to generalize this and define the goal as a rule of the implicit template:
Using:
foo :|check-defined-BAR
Please note that the check-defined-BAR is specified as an order only ( |... ).
Pros:
- (possibly) cleaner syntax
Minuses:
- Unable to specify custom error message
- Running
make -t (see Instead of executing recipes ) will contaminate your root directory with a large number of check-defined-... files. This is a sad flaw that template rules cannot be declared .PHONY .
I believe that these limitations can be overcome using eval magic and a secondary extension , although I'm not sure if it's worth it.