Is it possible to treat warnings as errors in a Makfile (and thus exit before Makefile proceeds)
Furthermore, is it possible to filter out which warning yields an error?
My use case: I want to use --warn-undefined-variables
in combination with this so that Makefile will exit when a variable is undefined, which is a very common source of error. Obviously I don't want to manually check for each variable as this is error-prone/tedious. I couldn't find anything on this, but it's a pretty important/basic feature.
Note: I'm not looking for -Werror
which is a gcc specific command not applicable to my use case.
If you're prepared to add a dependency to every target, you can make warnings into errors.
Here is a make file with an error in it ("SRCS" instead of "SRC"):
# Turn on the warning we want
MAKEFLAGS += --warn-undefined-variables
# Make sure MAKECMDGOALS is defined, so it doesn't cause an error itself
ifndef MAKECMDGOALS
MAKECMDGOALS = all
endif
SRC=hello.c
all: compile
# Fails if the Makefile contains any warnings.
# Run this Makefile with the same goals, but with the -n flag.
# Grep for warnings, and fail if any are found.
no-make-warnings:
! make -n $(MAKECMDGOALS) 2>&1 >/dev/null | grep warning
# Targets you want to check must depend on no-make-warnings
compile: no-make-warnings
gcc -o hello $(SRCS)
When I run it, I see this:
$ make
! make -n all 2>&1 >/dev/null | grep warning
Makefile:17: warning: undefined variable `SRCS'
make: *** [no-make-warnings] Error 1
You just need to make every target that you want to be checked depend on the target no-make-warnings
.
If someone knows how to do that automatically, please chime in.
The standard version of make does not support what you are looking for. However, it should not be difficult to build your own version of make to fulfill your use case.
Looking at the source code of make 3.82, check out the macro warn_undefined
in variable.h:
214 /* Warn that NAME is an undefined variable. */
215
216 #define warn_undefined(n,l) do{\
217 if (warn_undefined_variables_flag) \
218 error (reading_file, \
219 _("warning: undefined variable `%.*s'"), \
220 (int)(l), (n)); \
221 }while(0)
I have not tried this, but I think it should be sufficient to replace error
with fatal
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With