#!/bin/sh # Installs CPM, the C++ package manager and build system, then runs `cpm setup` to get a compiler and Ninja. # # curl -fsSL https://cpm-registry.com/install.sh | sh # # Environment: CPM_HOME (default ~/.cpm), CPM_NO_SETUP=1 (skip cpm setup), CPM_NO_MODIFY_PATH=1 (leave PATH alone). # Running it again updates CPM. Source: https://github.com/meirborowski/cpm-registry set -eu registry="https://cpm-registry.com" home="${CPM_HOME:-$HOME/.cpm}" say() { printf 'cpm-install: %s\n' "$1"; } fail() { printf 'cpm-install: error: %s\n' "$1" >&2; exit 1; } for tool in curl tar uname mktemp; do command -v "$tool" >/dev/null 2>&1 || fail "$tool is required" done case "$(uname -s)-$(uname -m)" in Linux-x86_64 | Linux-amd64) target=x86_64-linux ;; Darwin-arm64 | Darwin-aarch64) target=aarch64-macos ;; *) fail "there is no CPM release for $(uname -s) $(uname -m) yet; build it from source: https://github.com/meirborowski/CPM" ;; esac if command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | cut -d ' ' -f 1; } elif command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | cut -d ' ' -f 1; } else fail "sha256sum or shasum is required" fi # " " info=$(curl -fsSL "$registry/api/v1/cpm/latest/$target") || fail "cannot reach $registry" version=$(printf '%s' "$info" | cut -d ' ' -f 1) url=$(printf '%s' "$info" | cut -d ' ' -f 2) expected=$(printf '%s' "$info" | cut -d ' ' -f 3) tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT say "downloading CPM $version for $target" curl -fsSL -o "$tmp/cpm.tar.gz" "$url" || fail "cannot download $url" actual=$(sha256 "$tmp/cpm.tar.gz") [ "$actual" = "$expected" ] || fail "checksum mismatch for $url (expected $expected, got $actual)" tar -xzf "$tmp/cpm.tar.gz" -C "$tmp" mkdir -p "$home/bin" cp "$tmp"/cpm-*/cpm "$home/bin/cpm.new" chmod 755 "$home/bin/cpm.new" mv -f "$home/bin/cpm.new" "$home/bin/cpm" say "installed $home/bin/cpm" if [ "${CPM_NO_MODIFY_PATH:-}" != 1 ]; then cat > "$home/env" <> "$home/env" fi line=". \"$home/env\"" added="" # SHELL is unset in containers and CI; set -u would stop the script. login_shell="${SHELL:-}" for rc in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bashrc" "$HOME/.zshrc"; do # .profile, and .zshrc for zsh users (zsh never reads .profile), are created if missing; the others only # get the line if the shell uses them. if [ -f "$rc" ] || [ "$rc" = "$HOME/.profile" ] || { [ "$rc" = "$HOME/.zshrc" ] && [ "${login_shell##*/}" = zsh ]; }; then if ! grep -qsF "$line" "$rc"; then printf '\n%s\n' "$line" >> "$rc" added="$added $rc" fi fi done if [ -n "$added" ]; then say "put cpm on PATH in:$added" say "open a new terminal, or run: $line" fi fi PATH="$home/bin:$PATH" export PATH if [ "${CPM_NO_SETUP:-}" != 1 ]; then # Piped into sh, standard input is this script: give cpm setup the terminal for its questions. if [ -t 1 ] && (: < /dev/tty) 2>/dev/null; then "$home/bin/cpm" setup < /dev/tty else "$home/bin/cpm" setup --yes fi fi