Add the Windows CHICKEN 6 build and release scripts.

Upstream does not ship a Windows binary, so this tree fetches a release tarball, applies the MinGW patches, and publishes a UCRT64 prefix.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-09 01:09:55 +03:00
commit 4683bd4839
10 changed files with 452 additions and 0 deletions

3
.gitattributes vendored Normal file
View File

@@ -0,0 +1,3 @@
*.sh text eol=lf
*.patch text eol=lf
*.eml text eol=lf

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
work/
.env

44
README.md Normal file
View File

@@ -0,0 +1,44 @@
# chicken6-4-win
Chicken (6) 4 Windows 10+.
Upstream does not ship a Windows binary. This repo keeps the MinGW patches and builds a UCRT64 prefix, then publishes that prefix as a Gitea release. CHICKEN sources are not stored here.
## Kit
A clean Windows install is not a development machine. Start with [scoop](https://scoop.sh), then:
```powershell
scoop install git msys2
```
In the UCRT64 shell (`C:\Users\<you>\scoop\apps\msys2\current\ucrt64.exe`, or `bash` after `MSYSTEM=UCRT64`):
```sh
pacman -S --needed --noconfirm base-devel mingw-w64-ucrt-x86_64-gcc
```
Put `%USERPROFILE%\scoop\apps\msys2\current\ucrt64\bin` on `PATH` so `gcc` is the UCRT64 one.
Publishing reads `GITEA_TOKEN` from a gitignored `.env` in this directory:
```
GITEA_TOKEN=...
```
## Commands
```powershell
.\ck.ps1 fetch-sources
.\ck.ps1 patch
.\ck.ps1 build
.\ck.ps1 test
.\ck.ps1 publish
.\ck.ps1 clean
```
`publish` runs fetch, patch, build, and test, then uploads `work/dist/chicken-<version>-ucrt64-x86_64.zip`. The tag is `<upstream>-win<revision>` (`revision` in this repo, currently `6.0.0-win1`). If that tag already exists, publish stops. Bump `revision` to ship another build of the same upstream tarball.
`clean` deletes `work/` only.
`csc` in the zip still has `work/prefix` compiled into it. Consumers pass `-libdir` at wherever they unpacked the zip. Sex does that.

47
build.sh Normal file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Configure, build, and install into work/prefix. Run from a UCRT64 shell.
set -eu
root=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
src="$root/work/src"
prefix=$(cygpath -m "$root/work/prefix")
if [ ! -f "$src/configure" ]; then
echo "no sources in work/src; run: .\\ck.ps1 fetch-sources" >&2
exit 1
fi
case "${MSYSTEM:-}" in
UCRT64|MINGW64|CLANG64) ;;
*)
echo "build.sh needs an MSYS2 MinGW shell (UCRT64)." >&2
exit 1
;;
esac
machine=$(gcc -dumpmachine)
case "$machine" in
*mingw*) ;;
*)
echo "gcc targets ${machine}. Install the UCRT64 toolchain." >&2
exit 1
;;
esac
# The generated posixwin.c is what this tarball compiles. Keep it newer
# than posixwin.scm so make does not try to regenerate it.
if [ -f "$src/posixwin.c" ]; then
touch "$src/posixwin.c"
fi
jobs=$(nproc 2>/dev/null || echo 1)
(
cd "$src"
./configure --prefix "$prefix" --platform mingw
make -j"$jobs"
make install
)
export PATH="$(cygpath -u "$prefix")/bin:${PATH}"
echo "installed $prefix"
csc -version

206
ck.ps1 Normal file
View File

