Skip to content

Commit c13e56f

Browse files
committed
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.
1 parent 9994cd3 commit c13e56f

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
2+
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
23

34
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
45
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})

src/CMakeLists.txt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SET(LIBGIT2_INCLUDES
1616
"${CMAKE_CURRENT_BINARY_DIR}"
1717
"${libgit2_SOURCE_DIR}/src"
1818
"${libgit2_SOURCE_DIR}/include")
19+
SET(LIBGIT2_SYSTEM_INCLUDES "")
1920
SET(LIBGIT2_LIBS "")
2021

2122
# Installation paths
@@ -94,7 +95,7 @@ ADD_FEATURE_INFO(threadsafe THREADSAFE "threadsafe support")
9495

9596
IF (WIN32 AND EMBED_SSH_PATH)
9697
FILE(GLOB SRC_SSH "${EMBED_SSH_PATH}/src/*.c")
97-
LIST(APPEND LIBGIT2_INCLUDES "${EMBED_SSH_PATH}/include")
98+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${EMBED_SSH_PATH}/include")
9899
FILE(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
99100
SET(GIT_SSH 1)
100101
ENDIF()
@@ -107,7 +108,7 @@ IF (WIN32 AND WINHTTP)
107108
IF (MINGW)
108109
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
109110
LIST(APPEND LIBGIT2_LIBS winhttp)
110-
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
111+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
111112
ELSE()
112113
LIST(APPEND LIBGIT2_LIBS "winhttp")
113114
LIST(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
@@ -121,7 +122,7 @@ ELSE ()
121122
ENDIF ()
122123
IF (CURL_FOUND)
123124
SET(GIT_CURL 1)
124-
LIST(APPEND LIBGIT2_INCLUDES ${CURL_INCLUDE_DIRS})
125+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${CURL_INCLUDE_DIRS})
125126
LIST(APPEND LIBGIT2_LIBS ${CURL_LIBRARIES})
126127
LIST(APPEND LIBGIT2_PC_LIBS ${CURL_LDFLAGS})
127128
ENDIF()
@@ -174,7 +175,7 @@ IF (USE_HTTPS)
174175
ENDIF()
175176

176177
SET(GIT_SECURE_TRANSPORT 1)
177-
LIST(APPEND LIBGIT2_INCLUDES ${SECURITY_INCLUDE_DIR})
178+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${SECURITY_INCLUDE_DIR})
178179
LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_LIBRARIES} ${SECURITY_LIBRARIES})
179180
LIST(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS})
180181
ELSEIF (HTTPS_BACKEND STREQUAL "OpenSSL")
@@ -183,7 +184,7 @@ IF (USE_HTTPS)
183184
ENDIF()
184185

185186
SET(GIT_OPENSSL 1)
186-
LIST(APPEND LIBGIT2_INCLUDES ${OPENSSL_INCLUDE_DIR})
187+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${OPENSSL_INCLUDE_DIR})
187188
LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
188189
LIST(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS})
189190
LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
@@ -230,7 +231,7 @@ IF (USE_HTTPS)
230231
ENDIF()
231232

232233
SET(GIT_MBEDTLS 1)
233-
LIST(APPEND LIBGIT2_INCLUDES ${MBEDTLS_INCLUDE_DIR})
234+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
234235
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
235236
# mbedTLS has no pkgconfig file, hence we can't require it
236237
# https://github.com/ARMmbed/mbedtls/issues/228
@@ -285,7 +286,7 @@ ELSEIF (SHA1_BACKEND STREQUAL "mbedTLS")
285286
ADD_FEATURE_INFO(SHA ON "using mbedTLS")
286287
SET(GIT_SHA1_MBEDTLS 1)
287288
FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
288-
LIST(APPEND LIBGIT2_INCLUDES ${MBEDTLS_INCLUDE_DIR})
289+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
289290
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
290291
# mbedTLS has no pkgconfig file, hence we can't require it
291292
# https://github.com/ARMmbed/mbedtls/issues/228
@@ -298,21 +299,21 @@ ENDIF()
298299
# Include POSIX regex when it is required
299300
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
300301
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/regex" "${libgit2_BINARY_DIR}/deps/regex")
301-
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
302+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
302303
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
303304
ENDIF()
304305

