Search code examples
makefilegnu-make

Makefile ifneq returning false even when two strings are clearly different


I am trying to make a simple configuration in a Makefile to have it enable/disable a DEBUG flag to follow if the user uses either -d or --debug=... when calling make.

This seems to make sense to me, but for some reason the first ifneq conditional is not working. It always returns false, regardless of what is inputted. If I simply echo out the $(findstring --debug,-$(MAKEFLAGS)) command in the target, I get the expected --debug I'm looking for... so I'm confident that function is returning the string, as expected.

So why is the neq conditional check of --debug against an empty string returning false?

# By default, the `DEBUG` flag will follow whether the `make` command was called with the `-d` or `--debug` s:
#
# `make -d`
# `make --debug=basic`
#
# To enable this `DEBUG` flag without increasing `make` verbosity, set the flag manually:
#
# `make DEBUG=1`
#
ifneq (,$(findstring --debug,-$(MAKEFLAGS)))
DEBUG ?= 1
endif

ifneq (,$(findstring d,-$(filter-out --%,-$(MAKEFLAGS))))
DEBUG ?= 1
endif

DEBUG ?= 0
make                # Works as expected
make -d             # Works as expected
make --debug=basic  # Does not work

Solution

  • I can't reproduce your behavior, using GNU Make 4.4.1 on GNU/Linux. Please specify which version of GNU Make you're using, and on which platform (where you got your build of GNU Make from).

    Here's what I see:

    $ cat Makefile
    $(info MAKEFLAGS = '$(MAKEFLAGS)')
    
    ifneq (,$(findstring --debug,-$(MAKEFLAGS)))
    DEBUG ?= 1
    $(info matches --debug)
    endif
    
    ifneq (,$(findstring d,-$(filter-out --%,-$(MAKEFLAGS))))
    DEBUG ?= 1
    $(info matches -d)
    endif
    
    DEBUG ?= 0
    $(info DEBUG = $(DEBUG))
    

    Then:

    $ make -rR
    MAKEFLAGS = 'rR'
    DEBUG = 0
    make: *** No targets.  Stop.
    
    $ make -rR -d
      ...
    MAKEFLAGS = 'drR'
    matches -d
    DEBUG = 1
    
    $ make -rR --debug=basic
      ...
    MAKEFLAGS = 'rR --debug=basic'
    matches --debug
    DEBUG = 1
    

    Just to note, this:

    $(findstring d,-$(filter-out --%,-$(MAKEFLAGS))))
    

    is more reliably / straightforwardly written:

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