Files
jak-project/third-party/curl/docs/examples/smooth-gtk-thread.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

224 lines
6.0 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>
* A multi-threaded application that uses a progress bar to show
* status. It uses Gtk+ to make a smooth pulse.
* </DESC>
*/
/*
* Written by Jud Bishop after studying the other examples provided with
* libcurl.
*
* To compile (on a single line):
* gcc -ggdb `pkg-config --cflags --libs gtk+-2.0` -lcurl -lssl -lcrypto
* -lgthread-2.0 -dl smooth-gtk-thread.c -o smooth-gtk-thread
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <unistd.h>
#include <pthread.h>
#include <curl/curl.h>
#define NUMT 4
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int j = 0;
static gint num_urls = 9; /* make sure this is less than urls[] */
static const char * const urls[] = {
"90022",
"90023",
"90024",
"90025",
"90026",
"90027",
"90028",
"90029",
"90030"
};
static size_t write_cb(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
static void run_one(const gchar *http, int j)
{
CURL *curl;
curl = curl_easy_init();
if(curl) {
printf("j = %d\n", j);
FILE *outfile = fopen(urls[j], "wb");
if(outfile) {
/* Set the URL and transfer type */
curl_easy_setopt(curl, CURLOPT_URL, http);
/* Write to the file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
(void)curl_easy_perform(curl);
fclose(outfile);
}
curl_easy_cleanup(curl);
}
}
static void *pull_one_url(void *NaN)
{
/* protect the reading and increasing of 'j' with a mutex */
pthread_mutex_lock(&lock);
while(j < num_urls) {
gchar *http;
int i = j;
j++;
pthread_mutex_unlock(&lock);
http = g_strdup_printf("https://example.com/%s", urls[i]);
if(http) {
run_one(http, i);
g_free(http);
}
pthread_mutex_lock(&lock);
}
pthread_mutex_unlock(&lock);
return NULL;
}
static gboolean pulse_bar(gpointer data)
{
gdk_threads_enter();
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(data));
gdk_threads_leave();
/* Return true so the function is called again; returning false removes this
* timeout function.
*/
return TRUE;
}
static void *create_thread(void *progress_bar)
{
pthread_t tid[NUMT];
int i;
/* Make sure I do not create more threads than URLs. */
for(i = 0; i < NUMT && i < num_urls; i++) {
int error = pthread_create(&tid[i],
NULL, /* default attributes please */
pull_one_url,
NULL);
if(error)
fprintf(stderr, "Could not run thread number %d, errno %d\n", i, error);
else
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
}
/* Wait for all threads to terminate. */
for(i = 0; i < NUMT && i < num_urls; i++) {
pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d terminated\n", i);
}
/* This stops the pulsing if you have it turned on in the progress bar
section */
g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
"pulse_id")));
/* This destroys the progress bar */
gtk_widget_destroy(progress_bar);
/* [Un]Comment this out to kill the program rather than pushing close. */
#if 0
gtk_main_quit();
#endif
return NULL;
}
static gboolean cb_delete(GtkWidget *window, gpointer data)
{
gtk_main_quit();
return FALSE;
}
int main(int argc, const char **argv)
{
GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
/* Must initialize libcurl before any threads are started */
CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
if(result != CURLE_OK)
return (int)result;
/* Init thread */
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
gtk_init(&argc, &argv);
/* Base window */
top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* Frame */
outside_frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
/* Frame */
inside_frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
/* Progress bar */
progress_bar = gtk_progress_bar_new();
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progress_bar));
/* Make uniform pulsing */
gint pulse_ref = g_timeout_add(300, pulse_bar, progress_bar);
g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
GINT_TO_POINTER(pulse_ref));
gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
gtk_widget_show_all(top_window);
printf("gtk_widget_show_all\n");
g_signal_connect(G_OBJECT(top_window), "delete-event",
G_CALLBACK(cb_delete), NULL);
if(!g_thread_create(&create_thread, progress_bar, FALSE, NULL))
g_warning("cannot create the thread");
gtk_main();
gdk_threads_leave();
printf("gdk_threads_leave\n");
curl_global_cleanup();
return 0;
}