diff --git a/.forgejo/scripts/freebsd-build.sh b/.forgejo/scripts/freebsd-build.sh new file mode 100644 index 0000000..c05f788 --- /dev/null +++ b/.forgejo/scripts/freebsd-build.sh @@ -0,0 +1,143 @@ +#!/bin/sh +# Build hate-no-d inside a FreeBSD VM and copy the binary back. +# +# freebsd-build.sh [source-dir] +# +# Runs on Linux (CI job container or a workstation) and needs: +# qemu-system-x86, cloud-image-utils (cloud-localds), openssh-client, xz, curl +# +# The FreeBSD guest is booted with -snapshot, so every run starts from the +# pristine image: nothing is installed in it and nothing persists. +# +# Requires /dev/kvm. Under the Forgejo docker runner that means the runner +# config must pass it through, e.g. +# container: +# options: "--device /dev/kvm" +# Without KVM, QEMU falls back to TCG and a FreeBSD boot takes many minutes. +set -eu + +VERSION="${FREEBSD_VERSION:-15.1}" +ARCH=amd64 +BASE="https://download.freebsd.org/releases/VM-IMAGES/${VERSION}-RELEASE/${ARCH}/Latest" +IMAGE="FreeBSD-${VERSION}-RELEASE-${ARCH}-BASIC-CLOUDINIT-ufs.qcow2.xz" +SSH_PORT="${FREEBSD_SSH_PORT:-2222}" + +if [ $# -lt 1 ]; then + echo "usage: $0 [source-dir]" >&2 + exit 2 +fi +OUT_DIR=$(dirname "$1") +mkdir -p "$OUT_DIR" +OUT=$(cd "$OUT_DIR" && pwd)/$(basename "$1") +SRC=$(cd "${2:-.}" && pwd) + +WORK="${FBSD_WORK:-${TMPDIR:-/tmp}/fbsd-ci}" + +log() { echo "freebsd-build: $*"; } +die() { echo "freebsd-build: $*" >&2; exit 1; } + +[ -r /dev/kvm ] && [ -w /dev/kvm ] || die "/dev/kvm is not usable; the runner must expose it (container options: --device /dev/kvm)" + +mkdir -p "$WORK" +cd "$WORK" + +# --- guest credentials ------------------------------------------------------- +[ -f id_ci ] || ssh-keygen -t ed25519 -N '' -f id_ci -q +chmod 600 id_ci +PUB=$(cat id_ci.pub) + +cat > user-data < meta-data <<'EOF' +instance-id: fbsd-ci-1 +local-hostname: fbsd-ci +EOF + +# --- disk image -------------------------------------------------------------- +if [ ! -f disk.qcow2 ]; then + [ -f "$IMAGE" ] || { log "downloading $IMAGE"; curl -fsSL -o "$IMAGE" "$BASE/$IMAGE"; } + log "decompressing" + xz -dc -T0 "$IMAGE" > disk.qcow2 +fi + +log "building cloud-init seed" +cloud-localds seed.iso user-data meta-data + +# --- boot -------------------------------------------------------------------- +SSH_OPTS="-i $WORK/id_ci -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o BatchMode=yes -o ConnectTimeout=3" +SCP_OPTS="-i $WORK/id_ci -P $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o BatchMode=yes" + +rm -f console.log qemu.pid +qemu_pid="" + +cleanup() { + if [ -n "$qemu_pid" ] && kill -0 "$qemu_pid" 2>/dev/null; then + kill "$qemu_pid" 2>/dev/null || true + wait "$qemu_pid" 2>/dev/null || true + fi +} +trap cleanup EXIT INT TERM + +log "booting FreeBSD $VERSION" +qemu-system-x86_64 \ + -M q35 -cpu host -smp 2 -m 2048 \ + -enable-kvm \ + -drive file=disk.qcow2,if=virtio,format=qcow2,snapshot=on \ + -drive file=seed.iso,if=virtio,media=cdrom,readonly=on \ + -netdev "user,id=n0,hostfwd=tcp:127.0.0.1:${SSH_PORT}-:22" \ + -device virtio-net-pci,netdev=n0 \ + -display none -serial file:console.log \ + -daemonize -pidfile qemu.pid +qemu_pid=$(cat qemu.pid) + +ssh_vm() { ssh $SSH_OPTS "root@127.0.0.1" "$@"; } + +i=0 +until ssh_vm true 2>/dev/null; do + i=$((i + 1)) + if [ "$i" -gt 90 ]; then + tail -40 console.log >&2 || true + die "guest did not become reachable over SSH" + fi + sleep 2 +done +log "guest up after ~$((i * 2))s: $(ssh_vm 'uname -sr')" + +# --- build ------------------------------------------------------------------- +log "installing gmake in guest" +ssh_vm 'set -e; env ASSUME_ALWAYS_YES=yes pkg install -y gmake >/dev/null 2>&1; gmake --version | head -1' + +log "transferring source" +tar czf "$WORK/src.tar.gz" -C "$SRC" \ + --exclude=.git --exclude=.build --exclude=hate-no-d \ + --exclude='*.o' --exclude='*.a' --exclude='*.so' . +scp $SCP_OPTS "$WORK/src.tar.gz" "root@127.0.0.1:/root/src.tar.gz" + +# FreeBSD has no gcc; LuaJIT's Makefile defaults to it, so CC must be forced on +# the command line for it to reach the sub-make. +log "building" +ssh_vm 'set -e + rm -rf /root/src && mkdir -p /root/src + tar xzf /root/src.tar.gz -C /root/src + cd /root/src + gmake CC=cc clean >/dev/null 2>&1 || true + gmake CC=cc all' + +log "fetching binary" +mkdir -p "$(dirname "$OUT")" +scp $SCP_OPTS "root@127.0.0.1:/root/src/hate-no-d" "$OUT" + +log "built $OUT ($(wc -c < "$OUT") bytes)" diff --git a/.forgejo/scripts/release-asset.sh b/.forgejo/scripts/release-asset.sh new file mode 100644 index 0000000..e7999d5 --- /dev/null +++ b/.forgejo/scripts/release-asset.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# Attach one file to the release for $TAG, creating the release if needed. +# +# release-asset.sh [asset-name] +# +# Idempotent: re-running a tagged pipeline replaces the existing asset instead +# of failing, and concurrent jobs racing to create the same release converge on +# whichever POST wins. +# +# Environment: FORGEJO_API_URL, FORGEJO_TOKEN, REPO (owner/name), TAG +set -eu + +api="${FORGEJO_API_URL:?FORGEJO_API_URL is not set}" +repo="${REPO:?REPO is not set}" +tag="${TAG:?TAG is not set}" +token="${FORGEJO_TOKEN:?FORGEJO_TOKEN is not set}" + +case "$api" in + http*) ;; + *) + # An unexpanded ${{ github.server_url }} leaves this as a bare "/api/v1"; + # fail loudly instead of letting curl emit a cryptic URL error. + echo "release-asset: FORGEJO_API_URL is not a URL: '$api'" >&2 + echo " set github.server_url support or override the env var" >&2 + exit 1 + ;; +esac + +file="$1" +name="${2:-$(basename "$file")}" + +[ -f "$file" ] || { echo "release-asset: no such file: $file" >&2; exit 1; } + +auth="Authorization: token $token" +get_rid() { + curl -fsS -H "$auth" "$api/repos/$repo/releases/tags/$tag" | jq -r '.id // empty' +} + +rid="$(get_rid || true)" + +if [ -z "$rid" ]; then + echo "release-asset: creating release $tag" + curl -sS -o /dev/null -H "$auth" -H 'Content-Type: application/json' \ + -X POST "$api/repos/$repo/releases" \ + --data "$(jq -nc --arg t "$tag" '{tag_name: $t, name: $t}')" || true + + # A parallel job may have created it between our GET and POST. + i=0 + while [ -z "$rid" ] && [ "$i" -lt 5 ]; do + sleep 1 + rid="$(get_rid || true)" + i=$((i + 1)) + done +fi + +[ -n "$rid" ] || { echo "release-asset: cannot resolve release for $tag" >&2; exit 1; } + +# Replace a same-named asset so a re-run does not accumulate duplicates. +old="$(curl -fsS -H "$auth" "$api/repos/$repo/releases/$rid/assets" \ + | jq -r --arg n "$name" '.[] | select(.name == $n) | .id' | head -1)" + +if [ -n "$old" ]; then + echo "release-asset: replacing existing asset $name" + curl -fsS -o /dev/null -H "$auth" \ + -X DELETE "$api/repos/$repo/releases/$rid/assets/$old" +fi + +curl -fsS -H "$auth" -X POST \ + "$api/repos/$repo/releases/$rid/assets?name=$name" \ + -F "attachment=@$file" >/dev/null + +echo "release-asset: uploaded $name to $tag" diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml new file mode 100644 index 0000000..5b5c678 --- /dev/null +++ b/.forgejo/workflows/release.yml @@ -0,0 +1,332 @@ +# Build hate-no-d for every supported target and attach the binaries to a Forgejo release. +# +# push to master / pull request -> build only +# push a tag matching 'v*' -> build + release assets +# workflow_dispatch -> same as a branch push +# +# Release assets are uploaded straight to the REST API, one file per platform: +# hate-no-d-linux-{x86_64,i686,aarch64,armv7} +# hate-no-d-macos-{arm64,x86_64} +# hate-no-d-android-{arm64-v8a,armeabi-v7a,x86_64} +# hate-no-d-windows_mingw_w64-{x64,x86}.exe +# hate-no-d-ios-arm64-luajit.a +# hate-no-d-freebsd-x86_64 +# +# Only Apple's targets need more than the Linux runner, and they are gated +# behind a repository variable set to "true" (needs a matching runner): +# ENABLE_MACOS (macOS runner, label: macos) -> macos + ios jobs +name: release + +on: + push: + branches: [master] + tags: ['v*'] + pull_request: + workflow_dispatch: + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: true + +# Note: Forgejo ignores `permissions:` entirely. The automatic workflow token +# already has write access to its own repository, which is what the release +# steps rely on. Forked pull requests get a read-only token, but they never +# reach the upload steps (those are tag-gated). + +env: + FORGEJO_API_URL: ${{ github.server_url }}/api/v1 + FORGEJO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + TAG: ${{ github.ref_name }} + +jobs: + # --------------------------------------------------------------------------- + # Linux. Built on Debian bullseye so the binaries carry a glibc 2.31 floor + # (Debian 11, Ubuntu 20.04+, RHEL 9). Bumping the image silently raises that + # floor, so do not move it without meaning to. + # --------------------------------------------------------------------------- + linux: + name: linux/${{ matrix.arch }} + runs-on: docker + container: + image: node:20-bullseye # node + git: actions/checkout is a JS action + strategy: + fail-fast: false + matrix: + include: + - arch: x86_64 + machine: X86-64 + make_args: '' + packages: '' + # 32-bit target: HOST_CC defaults to $(CC), so leaving HOST_CC + # unset builds minilua/buildvm 32-bit too. LuaJIT requires host and + # target pointer sizes to match -- do not "optimise" this to + # HOST_CC=gcc. + - arch: i686 + machine: 'Intel 80386' + make_args: 'CC="gcc -m32"' + packages: 'gcc-multilib libc6-dev-i386' + # 64-bit host -> 64-bit target, so host tools stay native. + - arch: aarch64 + machine: AArch64 + make_args: 'CC=aarch64-linux-gnu-gcc HOST_CC=gcc' + packages: 'gcc-aarch64-linux-gnu qemu-user-static' + # 32-bit target: the 32-bit host tools are mandatory here. + - arch: armv7 + machine: ARM + make_args: 'CC=arm-linux-gnueabihf-gcc HOST_CC="gcc -m32"' + packages: 'gcc-arm-linux-gnueabihf qemu-user-static gcc-multilib libc6-dev-i386' + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + - name: Install toolchain + run: | + set -eu + apt-get update -qq + apt-get install -y --no-install-recommends \ + make gcc libc6-dev binutils jq curl ca-certificates ${{ matrix.packages }} + + - name: Build + run: make ${{ matrix.make_args }} all + + # Guards the cross builds: a mis-set CROSS/CC silently yields a host + # binary, which is otherwise indistinguishable until it is run. + - name: Verify architecture + run: | + set -eu + readelf -h ./hate-no-d | grep -F 'Machine:' | grep -F '${{ matrix.machine }}' + echo "architecture ok: ${{ matrix.machine }}" + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: sh .forgejo/scripts/release-asset.sh ./hate-no-d "hate-no-d-linux-${{ matrix.arch }}" + + # --------------------------------------------------------------------------- + # Android. One job for all three ABIs: the NDK is a ~630 MB download and + # sharing it beats three parallel copies. The resulting binaries are Bionic + # ELF, so they cannot be executed here -- only the ELF header is checked. + # --------------------------------------------------------------------------- + android: + name: android + runs-on: docker + container: + image: node:20-bookworm + env: + ANDROID_NDK_VERSION: r27c + ANDROID_NDK: /opt/android-ndk-r27c + ANDROID_API: '21' + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + - name: Install toolchain + # gcc-multilib is needed for the 32-bit host tools LuaJIT requires when + # the target is 32-bit (armeabi-v7a). + run: | + set -eu + apt-get update -qq + apt-get install -y --no-install-recommends \ + make gcc libc6-dev binutils jq curl ca-certificates unzip \ + gcc-multilib libc6-dev-i386 + + - name: Install NDK r27c + run: | + set -eu + url="https://dl.google.com/android/repository/android-ndk-${ANDROID_NDK_VERSION}-linux.zip" + curl -fsSL -o /tmp/ndk.zip "$url" + mkdir -p /opt + unzip -q /tmp/ndk.zip -d /opt + rm -f /tmp/ndk.zip + test -d "$ANDROID_NDK" + + - name: Build all ABIs + run: | + set -eu + mkdir -p dist + for abi in arm64-v8a armeabi-v7a x86_64; do + make clean >/dev/null 2>&1 || true + if [ "$abi" = armeabi-v7a ]; then + make PLATFORM=Android ANDROID_NDK="$ANDROID_NDK" ANDROID_API="$ANDROID_API" \ + ANDROID_ABI="$abi" HOST_CC="gcc -m32" all + else + make PLATFORM=Android ANDROID_NDK="$ANDROID_NDK" ANDROID_API="$ANDROID_API" \ + ANDROID_ABI="$abi" HOST_CC=gcc all + fi + readelf -h ./hate-no-d | grep -F 'Machine:' | grep -E 'AArch64|ARM|X86-64' + cp ./hate-no-d "dist/hate-no-d-android-$abi" + done + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: | + set -eu + for abi in arm64-v8a armeabi-v7a x86_64; do + sh .forgejo/scripts/release-asset.sh "dist/hate-no-d-android-$abi" "hate-no-d-android-$abi" + done + + # --------------------------------------------------------------------------- + # macOS. Apple SDKs are universal, so either runner arch can cross-build the + # other; only the target objects get -arch, the build tools stay native. + # --------------------------------------------------------------------------- + macos: + name: macos/${{ matrix.arch }} + if: vars.ENABLE_MACOS == 'true' + runs-on: macos + strategy: + fail-fast: false + matrix: + arch: [arm64, x86_64] + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + - name: Build + run: | + make \ + CFLAGS="-O2 -Wall -arch ${{ matrix.arch }}" \ + LUAJIT_EXTRA="TARGET_FLAGS=-arch ${{ matrix.arch }}" \ + all + + - name: Verify architecture + run: | + set -eu + file ./hate-no-d + file ./hate-no-d | grep -q '${{ matrix.arch }}' + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: sh .forgejo/scripts/release-asset.sh ./hate-no-d "hate-no-d-macos-${{ matrix.arch }}" + + # --------------------------------------------------------------------------- + # iOS. Deliberately produces no executable: the JIT is compiled out and the + # archive is linked into an Xcode app target. Requires jq for the upload. + # --------------------------------------------------------------------------- + ios: + name: ios/arm64 + if: vars.ENABLE_MACOS == 'true' + runs-on: macos + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + - name: Build libluajit.a + run: make PLATFORM=iOS IOS_ARCH=arm64 all + + - name: Verify archive + run: | + set -eu + lipo -info submodules/luajit/src/libluajit.a + lipo -info submodules/luajit/src/libluajit.a | grep -q arm64 + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: sh .forgejo/scripts/release-asset.sh submodules/luajit/src/libluajit.a hate-no-d-ios-arm64-luajit.a + + # --------------------------------------------------------------------------- + # FreeBSD. Built inside a FreeBSD VM booted with QEMU/KVM on the Linux runner, + # so no FreeBSD runner is needed: the tooling lives on the Linux side and only + # the compilation happens in the guest. + # + # The job container needs /dev/kvm, e.g. in the act_runner config: + # container: + # options: "--device /dev/kvm" + # Without it the script fails immediately rather than crawling along on TCG. + # + # Measured ~1m40s end to end: 70s boot, ~15s pkg install, ~14s build. + # --------------------------------------------------------------------------- + bsd: + name: freebsd/x86_64 + runs-on: docker + container: + image: node:20-bookworm + timeout-minutes: 20 + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + - name: Install QEMU + run: | + set -eu + apt-get update -qq + apt-get install -y --no-install-recommends \ + qemu-system-x86 cloud-image-utils openssh-client xz-utils jq + + - name: Build in FreeBSD VM + run: | + set -eu + mkdir -p dist + sh .forgejo/scripts/freebsd-build.sh dist/hate-no-d-freebsd-x86_64 . + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: sh .forgejo/scripts/release-asset.sh dist/hate-no-d-freebsd-x86_64 hate-no-d-freebsd-x86_64 + + # --------------------------------------------------------------------------- + # Windows. Cross-compiled with mingw-w64 on the Linux runner, so no Windows + # machine and no MSVC are involved: the result is a self-contained PE that + # imports only KERNEL32/msvcrt. build.bat remains for native MSVC builds. + # + # i686 needs a 32-bit host compiler: LuaJIT's buildvm runs natively and must + # have the target's pointer size. + # + # Releases are named hate-no-d-windows_mingw_w64-x64.exe and + # hate-no-d-windows_mingw_w64-x86.exe. The build itself stays hate-no-d.exe + # (same as build.bat); the toolchain-qualified name is only the asset name. + # --------------------------------------------------------------------------- + windows: + name: windows/${{ matrix.arch }} + runs-on: docker + container: + image: node:20-bookworm + strategy: + fail-fast: false + matrix: + include: + - arch: x86_64 + tag: windows_mingw_w64-x64 + host_cc: gcc + - arch: i686 + tag: windows_mingw_w64-x86 + host_cc: gcc -m32 + steps: + - name: Checkout + uses: https://data.forgejo.org/actions/checkout@v4 + with: + submodules: recursive + + # libc6-dev is not pulled in by gcc under --no-install-recommends, and + # without it the host build of minilua cannot find limits.h. + - name: Install toolchain + run: | + set -eu + apt-get update -qq + apt-get install -y --no-install-recommends \ + make gcc libc6-dev gcc-multilib mingw-w64 jq + + - name: Build + run: make PLATFORM=Windows WIN_ARCH=${{ matrix.arch }} HOST_CC="${{ matrix.host_cc }}" all + + - name: Verify executable + run: | + set -eu + bin=hate-no-d.exe + file "./$bin" | grep -q 'for MS Windows' + deps=$(${{ matrix.arch }}-w64-mingw32-objdump -p "./$bin" | awk '/DLL Name/{print $3}') + echo "$deps" + bad=$(printf '%s\n' "$deps" | grep -v -E '^(KERNEL32|msvcrt)\.dll$' || true) + [ -z "$bad" ] || { echo "unexpected runtime DLL dependency: $bad" >&2; exit 1; } + + - name: Attach to release + if: startsWith(github.ref, 'refs/tags/') + run: sh .forgejo/scripts/release-asset.sh ./hate-no-d.exe hate-no-d-${{ matrix.tag }}.exe diff --git a/.gitignore b/.gitignore index ccaa541..5510824 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ hate-no-d +hate-no-d.exe quick-start.lua diff --git a/Makefile b/Makefile index a4ffe4b..0dc7399 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ # make PLATFORM=Android ANDROID_NDK=/opt/android-ndk # make PLATFORM=Android ANDROID_NDK=... ANDROID_ABI=armeabi-v7a ANDROID_API=21 # make PLATFORM=iOS IOS_ARCH=arm64 IOS_MIN=12.0 +# make PLATFORM=Windows WIN_ARCH=x86_64 (cross build with mingw-w64) # luajit requires GNU Make (use `gmake` on FreeBSD/OpenBSD/NetBSD). ifndef .FEATURES @@ -32,6 +33,8 @@ else ifeq ($(PLATFORM),Darwin) OS_KIND := macos else ifeq ($(PLATFORM),iOS) OS_KIND := ios +else ifeq ($(PLATFORM),Windows) + OS_KIND := windows else ifneq (,$(filter $(PLATFORM),FreeBSD OpenBSD NetBSD DragonFly GNU/kFreeBSD)) OS_KIND := bsd else @@ -60,6 +63,35 @@ ifeq ($(OS_KIND),posix) LINK_TARGET := $(BIN) endif +# Windows (PE, cross-compiled from a POSIX host with mingw-w64) +# +# Native builds use build.bat. +# +# 64-bit targets build on any 64-bit host. i686 also needs a 32-bit host +# compiler, because LuaJIT's buildvm runs natively and its pointer size must +# match the target: +# make PLATFORM=Windows WIN_ARCH=i686 HOST_CC="gcc -m32" +# +# No export table: ffi.C resolves through msvcrt/kernel32 handles on Windows, +# unlike ELF where the Lua API has to be exported with -Wl,-E. +ifeq ($(OS_KIND),windows) + WIN_ARCH ?= x86_64 + + ifeq ($(filter $(WIN_ARCH),x86_64 i686),) + $(error WIN_ARCH must be x86_64 or i686) + endif + + CC := $(WIN_ARCH)-w64-mingw32-gcc + HOST_CC ?= gcc + PLAT_LIBS := -lm + BIN := $(BIN).exe + LINK_TARGET := $(BIN) + + # LuaJIT derives HOST_CC from CC, so label both to the sub-make: otherwise + # the host tools (minilua, buildvm) are compiled with the cross compiler. + LUAJIT_EXTRA := "CC=$(CC)" "HOST_CC=$(HOST_CC)" TARGET_SYS=Windows +endif + # macOS (Mach-O) # # ld64 exports main-executable globals by default; -export_dynamic only stops @@ -192,6 +224,7 @@ print-config: @echo "MACOSX_DEPLOYMENT_TARGET = $(MACOSX_DEPLOYMENT_TARGET) (HRT=$(MACOSX_HRT))" @echo "ANDROID_ABI / API = $(ANDROID_ABI) / $(ANDROID_API)" @echo "IOS_ARCH / MIN = $(IOS_ARCH) / $(IOS_MIN)" + @echo "WIN_ARCH = $(WIN_ARCH) (HOST_CC=$(HOST_CC))" clean: $(MAKE) -C $(LUAJIT_DIR) clean