Commit c13e56f91c3a15dd47b9543984e763ab74270aa9

Patrick Steinhardt 2018-06-25T14:12:53

cmake: distinguish internal and system include directories While we want to enforce strict C90 mode, this may cause issues with system provided header files which are themselves not strictly conforming. E.g. if a system header has C++ style comments, a compiler in strict C90 mode would produce an error and abort the build. As the user most likely doesn't want to change the system header, this would completely break the build on such systems. One example of this is mbedtls, which provides such header files. The problem can be worked around by distinguishing between system-provided and project-provided include directories. When adding include directories via "-isystem" instead of "-I", the compiler will skip certain checks and print out less warnings. To use system includes, we can simply add the "SYSTEM" flag to CMake's `INCLUDE_DIRECTORIES` and `TARGET_INCLUDE_DIRECTORIES` functions. Note that we have to split the include directories into two variables because of this, as we definitely still want to check for all warnings produced by our own header files.

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index ca7515e..d4dca5a 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,4 +1,5 @@
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
+INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 
 FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
 ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 2deed5f..b45f476 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -16,6 +16,7 @@ SET(LIBGIT2_INCLUDES
 	"${CMAKE_CURRENT_BINARY_DIR}"
 	"${libgit2_SOURCE_DIR}/src"
 	"${libgit2_SOURCE_DIR}/include")
+SET(LIBGIT2_SYSTEM_INCLUDES "")
 SET(LIBGIT2_LIBS "")
 
 # Installation paths
@@ -94,7 +95,7 @@ ADD_FEATURE_INFO(threadsafe THREADSAFE "threadsafe support")
 
 IF (WIN32 AND EMBED_SSH_PATH)
 	FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
-	LIST(APPEND LIBGIT2_INCLUDES "${EMBED_SSH_PATH}/include")
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
 	FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
 	SET(GIT_SSH 1)
 ENDIF()
@@ -107,7 +108,7 @@ IF (WIN32 AND WINHTTP)
 	IF (MINGW)
 		ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
 		LIST(APPEND LIBGIT2_LIBS winhttp)
-		LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
 	ELSE()
 		LIST(APPEND LIBGIT2_LIBS "winhttp")
 		LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
@@ -121,7 +122,7 @@ ELSE ()
 	ENDIF ()
 	IF (CURL_FOUND)
 		SET(GIT_CURL 1)
-		LIST(APPEND LIBGIT2_INCLUDES ${CURL_INCLUDE_DIRS})
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${CURL_INCLUDE_DIRS})
 		LIST(APPEND LIBGIT2_LIBS ${CURL_LIBRARIES})
 		LIST(APPEND LIBGIT2_PC_LIBS ${CURL_LDFLAGS})
 	ENDIF()
@@ -174,7 +175,7 @@ IF (USE_HTTPS)
 		ENDIF()
 
 		SET(GIT_SECURE_TRANSPORT 1)
-		LIST(APPEND LIBGIT2_INCLUDES ${SECURITY_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${SECURITY_INCLUDE_DIR})
 		LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_LIBRARIES} ${SECURITY_LIBRARIES})
 		LIST(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS})
 	ELSEIF (HTTPS_BACKEND STREQUAL "OpenSSL")
@@ -183,7 +184,7 @@ IF (USE_HTTPS)
 		ENDIF()
 
 		SET(GIT_OPENSSL 1)
-		LIST(APPEND LIBGIT2_INCLUDES ${OPENSSL_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${OPENSSL_INCLUDE_DIR})
 		LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
 		LIST(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS})
 		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
@@ -230,7 +231,7 @@ IF (USE_HTTPS)
 		ENDIF()
 
 		SET(GIT_MBEDTLS 1)
-		LIST(APPEND LIBGIT2_INCLUDES ${MBEDTLS_INCLUDE_DIR})
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
 		LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
 		# mbedTLS has no pkgconfig file, hence we can't require it
 		# https://github.com/ARMmbed/mbedtls/issues/228
@@ -285,7 +286,7 @@ ELSEIF (SHA1_BACKEND STREQUAL "mbedTLS")
 	ADD_FEATURE_INFO(SHA ON "using mbedTLS")
 	SET(GIT_SHA1_MBEDTLS 1)
 	FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
-	LIST(APPEND LIBGIT2_INCLUDES ${MBEDTLS_INCLUDE_DIR})
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
 	LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
 	# mbedTLS has no pkgconfig file, hence we can't require it
 	# https://github.com/ARMmbed/mbedtls/issues/228
@@ -298,21 +299,21 @@ ENDIF()
 # Include POSIX regex when it is required
 IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
 	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/regex" "${libgit2_BINARY_DIR}/deps/regex")
-	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
 	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
 ENDIF()
 
 # Optional external dependency: http-parser
 FIND_PACKAGE(HTTP_Parser)
 IF (USE_EXT_HTTP_PARSER AND HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
-	LIST(APPEND LIBGIT2_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
 	LIST(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
 	LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
 	ADD_FEATURE_INFO(http-parser ON "http-parser support")
 ELSE()
 	MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
 	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
-	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
 	LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
 	ADD_FEATURE_INFO(http-parser ON "http-parser support (bundled)")
 ENDIF()
@@ -321,7 +322,7 @@ ENDIF()
 IF(NOT USE_BUNDLED_ZLIB)
 	FIND_PACKAGE(ZLIB)
 	IF(ZLIB_FOUND)
-		LIST(APPEND LIBGIT2_INCLUDES ${ZLIB_INCLUDE_DIRS})
+		LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS})
 		LIST(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
 		IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
 			LIST(APPEND LIBGIT2_LIBS "z")
@@ -336,7 +337,7 @@ IF(NOT USE_BUNDLED_ZLIB)
 ENDIF()
 IF(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
 	ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
-	LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
 	LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
 	ADD_FEATURE_INFO(zlib ON "using bundled zlib")
 ENDIF()
@@ -347,7 +348,7 @@ IF (USE_SSH)
 ENDIF()
 IF (LIBSSH2_FOUND)
 	SET(GIT_SSH 1)
-	LIST(APPEND LIBGIT2_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
 	LIST(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
 	LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
 
@@ -376,7 +377,7 @@ IF (USE_ICONV)
 ENDIF()
 IF (ICONV_FOUND)
 	SET(GIT_USE_ICONV 1)
-	LIST(APPEND LIBGIT2_INCLUDES ${ICONV_INCLUDE_DIR})
+	LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR})
 	LIST(APPEND LIBGIT2_LIBS ${ICONV_LIBRARIES})
 	LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
 ENDIF()
@@ -454,14 +455,18 @@ LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal>)
 
 IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
 	INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
+	INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 ELSE()
 	TARGET_INCLUDE_DIRECTORIES(git2internal
 	    PRIVATE ${LIBGIT2_INCLUDES}
 	    PUBLIC ${libgit2_SOURCE_DIR}/include)
+	TARGET_INCLUDE_DIRECTORIES(git2internal
+	    SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
 ENDIF()
 
 SET(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE)
 SET(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE)
+SET(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE)
 SET(LIBGIT2_LIBS ${LIBGIT2_LIBS} PARENT_SCOPE)
 
 IF(XCODE_VERSION)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 82bf6d0..e0eb673 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -32,6 +32,7 @@ SET_SOURCE_FILES_PROPERTIES(
 	PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clar.suite)
 
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
+INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 
 ADD_EXECUTABLE(libgit2_clar ${SRC_CLAR} ${SRC_TEST} ${LIBGIT2_OBJECTS})