@@ -0,0 +1,206 @@
# Build and publish a UCRT64 CHICKEN 6 prefix.
# .\ck.ps1 fetch-sources | patch | build | test | publish | clean
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet('fetch-sources', 'patch', 'build', 'test', 'publish', 'clean')]
[string]$Command
)
$ErrorActionPreference = 'Stop'
$Root = $PSScriptRoot
$Work = Join-Path $Root 'work'
$Src = Join-Path $Work 'src'
$Prefix = Join-Path $Work 'prefix'
$Dist = Join-Path $Work 'dist'
$UpstreamVersion = '6.0.0'
$UpstreamSha256 = '92835552b1b687ad26737e429b5aba36510bf429f8816ec0f6d336c8cb41f443'
$UpstreamUrl = "https://code.call-cc.org/releases/$UpstreamVersion/chicken-$UpstreamVersion.tar.gz"
$Gitea = 'https://git.kotobank.ch'
$Owner = 'pkulev'
$RepoName = 'chicken6-4-win'
function Import-DotEnv {
$path = Join-Path $Root '.env'
if (-not (Test-Path -LiteralPath $path)) { return }
foreach ($line in Get-Content -LiteralPath $path) {
if ($line -match '^\s*#' -or $line -match '^\s*$') { continue }
$parts = $line.Split('=', 2)
if ($parts.Count -lt 2) { continue }
$name = $parts[0].Trim()
if ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name))) {
$value = $parts[1].Trim().Trim('"').Trim("'")
Set-Item -Path "Env:$name" -Value $value
}
}
}
function Find-Bash {
$roots = @()
if ($env:MSYS2_ROOT) { $roots += $env:MSYS2_ROOT }
$roots += (Join-Path $env:USERPROFILE 'scoop\apps\msys2\current')
$roots += 'C:\msys64'
foreach ($candidateRoot in $roots) {
if (-not $candidateRoot) { continue }
$candidate = Join-Path $candidateRoot 'usr\bin\bash.exe'
if (Test-Path -LiteralPath $candidate) { return $candidate }
}
Write-Error "MSYS2 bash.exe not found. Install MSYS2 (scoop install msys2) or set MSYS2_ROOT."
exit 1
}
function To-Msys([string]$Path) {
$full = (Resolve-Path -LiteralPath $Path).Path -replace '\\', '/'
if ($full -match '^([A-Za-z]):(.*)$') {
return '/' + $Matches[1].ToLower() + $Matches[2]
}
return $full
}
function Invoke-Ucrt([string]$Script) {
$bash = Find-Bash
$env:MSYSTEM = 'UCRT64'
$env:CHERE_INVOKING = '1'
$env:MSYS2_PATH_TYPE = 'strict'
$unix = To-Msys $Root
& $bash -lc "cd '$unix' && $Script"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
function Get-Tarball {
Join-Path $Work "chicken-$UpstreamVersion.tar.gz"
}
function Test-Sha256([string]$Path, [string]$Expected) {
$actual = (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $Expected.ToLower()) {
Write-Error "sha256 mismatch for $Path"
exit 1
}
}
function Invoke-FetchSources {
New-Item -ItemType Directory -Force -Path $Work | Out-Null
$tarball = Get-Tarball
$fresh = -not (Test-Path -LiteralPath $tarball)
if (-not $fresh) {
$actual = (Get-FileHash -LiteralPath $tarball -Algorithm SHA256).Hash.ToLower()
$fresh = $actual -ne $UpstreamSha256.ToLower()
}
if ($fresh) {
Write-Host "downloading $UpstreamUrl"
& curl.exe -fL --retry 2 -o $tarball $UpstreamUrl
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
Test-Sha256 $tarball $UpstreamSha256
if (Test-Path -LiteralPath $Src) {
Remove-Item -LiteralPath $Src -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $Src | Out-Null
& tar.exe -xzf $tarball -C $Src --strip-components=1
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
Write-Host "sources in $Src"
}
function Invoke-Patch {
if (-not (Test-Path -LiteralPath (Join-Path $Src 'configure'))) {
Write-Error "no sources; run: .\ck.ps1 fetch-sources"
exit 1
}
$stamp = Join-Path $Src '.patched'
if (Test-Path -LiteralPath $stamp) {
Write-Host "already patched"
return
}
$patches = Get-ChildItem -Path (Join-Path $Root 'patches') -Filter '*.patch' | Sort-Object Name
Push-Location $Root
try {
foreach ($patch in $patches) {
git apply --unsafe-paths --directory=work/src -p1 -- $patch.FullName
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
} finally {
Pop-Location
}
$generated = Join-Path $Src 'posixwin.c'
if (Test-Path -LiteralPath $generated) {
(Get-Item -LiteralPath $generated).LastWriteTime = Get-Date
}
Set-Content -LiteralPath $stamp -Value "patched`n" -Encoding ascii
Write-Host "patched"
}
function Invoke-Build {
Invoke-Ucrt './build.sh'
}
function Invoke-Test {
Invoke-Ucrt 'export PATH="$(cygpath -u "$PWD/work/prefix")/bin:$PATH"; make -C work/src check'
}
function Get-Revision {
$path = Join-Path $Root 'revision'
return (Get-Content -LiteralPath $path -Raw).Trim()
}
function Invoke-Publish {
Import-DotEnv
if ([string]::IsNullOrEmpty($env:GITEA_TOKEN)) {
Write-Error "GITEA_TOKEN is not set. Put it in .env (not committed)."
exit 1
}
Invoke-FetchSources
Invoke-Patch
Invoke-Build
Invoke-Test
if (-not (Test-Path -LiteralPath (Join-Path $Prefix 'bin\csc.exe'))) {
Write-Error "build did not install csc.exe"
exit 1
}
New-Item -ItemType Directory -Force -Path $Dist | Out-Null
$zipName = "chicken-$UpstreamVersion-ucrt64-x86_64.zip"
$zip = Join-Path $Dist $zipName
if (Test-Path -LiteralPath $zip) { Remove-Item -LiteralPath $zip -Force }
& tar.exe -a -cf $zip -C $Prefix .
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$tag = "$UpstreamVersion-win$(Get-Revision)"
$headers = @{ Authorization = "token $($env:GITEA_TOKEN)" }
$api = "$Gitea/api/v1/repos/$Owner/$RepoName"
try {
Invoke-RestMethod -Headers $headers -Uri "$api/releases/tags/$tag" | Out-Null
Write-Error "release $tag already exists. Bump revision."
exit 1
} catch {
$code = $_.Exception.Response.StatusCode.value__
if ($code -ne 404) { throw }
}
$body = @{
tag_name = $tag
target_commitish = 'main'
name = "CHICKEN $UpstreamVersion for Windows"
body = "UCRT64 x86_64 prefix. Unpack anywhere; csc needs ``-libdir <prefix>/lib``."
draft = $false
prerelease = $false
} | ConvertTo-Json
$release = Invoke-RestMethod -Method Post -Headers $headers -Uri "$api/releases" -Body $body -ContentType 'application/json; charset=utf-8'
$assetUri = "$api/releases/$($release.id)/assets?name=$zipName"
$bytes = [System.IO.File]::ReadAllBytes($zip)
Invoke-RestMethod -Method Post -Headers $headers -Uri $assetUri -Body $bytes -ContentType 'application/octet-stream' | Out-Null
Write-Host "published $tag"
}
switch ($Command) {
'fetch-sources' { Invoke-FetchSources }
'patch' { Invoke-Patch }
'build' { Invoke-Build }
'test' { Invoke-Test }
'publish' { Invoke-Publish }
'clean' {
if (Test-Path -LiteralPath $Work) {
Remove-Item -LiteralPath $Work -Recurse -Force
}
Write-Host "removed $Work"
}
}

View File

@@ -0,0 +1,21 @@
--- a/chicken.h
+++ b/chicken.h
@@ -980,7 +980,15 @@
# ifdef HAVE_SIGPROCMASK
# define C_sigprocmask sigprocmask
# endif
-# define C_setjmp setjmp
-# define C_longjmp longjmp
+/* MinGW's setjmp longjmps through SEH and crashes in RtlVirtualUnwind
+ on the alloca frames the compiler emits. CHICKEN uses longjmp only to
+ drop back to the trampoline, so the builtin jump is the right tool. */
+# if defined(__MINGW32__) && defined(__GNUC__)
+# define C_setjmp __builtin_setjmp
+# define C_longjmp __builtin_longjmp
+# else
+# define C_setjmp setjmp
+# define C_longjmp longjmp
+# endif
# define C_alloca alloca
# define C_strerror strerror

View File

@@ -0,0 +1,38 @@
--- a/posixwin.scm
+++ b/posixwin.scm
@@ -386,10 +386,15 @@
{
if (f_share_io[i])
{
+ /* bInheritHandle is independent of DUPLICATE_SAME_ACCESS.
+ STARTF_USESTDHANDLES needs an inheritable duplicate;
+ FALSE fails CreateProcess with ERROR_INVALID_HANDLE
+ when stderr is not a console. Not the SetHandleInformation
+ pass removed for ticket 765. */
success = DuplicateHandle(
cur_process, standard_io_handles[i],
cur_process, &child_io_handles[i],
- 0, FALSE, DUPLICATE_SAME_ACCESS);
+ 0, TRUE, DUPLICATE_SAME_ACCESS);
}
else
{
--- a/posixwin.c
+++ b/posixwin.c
@@ -458,10 +458,15 @@
{
if (f_share_io[i])
{
+ /* bInheritHandle is independent of DUPLICATE_SAME_ACCESS.
+ STARTF_USESTDHANDLES needs an inheritable duplicate;
+ FALSE fails CreateProcess with ERROR_INVALID_HANDLE
+ when stderr is not a console. Not the SetHandleInformation
+ pass removed for ticket 765. */
success = DuplicateHandle(
cur_process, standard_io_handles[i],
cur_process, &child_io_handles[i],
- 0, FALSE, DUPLICATE_SAME_ACCESS);
+ 0, TRUE, DUPLICATE_SAME_ACCESS);
}
else
{

View File

@@ -0,0 +1,37 @@
To: chicken-hackers@nongnu.org
Subject: [PATCH] MinGW: use __builtin_setjmp for the GC trampoline
Hi,
On MinGW-w64 with gcc, a garbage collection that longjmps back to the trampoline can access-violate inside ntdll!RtlVirtualUnwind. I hit this with CHICKEN 6.0.0, MSYS2 UCRT64, gcc 16.2.0, on 64-bit Windows 10. The first-chance and second-chance exceptions are the same instruction: RtlVirtualUnwind reads address 0 while walking the stack. There is no earlier user-mode fault. lldb can walk that same stack by frame pointers.
C_longjmp is only used to drop back to C_restart or gc_restart (runtime.c). MinGW's setjmp/longjmp are __intrinsic_setjmpex and unwind through SEH. That unwinder faults on the alloca frames the compiler emits. Recompiling library.c at -O0 or -O1 -fno-optimize-sibling-calls does not avoid it, and raising the PE stack reserve does not either: the nursery still fills and reclaim still runs.
A cons loop that forces a major collection exits 0. Calling current-directory enough times to collect does not. That procedure allocates a C_MAX_PATH bytevector with alloca, and the fatal C_save_and_reclaim happens on that frame. Earlier reclamations in the same run succeed. The same loop crashes in a static binary and in one linked against libchicken.dll.
__builtin_setjmp / __builtin_longjmp do not run the SEH unwinder. They only restore the stack, which is what the trampoline needs. The MinGW jmp_buf is larger than the five words the builtin uses. With that change in chicken.h, and runtime.c rebuilt, the current-directory loop and a normal static program that collects during current-directory both run. Other units do not call C_setjmp.
The same substitution was attached to ticket 1453 in 2018, guarded with __MINGW64__, for an MSVC executable loading a MinGW libchicken.dll. That ticket was closed wontfix on 27 September 2026 because MSVC is unsupported, and the change was never merged. The crash here is a MinGW-hosted binary. __MINGW32__ is defined for both 32-bit and 64-bit MinGW gcc; this was observed on x86-64.
--- a/chicken.h
+++ b/chicken.h
@@ -980,7 +980,15 @@
# ifdef HAVE_SIGPROCMASK
# define C_sigprocmask sigprocmask
# endif
-# define C_setjmp setjmp
-# define C_longjmp longjmp
+/* MinGW's setjmp longjmps through SEH and crashes in RtlVirtualUnwind
+ on the alloca frames the compiler emits. CHICKEN uses longjmp only to
+ drop back to the trampoline, so the builtin jump is the right tool. */
+# if defined(__MINGW32__) && defined(__GNUC__)
+# define C_setjmp __builtin_setjmp
+# define C_longjmp __builtin_longjmp
+# else
+# define C_setjmp setjmp
+# define C_longjmp longjmp
+# endif
# define C_alloca alloca
# define C_strerror strerror

View File

@@ -0,0 +1,53 @@
To: chicken-hackers@nongnu.org
Subject: [PATCH] Windows: pass inheritable standard handles to CreateProcess
Hi,
C_process, in the C embedded in posixwin.scm, duplicates a shared standard handle with bInheritHandle = FALSE, then calls CreateProcessW with bInheritHandles = TRUE and STARTF_USESTDHANDLES. Those handles have to be inheritable. DUPLICATE_SAME_ACCESS copies the access mask only; it does not keep the inherit flag. The pipe path already sets HANDLE_FLAG_INHERIT on the child's end. The share path does not.
Commit 2477220 (ticket 765) removed a later SetHandleInformation pass that tried to mark these duplicates inheritable, because Windows 7 rejects that on some parent handles. This patch does not restore that pass. DuplicateHandle itself is allowed to create the duplicate inheritable.
When standard error is not a console, the nested call fails with ERROR_INVALID_HANDLE, which this runtime maps to EBADF. Scheme reports it as (process) cannot execute process, errno 9. A console handle often still works, so a direct csc looks fine and a program spawned with (process ...) -- stderr shared, not (process* ...) -- fails when it spawns the next process. I saw this as a static compiler whose (process "cc" ...) failed only under a piped parent. After the flag is TRUE, the same nested spawn succeeded on 12 consecutive runs.
The 6.0.0 tarball also ships this function already translated into posixwin.c. The edit there is the same, and is included below so a release tree does not keep compiling the old copy.
--- a/posixwin.scm
+++ b/posixwin.scm
@@ -386,10 +386,15 @@
{
if (f_share_io[i])
{
+ /* bInheritHandle is independent of DUPLICATE_SAME_ACCESS.
+ STARTF_USESTDHANDLES needs an inheritable duplicate;
+ FALSE fails CreateProcess with ERROR_INVALID_HANDLE
+ when stderr is not a console. Not the SetHandleInformation
+ pass removed for ticket 765. */
success = DuplicateHandle(
cur_process, standard_io_handles[i],
cur_process, &child_io_handles[i],
- 0, FALSE, DUPLICATE_SAME_ACCESS);
+ 0, TRUE, DUPLICATE_SAME_ACCESS);
}
else
{
--- a/posixwin.c
+++ b/posixwin.c
@@ -458,10 +458,15 @@
{
if (f_share_io[i])
{
+ /* bInheritHandle is independent of DUPLICATE_SAME_ACCESS.
+ STARTF_USESTDHANDLES needs an inheritable duplicate;
+ FALSE fails CreateProcess with ERROR_INVALID_HANDLE
+ when stderr is not a console. Not the SetHandleInformation
+ pass removed for ticket 765. */
success = DuplicateHandle(
cur_process, standard_io_handles[i],
cur_process, &child_io_handles[i],
- 0, FALSE, DUPLICATE_SAME_ACCESS);
+ 0, TRUE, DUPLICATE_SAME_ACCESS);
}
else
{

1
revision Normal file
View File

@@ -0,0 +1 @@
1