Commit 28750a7d98ce5e23bac5c1d119109ded8e8aab73

Russell Belfer 2014-04-21T09:50:43

Merge pull request #2275 from jacquesg/apple-warning Check for compiler flag support instead of checking for a platform

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()