Search code examples
makefilewarningsundefinedbuild-error

How to treat a warning as an error in a Makefile?


  1. Is it possible to treat warnings as errors in a Makfile (and thus exit before Makefile proceeds)

  2. 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.


Solution

  • 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.