Files
jak-project/third-party/curl/docs/examples/log_failed_transfers.c
T
Alexander J. Semenuk 5d5e35fb9b fix: Windows toolchain compatibility (curl 8.21 re-vendor, endless reconfigure loop) (#4355)
## Problem

Windows builds break with a current local toolchain (Scoop LLVM 22.1.8,
CMake 4.4.0, VS 2026), in two independent ways:

1. The build stops at curl's deliberate guard: `#error "no non-blocking
method was found/used/set"` in `third-party/curl/lib/nonblock.c`.
2. From the second configure onward, `cmake --build` re-runs CMake in an
endless loop (observed 42 consecutive reconfigure cycles in a single
build). Likely the same mechanism behind the "endlessly building" VS
2026 note in `docs/setup/dev/vs.md`.

## Root cause

1. `third-party/curl/CMake/CurlTests.c` passes `int *` to
`ioctlsocket()`, whose third parameter is `u_long *`. Clang 22 promotes
`-Wincompatible-pointer-types` to a hard error in C, so the
`HAVE_IOCTLSOCKET_FIONBIO` try_compile silently fails and
`curl_config.h` never defines it. Upstream CI does not see this because
the windows-2022 runner image ships an older LLVM. GCC 14 promotes the
same warning to a hard error, which is very likely the `CurlTests.c.obj`
failure reported from MSYS2 in open-goal/jak-project#3551. Upstream curl
hit the identical problem with GCC 14 and fixed the probe in curl 8.8.0
(curl/curl#13578).
2. The root CMakeLists copies the build tree's `compile_commands.json`
into `<src>/build/` for clangd using `configure_file()`, which registers
its input as a configure dependency. CMake rewrites
`compile_commands.json` late in every generation, after
`CTestTestfile.cmake` and `cmake_install.cmake` (outputs of the same
Ninja regen rule), so once the dependency is registered the rule is
deterministically dirty and every `ninja` invocation re-runs CMake. A
pristine first configure is safe (the file does not exist yet, so the
`if(EXISTS ...)` guard skips the copy), which is why the loop looks
machine- or IDE-specific.

## Fix

1. Per review, re-vendor `third-party/curl` at the `curl-8_21_0` tag
(previously `curl-8_3_0`), which carries the upstream probe fix plus two
years of upstream development; `vendor.yaml` updated to match.
Adjustments the version jump forced:
- curl 8.15 removed the native macOS Secure Transport backend
(`CURL_USE_SECTRANSP`), so macOS now builds curl against OpenSSL like
Linux. The two macOS workflows install Homebrew `openssl@3` and export
`OPENSSL_ROOT_DIR` (keg-only), and the macOS setup docs gained the same
two lines.
- `CURL_BROTLI` / `CURL_ZSTD` switched to AUTO-detection in curl 8.10;
pinned OFF to keep the previous no-compression behavior and avoid
silently linking whatever the CI images happen to have.
- curl's new top-level `BUILD_EXAMPLES` cache option (default ON) leaked
into discord-rpc's identically named option and broke configure at a
nonexistent `examples/send-presence` directory; pinned OFF ahead of the
third-party subdirectories.

The diff is dominated by the mechanical tag-tree swap under
`third-party/curl` (linguist-vendored, collapsed in review). The
hand-written changes are `CMakeLists.txt`, the two macOS workflows,
`docs/setup/system/macos.md`, and `vendor.yaml`.
2. Swap `configure_file()` for `file(COPY ...)`: the same clangd copy
with no configure dependency registered. (`file(COPY_FILE ...
ONLY_IF_DIFFERENT)` would be cleaner still but requires CMake 3.21,
above the declared `cmake_minimum_required(VERSION 3.10)`.)

## Test plan

- [x] Fresh `cmake --preset Release-windows-clang` (LLVM 22, no cache
seeding) completes and logs `Enabled SSL backends: Schannel`; the
FIONBIO probe passes without the previous `#error`
- [x] Full Windows Release build from scratch in the branch worktree
(all 1422 targets)
- [x] goalc-test suite: 1509 passed, 0 failed
- [x] Second consecutive configure with `compile_commands.json` present:
the regen rule in `build.ninja` has no `compile_commands.json` input;
`<src>/build/compile_commands.json` is still refreshed for clangd
- [x] Repeated `ninja` invocations after a full build no longer re-run
CMake
- [x] macOS Intel and ARM CI green (first exercise of the OpenSSL
backend switch)

---

I work off a self-hosted forge, so this GitHub account is quiet; the
configure logs and ninja dirty-node traces from the investigation are
available if anyone wants the raw data.

(AI-assisted)
2026-07-27 19:19:18 -04:00

338 lines
8.7 KiB
C
Vendored
Generated

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Save failed transfer verbose log to disk
* </DESC>
*/
/*
*
* This example demonstrates per-transfer verbose logging to memory.
* The transfer's log is written to disk only if the transfer fails.
*
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen(), strerror(), vsnprintf() */
#endif
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <curl/curl.h>
#ifdef _WIN32
#include <windows.h>
#define unlink _unlink
#else
#include <strings.h>
#include <unistd.h>
#endif
struct mem {
/* 'buf' points to memory contents that is always null-terminated so that it
can be treated like a string if appropriate. 'recent' points to the most
recent data written to 'buf'. */
char *buf, *recent;
/* 'len' and 'allocsize' are the length and allocated size of 'buf' */
size_t len, allocsize;
};
struct transfer {
const char *url, *bodyfile, *logfile;
struct mem log;
FILE *bodyfp;
CURL *curl;
};
static void mem_reset(struct mem *mem)
{
free(mem->buf);
mem->buf = NULL;
mem->recent = NULL;
mem->len = 0;
mem->allocsize = 0;
}
/* expand free buffer space to needed size. return -1 or 'needed'. */
static int mem_need(struct mem *mem, size_t needed)
{
char *newbuf;
size_t newsize;
if(needed > (unsigned)INT_MAX)
return -1;
if(needed <= (mem->allocsize - mem->len))
return (int)needed;
/* min 4k makes reallocations much less frequent when lengths are small */
newsize = needed < 4096 ? 4096 : needed;
newsize += mem->len;
if(newsize < mem->len || newsize > (unsigned)INT_MAX)
return -1;
newbuf = realloc(mem->buf, newsize);
if(!newbuf)
return -1;
if(mem->recent && mem->buf != newbuf)
mem->recent = newbuf + (mem->recent - mem->buf);
mem->buf = newbuf;
mem->allocsize = newsize;
return (int)needed;
}
static int mem_addn(struct mem *mem, const char *buf, size_t len)
{
if(len + 1 < len || mem_need(mem, len + 1) < 0)
return -1;
mem->recent = mem->buf + mem->len;
memcpy(mem->recent, buf, len);
mem->len += len;
mem->buf[mem->len] = '\0';
return (int)len;
}
static int mem_add(struct mem *mem, const char *str)
{
return mem_addn(mem, str, strlen(str));
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 2, 3)))
#endif
static int mem_addf(struct mem *mem, const char *format, ...)
{
int i, x;
va_list va;
/* we need about 100 chars or less to write 95% of lines */
x = 128;
/* first try: there is probably enough memory to write everything.
second try: there is definitely enough memory to write everything. */
for(i = 0; i < 2; ++i) {
if(x < 0 || mem_need(mem, (size_t)x + 1) < 0)
break;
va_start(va, format);
x = vsnprintf(mem->buf + mem->len, mem->allocsize - mem->len, format, va);
va_end(va);
if(x >= 0 && (size_t)x < (mem->allocsize - mem->len)) {
mem->recent = mem->buf + mem->len;
mem->len += (size_t)x;
return x;
}
#ifdef _WIN32
/* Not all versions of Windows CRT vsnprintf are compliant with C99. Some
return -1 if buffer too small. Try _vscprintf to get the needed size. */
if(!i && x < 0) {
va_start(va, format);
x = _vscprintf(format, va);
va_end(va);
}
#endif
}
if(mem->buf)
mem->buf[mem->len] = '\0';
return -1;
}
static int mydebug(CURL *curl, curl_infotype type,
char *data, size_t size, void *userdata)
{
struct transfer *t = (struct transfer *)userdata;
static const char s_infotype[CURLINFO_END][3] = {
"* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
(void)curl;
switch(type) {
case CURLINFO_TEXT:
case CURLINFO_HEADER_OUT:
case CURLINFO_HEADER_IN:
/* mem_addn is faster than passing large data as %s to mem_addf */
mem_addn(&t->log, s_infotype[type], 2);
mem_addn(&t->log, data, size);
if(!size || data[size - 1] != '\n')
mem_add(&t->log, "\n");
break;
default:
break;
}
return 0;
}
static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
{
struct transfer *t = (struct transfer *)userdata;
return fwrite(ptr, size, nmemb, t->bodyfp);
}
int main(void)
{
CURLcode result;
unsigned i;
int total_failed = 0;
char errbuf[CURL_ERROR_SIZE] = { 0, };
struct transfer transfer[2];
memset(transfer, 0, sizeof(transfer));
transfer[0].url = "https://httpbin.org/get";
transfer[0].bodyfile = "200.txt";
transfer[0].logfile = "200_transfer_log.txt";
transfer[1].url = "https://httpbin.org/status/400";
transfer[1].bodyfile = "400.txt";
transfer[1].logfile = "400_transfer_log.txt";
result = curl_global_init(CURL_GLOBAL_ALL);
if(result != CURLE_OK) {
fprintf(stderr, "curl_global_init failed\n");
return (int)result;
}
/* You could enable global tracing for extra verbosity when verbosity is
enabled for a transfer. */
#if 0
curl_global_trace("all");
#endif
for(i = 0; i < sizeof(transfer) / sizeof(transfer[0]); ++i) {
int failed = 0;
struct transfer *t = &transfer[i];
t->curl = curl_easy_init();
if(!t->curl) {
fprintf(stderr, "curl_easy_init failed\n");
curl_global_cleanup();
return 1;
}
curl_easy_setopt(t->curl, CURLOPT_URL, t->url);
/* Enable following redirects */
curl_easy_setopt(t->curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Enable verbose logging to memory */
curl_easy_setopt(t->curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(t->curl, CURLOPT_DEBUGFUNCTION, mydebug);
curl_easy_setopt(t->curl, CURLOPT_DEBUGDATA, t);
/* Enable writing the body to a file */
curl_easy_setopt(t->curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(t->curl, CURLOPT_WRITEDATA, t);
/* Enable immediate error on HTTP status codes >= 400 in most cases,
instead of downloading the body to a file */
curl_easy_setopt(t->curl, CURLOPT_FAILONERROR, 1L);
/* Enable detailed error messages */
curl_easy_setopt(t->curl, CURLOPT_ERRORBUFFER, errbuf);
mem_addf(&t->log, "Downloading %s to file %s\n", t->url, t->bodyfile);
printf("%s", t->log.recent);
/* Create the body file */
t->bodyfp = fopen(t->bodyfile, "wb");
if(t->bodyfp) {
/* Perform the transfer */
result = curl_easy_perform(t->curl);
/* Save the body file */
fclose(t->bodyfp);
t->bodyfp = NULL;
if(result == CURLE_OK) {
/* You could retrieve more information about the transfer here via
curl_easy_getinfo and mark the transfer as failed if needed. */
mem_addf(&t->log, "Transfer successful.\n");
fprintf(stderr, "%s", t->log.recent);
failed = 0;
}
else {
mem_addf(&t->log, "Transfer failed: (%d) %s\n", (int)result,
(errbuf[0] ? errbuf : curl_easy_strerror(result)));
fprintf(stderr, "%s", t->log.recent);
failed = 1;
}
}
else {
mem_addf(&t->log, "Failed to create body output file %s: %s\n",
t->bodyfile, strerror(errno));
fprintf(stderr, "%s", t->log.recent);
failed = 1;
}
if(failed) {
FILE *fp = fopen(t->logfile, "wb");
if(fp && t->log.len == fwrite(t->log.buf, 1, t->log.len, fp))
fprintf(stderr, "Transfer log written to %s\n", t->logfile);
else {
fprintf(stderr, "Failed to write transfer log to %s: %s\n",
t->logfile, strerror(errno));
}
if(fp)
fclose(fp);
/* Depending on how the transfer failed a body file may or may not have
been written, and you may or may not want it. */
unlink(t->bodyfile);
++total_failed;
}
mem_reset(&t->log);
curl_easy_cleanup(t->curl);
t->curl = NULL;
printf("\n");
}
curl_global_cleanup();
return total_failed ? 1 : 0;
}