diff --git a/.github/actions/build-pack/action.yml b/.github/actions/build-pack/action.yml deleted file mode 100644 index 1ed7181..0000000 --- a/.github/actions/build-pack/action.yml +++ /dev/null @@ -1,73 +0,0 @@ -name: Build Typst.Net package -description: Builds and packs the Typst.Net Nuget package including native dependencies -inputs: - dotnet-version: - description: .NET SDK version to install - default: 11.x - rust-version: - description: Rust toolchain version to install - default: 1.91.1 - package-version: - description: Optional package version override - required: false - output-directory: - description: Output directory for packed artifacts - default: artifacts -runs: - using: composite - steps: - - name: Install .NET SDK - uses: actions/setup-dotnet@v5 - with: - dotnet-version: ${{ inputs.dotnet-version }} - - - name: Install build prerequisites - shell: bash - run: | - sudo apt-get update - sudo apt-get install -y --no-install-recommends \ - build-essential \ - pkg-config \ - libssl-dev \ - llvm \ - clang \ - zlib1g-dev \ - mingw-w64 \ - ca-certificates \ - musl-tools - - - name: Configure Musl Linker - shell: bash - run: | - if [ ! -f /usr/local/bin/x86_64-linux-musl-gcc ]; then - sudo ln -s $(which musl-gcc) /usr/local/bin/x86_64-linux-musl-gcc - fi - - - name: Install Rust toolchain - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - toolchain: ${{ inputs.rust-version }} - cache-workspaces: src/typst_core - rust-src-dir: src/typst_core - - - name: Add Rust cross-compilation targets - shell: bash - run: rustup target add x86_64-pc-windows-gnu x86_64-unknown-linux-musl - - - name: Restore .NET solution - shell: bash - run: dotnet restore typstsharp.slnx - - - name: Pack typstsharp - shell: bash - env: - CC_x86_64_unknown_linux_musl: x86_64-linux-musl-gcc - CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER: x86_64-linux-musl-gcc - run: | - set -euo pipefail - mkdir -p "${{ inputs.output-directory }}" - version_arg="" - if [ -n "${{ inputs.package-version }}" ]; then - version_arg="/p:Version=${{ inputs.package-version }}" - fi - dotnet pack src/typstsharp/typstsharp.csproj -m:1 -c Release -o "${{ inputs.output-directory }}" $version_arg diff --git a/.github/actions/build-runtime-pack/action.yml b/.github/actions/build-runtime-pack/action.yml new file mode 100644 index 0000000..3df2177 --- /dev/null +++ b/.github/actions/build-runtime-pack/action.yml @@ -0,0 +1,136 @@ +name: Build typstsharp runtime package +description: Builds typst_core and packs the per-RID typstsharp.runtime. NuGet package +inputs: + rid: + description: Target .NET Runtime Identifier (e.g. win-x64, win-arm64, linux-x64, linux-arm64, linux-musl-x64, linux-musl-arm64, osx-arm64) + required: true + rust-target: + description: Rust target triple (e.g. x86_64-pc-windows-msvc, aarch64-unknown-linux-gnu, aarch64-unknown-linux-musl) + required: true + package-version: + description: NuGet package version + required: true + dotnet-version: + description: .NET SDK version to install + default: 11.x + rust-version: + description: Rust toolchain version to install + default: stable + output-directory: + description: Output directory for packed artifacts + default: artifacts + docker-image: + description: Optional Linux container image used to build libtypst_core.so (e.g. manylinux_2_28 for glibc or rust:alpine for musl) + required: false +runs: + using: composite + steps: + - name: Install .NET SDK + uses: actions/setup-dotnet@v5 + with: + dotnet-version: ${{ inputs.dotnet-version }} + + - name: Install Rust toolchain (host) + if: inputs.docker-image == '' + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: ${{ inputs.rust-version }} + target: ${{ inputs.rust-target }} + cache-workspaces: src/typst_core + rust-src-dir: src/typst_core + + - name: Cache Cargo artifacts (container) + if: inputs.docker-image != '' + uses: actions/cache@v5 + with: + path: | + ${{ runner.temp }}/cargo-cache/cargo/registry/index + ${{ runner.temp }}/cargo-cache/cargo/registry/cache + ${{ runner.temp }}/cargo-cache/cargo/git/db + ${{ runner.temp }}/cargo-cache/target + key: container-cargo-${{ inputs.rid }}-${{ inputs.rust-version }}-${{ hashFiles('src/typst_core/Cargo.lock') }} + restore-keys: | + container-cargo-${{ inputs.rid }}-${{ inputs.rust-version }}- + + - name: Build native library in container (${{ inputs.rid }}) + if: inputs.docker-image != '' + shell: bash + run: | + set -euo pipefail + mkdir -p "${{ runner.temp }}/cargo-cache/cargo" "${{ runner.temp }}/cargo-cache/target" "${{ github.workspace }}/native-out" + docker run --rm \ + -v "${{ github.workspace }}:/workspace" \ + -v "${{ runner.temp }}/cargo-cache:/cargo-cache" \ + -w /workspace \ + -e "RUST_VERSION=${{ inputs.rust-version }}" \ + -e "RUST_TARGET=${{ inputs.rust-target }}" \ + -e "HOST_UID=$(id -u)" \ + -e "HOST_GID=$(id -g)" \ + -e "CARGO_HOME=/cargo-cache/cargo" \ + -e "CARGO_TARGET_DIR=/cargo-cache/target" \ + "${{ inputs.docker-image }}" \ + sh -c ' + set -eu + if echo "$RUST_TARGET" | grep -q -- "-musl$"; then + apk add --no-cache build-base perl linux-headers pkgconfig binutils + rustflags_var="CARGO_TARGET_$(echo "$RUST_TARGET" | tr "a-z-" "A-Z_")_RUSTFLAGS" + env -u RUSTFLAGS "$rustflags_var=-C target-feature=-crt-static" \ + cargo rustc --release --manifest-path /workspace/src/typst_core/Cargo.toml --target "$RUST_TARGET" --crate-type cdylib + out="/cargo-cache/target/$RUST_TARGET/release/libtypst_core.so" + readelf -d "$out" | grep NEEDED || true + if readelf -d "$out" | grep -q "libc\.so\.6"; then + echo "Error: musl binary unexpectedly links against glibc (libc.so.6)" >&2 + exit 1 + fi + if ! readelf -d "$out" | grep -q "libc\.musl-"; then + echo "Error: musl binary does not dynamically link against musl libc" >&2 + exit 1 + fi + else + dnf install -y perl-core + curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | \ + CARGO_HOME=/opt/cargo RUSTUP_HOME=/opt/rustup sh -s -- -y --default-toolchain "$RUST_VERSION" --profile minimal --no-modify-path + export PATH="/opt/cargo/bin:$PATH" + export RUSTUP_HOME=/opt/rustup + cargo rustc --release --manifest-path /workspace/src/typst_core/Cargo.toml --target "$RUST_TARGET" --crate-type cdylib + out="/cargo-cache/target/$RUST_TARGET/release/libtypst_core.so" + readelf -d "$out" | grep NEEDED || true + max_glibc=$(readelf --version-info "$out" | grep -oE "GLIBC_[0-9]+\.[0-9]+" | sort -Vu | tail -n1 || true) + echo "Maximum required GLIBC symbol version: ${max_glibc:-none}" + if readelf --version-info "$out" | grep -oE "GLIBC_[0-9]+\.[0-9]+" | awk -F"[._]" "{ if (\$2 > 2 || (\$2 == 2 && \$3 > 28)) exit 1 }"; then + echo "GLIBC compatibility check passed (<= 2.28)" + else + echo "Error: binary requires GLIBC newer than 2.28 ($max_glibc)" >&2 + exit 1 + fi + fi + cp "$out" /workspace/native-out/libtypst_core.so + chown -R "$HOST_UID:$HOST_GID" /workspace/native-out /workspace/src/typstsharp/Bindings.g.cs /cargo-cache + ' + + - name: Pack typstsharp.runtime.${{ inputs.rid }} (container artifact) + if: inputs.docker-image != '' + shell: bash + run: | + set -euo pipefail + mkdir -p "${{ inputs.output-directory }}" + dotnet pack src/typstsharp.runtime/typstsharp.runtime.csproj \ + -c Release \ + -p:RuntimeRid=${{ inputs.rid }} \ + -p:NativeLibraryPath="${{ github.workspace }}/native-out/libtypst_core.so" \ + -p:Version=${{ inputs.package-version }} \ + -o "${{ inputs.output-directory }}" + + - name: Pack typstsharp.runtime.${{ inputs.rid }} (host build) + if: inputs.docker-image == '' + shell: bash + env: + MSYS2_ARG_CONV_EXCL: "*" + run: | + set -euo pipefail + mkdir -p "${{ inputs.output-directory }}" + dotnet pack src/typstsharp.runtime/typstsharp.runtime.csproj \ + -c Release \ + -p:RuntimeRid=${{ inputs.rid }} \ + -p:Version=${{ inputs.package-version }} \ + -o "${{ inputs.output-directory }}" diff --git a/.github/workflows/build-packages.yml b/.github/workflows/build-packages.yml new file mode 100644 index 0000000..cb15f34 --- /dev/null +++ b/.github/workflows/build-packages.yml @@ -0,0 +1,196 @@ +name: build-packages + +on: + workflow_call: + inputs: + package_version: + description: NuGet package version for typstsharp and all runtime packages + required: true + type: string + run_tests: + description: Whether to run Rust native tests and local solution tests + required: false + default: false + type: boolean + +permissions: + contents: read + +jobs: + pack-managed: + name: Pack typstsharp (managed) + runs-on: ubuntu-latest + env: + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + + - name: Install .NET SDK + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.x + + - name: Pack typstsharp + shell: bash + run: | + set -euo pipefail + mkdir -p artifacts + dotnet pack src/typstsharp/typstsharp.csproj \ + -c Release \ + -p:SkipRustBuild=true \ + -p:Version=${{ inputs.package_version }} \ + -o artifacts + + - name: Upload managed package + uses: actions/upload-artifact@v7 + with: + name: typstsharp-managed-${{ inputs.package_version }} + path: artifacts/*.nupkg + + unit-tests: + name: Run unit tests (Rust & .NET) + if: inputs.run_tests + runs-on: ubuntu-latest + env: + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + + - name: Install .NET SDK + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.x + + - name: Install Linux build prerequisites + shell: bash + run: | + set -euo pipefail + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + build-essential \ + pkg-config \ + libssl-dev \ + zlib1g-dev \ + ca-certificates + + - name: Install Rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: stable + target: x86_64-unknown-linux-gnu + cache-workspaces: src/typst_core + rust-src-dir: src/typst_core + + # The Rust tests cover the FFI entry points directly. That is the only place a + # panic crossing the boundary shows up as a failing test rather than as a + # crashed test runner, so they have to run in their own right. + - name: Run native tests + shell: bash + run: cargo test --manifest-path src/typst_core/Cargo.toml --release + + - name: Run solution tests + shell: bash + run: dotnet test --solution typstsharp.slnx + + pack-runtime: + name: Pack typstsharp.runtime.${{ matrix.rid }} + runs-on: ${{ matrix.os }} + env: + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + strategy: + fail-fast: false + matrix: + include: + - rid: win-x64 + os: windows-latest + rust_target: x86_64-pc-windows-msvc + - rid: win-arm64 + os: windows-11-arm + rust_target: aarch64-pc-windows-msvc + - rid: linux-x64 + os: ubuntu-latest + rust_target: x86_64-unknown-linux-gnu + docker_image: quay.io/pypa/manylinux_2_28_x86_64 + - rid: linux-arm64 + os: ubuntu-24.04-arm + rust_target: aarch64-unknown-linux-gnu + docker_image: quay.io/pypa/manylinux_2_28_aarch64 + - rid: linux-musl-x64 + os: ubuntu-latest + rust_target: x86_64-unknown-linux-musl + docker_image: rust:alpine + - rid: linux-musl-arm64 + os: ubuntu-24.04-arm + rust_target: aarch64-unknown-linux-musl + docker_image: rust:alpine + - rid: osx-arm64 + os: macos-latest + rust_target: aarch64-apple-darwin + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + + - name: Build and pack runtime package + uses: ./.github/actions/build-runtime-pack + with: + rid: ${{ matrix.rid }} + rust-target: ${{ matrix.rust_target }} + docker-image: ${{ matrix.docker_image }} + package-version: ${{ inputs.package_version }} + + - name: Upload runtime package + uses: actions/upload-artifact@v7 + with: + name: typstsharp-runtime-${{ matrix.rid }}-${{ inputs.package_version }} + path: artifacts/*.nupkg + + assemble-packages: + name: Assemble NuGet packages + needs: + - pack-managed + - pack-runtime + runs-on: ubuntu-latest + + steps: + - name: Download all built packages + uses: actions/download-artifact@v7 + with: + pattern: typstsharp-*-${{ inputs.package_version }} + path: artifacts + merge-multiple: true + + - name: Verify expected packages exist + shell: bash + run: | + set -euo pipefail + ls -lh artifacts + expected=( + "typstsharp.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.win-x64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.win-arm64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.linux-x64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.linux-arm64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.linux-musl-x64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.linux-musl-arm64.${{ inputs.package_version }}.nupkg" + "typstsharp.runtime.osx-arm64.${{ inputs.package_version }}.nupkg" + ) + for pkg in "${expected[@]}"; do + if [ ! -f "artifacts/$pkg" ]; then + echo "Missing expected package: artifacts/$pkg" >&2 + exit 1 + fi + done + + - name: Upload combined NuGet artifact + uses: actions/upload-artifact@v7 + with: + name: typstsharp-${{ inputs.package_version }} + path: artifacts diff --git a/.github/workflows/pack.yml b/.github/workflows/pack.yml index 77cc2fa..b509904 100644 --- a/.github/workflows/pack.yml +++ b/.github/workflows/pack.yml @@ -11,13 +11,10 @@ permissions: contents: read jobs: - pack: + version: runs-on: ubuntu-latest outputs: package_version: ${{ steps.version.outputs.version }} - env: - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 - DOTNET_CLI_TELEMETRY_OPTOUT: 1 steps: - name: Checkout repository @@ -28,7 +25,7 @@ jobs: shell: bash run: | set -euo pipefail - base_version=$(grep -m1 '' src/typstsharp/typstsharp.csproj | sed -E 's/.*(.+)<\/Version>.*/\1/') + base_version=$(grep -m1 '' src/PackageMetadata.props | sed -E 's/.*(.+)<\/Version>.*/\1/') branch="${GITHUB_REF_NAME:-}" if [ -z "$branch" ] && [ -n "${GITHUB_HEAD_REF:-}" ]; then branch="$GITHUB_HEAD_REF" @@ -41,57 +38,53 @@ jobs: echo "Base version: $base_version" echo "Branch: ${branch:-unknown}" echo "Using package version: $package_version" - echo "PACKAGE_VERSION=$package_version" >> "$GITHUB_ENV" echo "version=$package_version" >> "$GITHUB_OUTPUT" - - name: Build package - uses: ./.github/actions/build-pack - with: - package-version: ${{ env.PACKAGE_VERSION }} - - # The Rust tests cover the FFI entry points directly. That is the only place a - # panic crossing the boundary shows up as a failing test rather than as a - # crashed test runner, so they have to run in their own right. - - name: Run native tests - shell: bash - run: cargo test --manifest-path src/typst_core/Cargo.toml --release - - - name: Run tests - shell: bash - run: dotnet test --solution typstsharp.slnx - - - name: Upload NuGet artifact - uses: actions/upload-artifact@v7 - with: - name: typstsharp-${{ env.PACKAGE_VERSION }} - path: artifacts + build: + needs: version + uses: ./.github/workflows/build-packages.yml + with: + package_version: ${{ needs.version.outputs.package_version }} + run_tests: true test-package: - needs: pack + name: Test package (${{ matrix.name }}) + needs: + - version + - build runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - - name: Windows + - name: Windows x64 os: windows-latest - - name: Ubuntu + - name: Windows ARM64 + os: windows-11-arm + - name: Ubuntu x64 os: ubuntu-latest - - name: Alpine (musl) + - name: Ubuntu ARM64 + os: ubuntu-24.04-arm + - name: macOS ARM64 + os: macos-latest + - name: Alpine x64 (musl) os: ubuntu-latest test_type: docker-alpine - - name: Fedora (new glibc) + - name: Alpine ARM64 (musl) + os: ubuntu-24.04-arm + test_type: docker-alpine + - name: Fedora x64 (new glibc) os: ubuntu-latest test_type: docker-fedora steps: - name: Checkout repository - uses: actions/checkout@v5 + uses: actions/checkout@v7 - name: Download package artifacts uses: actions/download-artifact@v7 with: - name: typstsharp-${{ needs.pack.outputs.package_version }} + name: typstsharp-${{ needs.version.outputs.package_version }} path: artifacts - name: Install .NET SDK @@ -100,26 +93,28 @@ jobs: with: dotnet-version: 11.x - - name: Run tests (Windows / Ubuntu) + - name: Run tests (native host) if: matrix.test_type == '' shell: bash env: MSYS2_ARG_CONV_EXCL: "*" run: | + set -euo pipefail dotnet nuget add source "${{ github.workspace }}/artifacts" --name local-artifacts dotnet nuget locals all --clear - dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} - dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} --no-restore + dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} + dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} --no-restore - name: Run tests (Alpine musl) if: matrix.test_type == 'docker-alpine' shell: bash run: | docker run --rm -v ${{ github.workspace }}:/workspace -w /workspace mcr.microsoft.com/dotnet/sdk:11.0-alpine sh -c " + set -eu dotnet nuget add source /workspace/artifacts --name local-artifacts dotnet nuget locals all --clear - dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} - dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} --no-restore + dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} + dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} --no-restore " - name: Run tests (Fedora) @@ -127,11 +122,12 @@ jobs: shell: bash run: | docker run --rm -v ${{ github.workspace }}:/workspace -w /workspace fedora:40 sh -c " + set -eu dnf install -y curl tar icu curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel 11.0 --install-dir /usr/share/dotnet ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet dotnet nuget add source /workspace/artifacts --name local-artifacts dotnet nuget locals all --clear - dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} - dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.pack.outputs.package_version }} --no-restore - " \ No newline at end of file + dotnet restore src/typstsharp.tests/typstsharp.tests.csproj -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} + dotnet test --project src/typstsharp.tests/typstsharp.tests.csproj -c Release -p:UsePackagedLibrary=true -p:PackageVersion=${{ needs.version.outputs.package_version }} --no-restore + " diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1470db9..333af0e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,23 +6,18 @@ on: - published permissions: - contents: write - packages: write - id-token: write # enable GitHub OIDC token issuance for this job + contents: read jobs: - publish: + version: runs-on: ubuntu-latest - env: - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 - DOTNET_CLI_TELEMETRY_OPTOUT: 1 + outputs: + package_version: ${{ steps.version.outputs.package_version }} steps: - - name: Checkout repository - uses: actions/checkout@v7 - - name: Determine package version id: version + shell: bash run: | set -euo pipefail tag_name="${GITHUB_REF#refs/tags/}" @@ -31,25 +26,62 @@ jobs: fi package_version=${tag_name##v} echo "Using package version: $package_version" - echo "PACKAGE_VERSION=$package_version" >> "$GITHUB_ENV" echo "package_version=$package_version" >> "$GITHUB_OUTPUT" - - name: Build package - uses: ./.github/actions/build-pack + build: + needs: version + uses: ./.github/workflows/build-packages.yml + with: + package_version: ${{ needs.version.outputs.package_version }} + run_tests: false + + publish: + needs: + - version + - build + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + id-token: write # enable GitHub OIDC token issuance for this job + env: + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + + steps: + - name: Install .NET SDK + uses: actions/setup-dotnet@v5 with: - package-version: ${{ env.PACKAGE_VERSION }} + dotnet-version: 11.x - # Get a short-lived NuGet API key - - name: NuGet login (OIDC → temp API key) + - name: Download package artifacts + uses: actions/download-artifact@v7 + with: + name: typstsharp-${{ needs.version.outputs.package_version }} + path: artifacts + + # Get a short-lived NuGet API key + - name: NuGet login (OIDC -> temp API key) uses: NuGet/login@v1 id: login with: user: evolvedlight - - name: Publish package to NuGet.org + - name: Publish runtime packages to NuGet.org + shell: bash + run: | + set -euo pipefail + dotnet nuget push artifacts/typstsharp.runtime.*.nupkg \ + --api-key ${{ steps.login.outputs.NUGET_API_KEY }} \ + --source https://api.nuget.org/v3/index.json \ + --skip-duplicate + + - name: Publish main package to NuGet.org + shell: bash run: | - dotnet nuget push artifacts/*.nupkg \ - --api-key ${{steps.login.outputs.NUGET_API_KEY}} \ + set -euo pipefail + dotnet nuget push "artifacts/typstsharp.${{ needs.version.outputs.package_version }}.nupkg" \ + --api-key ${{ steps.login.outputs.NUGET_API_KEY }} \ --source https://api.nuget.org/v3/index.json \ --skip-duplicate diff --git a/README.md b/README.md index 054b14b..17b2510 100644 --- a/README.md +++ b/README.md @@ -248,15 +248,26 @@ typstsharp: the next compilation of each template starts cold. Call it after lar than after every compilation. It is safe to call while other threads compile. They lose cached results and compute them again, and documents already compiled stay valid. -The packaged Linux library is statically linked against musl, whose allocator reuses freed memory for -later compilations but rarely hands it back to the operating system, so the resident size stops growing -rather than shrinking. Clearing the cache does not lower the peak of a single compilation either: to -bound that, split a large document into several smaller ones and clear the cache between them. +On Alpine / musl (`linux-musl-*`), musl's allocator reuses freed memory for later compilations but rarely hands it back to the operating system, so the resident size stops growing rather than shrinking. Clearing the cache does not lower the peak of a single compilation either: to bound that, split a large document into several smaller ones and clear the cache between them. + +## Supported Platforms & NuGet Packages + +`typstsharp` ships as a small managed package that depends on per-architecture native runtime packages (similar to `SkiaSharp`): + +- `typstsharp.runtime.win-x64` +- `typstsharp.runtime.win-arm64` +- `typstsharp.runtime.linux-x64` (glibc 2.28+ — Ubuntu, Debian, Fedora, RHEL, Amazon Linux, etc.) +- `typstsharp.runtime.linux-arm64` (glibc 2.28+ — Ubuntu ARM64, Debian ARM64, AWS Graviton, etc.) +- `typstsharp.runtime.linux-musl-x64` (Alpine Linux / musl) +- `typstsharp.runtime.linux-musl-arm64` (Alpine Linux ARM64 / musl) +- `typstsharp.runtime.osx-arm64` + +You only need to install the main `typstsharp` package. Portable builds (`dotnet run`, `dotnet test`, file-based apps) pick up your host platform's native binary automatically, and RID-specific publishes (`dotnet publish -r `) automatically include only the native library for that target runtime. ## Prerequisites - [.NET SDK 11.0](https://dotnet.microsoft.com/) – required to build the managed projects. -- [Rust toolchain](https://www.rust-lang.org/tools/install) (with `cargo`) – **only required if you are building the project from source.** The NuGet package includes pre-compiled native binaries. +- [Rust toolchain](https://www.rust-lang.org/tools/install) (with `cargo`) – **only required if you are building the project from source.** The NuGet packages include pre-compiled native binaries. ## Building @@ -267,15 +278,17 @@ bound that, split a large document into several smaller ones and clear the cache The build will automatically: -1. Run `cargo build --release` on `src/typst_core` for each target runtime identifier (RID). By default, this includes `win-x64`, `linux-x64`, and others. For local debug builds, it only builds for the host architecture. Note that the build will automatically fallback to `gnu` from `musl` on Linux if `musl-gcc` is not available on the system. -2. Stage the produced native libraries under `obj/`. -3. Add the libraries to the managed project's runtime assets so that `dotnet publish`/`dotnet pack` place the files under `runtimes//native/` in the final artifact. -4. For local development, the native binary for the host architecture is copied to the output directory of any project referencing `typstsharp`, ensuring it's available for debugging. +1. Run `cargo rustc --release --crate-type cdylib` on `src/typst_core` for the host runtime identifier (RID). +2. Copy the native binary for the host architecture to the output directory of any project referencing `typstsharp`, ensuring it is available for local runs and tests. -You can override the target runtimes by setting the `RustTargets` property (e.g., `dotnet build -p:RustTargets=win-x64`). On macOS, use `osx-arm64` for Apple Silicon or `osx-x64` for Intel: +You can override the target runtime by setting the `RustTargets` property (e.g., `dotnet build -p:RustTargets=osx-arm64`), or skip the Rust build when only compiling/packing the managed assembly (`-p:SkipRustBuild=true`): ```bash -dotnet build -p:RustTargets=osx-arm64 +# Pack the managed package (depends on the per-RID runtime packages) +dotnet pack src/typstsharp/typstsharp.csproj -c Release -p:SkipRustBuild=true -o artifacts + +# Pack a native runtime package for a specific RID (e.g. linux-x64, linux-arm64, linux-musl-x64, linux-musl-arm64, win-x64, win-arm64, osx-arm64) +dotnet pack src/typstsharp.runtime/typstsharp.runtime.csproj -c Release -p:RuntimeRid=osx-arm64 -o artifacts ``` ## Verifying the CLI diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 6972ae2..e54dc23 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -17,9 +17,10 @@ - `compiler.Compile(outputFile, format)` and `compiler.CompileSvg(...)` no longer copy the rendered output onto the managed heap before writing or decoding it. - Fonts are now discovered once per process for each font configuration, instead of once per `TypstCompiler`. Scanning the system fonts was most of what creating a compiler cost, so the static one-shot helpers such as `TypstCompiler.CompilePdf(source)` paid it on every call: with system fonts enabled, creating, compiling a short document and freeing measured 71 to 77 ms per call before and 1.4 to 1.8 ms after the first. Compilers with the same `Fonts` settings now also share the fonts they have loaded, rather than each holding a copy of every font it used. Two consequences: a font installed or added to a font directory after a configuration was first used is not seen until the process restarts, and the fonts a configuration has loaded stay in memory after its last compiler is disposed. `EvictCache()` does not release them. - `sysInputs` parameters on `TypstCompiler` and the `SetSysInputs` argument are now `IDictionary` instead of `Dictionary`, so a `ReadOnlyDictionary`, `ImmutableDictionary` or any other implementation can be passed. Existing calls that pass a `Dictionary` are unaffected. -- The native library is now built with size optimisation (`opt-level = "z"`), thin LTO (`lto = "thin"`), a single codegen unit, symbol stripping (`strip = "symbols"`), and single-target crate compilation (`--crate-type cdylib` / `staticlib`, with Rust mangled symbol hiding and stripping on Linux musl), so that LTO runs across the dependency graph and unreferenced Rust symbols and DWARF debug sections are stripped. The Linux `libtypst_core.so` drops from ~59 MB (and ~90–100 MB on musl / MinGW in the NuGet package) down to ~47–49 MB (~20 MB compressed in the `.nupkg`). +- The native library is now built with size optimisation (`opt-level = "z"`), thin LTO (`lto = "thin"`), a single codegen unit, symbol stripping (`strip = "symbols"`), and single-target crate compilation (`--crate-type cdylib`), so that LTO runs across the dependency graph and unreferenced symbols and DWARF debug sections are stripped. ### Added +- Added default native runtime support for `win-arm64`, `linux-arm64`, `linux-musl-x64`, `linux-musl-arm64`, and `osx-arm64` alongside `win-x64` and `linux-x64` (with `linux-x64` / `linux-arm64` built against glibc 2.28 and `linux-musl-*` built natively against musl), packaged as separate per-architecture NuGet packages (`typstsharp.runtime.`) referenced automatically by the main `typstsharp` package so individual package sizes stay small and RID-specific publishes (`dotnet publish -r `) ship only the matching native library. - Added `TypstCompiler.EvictCache()` to clear and release the memory held by Typst's incremental compilation cache. The cache is shared by the whole process and was only ever trimmed to what the last ten compilations used, so an application that compiled one large document kept memory in proportion to it for the next ten compilations, and could not release it any sooner. The cache is process-wide, so clearing it makes the next compilation of every compiler start cold; it is safe while other threads compile, and leaves compiled documents valid. The packaged Linux library uses musl, which reuses the freed memory rather than returning it to the operating system, and a single compilation's peak is unchanged, so a very large document still has to be split to bound that. - Added merged SVG export, rendering every page into a single SVG stacked top to bottom as Typst's `svg_merged` does. Use `compiler.CompileMergedSvg(gapInPoints)`, the static `TypstCompiler.CompileMergedSvg(...)` / `TypstCompiler.CompileMergedSvgFromFile(...)`, or the `svg-merged` format with the new `mergedGap` parameter on `CompileToDocument` and `Compile(outputFile, format)`. The gap between pages is given in points. - Added `compiler.CompileToDocument(...)`, returning a disposable `TypstDocument` that exposes the rendered output while it is still in the memory the native library allocated. `GetOutputSpan`, `OpenOutputStream`, `CopyOutputTo`, `WriteOutputToFile` and `RentOutput` read it without putting a multi-megabyte PDF on the large object heap; `GetOutputBytes` copies when a `byte[]` is what you need. diff --git a/src/PackageMetadata.props b/src/PackageMetadata.props new file mode 100644 index 0000000..d7b4b39 --- /dev/null +++ b/src/PackageMetadata.props @@ -0,0 +1,9 @@ + + + 0.0.1-typst0.15.0 + evolvedlight + typst;typesetting;pdf;rust + https://github.com/evolvedlight/typstsharp + MIT + + diff --git a/src/RustBuild.targets b/src/RustBuild.targets new file mode 100644 index 0000000..9cbbc19 --- /dev/null +++ b/src/RustBuild.targets @@ -0,0 +1,99 @@ + + + + $(NETCoreSdkPortableRuntimeIdentifier) + $(NETCoreSdkRuntimeIdentifier) + win-x64 + + false + + x86_64-pc-windows-msvc + x86_64-pc-windows-gnu + aarch64-pc-windows-msvc + + $([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildThisFileDirectory)', 'typst_core')))) + + + + + $(WindowsX64RustTargetTriple) + typst_core.dll + + + $(WindowsArm64RustTargetTriple) + typst_core.dll + + + x86_64-unknown-linux-gnu + libtypst_core.so + + + aarch64-unknown-linux-gnu + libtypst_core.so + + + x86_64-unknown-linux-musl + libtypst_core.so + CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS + + + aarch64-unknown-linux-musl + libtypst_core.so + CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS + + + aarch64-apple-darwin + libtypst_core.dylib + + + x86_64-apple-darwin + libtypst_core.dylib + + + + + + $([System.IO.Path]::Combine('$(RustCoreDir)', 'target', '%(TargetTriple)', 'release', '%(LibraryFileName)')) + runtimes/%(Identity)/native/%(LibraryFileName) + $([System.IO.Path]::Combine('runtimes', '%(Identity)', 'native', '%(LibraryFileName)')) + + + + + + + + + + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/typst_core/.cargo/config.toml b/src/typst_core/.cargo/config.toml index bbb4e38..92ab084 100644 --- a/src/typst_core/.cargo/config.toml +++ b/src/typst_core/.cargo/config.toml @@ -1,3 +1,5 @@ [target.x86_64-unknown-linux-musl] -linker = "x86_64-linux-musl-gcc" -rustflags = ["-C", "target-feature=+crt-static", "-C", "relocation-model=pic"] +rustflags = ["-C", "target-feature=-crt-static"] + +[target.aarch64-unknown-linux-musl] +rustflags = ["-C", "target-feature=-crt-static"] diff --git a/src/typst_core/exports.map b/src/typst_core/exports.map deleted file mode 100644 index e6f5020..0000000 --- a/src/typst_core/exports.map +++ /dev/null @@ -1,5 +0,0 @@ -{ - local: - _R*; - _ZN*; -}; diff --git a/src/typstsharp.runtime/README.md b/src/typstsharp.runtime/README.md new file mode 100644 index 0000000..1abfbdb --- /dev/null +++ b/src/typstsharp.runtime/README.md @@ -0,0 +1,5 @@ +# typstsharp native runtime package + +This package contains the pre-compiled native `typst_core` library for a specific target runtime identifier (RID), used by [`typstsharp`](https://www.nuget.org/packages/typstsharp). + +You normally do **not** need to install this package directly — install [`typstsharp`](https://www.nuget.org/packages/typstsharp) instead, which references the runtime packages automatically and lets the .NET SDK select the matching native binary for your platform. diff --git a/src/typstsharp.runtime/typstsharp.runtime.csproj b/src/typstsharp.runtime/typstsharp.runtime.csproj new file mode 100644 index 0000000..ff6356d --- /dev/null +++ b/src/typstsharp.runtime/typstsharp.runtime.csproj @@ -0,0 +1,47 @@ + + + + + + + net11.0 + false + false + true + true + + $(HostRustRuntime) + $(RuntimeRid) + true + + typstsharp.runtime.$(RuntimeRid) + Native Typst library (typst_core) for $(RuntimeRid), used by typstsharp. + + typstsharp.runtime.nuspec + $(MSBuildThisFileDirectory) + + + + + <_MatchedRuntime Include="@(RustRuntime)" Condition="'%(Identity)' == '$(RuntimeRid)'" /> + + + + + + + <_SelectedRuntime Include="@(RustRuntime)" Condition="'%(Identity)' == '$(RuntimeRid)'" /> + + + + <_ResolvedLibraryFileName>%(_SelectedRuntime.LibraryFileName) + <_ResolvedNativeLibraryPath Condition="'$(NativeLibraryPath)' != ''">$([System.IO.Path]::GetFullPath('$(NativeLibraryPath)')) + <_ResolvedNativeLibraryPath Condition="'$(_ResolvedNativeLibraryPath)' == ''">%(_SelectedRuntime.CargoOutput) + <_NuspecTags>$([System.String]::Copy('$(PackageTags)').Replace(';', ' ')) + id=$(PackageId);version=$(PackageVersion);authors=$(Authors);description=$(Description);tags=$(_NuspecTags);repositoryUrl=$(RepositoryUrl);license=$(PackageLicenseExpression);rid=$(RuntimeRid);libraryFileName=$(_ResolvedLibraryFileName);nativeLibraryPath=$(_ResolvedNativeLibraryPath) + + + + + + diff --git a/src/typstsharp.runtime/typstsharp.runtime.nuspec b/src/typstsharp.runtime/typstsharp.runtime.nuspec new file mode 100644 index 0000000..da59b75 --- /dev/null +++ b/src/typstsharp.runtime/typstsharp.runtime.nuspec @@ -0,0 +1,17 @@ + + + + $id$ + $version$ + $authors$ + $license$ + README.md + $description$ + $tags$ + + + + + + + diff --git a/src/typstsharp/typstsharp.csproj b/src/typstsharp/typstsharp.csproj index 7fa5f8e..0846aad 100644 --- a/src/typstsharp/typstsharp.csproj +++ b/src/typstsharp/typstsharp.csproj @@ -1,161 +1,58 @@ + + net10.0;net11.0 enable enable true - $(NoWarn);NU5118 typstsharp - 0.0.1-typst0.15.0 - evolvedlight A .NET wrapper for the Typst typesetting system. README.md - typst;typesetting;pdf;rust - https://github.com/evolvedlight/typstsharp - MIT + typstsharp.nuspec + $(MSBuildThisFileDirectory) + + - $(NETCoreSdkPortableRuntimeIdentifier) - $(NETCoreSdkRuntimeIdentifier) - win-x64 - linux-x64;win-x64;win-arm64 - - $(HostRustRuntime) - - $(DefaultRustTargets) - $(RustTargets);$(HostRustRuntime) - x86_64-pc-windows-msvc - x86_64-pc-windows-gnu - $([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildThisFileDirectory)', '..', 'typst_core')))) - $(TargetsForTfmSpecificContentInPackage);CollectRustNativeLibraries + + $(HostRustRuntime) - - - - - - - $(WindowsRustTargetTriple) - typst_core.dll - - - x86_64-unknown-linux-musl - libtypst_core.so - - - x86_64-apple-darwin - libtypst_core.dylib - - - aarch64-apple-darwin - libtypst_core.dylib - - - - - - - $([System.IO.Path]::Combine('$(RustCoreDir)','target','%(TargetTriple)','release','%(LibraryFileName)')) - runtimes/%(Identity)/native/%(LibraryFileName) - $([System.IO.Path]::Combine('runtimes','%(Identity)','native','%(LibraryFileName)')) - - - - - - - - - - - - - - - - - - false - true - - - - - - - x86_64-unknown-linux-gnu - $([System.IO.Path]::Combine('$(RustCoreDir)','target','x86_64-unknown-linux-gnu','release','libtypst_core.so')) - - - - - - - - - false - true - - - - - - - - + - + - - + - - - - - - - - - - - - - - - - + + - - - - - %(RustNativeFiles.PackagePath) - - - + - - - - - - - + + PreserveNewest %(Filename)%(Extension) false + false - + + + + + + <_NuspecTags>$([System.String]::Copy('$(PackageTags)').Replace(';', ' ')) + id=$(PackageId);version=$(PackageVersion);configuration=$(Configuration);authors=$(Authors);description=$(Description);tags=$(_NuspecTags);repositoryUrl=$(RepositoryUrl);license=$(PackageLicenseExpression) + - \ No newline at end of file + diff --git a/src/typstsharp/typstsharp.nuspec b/src/typstsharp/typstsharp.nuspec new file mode 100644 index 0000000..5a452dc --- /dev/null +++ b/src/typstsharp/typstsharp.nuspec @@ -0,0 +1,38 @@ + + + + $id$ + $version$ + $authors$ + $license$ + README.md + $description$ + $tags$ + + + + + + + + + + + + + + + + + + + + + + + + + + + +