Skip to content

cmake: fix preconditions for option CURL_STATIC_CRT=ON - #23336

Closed
vszakats wants to merge 7 commits into
curl:masterfrom
vszakats:cm-msvc-static-crt-fix
Closed

vszakats wants to merge 7 commits into
curl:masterfrom
vszakats:cm-msvc-static-crt-fix

Conversation

@vszakats

@vszakats vszakats commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Limit this option to static-only curl builds.

Enabling the static CRT for other configurations may successfuly build
them, but executing them do run into nasty problems due to using two CRT
copies at the same time. Also note that the static CRT setting applies
to tests too, which revealed these issues.

Also:

  • as it turns out, static CRT may be enabled for non-UCRT builds. After
    this patch, such configuration is possible again.
  • note, if these preconditions prevent doing a desired static-CRT build
    variation (e.g. a libcurl.dll built against static CRT). You can
    pass the necessary compiler/linker options via standard CMake options/envs.

I discovered these issues while enabling test runs on AppVeyor CI with
all supported MSVC version in various configurations, and
-DCURL_STATIC_CRT=ON set.

Symptoms of double CRT:

  • curl -V output with newline missing between 'Protocols:' and
    'Features:' lines, then runtests unable to detect feature and emitting
    a Perl warning for uninitialized $feat variable. (due to parallel
    output buffering for puts() and curl_mprintf())
    This is visible in master builds, e.g.:
    CM VS2019, Debug, x64, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests:
    
    
    WARNING: this libcurl is Debug-enabled, do not use in production
    
    curl 8.23.0-DEV (Windows) libcurl/8.23.0-DEV OpenSSL/3.0.18 (Schannel) WinLDAP
    Release-Date: [unreleased]
    Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt mqtts pop3 pop3s rtsp smtp smtps telnet tftp ws wssFeatures: alt-svc AsynchDNS Debug HSTS HTTPS-proxy HTTPSIG IPv6 Kerberos Largefile MultiSSL SPNEGO SSL SSPI threadsafe UnixSockets
    
    Ref: https://ci.appveyor.com/project/curlorg/curl/builds/54865570/job/urr2rgnr32hwllw2#L246
    Notice the missing newline before Features:, and appearing above
    WARNING: instead.
  • test1622 failing.
  • 'Debug Assertion Failed! Expression: _acrt_first_block == header'
    crashed in both the app and the libcurl.dll it loaded. (possibly)

Follow-up to 6140746 #16522
Follow-up to 049352d #16516
Follow-up to edfa537 #16456 #16394
Follow-up to 4fc6ebe #1621
Cherry-picked from #23306


  • perhaps allow static CRT for building libcurl.dll alone? i.e. without
    building curl tool and tests. This could work, though my understanding
    is that such dll needs to be used carefully to avoid hitting the issues
    that affected curl tests for example.
    (my understanding, is that building a static curl tool with static CRT,
    and building a shared libcurl with static CRT, in the same build pass
    is not possible. Or would need to split the shared/static pass into
    two, based on this condition. But even then, tests would not build or
    work correctly, unless maybe they are never built with static CRT. In
    which case though, e.g. tunits isn't really mimicing the real curl tool.
    Tricky.) -- edit: a libcurl.dll with a static CRT seems like an even more
    niche use-case after sleeping on it. The patch below may allow this,
    but it seems to be causing more headaches than it may solve. If you're
    aware of a reasonably non-niche example, please leave a comment.
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -376,10 +376,12 @@ else()
   set(LIB_SELECTED ${LIB_STATIC})
 endif()
 
+option(BUILD_TESTING "Build tests" ON)
+
 if(WIN32)
   option(CURL_STATIC_CRT "Build libcurl with static CRT with MSVC (/MT)" OFF)
   if(CURL_STATIC_CRT AND MSVC)
-    if(NOT BUILD_SHARED_LIBS)
+    if(NOT BUILD_SHARED_LIBS OR (NOT BUILD_CURL_EXE AND NOT BUILD_TESTING))
       set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
       set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "$<$<CONFIG:Release>:-MT>")
       set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "$<$<CONFIG:Debug>:-MTd>")
