It seems absurd that there wouldn't already be a question about this, but I can't find one. Anyway, I am writing a makefile, and I just want to have a conditional statement that sets different CFLAGS depending on whether GNU or Intel compilers are being used to compile the program, set by the envrionment variable CC.
Now, there is a section in the GNU make manual on this, and it says to do something like the following:
https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_7.html
ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
(ok setting libraries not flags, but whatever, same idea). Ok fine, but surely this is just an example of how conditionals work, because in practice this is obviously a stupid way to do this. The CC variable can contain all manner of things besides 'gcc' and still be effectively using a GNU C compiler. Even just specifying the absolute path to the compiler in CC will break this example.
So what is the sensible, robust, way to do this? Also, in case it matters, I want to do the same thing for C++ and Fortran compiler choices as well.
You are right that this is not so robust. A more robust way could be to use the findstring command, for example:
ifneq (,$(findstring gcc,$(CC)))
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif