diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c71e999 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,101 @@ +name: ci + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +jobs: + # The latest release on every supported host: install, then build and run a + # new package with the default toolchain. + latest: + name: latest (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, ubuntu-24.04-arm, macos-15, windows-2022] + runs-on: ${{ matrix.os }} + timeout-minutes: 40 + steps: + - uses: actions/checkout@v4 + - id: mcpp + uses: ./ + - name: The output names the active release + run: | + echo "output: ${{ steps.mcpp.outputs.version }}" + [ -n "${{ steps.mcpp.outputs.version }}" ] + [ "$(mcpp --version | head -1 | awk '{print $2}')" = "${{ steps.mcpp.outputs.version }}" ] + [ "${{ steps.mcpp.outputs.cache-hit }}" = false ] + - name: Build and run a new package + working-directory: ${{ runner.temp }} + run: | + mcpp new hello + cd hello + mcpp build + mcpp run + + # A pinned mcpp and xlings, a toolchain line, and the CN mirror. + pinned: + runs-on: ubuntu-24.04 + timeout-minutes: 40 + steps: + - uses: actions/checkout@v4 + - id: mcpp + uses: ./ + with: + version: 2026.11.16.1 + xlings-version: 2026.11.16.1 + mirror: CN + toolchains: | + llvm # one argument list per line + - name: The pinned releases are active + run: | + [ "${{ steps.mcpp.outputs.version }}" = 2026.11.16.1 ] + [ "$(mcpp --version | head -1 | awk '{print $2}')" = 2026.11.16.1 ] + [ "$(xlings --version | head -1 | awk '{print $2}')" = 2026.11.16.1 ] + mcpp toolchain list | grep -E '^ +\*? +llvm ' + + # The first job saves the cache; the second, started after it, restores it + # by the exact key. + cache-save: + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, windows-2022] + runs-on: ${{ matrix.os }} + timeout-minutes: 40 + steps: + - uses: actions/checkout@v4 + - uses: ./ + with: + cache: true + - working-directory: ${{ runner.temp }} + run: mcpp new hello && cd hello && mcpp build + + cache-restore: + needs: cache-save + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, windows-2022] + runs-on: ${{ matrix.os }} + timeout-minutes: 40 + steps: + - uses: actions/checkout@v4 + - id: mcpp + uses: ./ + with: + cache: true + - name: Restored by the exact key + run: '[ "${{ steps.mcpp.outputs.cache-hit }}" = true ]' + - working-directory: ${{ runner.temp }} + run: mcpp new hello && cd hello && mcpp build && mcpp run diff --git a/README.md b/README.md index 06e381a..6891d79 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,54 @@ # setup-mcpp -A GitHub Action that installs [mcpp](https://github.com/mcpp-community/mcpp), the C++23 modules build tool, on Linux, macOS and Windows runners. +A GitHub Action that installs [mcpp](https://github.com/mcpp-community/mcpp), the C++23 modules build tool, +on Linux, macOS and Windows runners. mcpp is installed through [xlings](https://github.com/openxlings/xlings) +and placed on `PATH`. -The action arrives with the first pull request. +```yaml +- uses: mcpp-community/setup-mcpp@v1 +- run: mcpp build && mcpp test +``` + +## Inputs + +| Input | Default | Meaning | +|---|---|---| +| `version` | `latest` | mcpp release, such as `2026.11.16.1` | +| `xlings-version` | `latest` | xlings release that installs mcpp | +| `mirror` | `GLOBAL` | package mirror, `GLOBAL` or `CN` | +| `toolchains` | | toolchains to install; each line is one `mcpp toolchain install` argument list | +| `cache` | `false` | keep `~/.xlings` and `~/.mcpp` in the Actions cache | + +The cache key names the operating system, the architecture, both releases, the toolchain list and the hash +of `mcpp.lock`. + +## Outputs + +| Output | Meaning | +|---|---| +| `version` | the installed mcpp release | +| `cache-hit` | `true` when the cache was restored by its exact key | + +## Example + +```yaml +jobs: + build: + strategy: + matrix: + os: [ubuntu-24.04, ubuntu-24.04-arm, macos-15, windows-2022] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: mcpp-community/setup-mcpp@v1 + with: + version: 2026.11.16.1 + cache: true + toolchains: | + llvm + - run: mcpp build --release +``` + +## License + +Apache-2.0 diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..dbd8ad8 --- /dev/null +++ b/action.yml @@ -0,0 +1,66 @@ +name: setup-mcpp +description: Install mcpp, the C++23 modules build tool, through xlings. +author: mcpp-community +branding: + icon: package + color: blue + +inputs: + version: + description: mcpp release to install, such as 2026.11.16.1, or latest. + default: latest + xlings-version: + description: xlings release that installs mcpp, or latest. + default: latest + mirror: + description: Package mirror for xlings and mcpp, GLOBAL or CN. + default: GLOBAL + toolchains: + description: Toolchains to install, one `mcpp toolchain install` argument list per line. + default: '' + cache: + description: Cache ~/.xlings and ~/.mcpp between runs (true or false). + default: 'false' + +outputs: + version: + description: The installed mcpp release. + value: ${{ steps.install.outputs.version }} + cache-hit: + description: "'true' when the cache was restored by its exact key." + value: ${{ steps.cache.outputs.cache-hit == 'true' }} + +runs: + using: composite + steps: + - id: resolve + if: inputs.cache == 'true' + shell: bash + env: + SETUP_MCPP_VERSION: ${{ inputs.version }} + SETUP_MCPP_XLINGS_VERSION: ${{ inputs.xlings-version }} + SETUP_MCPP_TOOLCHAINS: ${{ inputs.toolchains }} + run: bash "$GITHUB_ACTION_PATH/setup.sh" resolve + + # The key names the machine (installed binaries are per architecture), both + # releases, the toolchain list and the project's lock file. + - id: cache + if: inputs.cache == 'true' + uses: actions/cache@v4 + with: + path: | + ~/.xlings + ~/.mcpp + key: setup-mcpp-${{ runner.os }}-${{ runner.arch }}-xl${{ steps.resolve.outputs.xlings }}-${{ steps.resolve.outputs.mcpp }}-${{ steps.resolve.outputs.toolchains }}-${{ hashFiles('mcpp.lock') }} + restore-keys: | + setup-mcpp-${{ runner.os }}-${{ runner.arch }}-xl${{ steps.resolve.outputs.xlings }}-${{ steps.resolve.outputs.mcpp }}- + + - id: install + shell: bash + env: + SETUP_MCPP_VERSION: ${{ inputs.version }} + SETUP_MCPP_XLINGS_VERSION: ${{ inputs.xlings-version }} + SETUP_MCPP_MIRROR: ${{ inputs.mirror }} + SETUP_MCPP_TOOLCHAINS: ${{ inputs.toolchains }} + GITHUB_TOKEN: ${{ github.token }} + run: bash "$GITHUB_ACTION_PATH/setup.sh" install diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..bb1c3c4 --- /dev/null +++ b/setup.sh @@ -0,0 +1,138 @@ +#!/usr/bin/env bash +# setup.sh: the steps of action.yml. One script serves Linux, macOS and +# Windows (Git Bash on the hosted runners). +# +# setup.sh resolve versions and digests that name the cache +# setup.sh install xlings, then mcpp, then the requested toolchains +# +# Inputs arrive as SETUP_MCPP_* environment variables (action.yml). +set -euo pipefail + +version="${SETUP_MCPP_VERSION:-latest}" +version="${version#v}" +xlings_version="${SETUP_MCPP_XLINGS_VERSION:-latest}" +xlings_version="${xlings_version#v}" +mirror="${SETUP_MCPP_MIRROR:-GLOBAL}" +toolchains="${SETUP_MCPP_TOOLCHAINS:-}" + +case "$(uname -s)" in + MINGW* | MSYS* | CYGWIN*) windows=1; home="$USERPROFILE"; exe=.exe ;; + *) windows=0; home="$HOME"; exe= ;; +esac +bin="$home/.xlings/subos/current/bin" +xlings="$bin/xlings$exe" + +out() { echo "$1=$2" >> "$GITHUB_OUTPUT"; } +log() { echo "setup-mcpp: $*"; } +die() { echo "::error::setup-mcpp: $*"; exit 1; } + +# Highest release tag (vYYYY.M.D.N) of a GitHub repository. `git ls-remote` +# needs no API token and is not rate limited. +latest_tag() { + git ls-remote --tags --refs "https://github.com/$1" \ + | sed -n 's#.*refs/tags/v\([0-9][0-9.]*\)$#\1#p' \ + | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n | tail -1 +} + +# Runs "$@" up to three times, thirty seconds apart. +retry() { + local n + for n in 1 2 3; do + "$@" && return 0 + [ "$n" = 3 ] || { log "attempt $n failed; retrying"; sleep 30; } + done + return 1 +} + +resolve() { + local mv="$version" xv="$xlings_version" + [ "$mv" != latest ] || mv="$(latest_tag mcpp-community/mcpp)" + [ "$xv" != latest ] || xv="$(latest_tag openxlings/xlings)" + [ -n "$mv" ] && [ -n "$xv" ] || die "cannot resolve the latest release tags" + out mcpp "$mv" + out xlings "$xv" + # The toolchain list belongs to the cache: a cache saved before a line was + # added would never gain that toolchain. + out toolchains "$(printf '%s' "$toolchains" | git hash-object --stdin | cut -c1-12)" +} + +install_xlings() { + if [ -x "$xlings" ]; then + local have + have="$("$xlings" --version 2>/dev/null | head -1 | awk '{print $2}')" + if [ "$xlings_version" = latest ] || [ "$have" = "$xlings_version" ]; then + log "xlings $have present" + return 0 + fi + fi + local tmp script + tmp="$(mktemp -d)" + if [ "$xlings_version" = latest ]; then export XLINGS_VERSION=; else export XLINGS_VERSION="$xlings_version"; fi + export XLINGS_NON_INTERACTIVE=1 + if [ "$windows" = 1 ]; then + script="$tmp/quick_install.ps1" + retry curl -fsSL -o "$script" https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.ps1 + retry pwsh -NoProfile -ExecutionPolicy Bypass -File "$(cygpath -w "$script")" + else + script="$tmp/quick_install.sh" + retry curl -fsSL -o "$script" https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.sh + retry bash "$script" < /dev/null + fi + [ -x "$xlings" ] || die "xlings is not at $xlings after installation" +} + +# The index is refreshed before every attempt but the first of a pinned +# version; the third attempt reads the CN mirror (GitCode), the fourth fetches +# the index with git. The requested mirror is restored afterwards. +install_mcpp() { + local spec=mcpp attempt + [ "$version" = latest ] || spec="mcpp@$version" + "$xlings" config --mirror "$mirror" > /dev/null + for attempt in 1 2 3 4; do + case "$attempt" in + 1) [ "$version" != latest ] || "$xlings" update || true ;; + 2) "$xlings" update || true ;; + 3) "$xlings" config --mirror CN > /dev/null; "$xlings" update || true ;; + 4) XLINGS_INDEX_SOURCE=git "$xlings" update || true ;; + esac + if "$xlings" install "$spec" -y -u -g; then + "$xlings" config --mirror "$mirror" > /dev/null + return 0 + fi + sleep $((attempt * 15)) + done + "$xlings" config --mirror "$mirror" > /dev/null + die "xlings could not install $spec" +} + +install() { + install_xlings + export PATH="$bin:$PATH" + if [ "$windows" = 1 ]; then cygpath -w "$bin"; else echo "$bin"; fi >> "$GITHUB_PATH" + install_mcpp + + local mcpp="$bin/mcpp$exe" have + have="$("$mcpp" --version | head -1 | awk '{print $2}')" + [ "$version" = latest ] || [ "$have" = "$version" ] \ + || die "mcpp $version was requested, $have is active" + "$mcpp" self config --mirror "$mirror" + + local line + while IFS= read -r line || [ -n "$line" ]; do + line="${line%%#*}" + # shellcheck disable=SC2086 # each line is one argument list + set -- $line + [ "$#" -gt 0 ] || continue + log "mcpp toolchain install $*" + retry "$mcpp" toolchain install "$@" + done <<< "$toolchains" + + out version "$have" + log "mcpp $have ready" +} + +case "${1:-}" in + resolve) resolve ;; + install) install ;; + *) die "usage: setup.sh resolve|install" ;; +esac