I am trying to check for the presence of available compiler warning flags in my makefile.
Here is my first attempt:
HAS_WARNING = $(shell $(CXX) --help=warnings | grep $(1))
HAS_FLOAT_CONV := $(call HAS_WARNING,-Wfloat-conversion)
$(error "has=$(HAS_FLOAT_CONV)")
If I run this through make with bash -x
enabled, I can see the search term passed to grep
is not quoted, and so the initial -
is treated as an option:
$ make SHELL="/bin/bash -x"
+ g++ --help=warnings
+ grep -Wfloat-conversion
grep: invalid option -- 'W'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
makefile:10: *** "has=". Stop.
I need to somehow add quotes around my search term.
Everything I've tried has come up short:
Passing the search term in quotes:
HAS_WARNING = $(shell $(CXX) --help=warnings | grep "$(1)")
Result: No quotes added:
$ make SHELL="/usr/bin/bash -x"
+ g++ --help=warnings
+ grep -Wfloat-conversion
grep: invalid option -- 'W'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
makefile:10: *** "has=". Stop.
Passing the search term in escaped quotes:
HAS_WARNING = $(shell $(CXX) --help=warnings | grep \"$(1)\")
Result: The search term is passed in quoted quotes:
$ make SHELL="/usr/bin/bash -x"
+ g++ --help=warnings
+ grep '"-Wfloat-conversion"'
makefile:10: *** "has=". Stop.
Using addprefix
and addsuffix
:
HAS_WARNING = $(shell $(CXX) --help=warnings | grep $(addprefix ",$(addsuffix ",$(1))))
Result: No quotes added:
$ make SHELL="/usr/bin/bash -x"
+ g++ --help=warnings
+ grep -Wfloat-conversion
grep: invalid option -- 'W'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
makefile:10: *** "has=". Stop.
If I change the search term, to instead only search for "float-conversion"
, then the grep succeeds:
HAS_FLOAT_CONV := $(call HAS_WARNING,float-conversion)
Result:
$ make SHELL="/usr/bin/bash -x"
+ g++ --help=warnings
+ grep float-conversion
makefile:10: *** "has= -Wfloat-conversion Warn for implicit type conversions that cause loss of floating point precision.". Stop.
However, the pedant in me would like to search for the whole warning flag.
Is this possible?