@@ -1928,7 +1930,6 @@ set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
 set(_project_config "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake")
 set(_version_config "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
 
-option(BUILD_TESTING "Build tests" ON)
 if(BUILD_TESTING AND Perl_FOUND)
   set(CURL_BUILD_TESTING ON)
 else()

Let's drop the static CRT idea for a while, it's a mess:
https://ci.appveyor.com/project/curlorg/curl/builds/54862718/job/f2nbu2lupscfskb1

Also existing logic is incorrect. Pbly the version gate is wrong
(should be extended to all VS version *),
plus it should not only be limited to static curl.exe, but also
to static test binaries. If a static-CRT binary uses libcurl DLL,
there are two active CRTs, which is causing several of the oddities
seen since yesterday, when trying to run tests.
It cannot and does not work. Crashes, parallel buffering of
curl_mprintf() and puts(), causing lines output in the wrong
order -> Protocol/Feature -V lines sticking together -> Perl
unable to parse the Features: line -> Perl warning about
uninit $feat var.

Also possibly the assert/debug crash on exit is also a result
of this.
…ls on old MSVCs"

This reverts commit f6265f2.

This doesn't seem necessary, static CRT seems to work now with older MSVC.
Jobs are still flaky, so this assesment may yet change.
@vszakats vszakats added cmake Windows Windows-specific labels Oct 9, 2026
@vszakats
vszakats marked this pull request as draft October 9, 2026 17:58
@github-actions github-actions Bot added the build label Oct 9, 2026
@vszakats
vszakats marked this pull request as ready for review October 9, 2026 18:21
@vszakats vszakats changed the title cmake: fix conditions for CURL_STATIC_CRT=ON cmake: fix pre-conditions for CURL_STATIC_CRT=ON Oct 9, 2026
@vszakats vszakats changed the title cmake: fix pre-conditions for CURL_STATIC_CRT=ON cmake: fix pre-conditions for CURL_STATIC_CRT=ON option Oct 9, 2026
@vszakats
vszakats requested a lite review from Copilot October 9, 2026 18:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues remain.

0 open findings

What changed in this PR

Restricts CURL_STATIC_CRT=ON to static-only CMake builds to avoid mixed CRT runtime issues.

Changes:

  • Applies static CRT settings only when shared libraries are disabled.
  • Updates CMake documentation and warning text.
File Summary
docs/​INSTALL-CMAKE.md Documents static library and executable requirements.
CMakeLists.txt Restricts static CRT configuration to static builds.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jay

jay commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

It should be possible to use a DLL with its own static CRT though typically if there's a static CRT it is set in the configuration you are aiming for. However I think that requiring this could possibly break builds (of course they could adjust) but I just don't know what use cases there are for this. Maybe there are some? I'd save this to the next feature window, or maybe ask on the mailing list if anyone is doing it the DLL+static CRT way and why.

@vszakats

vszakats commented Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

It should be possible to use a DLL with its own static CRT though typically if there's a static CRT it is set in the configuration you are aiming for. However I think that requiring this could possibly break builds (of course they could adjust) but I just don't know what use cases there are for this. Maybe there are some? I'd save this to the next feature window, or maybe ask on the mailing list if anyone is doing it the DLL+static CRT way and why.

Yes, I suspect there may be possible valid cases when a static CRT makes
sense, and may even work, but it becomes so complicated, that I'm not
planning to go there this time. If someone is interested in those, it's always
possible to pass the necessary compiler/linker options manually to CMake,
so one does not need to depend on this option for cases it doesn't cover.
It's also an option to run multiple builds, if someone is interested in a curl tool
with static CRT and a libcurl.dll in addition.

Based on the little/no feedback, or requests, my suspicion is that there is very
little demand for all this, esp. considering it's always been somewhat broken.

Waiting for the next feature window sounds overly long to me, it'd block the
other work pending in #23306. I view this as a bugfix.

@vszakats vszakats changed the title cmake: fix pre-conditions for CURL_STATIC_CRT=ON option cmake: fix preconditions for CURL_STATIC_CRT=ON option Oct 9, 2026
@vszakats vszakats changed the title cmake: fix preconditions for CURL_STATIC_CRT=ON option cmake: fix preconditions for option CURL_STATIC_CRT=ON Oct 9, 2026
@vszakats
vszakats marked this pull request as draft October 9, 2026 23:18
@MarcelRaad

Copy link
Copy Markdown
Member

I guess I'm missing something. What exactly does this block or fix? Isn't only the case when the user explicitly requests CURL_STATIC_CRT manually affected, and the proper fix to just not request that explicitly for cases in which it doesn't work correctly?

What I'm concerned about is if the user e.g. builds both static and shared versions of libcurl, without tests or the tool, and uses a static CRT in their application. Before this, they got versions working correctly, even if they had to know what they're doing for the DLL. Now, after updating libcurl, they get their manually set flag ignored, a CMake warning that's easy to miss, and later linker errors in their application that they need to track down. For such cases, I'd rather let the curl build fail than doing something different to what's explicitly requested by the user.

@vszakats

vszakats commented Oct 10, 2026 •

Copy link
Copy Markdown
Member Author

I guess I'm missing something. What exactly does this block or fix? Isn't only the case when the user explicitly requests CURL_STATIC_CRT manually affected, and the proper fix to just not request that explicitly for cases in which it doesn't work correctly?

When passing CURL_STATIC_CRT=ON, the logic deciding if it's
a valid option or not was incorrect. This lead me spending 2-3 days
trying to figure out why the most basic tests are broken, including
such basic thing as curl -V. IMO this is not how a custom option
should work. Hence this fix.

What I'm concerned about is if the user e.g. builds both static and shared versions of libcurl, without tests or the tool, and uses a static CRT in their application. Before this, they got versions working correctly, even if they had to know what they're doing for the DLL. Now, after updating libcurl, they get their manually set flag ignored, a CMake warning that's easy to miss, and later linker errors in their application that they need to track down. For such cases, I'd rather let the curl build fail than doing something different to what's explicitly requested by the user.

I don't find this useful, sorry. Is this a common case, that suddently
will cause breakage for many users? It's my 3rd round spending a
lot of effort into crashing/broken binaries caused by static CRT, yet,
so far nobody else was interested to give any (constructive) feedback.
And now suddently a corner case is the biggest concern. And we're
also prohibited to show a warning, because nobody is reading those?

Frustrating!

edit: What would be helpful is any actual proposal or idea that help
fixing the issues at hand, while addressing any concern that may
remain.

@vszakats
vszakats marked this pull request as ready for review October 10, 2026 16:36
@MarcelRaad

Copy link
Copy Markdown
Member

I'm not saying I'm against this, or that strongly suggesting to users not to use this combination is a bad idea.

What I'm trying to say is that I'd rather turn the warning into an error instead of doing the opposite of what the user explicitly requests. Either way, this is a breaking change for users running into this case as they will get a broken build - either immediately when building libcurl, or later when building their application. As a user, I always prefer the former.

@vszakats

vszakats commented Oct 10, 2026 •

Copy link
Copy Markdown
Member Author

I'm not saying I'm against this, or that strongly suggesting to users not to use this combination is a bad idea.

What I'm trying to say is that I'd rather turn the warning into an error instead of doing the opposite of what the user explicitly requests. Either way, this is a breaking change for users running into this case as they will get a broken build - either immediately when building libcurl, or later when building their application. As a user, I always prefer the former.

For testing purposes this option is enabled for all jobs in AppVeyor CI,
relying on the CMake logic to leave it enabled when we know it works.
Making it a fatal error would need to manage it manually for each job,
also losing test coverage.

We aim to warn only when it makes sense to avoid fatigue; for this
reason I expect warnings to be observed. If the warning is unnoticed,
wouldn't the build be different enough to cause a noticable fallout
afterwards? Either way, I'm not sure we can fix that. This feature also
falls into the 'static build' category, which is inherently complex and
fragile, therefore also assumes extra altertness, I think.

vszakats added a commit that referenced this pull request Oct 15, 2026
When a curl binary is built with Visual Studio in debug mode, it uses
the matching, debug-enabled CRT. The debug CRT by default pops up an
interactive GUI dialog by default, also on CI machines, both on AppVeyor
CI and GHA, on `assert()` and other low-level failures. This causes the
CI job to appear hung, with nothing printed to the CI log, invisibly
waiting for someone clicking on a GUI MessageBox. On a remote/headless
CI machine this is unworkable. The job then quits on timeout, after
blocking other queued tasks (for 1 hour on AppVeyor CI, 10-15 minutes in
GHA/windows).

To make such failures CI-friendly, fix by configuring this behavior in
`curl.exe`, `servers.exe` and unit test binaries, to output all errors
to stderr, and to `OutputDebugString()`, overiding the GUI popup.

This can save a lot of hair-pulling.

Example output, after this patch:
```
Assertion failed: r == 10, file C:\projects\curl\tests\libtest\first.c, line 272
   abort() has been called
```

Note, the default debug CRT behavior depends on a number of things, e.g.
on a Server Core (or similar) Windows edition, or when run under a
debugger. Assuming the default behavior is the expected one in most
cases, this patch only reconfigures it for builds made in the curl CI.
If you want to enable it, set the `CURL_CI` env to non-empty at
configure time, or build with the `-DCURL_DEBUG_MSVC_CRTDBG` C macro.

This issue does not apply to mingw-w64 builds, as tested with both UCRT
and MSVCRT. They output the assert text to the CI logs out-of-the-box.

Refs:
https://learn.microsoft.com/cpp/c-runtime-library/reference/crtsetreportfile
https://learn.microsoft.com/cpp/c-runtime-library/reference/crtsetreportmode
https://learn.microsoft.com/cpp/c-runtime-library/reference/set-error-mode

Ref: #23336
Ref: edfa537 #16456
Ref: #14854
Cherry-picked from #23306

Closes #23400
@vszakats vszakats closed this in af9d2dd Oct 16, 2026
@vszakats
vszakats deleted the cm-msvc-static-crt-fix branch October 16, 2026 10:11
@vszakats vszakats mentioned this pull request Oct 16, 2026
9 of 10 tasks
vszakats added a commit that referenced this pull request Oct 18, 2026
- in jobs building tests, also run them.
- to maintain CI cycle times, only run unit tests by default, and run
  all (supported) tests in just one job, carefully selected to be the
  one running them the fastest.
  Per job, it takes ~15 seconds to run ~80 unit tests.
  It takes 5 minutes to run all (~1600) tests. Total job time 5.5-6
  minutes.
  If the full test run turns out to be annoying or grow slow, we disable
  or limit it to a subset of tests.
- run tests with `-j10` parallelism. It results in the fastest runs,
  based on tests.
- to avoid the MSBuild test breaker heuristics, and to improve
  performance, run tests by directly invoking `runtests.pl` (not via
  CMake -> MSBuild -> `runtests.pl`).
  It saves 10-40 seconds per full test run.
- when running directly via `runtests.pl`, exclude test 1119 and 1167.
  It's not compatible with Visual Studio generator builds.
- run the artifact upload in parallel with running tests.

Running Windows tests in multiple environments makes it more likely they
can run on local machines, too, also making them easier to debug.
AppVeyor also supports remote access via RDP, which may help debugging
notorious Windows issues live, with some luck of reproducing it in a run
with RDP access explicitly enabled beforehand.

Fixes/improvements via this PR, merged separately:

dc99e12 #23467 processhelp.pm: improve task info in trace output (Windows)
70f3f85 #23471 test1488: drop unused C preprocessor logic
af9d2dd #23336 cmake: fix preconditions for option `CURL_STATIC_CRT=ON`
adf8b78 #23428 setopt: fix setting `CURLWS_NOAUTOPONG` with compilers lacking C99 bool type
780405b #23416 tests: add `--dump-module-paths` debug option (Windows)
bc22977 #23400 msvc: replace debug CRT GUI popups with stderr output
87a1dc7 #23396 appveyor: minor improvements
67f3510 #23381 runtests: add `-S` option to show success logs
e9af987 #23378 tests/server: fix crashing with VS2013 and earlier
8cdee8a #23377 lib557: exclude three floating point test cases for VS2013 and earlier
1aac64a #23375 test1119, 1167: disable MSYS2 argument conversion narrowly
49f003f #23337 unit1664: add workaround for VS2019 16.3 - VS2022 17.4 x64 miscompile
b8f71d6 #23345 test428: skip when running on older MSYS2 versions
2f3f99d #23339 test1119, 1167: disable MSYS2 argument conversion
098d3a0 #23332 runtests: unset `CURL_MEMDEBUG` for curl requests made to the CI API
d3e7caf #23335 GHA/windows: drop unused `-ac` runtests options
2874cc6 #23317 mqttd: work around bogus VS2019 32-bit compiler warning
e8adc72 #23315 schannel: fix guard for old MSVC workaround

Follow-up to 41198e0 #20390

Closes #23306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants