]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cmake: restrict static CRT builds to static curl exe, test in CI
authorViktor Szakats <commit@vsz.me>
Mon, 24 Feb 2025 16:27:57 +0000 (17:27 +0100)
committerViktor Szakats <commit@vsz.me>
Mon, 24 Feb 2025 20:00:31 +0000 (21:00 +0100)
Static CRT crashes MSVCR* MSVC builds (in VS2008, VS2010, VS2012,
VS2013) according to CI and local tests. The reproducible crash happens
in `curl_mfprintf() -> fputc(s, stderr)` when trying to display the
warning message in `curl -V`. `stderr` is non-NULL and resolves to `2`.
This reproducer needs a debug-enabled build, but it's unrelated to debug
features or curl's memory tracker. It happens regardless of unity build,
CPU architecture or `DllMain()` use. Example from VS2013:

```
+ _bld/src/Debug/curl.exe --disable --version
./appveyor.sh: line 124:   203 Segmentation fault      "${curl}" --disable --version
```
Ref: https://ci.appveyor.com/project/curlorg/curl/builds/51570451/job/ojpdqrsm1hmpmq6a#L210

Another crash happened in an UCRT build (VS2017) with a couple of
`printf()`s added to curl's `main()` function:

```
Microsoft Visual C++ Runtime Library
Debug Assertion Failed!
Program: C:/projects/curl/bld/src/Debug/curl.exe
File: minkernel/crts/ucrt/src/appcrt/heap/debug_heap.cpp
Line: 996
Expression: _act_first_block == header
```
(it hangs the job in CI due to the GUI popup)
Ref: https://github.com/curl/curl/pull/16394#issuecomment-2677181716

To avoid actual and potential issues, this patch issues a warning on
the shared-libcurl + static-CRT combination and falls back to the
default, shared CRT. IOW a static CRT build now requires a static curl
exe when using the `CURL_STATIC_CRT=ON` option.

Follow-up to 4fc6ebe18a607764194ee23e5aa898a027fe3c60 #1621
Cherry-picked from #16394 (with more details there)

Closes #16456

CMakeLists.txt
appveyor.sh
docs/INSTALL-CMAKE.md

index 256b5d6679f106e273850facb4ccb602471e7115..be4a1822aee42acc6b829d3d556516e05debd06c 100644 (file)
@@ -217,13 +217,6 @@ option(ENABLE_ARES "Enable c-ares support" OFF)
 option(CURL_DISABLE_INSTALL "Disable installation targets" OFF)
 
 if(WIN32)
-  option(CURL_STATIC_CRT "Build libcurl with static CRT with MSVC (/MT)" OFF)
-  if(CURL_STATIC_CRT AND MSVC)
-    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
-    string(APPEND CMAKE_C_FLAGS_RELEASE " -MT")
-    string(APPEND CMAKE_C_FLAGS_DEBUG " -MTd")
-  endif()
-
   option(ENABLE_UNICODE "Use the Unicode version of the Windows API functions" OFF)
   if(WINDOWS_STORE OR WINCE)
     set(ENABLE_UNICODE ON)
@@ -356,6 +349,20 @@ else()
   set(LIB_SELECTED ${LIB_STATIC})
 endif()
 
+if(WIN32)
+  option(CURL_STATIC_CRT "Build libcurl with static CRT with MSVC (/MT)" OFF)
+  if(CURL_STATIC_CRT AND MSVC)
+    if(BUILD_STATIC_CURL)
+      set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
+      string(APPEND CMAKE_C_FLAGS_RELEASE " -MT")
+      string(APPEND CMAKE_C_FLAGS_DEBUG " -MTd")
+    else()
+      message(WARNING "Static CRT requires curl executable built with static libcurl "
+        "(BUILD_STATIC_LIBS=ON and BUILD_STATIC_CURL=ON).")
+    endif()
+  endif()
+endif()
+
 # Override to force-disable or force-enable the use of pkg-config.
 if((UNIX AND NOT ANDROID AND (NOT APPLE OR CMAKE_SYSTEM_NAME MATCHES "Darwin")) OR
    VCPKG_TOOLCHAIN OR
index d1363e72e6cbf53e740d06ad63cf46e98242bcc5..9388a6d33b69fabc58ef7273567f1e5aa7834cf2 100644 (file)
@@ -58,6 +58,7 @@ if [ "${BUILD_SYSTEM}" = 'CMake' ]; then
       -DCMAKE_UNITY_BUILD="${UNITY}" -DCURL_TEST_BUNDLES=ON \
       -DCURL_WERROR=ON \
       -DBUILD_SHARED_LIBS="${SHARED}" \
+      -DCURL_STATIC_CRT=ON \
       -DENABLE_DEBUG="${DEBUG}" \
       -DENABLE_UNICODE="${ENABLE_UNICODE}" \
       -DHTTP_ONLY="${HTTP_ONLY}" \
index e55d951496688bc719de463e56377ae23eaf55f2..4479d4a36575c61d69f17fb120a6a50c2f22188d 100644 (file)
@@ -160,7 +160,7 @@ assumes that CMake generates `Makefile`:
 - `CURL_LIBCURL_VERSIONED_SYMBOLS`:         Enable libcurl versioned symbols. Default: `OFF`
 - `CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX`:  Override default versioned symbol prefix. Default: `<TLS-BACKEND>_` or `MULTISSL_`
 - `CURL_LTO`:                               Enable compiler Link Time Optimizations. Default: `OFF`
-- `CURL_STATIC_CRT`:                        Build libcurl with static CRT with MSVC (`/MT`). Default: `OFF`
+- `CURL_STATIC_CRT`:                        Build libcurl with static CRT with MSVC (`/MT`) (requires static curl executable). Default: `OFF`
 - `CURL_TARGET_WINDOWS_VERSION`:            Minimum target Windows version as hex string.
 - `CURL_TEST_BUNDLES`:                      Bundle `libtest` and `unittest` tests into single binaries. Default: `OFF`
 - `CURL_WERROR`:                            Turn compiler warnings into errors. Default: `OFF`