name: Linux

# Builds and tests the package on Linux, and checks the release tool and its package there.

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch: {}

concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

defaults:
  run:
    shell: bash

jobs:
  linux:
    name: Build & test (Linux ${{ matrix.arch }})
    runs-on: ${{ matrix.runner }}
    # The release tool needs the glibc version that it's built against or a newer one. Red Hat UBI 9
    # has glibc 2.34, the oldest version that the package supports. The Publish workflow builds the
    # tool in the same image.
    container: swift:6.4.0-rhel-ubi9@sha256:802a003f1eacbca9c20664a8ba79621d13f4538c3d329e745b7f8d53c70604a3
    # The limit stops a hang, for example in the plugin server, from holding a runner for the default
    # 6 hours.
    timeout-minutes: 30
    env:
      # The newest glibc symbol version that the release tool may need. The READMEs of the Linux
      # packages name it.
      MAX_GLIBC_VERSION: '2.34'
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            runner: ubuntu-24.04
          - arch: arm64
            runner: ubuntu-24.04-arm
    steps:
      - name: Checkout
        uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5

      - name: Setup Node
        uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
        with:
          node-version: 24

      - name: Setup pnpm
        uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0

      - name: Cache SwiftPM build
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: .build
          key: spm-${{ github.repository }}-${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('Package.resolved') }}
          restore-keys: |
            spm-${{ github.repository }}-${{ runner.os }}-${{ matrix.arch }}-

      - name: Build
        run: swift build

      - name: Test
        run: swift test

      - name: Build release tool
        # Runs `pnpm run build` (scripts/build.js): builds the release plugin binary for this
        # architecture as platforms/linux-<arch>/ExpoModulesMacros.
        run: pnpm run build

      - name: Verify the release tool
        # With no arguments it is the plugin server, which exits when stdin closes; with arguments it
        # is the scanner CLI.
        run: |
          tool="platforms/linux-${{ matrix.arch }}/ExpoModulesMacros"
          # The Swift runtime must be linked in statically, so no Swift library may be a dependency.
          needed="$(readelf --dynamic "$tool" | sed -n 's/.*(NEEDED).*\[\(.*\)\]$/\1/p')"
          echo "Needed libraries:" $needed
          if echo "$needed" | grep -E 'swift|Foundation|dispatch|BlocksRuntime'; then
            echo "The release tool links a Swift library dynamically"
            exit 1
          fi
          glibc="$(objdump -T "$tool" | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/^GLIBC_//' | sort -uV | tail -n 1)"
          echo "Newest glibc symbol version: $glibc"
          if [ "$(printf '%s\n' "$glibc" "$MAX_GLIBC_VERSION" | sort -V | tail -n 1)" != "$MAX_GLIBC_VERSION" ]; then
            echo "The release tool needs glibc $glibc, newer than $MAX_GLIBC_VERSION"
            exit 1
          fi
          "$tool" < /dev/null
          "$tool" --help > /dev/null

      - name: Pack the platform package
        # Checks what `npm pack` would publish from `platforms/linux-<arch>`, where the release build
        # put the tool: the executable with its executable bit, the README, the license and the
        # manifest, with the `os`, `cpu` and `libc` fields for this machine.
        run: |
          # The `./` matters: npm reads `platforms/linux-<arch>` as a GitHub repository.
          npm pack "./platforms/linux-${{ matrix.arch }}" --pack-destination "$RUNNER_TEMP" --json > "$RUNNER_TEMP/pack.json"
          node - "$RUNNER_TEMP/pack.json" "${{ matrix.arch }}" <<'EOF'
          const fs = require('node:fs');
          const [packPath, arch] = process.argv.slice(2);
          const [pack] = JSON.parse(fs.readFileSync(packPath, 'utf8'));
          const files = pack.files.map((file) => file.path).sort().join(',');
          console.log(`${pack.filename}: ${files} (${pack.size} bytes packed)`);
          if (files !== 'ExpoModulesMacros,LICENSE,README.md,package.json') {
            throw new Error(`Unexpected files: ${files}`);
          }
          const tool = pack.files.find((file) => file.path === 'ExpoModulesMacros');
          if ((tool.mode & 0o111) === 0) {
            throw new Error(`ExpoModulesMacros is packed without the executable bit (mode ${tool.mode.toString(8)})`);
          }
          const manifest = JSON.parse(fs.readFileSync(`platforms/linux-${arch}/package.json`, 'utf8'));
          const fields = [manifest.os, manifest.cpu, manifest.libc].join(' / ');
          if (fields !== `linux / ${arch} / glibc`) {
            throw new Error(`Unexpected os / cpu / libc: ${fields}`);
          }
          fs.appendFileSync(process.env.GITHUB_ENV, `PLATFORM_PACKAGE_TARBALL=${process.env.RUNNER_TEMP}/${pack.filename}\n`);
          EOF

      - name: Scan through the TypeScript wrapper
        # Installs the packed platform package. The wrapper must find the executable in
        # `@expo/modules-macros-linux-<arch>`, as it would in a project, not the local build.
        run: |
          pnpm install
          pnpm add --save-optional "$PLATFORM_PACKAGE_TARBALL"
          pnpm run build:ts
          node scripts/check-scan.js .

