Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/bindings-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
features: doubled
example: transcript
python-check: ffi/tests/api.py
# A module path a proxy could serve. No go-check, as no swift-check: api.go needs the
# wasm32 items this export lacks, so the consumer only loads the runtime.
go-module: example.com/api
# No swift-check: api.swift compiles against the fixture's own test export, of the host
# and wasm32-wasip1, not this one's targets. Its transcript stays the exporter's tests'.

Expand All @@ -28,3 +31,5 @@ jobs:
manifest-path: ffi/target/moq-ffi/Cargo.toml
python-check: ffi/moq/moq.py
swift-check: ffi/moq/moq.swift
# Under the export's own module, `moq`, which moq.go imports.
go-check: ffi/moq/moq.go
118 changes: 114 additions & 4 deletions .github/workflows/bindings.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Binding packages

# The shared, read-only builder of a crate's rust2-ffi Python wheels and Swift package, which
# The shared, read-only builder of a crate's rust2-ffi Python wheels, Swift package, and Go
# module, which
# a release workflow ffi/release/package.py's `init` writes calls at a pinned rust2 commit.
# It publishes nothing and holds no credentials: the caller's own jobs publish what it
# uploads, once its consumers pass. See ffi/release/README.md.
Expand All @@ -27,6 +28,14 @@ on:
description: The main.swift of an executable run on the installed Swift package
default: ''
type: string
go-module:
description: The Go module's path; without it, the export's own, which no proxy serves
default: ''
type: string
go-check:
description: The main.go of a program, importing the module's packages, run against it
default: ''
type: string
outputs:
version:
description: The package's Cargo version
Expand Down Expand Up @@ -118,9 +127,10 @@ jobs:
- host: apple
os: macos-26
targets: aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim
# MSVC's library for the wheel, MinGW's for Go, which links through MinGW.
- host: windows
os: windows-latest
targets: x86_64-pc-windows-msvc
targets: x86_64-pc-windows-msvc x86_64-pc-windows-gnu
runs-on: ${{ matrix.os }}
timeout-minutes: 60
defaults:
Expand Down Expand Up @@ -194,7 +204,7 @@ jobs:
targets: aarch64-apple-ios-sim
- host: windows
os: windows-latest
targets: x86_64-pc-windows-msvc
targets: x86_64-pc-windows-msvc x86_64-pc-windows-gnu
runs-on: ${{ matrix.os }}
timeout-minutes: 90
defaults:
Expand Down Expand Up @@ -248,9 +258,10 @@ jobs:
- name: Export
env:
TARGETS: ${{ matrix.targets }}
GO_MODULE: ${{ inputs.go-module }}
run: |
read -ra targets <<< "$TARGETS"
python3 -I rust2/ffi/release/package.py export --manifest-path "source/$MANIFEST" --features "$FEATURES" --api api --out export "${targets[@]}"
python3 -I rust2/ffi/release/package.py export --manifest-path "source/$MANIFEST" --features "$FEATURES" --api api --go-module "$GO_MODULE" --out export "${targets[@]}"
- name: Build
env:
TARGETS: ${{ matrix.targets }}
Expand All @@ -262,10 +273,12 @@ jobs:
env:
PYTHON_CHECK: ${{ inputs.python-check }}
SWIFT_CHECK: ${{ inputs.swift-check }}
GO_CHECK: ${{ inputs.go-check }}
run: |
mkdir -p dist/checks
if [[ -n "$PYTHON_CHECK" ]]; then cp "source/$PYTHON_CHECK" dist/checks/check.py; fi
if [[ -n "$SWIFT_CHECK" ]]; then cp "source/$SWIFT_CHECK" dist/checks/main.swift; fi
if [[ -n "$GO_CHECK" ]]; then cp "source/$GO_CHECK" dist/checks/main.go; fi
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ${{ needs.facts.outputs.name }}-build-${{ matrix.host }}
Expand Down Expand Up @@ -419,3 +432,100 @@ jobs:
env:
MODULE: ${{ needs.facts.outputs.module }}
run: bash rust2/ffi/release/consume-swift.sh swift apple

