Search code examples
makefile

How to access make options in Makefile


I want to detect if "-s" is set in the Makefile command line. Is there a variable that captures make options that I can test?

For example, I want to do something like:

Makefile:

if ($(MAKE_OPTIONS), -s)
    #do something
endif

Invocation:

make $(MAKEFILE_OPTIONS) -C some_dir

Solution

  • Unfortunately it's tricky to find a really portable (across different releases of GNU make) way to do this. Mark's solution gives false positives, if you have set any flag with an "s" in the name (e.g., --warn-undefined-variables).

    In GNU make 4.0 and above, the layout of MAKEFLAGS is well-defined and you can use this:

    $(findstring s,$(firstword -$(MAKEFLAGS))
    

    to robustly and reliably tell if -s was given. However this won't work reliably in versions of GNU make prior to 4.0. You can use this:

    $(findstring s,$(firstword -$(MAKEFLAGS)))$(filter -s,$(MAKEFLAGS))
    

    This expands to the empty string if -s was not given, or a non-empty string if it was. I believe that will work in all versions of GNU make.