|
| 1 | +#!/bin/bash |
| 2 | +# Usage: ./install_cmake.sh 3.28.3 |
| 3 | +# Installs a specific CMake version on macOS from Kitware releases. |
| 4 | +# Layout: |
| 5 | +# /usr/local/cmake-<version>/CMake.app/Contents/bin/{cmake,ccmake,cpack,ctest} |
| 6 | +# Symlinks: |
| 7 | +# /usr/local/bin/{cmake,ccmake,cpack,ctest} -> .../Contents/bin/<tool> |
| 8 | +# Works on both Intel and Apple Silicon (uses "macos-universal" tarball). |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +if [ -z "${1:-}" ]; then |
| 13 | + echo "Usage: $0 <cmake-version>" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +VERSION="$1" |
| 18 | +INSTALL_DIR="/usr/local/cmake-$VERSION" |
| 19 | +TARBALL="cmake-$VERSION-macos-universal.tar.gz" |
| 20 | +URL="https://github.com/Kitware/CMake/releases/download/v$VERSION/$TARBALL" |
| 21 | +TMPFILE="/tmp/$TARBALL" |
| 22 | + |
| 23 | +# Require basic tools |
| 24 | +for cmd in curl tar; do |
| 25 | + command -v "$cmd" >/dev/null || { echo "Missing $cmd"; exit 1; } |
| 26 | +done |
| 27 | + |
| 28 | +# Skip if already installed |
| 29 | +if [ -d "$INSTALL_DIR" ] && [ -x "$INSTALL_DIR/CMake.app/Contents/bin/cmake" ]; then |
| 30 | + echo "CMake $VERSION already installed at $INSTALL_DIR" |
| 31 | +else |
| 32 | + echo "Downloading $URL ..." |
| 33 | + # --fail makes curl exit non-zero on 404/5xx; -L follows redirects |
| 34 | + curl -fL "$URL" -o "$TMPFILE" |
| 35 | + |
| 36 | + echo "Creating $INSTALL_DIR and extracting..." |
| 37 | + sudo mkdir -p "$INSTALL_DIR" |
| 38 | + |
| 39 | + # The tarball contains a top-level directory (e.g., cmake-<ver>-macos-universal/*) |
| 40 | + # We strip that first component so CMake.app ends up directly under $INSTALL_DIR. |
| 41 | + sudo tar -xzf "$TMPFILE" -C "$INSTALL_DIR" --strip-components=1 |
| 42 | + |
| 43 | + # Work around Gatekeeper quarantine if present (harmless if attribute absent) |
| 44 | + if command -v xattr >/dev/null; then |
| 45 | + sudo xattr -dr com.apple.quarantine "$INSTALL_DIR/CMake.app" || true |
| 46 | + fi |
| 47 | + |
| 48 | + # Sanity check |
| 49 | + if [ ! -x "$INSTALL_DIR/CMake.app/Contents/bin/cmake" ]; then |
| 50 | + echo "Error: cmake binary not found under $INSTALL_DIR/CMake.app/Contents/bin" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +fi |
| 54 | + |
| 55 | +echo "Linking command-line tools into /usr/local/bin ..." |
| 56 | +for tool in cmake ccmake cpack ctest; do |
| 57 | + sudo ln -sfn "$INSTALL_DIR/CMake.app/Contents/bin/$tool" "/usr/local/bin/$tool" |
| 58 | +done |
| 59 | + |
| 60 | +# Optional: link cmake-gui (will open the GUI app) |
| 61 | +if [ -x "$INSTALL_DIR/CMake.app/Contents/bin/cmake-gui" ]; then |
| 62 | + sudo ln -sfn "$INSTALL_DIR/CMake.app/Contents/bin/cmake-gui" /usr/local/bin/cmake-gui |
| 63 | +fi |
| 64 | + |
| 65 | +echo "CMake $VERSION installed successfully." |
| 66 | +which cmake |
| 67 | +cmake --version |
0 commit comments