# The module as a GOPROXY serves it: the export's packages, and each Go target's library
# beside the runtime that links it.
go:
needs: [facts, build, agree]
runs-on: ubuntu-24.04
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
path: source
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ needs.facts.outputs.rust2 }}
ref: ${{ needs.facts.outputs.rust2-sha }}
path: rust2
persist-credentials: false
- uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
if: needs.facts.outputs.fixture == 'true'
with:
tool: just
- name: Stand-in
if: needs.facts.outputs.fixture == 'true'
working-directory: source
run: just ffi standin && cp ffi/moq/ffi.toml ffi/target/moq-ffi/ffi.toml
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: ${{ needs.facts.outputs.name }}-build-*
path: builds
# The go command reads the module's go.mod.
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version: stable
cache: false
- name: Module
env:
NAME: ${{ needs.facts.outputs.name }}
run: |
mkdir libs
cp builds/*/go/*.a libs/
cd source
python3 -I ../rust2/ffi/release/package.py go --manifest-path "$MANIFEST" --export "../builds/$NAME-build-linux-x64/export" --libs ../libs --out ../proxy >> "$GITHUB_STEP_SUMMARY"
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ${{ needs.facts.outputs.name }}-go
path: proxy
if-no-files-found: error

# A new module whose only requirement is the built one, unpacked from its zip, as `go get`
# unpacks it: built and run on each platform with no checkout, Rust, or CGO_LDFLAGS.
go-consumer:
needs: [facts, go]
strategy:
fail-fast: false
matrix:
include:
- host: linux-x64
os: ubuntu-24.04
- host: linux-arm64
os: ubuntu-24.04-arm
- host: apple-darwin
os: macos-26
- host: windows
os: windows-latest
runs-on: ${{ matrix.os }}
timeout-minutes: 30
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ needs.facts.outputs.rust2 }}
ref: ${{ needs.facts.outputs.rust2-sha }}
path: rust2
sparse-checkout: ffi/release
persist-credentials: false
# The newest Go, whose cgo checks what a #cgo line links most strictly.
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version: stable
cache: false
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: ${{ needs.facts.outputs.name }}-go
path: proxy
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: ${{ needs.facts.outputs.name }}-build-${{ matrix.host }}
path: dist
- name: Build and run
run: |
args=()
if [[ -f dist/checks/main.go ]]; then args+=(--check dist/checks/main.go); fi
if [[ -f dist/native.txt ]]; then args+=(--native dist/native.txt); fi
python3 -I rust2/ffi/release/package.py consume-go --proxy proxy "${args[@]}"
28 changes: 22 additions & 6 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,22 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u

`go/` holds a Go package per crate whose items the facade exports, over the C header
through cgo, for Go 1.24 or later, and the runtime they share, `go/rt`. Each includes
`<prefix>.h` from the `include/` beside `go/`, as the export lays them out, and links the
library through `CGO_LDFLAGS`. A crate's package is its name without the prefix and its
`<prefix>.h` from `go/rt`, so `go/` is a whole module, as a release's zip holds it. The
runtime links each exported target's static library from beside itself, as
`rt/lib<prefix>_<goos>_<goarch>.a`, by a `#cgo <goos>,<goarch> LDFLAGS` line naming it and the
system libraries it needs, as rustc's `--print native-static-libs` prints them, which the
check of each target reads from its crates' `#[link]` attributes and build scripts. So a
build needs no `CGO_LDFLAGS`, and on a platform with no library, `rt/platform.go` fails to
compile. Go links Windows through MinGW, so only `x86_64-pc-windows-gnu` and
`aarch64-pc-windows-gnullvm` are Windows targets it links, and iOS the device's library, as
Go builds the simulator's from the same `ios/arm64`. A library a crate ships rather than the
system, as winapi's `winapi_<dll>` import libraries for MinGW, is not found where the Go
links: the release builder exports and builds with `WINAPI_NO_BUNDLED_LIBRARIES=1`, so
winapi links the system's; Linux and macOS lines exclude Android
and iOS, which Go also counts as `linux` and `darwin`. The export puts no library there:
copy it in, as [the release builder](release/README.md) does, without the LLVM bitcode
(`.llvmbc`, `__LLVM`) that Go refuses, from 1.26.11 and 1.27.4, in an archive a `#cgo` line
names. A crate's package is its name without the prefix and its
`_`: moq-mux's is `mux`, and serde_json's `serdejson`, but keeps the prefix where its
strip leaves no leading letter (`moq_2d` is `moq2d`). One that a Go keyword, `C`, `main`,
the runtime, or an earlier crate's package takes gains a trailing `_`, as a crate `rt` is
Expand All @@ -981,10 +995,12 @@ while Rust's modules may, so a package per module could not hold moq. An instanc
generic type, or a function naming one, belongs to the last of its own and its arguments'
crates in Cargo's order, which can name them all: moq's
`mux::catalog::Producer<hang::catalog::Extra>` is in `mux`. The packages' import path is
the module of the `go.mod` above `--out`, as `go mod edit -json` reads it, with the path to
`go/`; or else `<prefix>`, the module of the `go.mod` the export writes in `go/`, which a
caller requires and replaces with that directory. The export needs `go` on PATH whenever it
reads a go.mod.
the module of the module `go/` is in, as Go finds it: that of a `go.mod` in `go/` itself,
which the export keeps as it is, as a release's names its module path, or else of the
`go.mod` above `--out`, with the path to `go/`, each as `go mod edit -json` reads it; or else
`<prefix>`, the module of the `go.mod` the export writes in `go/`, which a caller requires and
replaces with that directory. The export needs `go` on PATH whenever it reads a go.mod
other than one in its own form, `module <path>` and `go 1.24`.

- **Names** are the C names without the prefix and the segments outside the item's crate,
such as the facade's module re-exporting it, in Pascal case: `moq_track_Producer` is
Expand Down
20 changes: 12 additions & 8 deletions ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ bench-build checkout="": (moq-export checkout) (moq-web checkout) ts-program cod
cc -O2 -std=c11 -Wall -Wextra -Werror -I "$moqc/include" -I "$hooks" moq/bench/moq-c.c "$src/target/release/libmoq.a" "$hooks/libcodspeed.a" -lpthread -ldl -lm -o target/bench-moq-c
c++ -O2 -std=c++17 -Wall -Wextra -Werror -I "$out/include" -I "$hooks" moq/bench/export.cpp "$out/target/release/libmoq.a" "$hooks/libcodspeed.a" -lpthread -ldl -lm -o target/bench-export-cpp
{{ quote(just_executable()) }} go-module bench-go moq/bench/export.go
read -ra native < target/moq-native
# The include path goes in CGO_CPPFLAGS, so cgo keeps its default -O2 -g in CGO_CFLAGS.
export CGO_CPPFLAGS="-I$hooks" CGO_LDFLAGS="$PWD/$out/target/release/libmoq.a $hooks/libcodspeed.a ${native[*]}" GOFLAGS=-buildvcs=false
# The include path goes in CGO_CPPFLAGS, so cgo keeps its default -O2 -g in CGO_CFLAGS;
# the runtime's #cgo line links moq.
export CGO_CPPFLAGS="-I$hooks" CGO_LDFLAGS="$hooks/libcodspeed.a" GOFLAGS=-buildvcs=false
(cd target/bench-go && go build -o bench .)
{{ quote(just_executable()) }} dart-program bench-dart moq/bench/export.dart
rm -rf target/bench-kt
Expand Down Expand Up @@ -257,9 +257,9 @@ moq checkout="": (moq-cpp checkout)
mypy --strict --python-version 3.11 --cache-dir target/mypy "$out/python/moq.py"
pyright -p tests/pyright.json "$out/python/moq.py"
{{ quote(just_executable()) }} moq-swift-run
# The Go, through cgo, linking the library and what it needs besides.
# The Go, through cgo, whose runtime's #cgo line links the library and what it needs besides.
{{ quote(just_executable()) }} go-module moq-go moq/moq.go
export CGO_LDFLAGS="$PWD/$out/target/release/libmoq.a ${native[*]}" GOFLAGS=-buildvcs=false
export GOFLAGS=-buildvcs=false
(cd target/moq-go/export && go vet ./...)
(cd target/moq-go && go vet ./... && go run .)
# The Dart, through the shared library the dart feature builds.
Expand Down Expand Up @@ -341,17 +341,21 @@ swift-target macos:
#!/usr/bin/env bash
if [ "{{ os() }}" = macos ]; then echo "-target $(uname -m)-apple-macos{{ macos }}"; fi

# Lay out target/<dir> as a Go module whose main package is <main>, requiring the export's module of a package per crate in export/, beside the header it includes, linking the library through cgo.
# Lay out target/<dir> as a Go module whose main package is <main>, requiring the export's module of a package per crate in export/, with the library its runtime's #cgo line links, less the LLVM bitcode the go command refuses there.
[private]
go-module dir main:
#!/usr/bin/env bash
set -euo pipefail
out=target/export/moq
dir=target/{{ dir }}
rm -rf "$dir"
mkdir -p "$dir/include"
mkdir -p "$dir"
cp -r "$out/go" "$dir/export"
cp "$out/include/moq.h" "$dir/include/"
lib=libmoq_$(go env GOOS)_$(go env GOARCH).a
grep -qF "LDFLAGS: \${SRCDIR}/$lib" "$dir/export/rt/rt.go" || { echo "the export's Go links no $lib" >&2; exit 1; }
"$(rustc --print sysroot)/lib/rustlib/$(rustc -vV | sed -n 's/^host: //p')/bin/llvm-objcopy" \
--remove-section=.llvmbc --remove-section=.llvmcmd --remove-section=__LLVM,__bitcode --remove-section=__LLVM,__cmdline \
"$out/target/release/libmoq.a" "$dir/export/rt/$lib"
cp {{ main }} "$dir/main.go"
printf 'module moqtest\n\ngo 1.24\n\nrequire moq v0.0.0\n\nreplace moq => ./export\n' > "$dir/go.mod"

Expand Down
Loading
Loading