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

351 lines
12 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>
* Set your system time from a remote HTTP server's Date: header.
* </DESC>
*/
/* This example code only builds as-is on Windows.
*
* Synchronising your computer clock via Internet time server usually relies
* on DAYTIME, TIME, or NTP protocols. These protocols provide good accurate
* time synchronization but it does not work well through a firewall/proxy.
* Some adjustment has to be made to the firewall/proxy for these protocols to
* work properly.
*
* There is an indirect method. Since most webserver provide server time in
* their HTTP header, therefore you could synchronise your computer clock
* using HTTP protocol which has no problem with firewall/proxy.
*
* For this software to work, you should take note of these items.
* 1. Your firewall/proxy must allow your computer to surf Internet.
* 2. Webserver system time must in sync with the NTP time server,
* or at least provide an accurate time keeping.
* 3. Webserver HTTP header does not provide the milliseconds units,
* so there is no way to get an accurate time.
* 4. This software could only provide an accuracy of +- a few seconds,
* as Round-Trip delay time is not taken into consideration.
* Compensation of network, firewall/proxy delay cannot be done by dividing
* the Round-Trip delay time by half.
* 5. Win32 SetSystemTime() API sets your computer clock according to
* GMT/UTC time. Therefore your computer timezone must be properly set.
* 6. Webserver data should not be cached by the proxy server. Some
* webserver provide Cache-Control to prevent caching.
*
* Usage:
* This software synchronises your computer clock only when you issue
* it with --synctime. By default, it only display the webserver's clock.
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), gmtime(),
localtime(), sscanf() */
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <curl/curl.h>
#ifdef _WIN32
#if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602)) || \
defined(WINAPI_FAMILY)
# include <winapifamily.h>
# if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
# define CURL_WINDOWS_UWP
# endif
#include <windows.h>
#endif
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#define SYNCTIME_UA "synctime/1.0"
struct conf {
char http_proxy[256];
char proxy_user[256];
char timeserver[256];
};
static int ShowAllHeader;
static int AutoSyncTime;
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
static SYSTEMTIME SYSTime;
static SYSTEMTIME LOCALTime;
static const char *DayStr[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *MthStr[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#endif
static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite(ptr, size, nmemb, stream);
return nmemb * size;
}
/* Remember: do not assume headers are passed on null-terminated! */
static size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
void *stream)
{
(void)stream;
if(ShowAllHeader == 1)
fprintf(stderr, "%.*s", (int)nmemb, (char *)ptr);
if((nmemb >= 5) && !strncmp((const char *)ptr, "Date:", 5)) {
if(ShowAllHeader == 0)
fprintf(stderr, "HTTP Server. %.*s", (int)nmemb, (char *)ptr);
if(AutoSyncTime == 1) {
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
char TmpStr1[26], TmpStr2[26];
int RetVal = 0;
char *field = ptr;
*TmpStr1 = 0;
*TmpStr2 = 0;
if(nmemb && (field[nmemb - 1] == '\n')) {
char header[100];
size_t len = nmemb < sizeof(header) ? nmemb : sizeof(header) - 1;
memcpy(header, field, len);
header[len] = 0; /* null-terminate local copy */
RetVal = sscanf(header, "Date: %25s %hu %25s %hu %hu:%hu:%hu",
TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
&SYSTime.wHour, &SYSTime.wMinute,
&SYSTime.wSecond);
}
if(RetVal == 7) {
int i;
SYSTime.wMilliseconds = 500; /* adjust to midpoint, 0.5 sec */
for(i = 0; i < 12; i++) {
if(!strcmp(MthStr[i], TmpStr2)) {
SYSTime.wMonth = (WORD)(i + 1);
break;
}
}
AutoSyncTime = 3; /* Computer clock is adjusted */
}
else {
AutoSyncTime = 0; /* Error in sscanf() fields conversion */
}
#endif
}
}
if((nmemb >= 12) && !strncmp((const char *)ptr, "X-Cache: HIT", 12)) {
fprintf(stderr, "ERROR: HTTP Server data is cached."
" Server Date is no longer valid.\n");
AutoSyncTime = 0;
}
return nmemb * size;
}
static void SyncTime_CURL_Init(CURL *curl, const char *proxy_port,
const char *proxy_user_password)
{
if(strlen(proxy_port) > 0)
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);
if(strlen(proxy_user_password) > 0)
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);
curl_easy_setopt(curl, CURLOPT_USERAGENT, SYNCTIME_UA);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, SyncTime_CURL_WriteHeader);
}
static CURLcode SyncTime_CURL_FetchHead(CURL *curl, const char *URL_Str)
{
CURLcode result;
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
result = curl_easy_perform(curl);
return result; /* CURLE_OK */
}
static void showUsage(void)
{
fprintf(stderr, "synctime: Synchronising computer clock with time server"
" using HTTP protocol.\n");
fprintf(stderr, "Usage : synctime [Option]\n");
fprintf(stderr, "Options :\n");
fprintf(stderr, " --server=WEBSERVER Use this time server instead"
" of default.\n");
fprintf(stderr, " --showall Show all HTTP header.\n");
fprintf(stderr, " --synctime Synchronising computer clock"
" with time server.\n");
fprintf(stderr, " --proxy-user=USER[:PASS] Set proxy username and"
" password.\n");
fprintf(stderr, " --proxy=HOST[:PORT] Use HTTP proxy on given"
" port.\n");
fprintf(stderr, " --help Print this help.\n");
fprintf(stderr, "\n");
}
int main(int argc, const char *argv[])
{
CURLcode result;
CURL *curl;
struct conf conf;
int RetValue;
ShowAllHeader = 0; /* Do not show HTTP Header */
AutoSyncTime = 0; /* Do not synchronise computer clock */
RetValue = 0; /* Successful Exit */
memset(&conf, 0, sizeof(conf));
if(argc > 1) {
int OptionIndex = 1;
while(OptionIndex < argc) {
if(!strncmp(argv[OptionIndex], "--server=", 9))
snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
&argv[OptionIndex][9]);
if(!strcmp(argv[OptionIndex], "--showall"))
ShowAllHeader = 1;
if(!strcmp(argv[OptionIndex], "--synctime"))
AutoSyncTime = 1;
if(!strncmp(argv[OptionIndex], "--proxy-user=", 13))
snprintf(conf.proxy_user, sizeof(conf.proxy_user) - 1, "%s",
&argv[OptionIndex][13]);
if(!strncmp(argv[OptionIndex], "--proxy=", 8))
snprintf(conf.http_proxy, sizeof(conf.http_proxy) - 1, "%s",
&argv[OptionIndex][8]);
if(!strcmp(argv[OptionIndex], "--help") ||
!strcmp(argv[OptionIndex], "/?")) {
showUsage();
return 0;
}
OptionIndex++;
}
}
if(*conf.timeserver == 0) /* Use default server for time information */
snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
"https://www.ntp.org/");
/* Init CURL before usage */
result = curl_global_init(CURL_GLOBAL_ALL);
if(result != CURLE_OK)
return (int)result;
curl = curl_easy_init();
if(curl) {
struct tm *lt;
struct tm *gmt;
time_t tt;
time_t tt_local;
time_t tt_gmt;
double tzonediffFloat;
int tzonediffWord;
char timeBuf[61] = "";
char tzoneBuf[16];
SyncTime_CURL_Init(curl, conf.http_proxy, conf.proxy_user);
/* Calculating time diff between GMT and localtime */
tt = time(0);
lt = localtime(&tt);
tt_local = mktime(lt);
gmt = gmtime(&tt);
tt_gmt = mktime(gmt);
tzonediffFloat = difftime(tt_local, tt_gmt);
tzonediffWord = (int)(tzonediffFloat / 3600.0);
if(tzonediffWord == (int)(tzonediffFloat / 3600.0))
snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'00'", tzonediffWord);
else
snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'30'", tzonediffWord);
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
/* Get current system time and local time */
GetSystemTime(&SYSTime);
GetLocalTime(&LOCALTime);
snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
#endif
fprintf(stderr, "Fetch: %s\n\n", conf.timeserver);
fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);
/* HTTP HEAD command to the Webserver */
SyncTime_CURL_FetchHead(curl, conf.timeserver);
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
GetLocalTime(&LOCALTime);
snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
fprintf(stderr, "\nAfter HTTP. Date: %s%s\n", timeBuf, tzoneBuf);
if(AutoSyncTime == 3) {
/* Synchronising computer clock */
if(!SetSystemTime(&SYSTime)) { /* Set system time */
fprintf(stderr, "ERROR: Unable to set system time.\n");
RetValue = 1;
}
else {
/* Successfully re-adjusted computer clock */
GetLocalTime(&LOCALTime);
snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear,
LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
LOCALTime.wMilliseconds);
fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf);
}
}
#endif
/* Cleanup before exit */
memset(&conf, 0, sizeof(conf));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return RetValue;
}