Merge pull request #2275 from jacquesg/apple-warning Check for compiler flag support instead of checking for a platform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 918e5b8..704770b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/")
INCLUDE(CheckLibraryExists)
+INCLUDE(AddCFlagIfSupported)
# Build options
#
@@ -287,7 +288,7 @@ IF (MSVC)
# Precompiled headers
ELSE ()
- SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wdeclaration-after-statement ${CMAKE_C_FLAGS}")
+ SET(CMAKE_C_FLAGS "-D_GNU_SOURCE -Wall -Wextra ${CMAKE_C_FLAGS}")
IF (WIN32 AND NOT CYGWIN)
SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
@@ -301,16 +302,22 @@ ELSE ()
ADD_DEFINITIONS(-D__USE_MINGW_ANSI_STDIO=1)
ELSEIF (BUILD_SHARED_LIBS)
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fPIC")
+ ADD_C_FLAG_IF_SUPPORTED(-fvisibility=hidden)
+
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
ENDIF ()
+
+ ADD_C_FLAG_IF_SUPPORTED(-Wno-missing-field-initializers)
+ ADD_C_FLAG_IF_SUPPORTED(-Wstrict-aliasing=2)
+ ADD_C_FLAG_IF_SUPPORTED(-Wstrict-prototypes)
+ ADD_C_FLAG_IF_SUPPORTED(-Wdeclaration-after-statement)
+ ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-const-variable)
+ ADD_C_FLAG_IF_SUPPORTED(-Wno-unused-function)
+
IF (APPLE) # Apple deprecated OpenSSL
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
+ ADD_C_FLAG_IF_SUPPORTED(-Wno-deprecated-declarations)
+ ENDIF()
- # With clang, disable some annoying extra warnings
- IF (NOT CMAKE_COMPILER_IS_GNUCC)
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-const-variable -Wno-unused-function")
- ENDIF()
- ENDIF ()
IF (PROFILE)
SET(CMAKE_C_FLAGS "-pg ${CMAKE_C_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "-pg ${CMAKE_EXE_LINKER_FLAGS}")
@@ -335,7 +342,7 @@ ENDIF()
IF (THREADSAFE)
IF (NOT WIN32)
- find_package(Threads REQUIRED)
+ FIND_PACKAGE(Threads REQUIRED)
ENDIF()
ADD_DEFINITIONS(-DGIT_THREADS)
@@ -366,7 +373,7 @@ IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
ADD_DEFINITIONS(-DGIT_ARCH_32)
ELSE()
- message(FATAL_ERROR "Unsupported architecture")
+ MESSAGE(FATAL_ERROR "Unsupported architecture")
ENDIF()
# Compile and link libgit2
@@ -455,7 +462,7 @@ ENDIF ()
IF (TAGS)
FIND_PROGRAM(CTAGS ctags)
IF (NOT CTAGS)
- message(FATAL_ERROR "Could not find ctags command")
+ MESSAGE(FATAL_ERROR "Could not find ctags command")
ENDIF ()
FILE(GLOB_RECURSE SRC_ALL *.[ch])
diff --git a/cmake/Modules/AddCFlagIfSupported.cmake b/cmake/Modules/AddCFlagIfSupported.cmake
new file mode 100644
index 0000000..67fc895
--- /dev/null
+++ b/cmake/Modules/AddCFlagIfSupported.cmake
@@ -0,0 +1,16 @@
+# - Append compiler flag to CMAKE_C_FLAGS if compiler supports it
+# ADD_C_FLAG_IF_SUPPORTED(<flag>)
+# <flag> - the compiler flag to test
+# This internally calls the CHECK_C_COMPILER_FLAG macro.
+
+INCLUDE(CheckCCompilerFlag)
+
+MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG)
+ STRING(TOUPPER ${_FLAG} UPCASE)
+ STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
+ CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
+
+ IF(IS_${UPCASE_PRETTY}_SUPPORTED)
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
+ ENDIF()
+ENDMACRO()