305306
# Optional external dependency: http-parser
306307
FIND_PACKAGE(HTTP_Parser)
307308
IF (USE_EXT_HTTP_PARSER AND HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
308-
LIST(APPEND LIBGIT2_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
309+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
309310
LIST(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
310311
LIST(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
311312
ADD_FEATURE_INFO(http-parser ON "http-parser support")
312313
ELSE()
313314
MESSAGE(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
314315
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
315-
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
316+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
316317
LIST(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
317318
ADD_FEATURE_INFO(http-parser ON "http-parser support (bundled)")
318319
ENDIF()
@@ -321,7 +322,7 @@ ENDIF()
321322
IF(NOT USE_BUNDLED_ZLIB)
322323
FIND_PACKAGE(ZLIB)
323324
IF(ZLIB_FOUND)
324-
LIST(APPEND LIBGIT2_INCLUDES ${ZLIB_INCLUDE_DIRS})
325+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS})
325326
LIST(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
326327
IF(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
327328
LIST(APPEND LIBGIT2_LIBS "z")
@@ -336,7 +337,7 @@ IF(NOT USE_BUNDLED_ZLIB)
336337
ENDIF()
337338
IF(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
338339
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
339-
LIST(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
340+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
340341
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
341342
ADD_FEATURE_INFO(zlib ON "using bundled zlib")
342343
ENDIF()
@@ -347,7 +348,7 @@ IF (USE_SSH)
347348
ENDIF()
348349
IF (LIBSSH2_FOUND)
349350
SET(GIT_SSH 1)
350-
LIST(APPEND LIBGIT2_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
351+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
351352
LIST(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
352353
LIST(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
353354

@@ -376,7 +377,7 @@ IF (USE_ICONV)
376377
ENDIF()
377378
IF (ICONV_FOUND)
378379
SET(GIT_USE_ICONV 1)
379-
LIST(APPEND LIBGIT2_INCLUDES ${ICONV_INCLUDE_DIR})
380+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${ICONV_INCLUDE_DIR})
380381
LIST(APPEND LIBGIT2_LIBS ${ICONV_LIBRARIES})
381382
LIST(APPEND LIBGIT2_PC_LIBS ${ICONV_LIBRARIES})
382383
ENDIF()
@@ -454,14 +455,18 @@ LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:git2internal>)
454455

455456
IF (${CMAKE_VERSION} VERSION_LESS 2.8.12)
456457
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
458+
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
457459
ELSE()
458460
TARGET_INCLUDE_DIRECTORIES(git2internal
459461
PRIVATE ${LIBGIT2_INCLUDES}
460462
PUBLIC ${libgit2_SOURCE_DIR}/include)
463+
TARGET_INCLUDE_DIRECTORIES(git2internal
464+
SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES})
461465
ENDIF()
462466

463467
SET(LIBGIT2_OBJECTS ${LIBGIT2_OBJECTS} PARENT_SCOPE)
464468
SET(LIBGIT2_INCLUDES ${LIBGIT2_INCLUDES} PARENT_SCOPE)
469+
SET(LIBGIT2_SYSTEM_INCLUDES ${LIBGIT2_SYSTEM_INCLUDES} PARENT_SCOPE)
465470
SET(LIBGIT2_LIBS ${LIBGIT2_LIBS} PARENT_SCOPE)
466471

467472
IF(XCODE_VERSION)

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SET_SOURCE_FILES_PROPERTIES(
3232
PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clar.suite)
3333

3434
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
35+
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
3536

3637
ADD_EXECUTABLE(libgit2_clar ${SRC_CLAR} ${SRC_TEST} ${LIBGIT2_OBJECTS})
3738

0 commit comments

Comments
 (0)