gccmakefileversion-detection

Makefile: Emit warning gcc version is lower than 4.8.0


The Makefile parameters to gcc

A certain Makefile` in an Open-Source project contains the following call:

CFLAGS = -I$(RM_INCLUDE_DIR) -Wall -g -fPIC -lc -lm -Og -std=gnu99

The -Og parameter was introduced to gcc 4.8. My OSX contained gcc 4.2.1, and it failed with a rather confusing error message.

The problem

Is there an elegant and standard (i.e., works on any POSIX environment) way to check the version of gcc and emit a warning if it's lower than 4.8?

The problem is that gcc --version has different output formats:

$ gcc --version
gcc (Homebrew gcc 5.3.0) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

My question

Is there an elegant, Makefile-compatible and standard way to check the version of gcc, and emit a warning if the version is lower than 4.8.x?


Solution

  • Following Checking the gcc version in a Makefile?, I ended up with the following code:

    # Warn if gcc version is lower than 4.8 (-Og was introduced in this version)
    MIN_GCC_VERSION = "4.8"
    GCC_VERSION := "`gcc -dumpversion`"
    IS_GCC_ABOVE_MIN_VERSION := $(shell expr "$(GCC_VERSION)" ">=" "$(MIN_GCC_VERSION)")
    ifeq "$(IS_GCC_ABOVE_MIN_VERSION)" "1"
        GCC_VERSION_STRING := "GCC version OK $(GCC_VERSION) >= $(MIN_GCC_VERSION)"
    else
        GCC_VERSION_STRING := "ERROR: gcc version $(GCC_VERSION) is lower than $(MIN_GCC_VERSION), 'gcc -Og' might fail."
    endif
    

    Notes:

    Thanks sycko and madscientist for your useful comments!