diff --git a/.github/workflows/bindings-test.yml b/.github/workflows/bindings-test.yml index 419c5209f..8fba1700d 100644 --- a/.github/workflows/bindings-test.yml +++ b/.github/workflows/bindings-test.yml @@ -1,6 +1,6 @@ name: Binding packages test -# bindings.yml on the api fixture, whose Python and web consumers must print native Rust's +# bindings.yml on the api fixture, whose Python, C++, and web consumers must print native Rust's # transcript byte for byte, and on moq's stand-in facade at the exporter's moq pin, its first consumer. # Read-only: nothing here publishes. on: @@ -18,6 +18,8 @@ jobs: features: doubled example: transcript python-check: ffi/tests/api.py + # api.cpp rather than api.c, whose broken cases name items only wasm32 has. + c-check: ffi/tests/api.cpp web-manifest-path: ffi/fixtures/api/Cargo.toml scope: kixel-ai web-check: ffi/tests/api.ts @@ -34,6 +36,7 @@ jobs: manifest-path: ffi/target/moq-ffi/Cargo.toml python-check: ffi/moq/moq.py swift-check: ffi/moq/moq.swift + c-check: ffi/moq/moq.cpp # moq's wasm32 stand-in, which leaves moq-tokio out, as no wasm32 target builds it. web-manifest-path: ffi/target/moq-ffi-web/Cargo.toml scope: kixel-ai diff --git a/.github/workflows/bindings.yml b/.github/workflows/bindings.yml index d9f267eb9..09619381f 100644 --- a/.github/workflows/bindings.yml +++ b/.github/workflows/bindings.yml @@ -1,7 +1,7 @@ name: Binding packages -# The shared, read-only builder of a crate's rust2-ffi Python wheels, Swift package, Go -# module, and web package, which a release workflow ffi/release/package.py's `init` writes +# The shared, read-only builder of a crate's rust2-ffi Python wheels, Swift package, C archives, +# Go module, and web package, 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. @@ -28,6 +28,10 @@ on: description: The main.swift of an executable run on the installed Swift package default: '' type: string + c-check: + description: A C or C++ program built by clean CMake and pkg-config consumers of each C archive + default: '' + type: string web-manifest-path: description: The Cargo.toml of the package whose wasm32-wasip1 export the npm and JSR package holds, often manifest-path; without it, no web package default: '' @@ -327,11 +331,14 @@ jobs: env: PYTHON_CHECK: ${{ inputs.python-check }} SWIFT_CHECK: ${{ inputs.swift-check }} + C_CHECK: ${{ inputs.c-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 + # Its extension says whether it is C or C++. + if [[ -n "$C_CHECK" ]]; then mkdir dist/checks/c && cp "source/$C_CHECK" dist/checks/c/; fi if [[ -n "$GO_CHECK" ]]; then cp "source/$GO_CHECK" dist/checks/main.go; fi - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: @@ -525,6 +532,57 @@ jobs: MODULE: ${{ needs.facts.outputs.module }} run: bash rust2/ffi/release/consume-swift.sh swift apple + # Each C archive unpacked alone, with no checkout, Rust, or source path: a new CMake project + # finding it by its CPS file and by its config package, and on Linux and macOS a pkg-config + # build, each linking the static and the shared library. + c: + needs: [facts, build, agree] + 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: 20 + 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 + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ needs.facts.outputs.name }}-build-${{ matrix.host }} + path: dist + # The oldest CMake that reads CPS is 4.3; the config package serves older ones too. + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: '3.13' + - name: Consume + run: | + python -m pip install cmake==4.4.5 + if [[ "$RUNNER_OS" == macOS ]] && ! command -v pkg-config > /dev/null; then brew install pkgconf; fi + shopt -s nullglob + archives=(dist/c/*) + [[ ${#archives[@]} == 1 ]] || { echo "::error::expected one C archive, found ${archives[*]}"; exit 1; } + args=(--archive "${archives[0]}") + if [[ -f dist/native.txt ]]; then args+=(--native dist/native.txt); fi + checks=(dist/checks/c/*) + if [[ ${#checks[@]} == 1 ]]; then args+=(--check "${checks[0]}"); fi + python -I rust2/ffi/release/package.py consume-c "${args[@]}" + # The web binding's npm and JSR packages: web-manifest-path exported for wasm32-wasip1 # alone, the binding's only target, and its module built beside it; with native Rust's # output on that target, which the consumers compare with. diff --git a/ffi/README.md b/ffi/README.md index bed53024a..ff6d08717 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -481,7 +481,9 @@ packages place it in. - **Names** are the C names without the prefix, in a namespace of it: `moq_track_Producer` is `moq::track_Producer`, and its method `moq_track_Producer_append_group` is - `moq::track_Producer::append_group`. A keyword takes a trailing `_` (`counter::new_`), + `moq::track_Producer::append_group`. A keyword, or a macro GNU dialects predefine + (`unix`, `linux`, `i386`, as in moq-tokio's `tokio::unix_`, and each of them followed by + underscores, so `unix_` becomes `unix__`), takes a trailing `_` (`counter::new_`), as does a member named as its class or one of its fields, and a parameter named as a member or as anything in the namespace, which it would hide. - **Objects** are classes owning their handle through a `std::unique_ptr` whose deleter diff --git a/ffi/release/README.md b/ffi/release/README.md index aad095b91..ec20054b4 100644 --- a/ffi/release/README.md +++ b/ffi/release/README.md @@ -1,8 +1,8 @@ # Binding releases A crate's [rust2-ffi](/ffi/README.md) export, released as Python wheels, a Swift package, -a Go module, and a web package on npm and JSR. Two halves, split where Cargo's knowledge -ends: +C and C++ archives, a Go module, and a web package on npm and JSR. Two halves, split where +Cargo's knowledge ends: - **The builder**, [`bindings.yml`](/.github/workflows/bindings.yml) over `package.py`, shared and read-only. It derives everything from Cargo metadata and the export: package @@ -56,8 +56,9 @@ A Cargo prerelease, such as `1.0.0-rc.1`, is a GitHub prerelease too. The builder takes the crate's `Cargo.toml`, the `go-module` path, which without it is the export's own `` that no proxy serves, and optional inputs the caller's tests own: `features` to ship, an `example` whose stdout is native Rust's transcript, and the -`python-check`, `swift-check`, and `go-check` programs that must print it through the -installed packages. Without a check, a consumer only imports the module. The web package +`python-check`, `swift-check`, `c-check`, and `go-check` programs that must print it through +the installed packages. Without a check, a consumer only imports the module, or links a +function of each C and C++ header. A `c-check` is C or C++ by its extension. The web package takes `web-manifest-path`, the package exported for `wasm32-wasip1`, often the crate's own, which no Cargo fact says builds for the web, and `scope`; and an optional `web-check`, a TypeScript module importing the binding as `./web/.ts` does beside an export, @@ -97,7 +98,36 @@ crate apart from it takes `web-features` and `web-example` instead. revision, since a package resolved by version cannot depend on a revision. Rust's native link libraries become linker settings, per platform where they differ; any other flag fails the build. -7. Go: one module for every target, at the module path and `v`, the major +7. C and C++: an archive per wheel target, `--.tar.gz`, or `.zip` + on Windows, built like the wheels, Linux's against glibc 2.28 and macOS's at 11.0. It + holds `include/` with both headers, the static and shared libraries, and three ways to + find them, each naming the native libraries the static library needs, from rustc's + `--print native-static-libs`: + - `lib/cmake//-config.cmake`, whose `find_package()` gives `::`, + the static library, and `::_shared`; + - `lib/cps/.cps`, the [Common Package Specification](https://cps-org.github.io/cps/) + of the same two components, which CMake 4.3 and newer prefers; + - `lib/pkgconfig/.pc`, which links the shared library, as the linker prefers it to + the static one beside it; `--static` adds the native libraries, for a prefix that holds + only the static library. None on Windows, whose MSVC has no pkg-config convention. + + The shared library's SONAME, or `@rpath` install name on macOS, holds the whole version, + `lib.so.0.4.12` or `lib.0.4.12.dylib`: every binding checks at load that the + library's [contract](/ffi/README.md#the-c-abi) is its header's, so no two releases' + libraries can stand in for each other. A build-time request is another matter, as header + and library come together: the CMake and CPS versions accept one within Cargo's + compatible line, 0.4 for 0.4.12, and a prerelease only by a request naming no version. + Both targets require C11 and C++17 of what links them, the headers' floor. + On Windows it is `bin/.dll` with `lib/.dll.lib`, beside the static `lib/.lib`. + + The static library is one object prelinked from Rust's archive (`ld -r`, then `objcopy` + on ELF and the linker's exported-symbols list on Mach-O) whose only global symbols are + the C ABI's, `_*`. So its std and compiler-builtins, with unmangled symbols such as + `__addvdi3`, cannot clash with another Rust static library in the same program, which + each build checks by linking one beside it. MSVC's linker has no relocatable link, so the + Windows static library keeps every symbol: a program with two Rust static libraries + there links the DLL instead. +8. Go: one module for every target, at the module path and `v`, the major version's `/vN` suffix checked: the export's `go/`, whose runtime's `#cgo` lines link `rt/lib__.a` for `linux/amd64` and `linux/arm64` (built against glibc 2.28), `darwin/arm64` and `ios/arm64` (the Swift package's macOS and iOS device @@ -112,35 +142,38 @@ crate apart from it takes `web-features` and `web-example` instead. limit, each platform's library moves to a module of its own, whose one package's `#cgo` line links it and which the runtime imports under that platform's build constraint, so `go get` downloads only the platform it builds for. -8. The release's `api.json`, and `changes.md`, the items removed, changed, and added since +9. The release's `api.json`, and `changes.md`, the items removed, changed, and added since the previous release's, by kind and Rust path; the caller's release publishes the first as an asset and the second as its notes. -9. Consumers, with no checkout, Rust, or source path: each wheel installed with - `--no-index --no-deps` into a fresh environment on its oldest CPython, run with `-I` - from an empty directory; and the Swift package built by its own scheme for macOS, iOS, - and the iOS Simulator, then consumed by a new executable package that runs the check; - and the Go module, unpacked from its zip into a new module that requires it, vetted, - built with the newest Go and no `CGO_LDFLAGS`, and run on Linux x64 and arm64, macOS, and - Windows. With an `example`, each must print native Rust's stdout byte for byte. -8. Web: `web-manifest-path` exported for `wasm32-wasip1` alone, whose `web/` holds the - TypeScript, its JavaScript and declarations, and a `package.json` - ([TypeScript (web)](/ffi/README.md#typescript-web)), and its module built at - `opt-level = "s"` unless the crate's release profile sets a level, as a page downloads - it. The npm package is the JavaScript, declarations, and module, named - `@/` with Cargo's description, license, and URLs, packed by `npm pack`; - the JSR package is the TypeScript and module with a `jsr.json` exporting each module by - name, refused past JSR's 20 MiB. The example runs for `wasm32-wasip1` under Node's WASI, - native Rust on the binding's target. A new npm project installs the tarball, as an - app would, and the `web-check`, or else a `load()`, runs on Node, Deno, and Bun, and - bundled by Vite and by webpack into a browser's script that Node runs, each finding the - module where `load` looks by default; and `deno publish --dry-run` checks the JSR - package, its types against Deno's included. +10. Consumers, with no checkout, Rust, or source path: each wheel installed with + `--no-index --no-deps` into a fresh environment on its oldest CPython, run with `-I` + from an empty directory; and the Swift package built by its own scheme for macOS, iOS, + and the iOS Simulator, then consumed by a new executable package that runs the check; + and each C archive unpacked alone, built by a new CMake project through its CPS file and + through its config package, and on Linux and macOS by pkg-config, linking each library, + where a shared consumer must record the versioned name; and the Go module, unpacked from + its zip into a new module that requires it, vetted, built with the newest Go and no + `CGO_LDFLAGS`, and run on Linux x64 and arm64, macOS, and Windows. + With an `example`, each must print native Rust's stdout byte for byte. +11. Web: `web-manifest-path` exported for `wasm32-wasip1` alone, whose `web/` holds the + TypeScript, its JavaScript and declarations, and a `package.json` + ([TypeScript (web)](/ffi/README.md#typescript-web)), and its module built at + `opt-level = "s"` unless the crate's release profile sets a level, as a page downloads + it. The npm package is the JavaScript, declarations, and module, named + `@/` with Cargo's description, license, and URLs, packed by `npm pack`; + the JSR package is the TypeScript and module with a `jsr.json` exporting each module by + name, refused past JSR's 20 MiB. The example runs for `wasm32-wasip1` under Node's WASI, + native Rust on the binding's target. A new npm project installs the tarball, as an + app would, and the `web-check`, or else a `load()`, runs on Node, Deno, and Bun, and + bundled by Vite and by webpack into a browser's script that Node runs, each finding the + module where `load` looks by default; and `deno publish --dry-run` checks the JSR + package, its types against Deno's included. rust2 runs it on every manual Check and nightly through [`bindings-test.yml`](/.github/workflows/bindings-test.yml): on the api fixture, whose -wheels and web package must print its native transcript, and on moq's stand-in facade at -the exporter's moq pin, through `moq/moq.py`, `moq/moq.swift`, and `moq/moq.go`, with its -wasm32 stand-in as the web package. +wheels, C++ consumers, and web package must print its native transcript, and on moq's +stand-in facade at the exporter's moq pin, through `moq/moq.py`, `moq/moq.swift`, +`moq/moq.cpp`, and `moq/moq.go`, with its wasm32 stand-in as the web package. ## Other registries diff --git a/ffi/release/package.py b/ffi/release/package.py index 2e6012aff..6bd602175 100644 --- a/ffi/release/package.py +++ b/ffi/release/package.py @@ -1,4 +1,4 @@ -"""Build and check the Python wheels, Swift package, Go module, and web package of a crate's rust2-ffi export. +"""Build and check the Python wheels, Swift package, C archives, Go module, and web package of a crate's rust2-ffi export. bindings.yml runs each step on the host it needs; release.yml is the caller `init` writes. Every fact about the package comes from Cargo metadata or the export, so nothing here @@ -19,6 +19,7 @@ import shutil import subprocess import sys +import tarfile import tempfile import tomllib import zipfile @@ -37,7 +38,7 @@ "aarch64-apple-ios": "ios", "aarch64-apple-ios-sim": "ios", } -# Every target a wheel is built for. +# Every target a wheel and a C archive are built for, each of 8-byte pointers, as the CMake version file checks. WHEEL_TARGETS = [ "x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", @@ -287,7 +288,8 @@ def digest(export, lib): """A hash of the generated bindings, which every build host's export must agree on.""" hasher = hashlib.sha256() export = Path(export) - paths = [export / "python" / f"{lib}.py", export / "include" / f"{lib}.h", *sorted((export / "swift").iterdir()), + headers = sorted(p for p in (export / "include").rglob("*") if p.is_file()) + paths = [export / "python" / f"{lib}.py", *headers, *sorted((export / "swift").iterdir()), *sorted(p for p in (export / "go").rglob("*") if p.is_file())] for path in paths: hasher.update(path.relative_to(export).as_posix().encode() + b"\0") @@ -449,6 +451,558 @@ def local_manifest(state, package, framework): (package / "Package.swift").write_text(package_swift(state["module"], state["collections"], state["settings"], binary), encoding="utf-8") +# The CPS revision CMake 4.4 writes, the oldest CMake that both reads and writes it. +CPS_VERSION = "0.14.1" + + +def compat_version(version): + """The oldest version `version` serves a build-time request for, by Cargo's compatibility. + + Cargo calls 1.2.3 compatible with 1.x, 0.4.2 with 0.4.x, and 0.0.3 with itself; a + prerelease promises nothing, so it serves only itself. + """ + match = re.fullmatch(r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?", version) + if match is None: + raise SystemExit(f"version {version} is not semver") + major, minor, patch, pre, _ = match.groups() + if pre: + return f"{major}.{minor}.{patch}{pre}" + if major != "0": + return f"{major}.0.0" + if minor != "0": + return f"0.{minor}.0" + return f"0.0.{patch}" + + +def soname_version(version): + """The version a shared library's name holds: all of it, as each release is its own ABI. + + Every binding checks at load that the library's contract, a hash of everything its header + declares, is its header's, so even an addition makes two releases' libraries differ. + """ + compat_version(version) + return version.split("+")[0] + + +def c_files(lib, target, version): + """The archive's library paths: static, shared, the shared library's link name, and its import library.""" + if "windows" in target: + return {"static": f"lib/{lib}.lib", "shared": f"bin/{lib}.dll", "link": None, "implib": f"lib/{lib}.dll.lib"} + if "apple" in target: + return {"static": f"lib/lib{lib}.a", "shared": f"lib/lib{lib}.{soname_version(version)}.dylib", "link": f"lib/lib{lib}.dylib", + "implib": None} + return {"static": f"lib/lib{lib}.a", "shared": f"lib/lib{lib}.so.{soname_version(version)}", "link": f"lib/lib{lib}.so", "implib": None} + + +def native_libs(flags, target): + """rustc's native-static-libs as (kind, name) pairs: a `lib`, an Apple `framework`, or an MSVC linker `flag`.""" + out = [] + args = iter(shlex.split(flags)) + for flag in args: + if "windows" in target: + if flag.endswith(".lib"): + out.append(("lib", flag.removesuffix(".lib"))) + elif re.fullmatch(r"/defaultlib:[A-Za-z0-9_]+", flag): + out.append(("flag", flag)) + else: + raise SystemExit(f"unsupported native linker flag on {target}: {flag}") + elif flag.startswith("-l") and len(flag) > 2: + out.append(("lib", flag[2:])) + elif flag == "-framework" and "apple" in target: + out.append(("framework", next(args))) + else: + raise SystemExit(f"unsupported native linker flag on {target}: {flag}") + return out + + +def pkg_config(info, natives): + """`.pc`, relative to itself: the shared library by default, and what `--static` adds.""" + private = " ".join(f"-l{name}" if kind == "lib" else f"-framework {name}" for kind, name in natives) + description = " ".join((info["description"] or f"The C and C++ binding of {info['name']}").split()) + lines = [ + "prefix=${pcfiledir}/../..", + "includedir=${prefix}/include", + "libdir=${prefix}/lib", + "", + f"Name: {info['name']}", + f"Description: {description}", + f"Version: {info['version']}", + ] + url = info["homepage"] or info["repository"] + if url: + lines.append(f"URL: {url}") + lines += [f"Libs: -L${{libdir}} -l{info['lib']}", f"Libs.private: {private}", "Cflags: -I${includedir}"] + return "\n".join(lines) + "\n" + + +def cmake_quote(value): + """A CMake bracket argument, which takes any text but its own closing bracket.""" + if "]==]" in value: + raise SystemExit(f"{value!r} has no CMake spelling here") + return f"[==[{value}]==]" + + +def cmake_config(info, target, files, natives): + """`-config.cmake`: `::` the static library, `::_shared` the shared one, each at C11 and C++17 or newer.""" + lib = info["lib"] + libs = [name if kind == "lib" else f"-framework {name}" for kind, name in natives if kind != "flag"] + options = [name for kind, name in natives if kind == "flag"] + prefix = f"${{_{lib}_prefix}}" + shared = [f'IMPORTED_LOCATION "{prefix}/{files["shared"]}"'] + if files["implib"]: + shared.append(f'IMPORTED_IMPLIB "{prefix}/{files["implib"]}"') + shared_props = "\n ".join(shared) + return f"""# find_package({lib}): {info['name']} {info['version']}'s C and C++ binding for {target}. +# {lib}::{lib} links the static library and what it needs of the system; {lib}::{lib}_shared links the +# shared library. Generated by rust2-ffi's release builder. + +get_filename_component(_{lib}_prefix "${{CMAKE_CURRENT_LIST_DIR}}/../../.." ABSOLUTE) + +if(NOT TARGET {lib}::{lib}) + add_library({lib}::{lib} STATIC IMPORTED) + set_target_properties({lib}::{lib} PROPERTIES + IMPORTED_LOCATION "{prefix}/{files['static']}" + IMPORTED_LINK_INTERFACE_LANGUAGES C + INTERFACE_INCLUDE_DIRECTORIES "{prefix}/include" + INTERFACE_COMPILE_FEATURES "c_std_11;cxx_std_17" + INTERFACE_LINK_LIBRARIES {cmake_quote(';'.join(libs))} + INTERFACE_LINK_OPTIONS {cmake_quote(';'.join(options))} + ) +endif() + +if(NOT TARGET {lib}::{lib}_shared) + add_library({lib}::{lib}_shared SHARED IMPORTED) + set_target_properties({lib}::{lib}_shared PROPERTIES + {shared_props} + INTERFACE_INCLUDE_DIRECTORIES "{prefix}/include" + INTERFACE_COMPILE_FEATURES "c_std_11;cxx_std_17" + ) +endif() + +unset(_{lib}_prefix) +""" + + +def cmake_version(info): + """`-config-version.cmake`: Cargo's compatibility, a range's upper bound, and 64-bit consumers alone.""" + compat = compat_version(info["version"]) + if "-" in info["version"].split("+")[0]: + # CMake's versions are numbers alone, so no request can name a prerelease, and none + # may take one for the release it precedes. + return f"""# The version check of find_package({info['lib']}): {info['version']} is a prerelease, which only a +# request naming no version finds. Generated by rust2-ffi's release builder. + +set(PACKAGE_VERSION "{info['version']}") +set(PACKAGE_VERSION_COMPATIBLE FALSE) +""" + return f"""# The version check of find_package({info['lib']}): a request from {compat} up to this version is met, +# as Cargo's compatibility has it, by a 64-bit consumer. Generated by rust2-ffi's release builder. + +set(PACKAGE_VERSION "{info['version']}") + +if(CMAKE_SIZEOF_VOID_P AND NOT CMAKE_SIZEOF_VOID_P STREQUAL "8") + set(PACKAGE_VERSION "{info['version']} (64-bit)") + set(PACKAGE_VERSION_UNSUITABLE TRUE) + return() +endif() + +set(PACKAGE_VERSION_COMPATIBLE FALSE) +if(NOT PACKAGE_FIND_VERSION VERSION_LESS "{compat}" AND NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) + set(PACKAGE_VERSION_COMPATIBLE TRUE) +endif() +if(PACKAGE_VERSION_COMPATIBLE AND PACKAGE_FIND_VERSION_RANGE) + if(PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE") + if(PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX) + set(PACKAGE_VERSION_COMPATIBLE FALSE) + endif() + elseif(PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX) + set(PACKAGE_VERSION_COMPATIBLE FALSE) + endif() +endif() +if(PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) + set(PACKAGE_VERSION_EXACT TRUE) +endif() +""" + + +def cps(info, files, natives): + """`.cps`, the Common Package Specification's description: the same two components.""" + lib = info["lib"] + compat = compat_version(info["version"]) + static = { + "type": "archive", + "location": f"@prefix@/{files['static']}", + "includes": ["@prefix@/include"], + "compile_features": ["c11", "c++17"], + "link_languages": ["c"], + } + libs = [name if kind == "lib" else f"-framework {name}" for kind, name in natives if kind != "flag"] + flags = [name for kind, name in natives if kind == "flag"] + if libs: + static["link_libraries"] = libs + if flags: + static["link_flags"] = flags + shared = {"type": "dylib", "location": f"@prefix@/{files['shared']}", "includes": ["@prefix@/include"], + "compile_features": ["c11", "c++17"]} + if files["implib"]: + shared["link_location"] = f"@prefix@/{files['implib']}" + description = { + "cps_version": CPS_VERSION, + "name": lib, + "version": info["version"], + "compat_version": compat, + "cps_path": "@prefix@/lib/cps", + # A simple version ignores a prerelease's suffix; a custom one matches only itself. + "version_schema": "custom" if "-" in info["version"].split("+")[0] else "simple", + # `_shared` is never ``, whatever the library is named. + "components": {lib: static, f"{lib}_shared": shared}, + "default_components": [lib], + } + if info["description"]: + description["description"] = " ".join(info["description"].split()) + if info["license"]: + description["license"] = info["license"] + if info["homepage"] or info["repository"]: + description["website"] = info["homepage"] or info["repository"] + return json.dumps(description, indent=2) + "\n" + + +def exported(shared, lib, target): + """The C ABI's symbols, `_*`, that the shared library exports, as the object format spells them.""" + if "apple" in target: + names = output("nm", "--no-llvm-bc", "-gUj", shared).split() + want = f"_{lib}_" + else: + names = [line.split()[-1] for line in output("nm", "-D", "--defined-only", shared).splitlines() if line.strip()] + want = f"{lib}_" + found = sorted({name for name in names if name.startswith(want)}) + if not found: + raise SystemExit(f"{shared} exports no {want}* symbol") + return found + + +def undefined(path, kind): + """The undefined symbols of an ELF object or archive that nm marks `kind`: `U` strong, `w` weak.""" + lines = output("nm", "--undefined-only", path).splitlines() + return {line.split()[1] for line in lines if line.split()[:1] == [kind] and len(line.split()) == 2} + + +def commons_source(nm): + """C defining each common symbol `nm -m` lists that no member defines, at its largest size and alignment.""" + commons, defined = {}, set() + for line in nm.splitlines(): + common = re.fullmatch(r"([0-9a-f]+) \(common\) (?:\(alignment 2\^([0-9]+)\) )?external (\S+)", line.strip()) + if common: + size, align = int(common.group(1), 16), int(common.group(2) or 0) + old_size, old_align = commons.get(common.group(3), (0, 0)) + commons[common.group(3)] = (max(size, old_size), max(align, old_align)) + elif re.search(r"\) (?:weak )?(?:private )?external ", line) and "(undefined)" not in line: + defined.add(line.split()[-1]) + return "".join( + f'__attribute__((aligned({1 << align}))) char common{i}[{size}] __asm__("{name}");\n' + for i, (name, (size, align)) in enumerate(sorted((n, c) for n, c in commons.items() if n not in defined)) + ) or "typedef int empty;\n" + + +def define_commons(static, arch, work): + """An object defining each C tentative definition in a Mach-O archive that no member defines. + + Apple's clang emits `int x;` as a common symbol, such as aws-lc's `tty_in`, which a + relocatable link leaves common, and global, past the exported-symbols list; Apple's + current linker has no `-d` to define it. Defined here at its size and alignment, it is + an ordinary symbol the list hides. + """ + source = work / "commons.c" + source.write_text(commons_source(output("nm", "--no-llvm-bc", "-m", static)), encoding="utf-8") + obj = work / "commons.o" + run("cc", "-c", "-fno-common", *arch, source, "-o", obj) + return obj + + +def hide(static, shared, lib, target, work): + """The static library as one prelinked object exporting only the C ABI, so its Rust runtime cannot clash. + + Another Rust static library in the same program brings its own std and compiler-builtins, + whose unmangled symbols, such as `__addvdi3`, would otherwise collide. MSVC's linker has no + relocatable link, so a Windows static library keeps every symbol. + """ + keep = exported(shared, lib, target) + names = work / "exported.txt" + names.write_text("\n".join(keep) + "\n", encoding="utf-8") + roots = [arg for name in keep for arg in ("-u", name)] + merged, hidden, archive = work / "merged.o", work / "hidden.o", work / static.name + if "apple" in target: + arch = ["-arch", "arm64", f"-mmacosx-version-min={MACOS_WHEEL}"] + run("cc", "-r", "-nostdlib", *arch, *(f"-Wl,{arg}" for arg in roots), f"-Wl,-exported_symbols_list,{names}", + define_commons(static, arch, work), static, "-o", hidden) + run("libtool", "-static", "-o", archive, hidden) + # Rust's objects embed LLVM bitcode newer than Xcode's nm reads. + found = output("nm", "--no-llvm-bc", "-gUj", hidden).split() + else: + # Only the members the C ABI reaches, without the sections it never references. + run("ld", "-r", "--gc-sections", *roots, static, "-o", merged) + # What std looks up only if the system has it, such as a newer glibc's + # `posix_spawn_file_actions_addchdir`, stays weak: binutils 2.42's `ld -r + # --gc-sections` makes it local, which objcopy makes strong, failing a link on an + # older glibc. + weak = undefined(static, "w") - undefined(static, "U") + weaken = work / "weak.txt" + weaken.write_text("".join(f"{name}\n" for name in sorted(weak)), encoding="utf-8") + # A COMDAT group would still merge with another library's copy, such as std's + # `DW.ref.rust_eh_personality`, whose symbol here is now local: as plain sections, + # each library keeps its own. + run("objcopy", "--remove-section=.group", f"--keep-global-symbols={names}", f"--weaken-symbols={weaken}", + merged, hidden) + if "There are no section groups" not in output("readelf", "-g", hidden): + raise SystemExit(f"the prelinked {static.name} still holds section groups") + strong = sorted(undefined(hidden, "U") & weak) + if strong: + raise SystemExit(f"the prelinked {static.name} needs what Rust's archive only looks up, such as {strong[:5]}") + run("ar", "rcsD", archive, hidden) + found = [line.split()[-1] for line in output("nm", "-g", "--defined-only", hidden).splitlines() if line.strip()] + if sorted(set(found)) != keep: + extra = sorted(set(found) - set(keep))[:10] + lines = [line for line in output("nm", *(["--no-llvm-bc"] if "apple" in target else []), "-m" if "apple" in target else "-S", + hidden).splitlines() if line.split() and line.split()[-1] in extra] + sys.stderr.write("\n".join(lines) + "\n") + raise SystemExit(f"the prelinked {static.name} exports other symbols than the C ABI, such as {extra}") + return archive + + +def coexist(archive, include, lib, natives, manifest, target, work): + """Link the prelinked static library with another Rust static library, in either order, and run it.""" + other = work / "other" + (other / "src").mkdir(parents=True) + (other / "Cargo.toml").write_text('[package]\nname = "other"\nversion = "0.0.0"\nedition = "2021"\n\n' + '[lib]\ncrate-type = ["staticlib"]\n\n[workspace]\n', encoding="utf-8") + (other / "src/lib.rs").write_text('#[no_mangle]\npub extern "C" fn rust2_other_sum(a: i64, b: i64) -> i64 {\n' + '\t[a, b].to_vec().iter().sum::() + std::env::args().count() as i64\n}\n', + encoding="utf-8") + env = {**os.environ, "MACOSX_DEPLOYMENT_TARGET": MACOS_WHEEL} if "apple" in target else None + run("cargo", "build", "--quiet", "--release", "--manifest-path", other / "Cargo.toml", "--target", target, + "--target-dir", work / "target", cwd=Path(manifest).resolve().parent, env=env) + theirs = work / "target" / target / "release" / "libother.a" + main = work / "main.c" + main.write_text(f'#include \n#include "{lib}.h"\n\nint64_t rust2_other_sum(int64_t a, int64_t b);\n\n' + f"int main(void) {{\n\t{lib}_buf buf = {lib}_buf_copy(({lib}_buf){{.ptr = (const uint8_t *)\"hi\", .len = 2}});\n" + f'\tprintf("%zu %lld\\n", buf.len, (long long)rust2_other_sum(40, 1));\n\t{lib}_buf_free(buf);\n\treturn 0;\n}}\n', + encoding="utf-8") + flags = [arg for kind, name in natives for arg in ((f"-l{name}",) if kind == "lib" else ("-framework", name))] + for order in ((archive, theirs), (theirs, archive)): + exe = work / "coexist" + run("cc", "-std=c11", "-I", include, main, *order, *flags, "-o", exe) + if output(exe) != "2 42\n": + raise SystemExit(f"{archive.name} beside another Rust static library printed the wrong result") + + +def check_soname(shared, soname, target): + """Fail unless the shared library names itself by its versioned name.""" + if "apple" in target: + found = output("otool", "-D", shared).splitlines()[1:] + want = f"@rpath/{soname}" + else: + found = re.findall(r"\(SONAME\)\s+Library soname: \[(.*)\]", output("readelf", "-d", shared)) + want = soname + if found != [want]: + raise SystemExit(f"{shared} names itself {found}, not {want}") + + +def c_archive(info, manifest, export_dir, target, out): + """The C and C++ binding for `target`: headers, both libraries, and pkg-config, CMake, and CPS files.""" + lib, version = info["lib"], info["version"] + files = c_files(lib, target, version) + soname = Path(files["shared"]).name + linux, apple = "linux" in target, "apple" in target + link_args = [] + if linux: + link_args = ["-C", f"link-arg=-Wl,-soname,{soname}"] + elif apple: + # The last -install_name rustc's linker sees wins over its own unversioned one. + link_args = ["-C", f"link-arg=-Wl,-install_name,@rpath/{soname}"] + env = {"MACOSX_DEPLOYMENT_TARGET": MACOS_WHEEL} if apple else {} + # The C library alone, as the Swift package's: the glue's language features add symbols + # a C program cannot resolve. Its own target directory, since its link arguments differ. + # cargo-zigbuild's own `rustc`, as Cargo knows only its `zigbuild`, from the package's + # directory for its toolchain. + command = ["cargo-zigbuild", "rustc"] if linux else ["cargo", "rustc"] + built = run(*command, "--color", "never", "--locked", "--release", "--manifest-path", export_dir / "Cargo.toml", + "--target", f"{target}.{GLIBC}" if linux else target, "--target-dir", export_dir / "target-c", + "--lib", "--crate-type", "staticlib,cdylib", "--", "--print=native-static-libs", *link_args, + cwd=Path(manifest).resolve().parent, env={**os.environ, **env}, capture_output=True, encoding="utf-8") + sys.stderr.write(built.stderr) + notes = re.findall(r"note: native-static-libs: (.*)", built.stderr) + if len(notes) != 1: + raise SystemExit(f"rustc reported no native libraries for {target}") + natives = native_libs(notes[0].strip(), target) + release = export_dir / "target-c" / target / "release" + name = f"{info['name']}-{version}-{target}" + with tempfile.TemporaryDirectory() as raw: + raw = Path(raw) + root = raw / name + shutil.copytree(export_dir / "include", root / "include") + for path in licenses(info): + shutil.copy(path, root) + for key in ("static", "shared", "implib"): + if files[key]: + (root / files[key]).parent.mkdir(parents=True, exist_ok=True) + if "windows" in target: + shutil.copy(release / f"{lib}.lib", root / files["static"]) + shutil.copy(release / f"{lib}.dll", root / files["shared"]) + shutil.copy(release / f"{lib}.dll.lib", root / files["implib"]) + else: + shared = release / f"lib{lib}.{'dylib' if apple else 'so'}" + check_soname(shared, soname, target) + shutil.copy(shared, root / files["shared"]) + (root / files["link"]).symlink_to(soname) + hidden = raw / "hide" + hidden.mkdir() + shutil.copy(hide(release / f"lib{lib}.a", shared, lib, target, hidden), root / files["static"]) + coexist(root / files["static"], root / "include", lib, natives, manifest, target, hidden) + (root / "lib/pkgconfig").mkdir(parents=True) + (root / f"lib/pkgconfig/{lib}.pc").write_text(pkg_config(info, natives), newline="\n", encoding="utf-8") + cmake = root / "lib/cmake" / lib + cmake.mkdir(parents=True) + (cmake / f"{lib}-config.cmake").write_text(cmake_config(info, target, files, natives), newline="\n", + encoding="utf-8") + (cmake / f"{lib}-config-version.cmake").write_text(cmake_version(info), newline="\n", encoding="utf-8") + (root / "lib/cps").mkdir(parents=True) + (root / f"lib/cps/{lib}.cps").write_text(cps(info, files, natives), newline="\n", encoding="utf-8") + (out / "c").mkdir(parents=True, exist_ok=True) + return pack(root, out / "c" / (name + (".zip" if "windows" in target else ".tar.gz"))) + + +def pack(root, path): + """`root` as a ZIP or gzipped tarball under its own name, owned by no one, readable by all.""" + if path.suffix == ".zip": + with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as archive: + for file in sorted(p for p in root.rglob("*") if p.is_file()): + archive.write(file, file.relative_to(root.parent).as_posix()) + return path + + def owned(member): + member.uid = member.gid = 0 + member.uname = member.gname = "" + if not member.issym(): + member.mode = 0o755 if member.isdir() or member.mode & 0o100 else 0o644 + return member + + with tarfile.open(path, "w:gz") as archive: + archive.add(root, root.name, filter=owned) + return path + + +def c_check(lib, check, work): + """The consumer's program: the caller's check, or else one that links a function of each header.""" + if check: + source = work / Path(check).name + shutil.copy(check, source) + return source + source = work / "smoke.cpp" + source.write_text(f'#include "{lib}.h"\n#include "{lib}.hpp"\n\n' + f"int main() {{\n\tvoid (*volatile fn)({lib}_buf) = {lib}_buf_free;\n" + f"\treturn fn == nullptr;\n}}\n", encoding="utf-8") + return source + + +def run_check(exe, native, what, env=None): + """Run a consumer from an empty directory, and compare its output with native Rust's.""" + with tempfile.TemporaryDirectory() as clean: + result = subprocess.run([str(exe)], capture_output=True, cwd=clean, env={**os.environ, **(env or {})}) + sys.stderr.buffer.write(result.stderr) + if result.returncode: + raise SystemExit(f"{what} failed with status {result.returncode}") + if native: + compare(Path(native).read_bytes(), result.stdout, what) + print(f"{what}: ok") + + +def check_linked(exe, soname, target): + """Fail unless a shared consumer loads the library by its versioned name.""" + if "windows" in target: + return + if "apple" in target: + found = f"@rpath/{soname}" in output("otool", "-L", exe) + else: + found = soname in re.findall(r"\(NEEDED\)\s+Shared library: \[(.*)\]", output("readelf", "-d", exe)) + if not found: + raise SystemExit(f"{exe} does not load {soname}") + + +def consume_c(archive, check, native): + """Build the check in clean consumers of the unpacked archive: CMake by its CPS file and by its config package, and pkg-config.""" + archive = Path(archive).resolve() + with tempfile.TemporaryDirectory() as tmp: + consume_c_in(Path(tmp), archive, check, native) + + +def consume_c_in(work, archive, check, native): + """`consume_c` in the scratch directory `work`, which the caller deletes.""" + if archive.suffix == ".zip": + shutil.unpack_archive(archive, work / "unpacked") + else: + shutil.unpack_archive(archive, work / "unpacked", filter="data") + (prefix,) = (work / "unpacked").iterdir() + (cps_file,) = (prefix / "lib/cps").glob("*.cps") + description = json.loads(cps_file.read_text(encoding="utf-8")) + lib, version = description["name"], description["version"] + target = prefix.name.rsplit(f"-{version}-", 1)[1] + # CMake spells a version in numbers alone, so a prerelease is found by none. + request = "" if "-" in version.split("+")[0] else " " + re.match(r"[0-9]+\.[0-9]+\.[0-9]+", version).group() + files = c_files(lib, target, version) + soname = Path(files["shared"]).name + source = c_check(lib, check, work) + # Only the caller's check prints the transcript; the smoke program prints nothing. + native = native if check else None + windows = "windows" in target + # Where the shared consumers find the library at run time; elsewhere the link records it. + path_env = {"PATH": f"{prefix / 'bin'}{os.pathsep}{os.environ['PATH']}"} if windows else {} + for mode in ("cps", "config"): + project = work / f"cmake-{mode}" + project.mkdir() + configs = f" CONFIGS {lib}-config.cmake" if mode == "config" else "" + found = r"\.cps$" if mode == "cps" else r"-config\.cmake$" + (project / "CMakeLists.txt").write_text(f"""cmake_minimum_required(VERSION 4.3) +project(check LANGUAGES C CXX) +find_package({lib}{request} REQUIRED{configs}) +if(NOT {lib}_CONFIG MATCHES "{found}") + message(FATAL_ERROR "found ${{{lib}_CONFIG}}, not the {mode} file") +endif() +add_executable(check-static {cmake_quote(source.as_posix())}) +target_link_libraries(check-static PRIVATE {lib}::{lib}) +add_executable(check-shared {cmake_quote(source.as_posix())}) +target_link_libraries(check-shared PRIVATE {lib}::{lib}_shared) +""", encoding="utf-8") + build = project / "build" + run("cmake", "-S", project, "-B", build, "-DCMAKE_BUILD_TYPE=Release", f"-DCMAKE_PREFIX_PATH={prefix}", + "-DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF", "-DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF") + run("cmake", "--build", build, "--config", "Release") + for linkage in ("static", "shared"): + (exe,) = [p for p in build.rglob(f"check-{linkage}*") if p.is_file() and os.access(p, os.X_OK) + and p.suffix in ("", ".exe")] + if linkage == "shared": + check_linked(exe, soname, target) + run_check(exe, native, f"CMake's {mode} consumer, {linkage}", path_env if linkage == "shared" else None) + if windows: + return + compiler = ["c++", "-std=c++17"] if source.suffix in (".cpp", ".cc", ".cxx") else ["cc", "-std=c11"] + # The shared library by default, and the static library where it is all a prefix holds. + static_prefix = work / "static" / prefix.name + shutil.copytree(prefix, static_prefix, symlinks=True) + for path in (static_prefix / "lib").iterdir(): + if path.name.startswith(f"lib{lib}.so") or path.suffix == ".dylib": + path.unlink() + for linkage, root, flags in (("shared", prefix, []), ("static", static_prefix, ["--static"])): + env = {**os.environ, "PKG_CONFIG_PATH": "", "PKG_CONFIG_LIBDIR": str(root / "lib/pkgconfig")} + cflags = shlex.split(output("pkg-config", "--cflags", *flags, lib, env=env)) + libs = shlex.split(output("pkg-config", "--libs", *flags, lib, env=env)) + exe = work / f"pkg-config-{linkage}" + rpath = [f"-Wl,-rpath,{root / 'lib'}"] if linkage == "shared" else [] + run(*compiler, *cflags, source, *libs, *rpath, "-o", exe) + if linkage == "shared": + check_linked(exe, soname, target) + run_check(exe, native, f"pkg-config's consumer, {linkage}") + + def compare(native, actual, what): """Fail with a diff unless a consumer printed exactly what native Rust printed.""" if native == actual: @@ -712,7 +1266,7 @@ def go_library(args, export_dir, lib, target, library, native, out): def build(args): - """This host's wheels, Apple and Go static libraries, the export's sources, and native output.""" + """This host's wheels, C archives, Apple and Go static libraries, the export's sources, and native output.""" info = facts(args.manifest_path) lib, export_dir, out = info["lib"], Path(args.export).resolve(), Path(args.out).resolve() crate = export_dir / "Cargo.toml" @@ -732,6 +1286,7 @@ def build(args): library = export_dir / "target-python" / target / "release" / library_name(lib, target) with tempfile.TemporaryDirectory() as raw: repair(write_wheel(info, export_dir, library, target, raw), target, out / "wheels") + c_archive(info, args.manifest_path, export_dir, target, out) if target in APPLE_TARGETS: apple = APPLE_TARGETS[target] name = "MACOSX_DEPLOYMENT_TARGET" if apple == "macos" else "IPHONEOS_DEPLOYMENT_TARGET" @@ -1204,6 +1759,10 @@ def main(): group.add_argument("--check") group.add_argument("--import", dest="module") p.add_argument("--native") + p = sub.add_parser("consume-c", help="build a check program, or a link of each header, on the C archive") + p.add_argument("--archive", required=True) + p.add_argument("--check") + p.add_argument("--native") p = sub.add_parser("compare", help="compare a consumer's output with native Rust's") p.add_argument("native") p.add_argument("actual") @@ -1260,6 +1819,8 @@ def main(): consume_go(args.proxy, args.check, args.native) elif args.command == "consume-python": consume_python(args.check, args.native, args.module) + elif args.command == "consume-c": + consume_c(args.archive, args.check, args.native) elif args.command == "compare": compare(Path(args.native).read_bytes(), Path(args.actual).read_bytes(), "installed Swift package") elif args.command == "init": diff --git a/ffi/release/release.yml b/ffi/release/release.yml index ca458340d..903fdfd3b 100644 --- a/ffi/release/release.yml +++ b/ffi/release/release.yml @@ -1,9 +1,9 @@ name: Release @NAME@ bindings # Written by rust2's `ffi/release/package.py init`; yours to edit. rust2's builder, pinned -# below, exports @NAME@'s pub API, builds its Python wheels, Swift package, and web package, -# and checks them in clean consumers, all read-only. The jobs after it publish only a tag's -# build: +# below, exports @NAME@'s pub API, builds its Python wheels, Swift package, C archives, and +# web package, and checks them in clean consumers, all read-only. The jobs after it publish +# only a tag's build: # # - PyPI: the project @NAME@, through the trusted publisher this repository registers there # for this workflow and the `pypi` environment. @@ -11,6 +11,7 @@ name: Release @NAME@ bindings # the Package.swift pushed to @MIRROR@ names by URL and checksum, tagged . # The mirror's token comes from a GitHub App installed on it, whose ID and key live in # the `swift-mirror` environment. +# - C and C++: --.tar.gz, or .zip on Windows, on the same release. # - npm and JSR: the web package, when the build makes one, through the trusted publisher # npm registers for this workflow and the `npm` environment, and JSR's link to this # repository, each with provenance. @@ -47,8 +48,8 @@ jobs: go-module: @GO_MODULE@ @WEB@ # Optional: cargo features to ship, an example of the crate whose output every check - # program must print, and those programs: `python-check`, `swift-check`, `go-check`, and - # `web-check`. + # program must print, and those programs: `python-check`, `swift-check`, `c-check`, + # `go-check`, and `web-check`. tag: needs: build @@ -129,11 +130,11 @@ jobs: with: packages-dir: dist/wheels/ - # The XCFramework the mirror's Package.swift names, uploaded once: a consumer that - # resolved its checksum must never see other bytes at that URL. The release stays a draft - # until its asset is up, as an immutable release takes no asset once published. - # The release's API, api.json, is an asset too: the next release's build checks its own - # against it, and its notes list what changed since the previous release's. + # The XCFramework the mirror's Package.swift names and each target's C archive, uploaded + # once: a consumer that resolved a checksum must never see other bytes at that URL. The + # release stays a draft until its assets are up, as an immutable release takes none once + # published. The release's API, api.json, is an asset too: the next release's build checks + # its own against it, and its notes list what changed since the previous release's. github-release: needs: [build, tag] runs-on: ubuntu-24.04 @@ -144,6 +145,11 @@ jobs: with: name: @NAME@-swift path: swift + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: @NAME@-build-* + path: dist + merge-multiple: true - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: name: @NAME@-release @@ -152,19 +158,20 @@ jobs: env: GH_TOKEN: ${{ github.token }} TAG: ${{ github.ref_name }} - ASSET: ${{ needs.build.outputs.module }}FFI.xcframework.zip + XCFRAMEWORK: swift/${{ needs.build.outputs.module }}FFI.xcframework.zip # Cargo's prerelease, such as 1.0.0-rc.1, is GitHub's too. PRERELEASE: ${{ contains(needs.build.outputs.version, '-') }} run: | if ! gh release view "$TAG" --repo "$GITHUB_REPOSITORY" > /dev/null; then gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --verify-tag --draft --prerelease="$PRERELEASE" --title "$TAG" --notes-file release/changes.md fi - for asset in "swift/$ASSET" release/api.json; do - name=$(basename "$asset") - if gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --pattern "$name" --dir existing; then - cmp "existing/$name" "$asset" || { echo "::error::$TAG already hosts another $name"; exit 1; } + shopt -s nullglob + for path in "$XCFRAMEWORK" release/api.json dist/c/*; do + asset=$(basename "$path") + if gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --pattern "$asset" --dir existing; then + cmp "existing/$asset" "$path" || { echo "::error::$TAG already hosts another $asset"; exit 1; } else - gh release upload "$TAG" --repo "$GITHUB_REPOSITORY" "$asset" + gh release upload "$TAG" --repo "$GITHUB_REPOSITORY" "$path" fi done gh release edit "$TAG" --repo "$GITHUB_REPOSITORY" --draft=false diff --git a/ffi/release/test_package.py b/ffi/release/test_package.py index 2f53fe02f..d2539e808 100644 --- a/ffi/release/test_package.py +++ b/ffi/release/test_package.py @@ -3,6 +3,7 @@ import importlib.util import json from pathlib import Path +import tarfile import tempfile import unittest import zipfile @@ -334,6 +335,106 @@ def test_manifest(self): self.assertIn('dependencies: ["MoqFFI", .product(name: "BasicContainers", package: "swift-collections")]', text) +class CArchive(unittest.TestCase): + def test_versions_compat_and_soname(self): + self.assertEqual(package.compat_version("1.4.2"), "1.0.0") + self.assertEqual(package.compat_version("0.4.12"), "0.4.0") + self.assertEqual(package.compat_version("0.0.3"), "0.0.3") + self.assertEqual(package.compat_version("2.0.0-rc.1"), "2.0.0-rc.1") + self.assertEqual(package.soname_version("1.2.3+build.5"), "1.2.3") + self.assertEqual(package.soname_version("2.0.0-rc.1"), "2.0.0-rc.1") + for version in ("1.0", "01.2.3", "v1.2.3"): + with self.assertRaises(SystemExit, msg=version): + package.soname_version(version) + + def test_files_per_platform(self): + self.assertEqual(package.c_files("moq", "x86_64-unknown-linux-gnu", "0.4.12")["shared"], "lib/libmoq.so.0.4.12") + self.assertEqual(package.c_files("moq", "aarch64-apple-darwin", "1.0.0")["shared"], "lib/libmoq.1.0.0.dylib") + windows = package.c_files("moq", "x86_64-pc-windows-msvc", "1.0.0") + self.assertEqual((windows["static"], windows["shared"], windows["implib"]), + ("lib/moq.lib", "bin/moq.dll", "lib/moq.dll.lib")) + + def test_native_libraries_by_platform(self): + self.assertEqual(package.native_libs("-lgcc_s -lutil -lc", "x86_64-unknown-linux-gnu"), + [("lib", "gcc_s"), ("lib", "util"), ("lib", "c")]) + self.assertEqual(package.native_libs("-framework Security -liconv", "aarch64-apple-darwin"), + [("framework", "Security"), ("lib", "iconv")]) + self.assertEqual(package.native_libs("kernel32.lib ntdll.lib /defaultlib:msvcrt", "x86_64-pc-windows-msvc"), + [("lib", "kernel32"), ("lib", "ntdll"), ("flag", "/defaultlib:msvcrt")]) + for flags, target in (("-Wl,-dead_strip", "aarch64-apple-darwin"), ("-framework Security", "x86_64-unknown-linux-gnu"), + ("-lkernel32", "x86_64-pc-windows-msvc")): + with self.assertRaises(SystemExit, msg=flags): + package.native_libs(flags, target) + + def test_descriptions(self): + natives = [("framework", "Security"), ("lib", "iconv")] + files = package.c_files("moq", "aarch64-apple-darwin", "0.4.12") + pc = package.pkg_config(info("/src"), natives) + self.assertIn("Libs: -L${libdir} -lmoq\nLibs.private: -framework Security -liconv\n", pc) + self.assertIn("Description: Media over QUIC, bindings\n", pc) + config = package.cmake_config(info("/src"), "aarch64-apple-darwin", files, natives) + self.assertIn("add_library(moq::moq STATIC IMPORTED)", config) + self.assertIn("add_library(moq::moq_shared SHARED IMPORTED)", config) + self.assertIn('IMPORTED_LOCATION "${_moq_prefix}/lib/libmoq.0.4.12.dylib"', config) + self.assertIn("INTERFACE_LINK_LIBRARIES [==[-framework Security;iconv]==]", config) + self.assertEqual(config.count('INTERFACE_COMPILE_FEATURES "c_std_11;cxx_std_17"'), 2) + self.assertIn('VERSION_LESS "0.4.0"', package.cmake_version(info("/src"))) + cps = json.loads(package.cps(info("/src"), files, natives)) + self.assertEqual((cps["name"], cps["version"], cps["compat_version"]), ("moq", "0.4.12", "0.4.0")) + self.assertEqual(cps["default_components"], ["moq"]) + self.assertEqual(cps["components"]["moq"]["link_libraries"], ["-framework Security", "iconv"]) + self.assertEqual(cps["components"]["moq_shared"]["location"], "@prefix@/lib/libmoq.0.4.12.dylib") + self.assertEqual(cps["website"], "https://github.com/moq-dev/moq") + self.assertEqual(cps["version_schema"], "simple") + prerelease = info("/src", version="2.0.0-rc.1") + self.assertEqual(json.loads(package.cps(prerelease, files, natives))["version_schema"], "custom") + self.assertIn("set(PACKAGE_VERSION_COMPATIBLE FALSE)\n", package.cmake_version(prerelease)) + self.assertNotIn("VERSION_LESS", package.cmake_version(prerelease)) + windows = package.cps(info("/src"), package.c_files("moq", "x86_64-pc-windows-msvc", "0.4.12"), + [("lib", "ntdll"), ("flag", "/defaultlib:msvcrt")]) + windows = json.loads(windows)["components"] + self.assertEqual(windows["moq_shared"]["link_location"], "@prefix@/lib/moq.dll.lib") + self.assertEqual(windows["moq"]["link_flags"], ["/defaultlib:msvcrt"]) + + def test_commons_are_defined_unless_a_member_defines_them(self): + nm = """ +libmoq.a(ui.o): +0000000000000008 (common) (alignment 2^3) external _tty_in +0000000000000004 (common) (alignment 2^2) external _is_a_tty +0000000000000010 (common) (alignment 2^4) external _shared + (undefined) external _tty_out + +libmoq.a(other.o): +0000000000000008 (common) (alignment 2^3) external _tty_out +0000000000000020 (common) external _shared +0000000000000000 (__DATA,__bss) external _is_a_tty +0000000000000008 (common) (alignment 2^3) external _state +0000000000000010 (__DATA,__data) private external _state +""" + self.assertEqual(package.commons_source(nm), + '__attribute__((aligned(16))) char common0[32] __asm__("_shared");\n' + '__attribute__((aligned(8))) char common1[8] __asm__("_tty_in");\n' + '__attribute__((aligned(8))) char common2[8] __asm__("_tty_out");\n') + self.assertEqual(package.commons_source("0000 (__TEXT,__text) external _moq_x\n"), "typedef int empty;\n") + + def test_archive_is_owned_by_no_one(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) / "moq-ffi-0.4.12-x86_64-unknown-linux-gnu" + (root / "lib").mkdir(parents=True) + (root / "lib/libmoq.so.0.4.12").write_bytes(b"\x7fELF") + (root / "lib/libmoq.so.0.4.12").chmod(0o700) + (root / "lib/libmoq.so").symlink_to("libmoq.so.0.4.12") + (root / "lib/libmoq.a").write_bytes(b"!\n") + (root / "lib/libmoq.a").chmod(0o600) + path = package.pack(root, Path(tmp) / "out.tar.gz") + with tarfile.open(path) as archive: + members = {m.name.split("/", 1)[-1]: m for m in archive.getmembers()} + self.assertEqual(members["lib/libmoq.so.0.4.12"].mode, 0o755) + self.assertEqual(members["lib/libmoq.a"].mode, 0o644) + self.assertEqual(members["lib/libmoq.so"].linkname, "libmoq.so.0.4.12") + self.assertEqual({(m.uid, m.uname) for m in members.values()}, {(0, "")}) + + class Template(unittest.TestCase): def test_fills_every_identity(self): text = package.template(info("/src"), "rs/moq-ffi/Cargo.toml", "a" * 40, "moq-dev/moq-swift-ffi", "moq.dev/moq-ffi") diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index b95aa2091..aabb51708 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -2113,6 +2113,10 @@ pub(crate) fn escape(name: &str) -> String { "wchar_t", "xor", "xor_eq", + // Macros GNU dialects predefine, as CMake's default `-std=gnu11` and `-std=gnu++17` do. + "i386", + "linux", + "unix", ]; // `out0`, `out1`, ... are a returned tuple's. let out = name diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index e13d760c1..ec18dd1e1 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -2173,9 +2173,12 @@ pub(crate) fn escape(name: &str) -> String { "std", "self", ]; + // Macros GNU dialects predefine, as CMake's default `-std=gnu++17` does. Each name of + // one and trailing `_`s takes another, so `unix` and `unix_` stay apart. + const MACROS: &[&str] = &["i386", "linux", "unix"]; match name.chars().next() { Some(c) if c.is_ascii_digit() => format!("_{name}"), - _ if KEYWORDS.contains(&name) => format!("{name}_"), + _ if KEYWORDS.contains(&name) || MACROS.contains(&name.trim_end_matches('_')) => format!("{name}_"), _ => name.to_string(), } } @@ -2205,6 +2208,16 @@ mod tests { assert_eq!(kept_apart(escape("seed"), &taken), "seed"); } + #[test] + fn predefined_macros_escape_apart() { + assert_eq!(escape("unix"), "unix_"); + assert_eq!(escape("unix_"), "unix__"); + assert_eq!(escape("linux__"), "linux___"); + assert_eq!(escape("unixes"), "unixes"); + // A keyword keeps its single escape, so a field `type_` stays `type_`. + assert_eq!(escape("type_"), "type_"); + } + #[test] fn parameters_keep_apart_from_what_they_would_hide() { let taken = |name: &str| ["a", "size", "size_"].contains(&name); diff --git a/ffi/surface/moq/cpp.hpp b/ffi/surface/moq/cpp.hpp index f53295c6f..c3c26a97e 100644 --- a/ffi/surface/moq/cpp.hpp +++ b/ffi/surface/moq/cpp.hpp @@ -8819,7 +8819,7 @@ namespace moq { quic = 0, web_socket = 1, tcp = 2, - unix = 3, + unix_ = 3, web_transport = 4, tls = 5, }; diff --git a/quest/m1/ffi-export/cpp-name-collisions.md b/quest/m1/ffi-export/cpp-name-collisions.md new file mode 100644 index 000000000..5d24019dd --- /dev/null +++ b/quest/m1/ffi-export/cpp-name-collisions.md @@ -0,0 +1,26 @@ +# [S] C++ names that escape alike stay apart + +## Goal + +Two Rust names in one C++ scope that escape to one C++ name, such as the enum variants +`Delete` and `Delete_` (both `delete_`), export to distinct names or fail the export with a span at the second item, never a +header with a duplicate member that fails at the consumer's compile. This covers enum +values, record fields, methods, and parameters. + +## Plan + +Decided with the maintainer on 2026-11-19, in the pick of [#873](https://github.com/kixel-ai/rust2/pull/873): ✅ kept (rejected: dropping it). + +Found by the review of [kixel-ai/rust2#873](https://github.com/kixel-ai/rust2/pull/873). +That PR escapes the macros GNU dialects predefine (`unix`, `linux`, `i386`) so that the +whole family stays apart: `unix_` becomes `unix__`. Keywords keep their older single +escape, since a field `type_` is common Rust and renaming it would touch every such API. + +`cpp::escape` cannot simply append another `_` to a name that already ends in one, as +`abi::escape` does for C: it also escapes C names, which C's clash rule keeps unique and +which may end in `_` (`audio_encoder_finish_`), and every Rust name ending in `_`, such as +a field `type_`, would change in C++. Recommended: register each scope's escaped names as +the C++ generator already does for a member named as its class, and give the later +collision a trailing `_`. Refuse with a span only where that rule cannot apply. + +Public API: C++ names of the colliding items only. diff --git a/quest/m1/ffi-packaging/c.md b/quest/m1/ffi-packaging/c.md deleted file mode 100644 index 5a075c06e..000000000 --- a/quest/m1/ffi-packaging/c.md +++ /dev/null @@ -1,23 +0,0 @@ -# [M] C and C++ release archives - -## Goal - -The builder releases a per-target archive of the C and C++ binding: headers, static and -shared libraries, a `.pc`, a CMake config package for `find_package`, and a CPS -file, each carrying the native libraries a program linking it needs. The shared library -has a versioned SONAME, and the static library exposes only `_*` symbols. A clean -CMake consumer and a pkg-config consumer build and run the transcript. - -## Plan - -Found by the 2026-11-18 language audit: moq's libmoq ships `find_package(moq)` files and -`moq.pc.in` (`rs/libmoq/` in moq-dev/moq); a test cdylib with the export's `crate-type` -has no SONAME; the static library exports about 2,100 global symbols, including -compiler-builtins' unmangled `__addvdi3`, which can collide with another Rust static -library in one program. [CMake 4.3 made CPS stable](https://www.kitware.com/common-package-specification-is-out-the-gate/). - -Recommended: hide symbols with a prelinked object (`ld -r`, then `objcopy ---keep-global-symbols`) on ELF and Mach-O, and say what MSVC gets. The native link -libraries come from rustc's `--print native-static-libs`, as the tests already read them. - -Public API: the archive layout, and the SONAME.