Skip to content

Commit 13cb9f7

Browse files
committed
cmake: correctly detect if system provides regcomp
We assume that if we are on Win32, Amiga OS, Solaris or SunOS, that the regcomp(3P) function cannot be provided by the system. Thus we will in these cases always include our own, bundled regex sources to make a regcomp implementation available. This test is obviously very fragile, and we have seen it fail on MSYS2/MinGW systems, which do in fact provide the regcomp symbol. The effect is that during compilation, we will use the "regex.h" header provided by MinGW, but use symbols provided by ourselves. This in fact may cause subtle memory layout issues, as the structure made available via MinGW doesn't match what our bundled code expects. There's one more problem with our regex detection: on the listed platforms, we will incorrectly include the bundled regex code even in case where the system provides regcomp_l(3), but it will never be used for anything. Fix the issue by improving our regcomp detection code. Instead of relying on a fragile listing of platforms, we can just use `CHECK_FUNCTION_EXISTS` instead. This will not in fact avoid the header-ordering problem. But we can assume that as soon as a system-provided "regex.h" header is provided, that `CHECK_FUNCTION_EXISTS` will now correctly find the desired symbol and thus not include our bundled regex code.
1 parent bc5b19e commit 13cb9f7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/CMakeLists.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,23 @@ IF (ENABLE_TRACE STREQUAL "ON")
4848
ENDIF()
4949
ADD_FEATURE_INFO(tracing GIT_TRACE "tracing support")
5050

51+
# Use `regcomp_l` if available
5152
CHECK_SYMBOL_EXISTS(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
5253
IF (HAVE_REGCOMP_L)
5354
SET(GIT_USE_REGCOMP_L 1)
5455
ENDIF ()
5556

57+
# Otherwise, we either want to use system's `regcomp` or our
58+
# bundled regcomp code, if system doesn't provide `regcomp`.
59+
IF(NOT HAVE_REGCOMP_L)
60+
CHECK_FUNCTION_EXISTS(regcomp HAVE_REGCOMP)
61+
IF(NOT HAVE_REGCOMP)
62+
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/regex" "${libgit2_BINARY_DIR}/deps/regex")
63+
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
64+
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
65+
ENDIF()
66+
ENDIF()
67+
5668
CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
5769
IF (HAVE_FUTIMENS)
5870
SET(GIT_USE_FUTIMENS 1)
@@ -294,13 +306,6 @@ ELSE()
294306
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend ${SHA1_BACKEND}")
295307
ENDIF()
296308

297-
# Include POSIX regex when it is required
298-
IF(WIN32 OR AMIGA OR CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
299-
ADD_SUBDIRECTORY("${libgit2_SOURCE_DIR}/deps/regex" "${libgit2_BINARY_DIR}/deps/regex")
300-
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES "${libgit2_SOURCE_DIR}/deps/regex")
301-
LIST(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:regex>)
302-
ENDIF()
303-
304309
# Optional external dependency: http-parser
305310
FIND_PACKAGE(HTTP_Parser)
306311
IF (USE_EXT_HTTP_PARSER AND HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)

0 commit comments

Comments
 (0)