Only add warning and std flags if supported by compiler (#144)

This fixes errors like those reported in #143 on Solaris
(and probably other platforms where gcc is not the native
compiler):

c99: -W option with unknown program all

This only fixes it for cmake, but presumably similar checks
could be added for autoconf as well.
feature/tap-sh
✈ Graham ✈ 2017-09-24 13:52:49 -04:00 committed by William Pitcock
parent fae657101c
commit e9c2e6f127
1 changed files with 21 additions and 1 deletions

View File

@ -61,7 +61,27 @@ IF (WIN32)
# Ignore warning C4996: 'strncpy'
ADD_DEFINITIONS("-D_CRT_SECURE_NO_WARNINGS=1")
ELSE()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat=2 -std=gnu99 -O2 -g")
INCLUDE(CheckCCompilerFlag)
CHECK_C_COMPILER_FLAG("-Wall" COMPILER_HAS_WALL)
IF (COMPILER_HAS_WALL)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
ENDIF()
CHECK_C_COMPILER_FLAG("-Wextra" COMPILER_HAS_WEXTRA)
IF (COMPILER_HAS_WEXTRA)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
ENDIF()
CHECK_C_COMPILER_FLAG("-Wformat=2" COMPILER_HAS_WFORMAT)
IF (COMPILER_HAS_WFORMAT)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2")
ENDIF()
CHECK_C_COMPILER_FLAG("-std=gnu99" COMPILER_HAS_STD_GNU99)
CHECK_C_COMPILER_FLAG("-std=c99" COMPILER_HAS_STD_C99)
IF (COMPILER_HAS_STD_GNU99)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
ELSEIF (COMPILER_HAS_STD_C99)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
ENDIF()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g")
ENDIF()
INCLUDE_DIRECTORIES(${pkgconf_SOURCE_DIR} ${pkgconf_BINARY_DIR})