1370b7887SLouis Dionne#!/usr/bin/env bash
2370b7887SLouis Dionne
3370b7887SLouis Dionne# This simple script can be used to set up a CI node running MacOS.
4370b7887SLouis Dionne# An additional requirement that is *not* handled by this script is the
5370b7887SLouis Dionne# installation of Xcode, which requires manual intervention.
604942a7fSLouis Dionne#
718e21e68SLouis Dionne# This script should first be run from an administrator account to install
818e21e68SLouis Dionne# the dependencies necessary for running CI. It can be run without having
918e21e68SLouis Dionne# to clone the LLVM repository with:
1004942a7fSLouis Dionne#
1104942a7fSLouis Dionne#   $ /bin/bash -c "$(curl -fsSl https://raw.githubusercontent.com/llvm/llvm-project/main/libcxx/utils/ci/macos-ci-setup)"
1204942a7fSLouis Dionne#
139a460b84SLouis Dionne# If you perform system updates, you should re-run the script from the
149a460b84SLouis Dionne# administrator account -- this allows updating the packages used for
159a460b84SLouis Dionne# CI and the BuildKite agent tags.
169a460b84SLouis Dionne#
1704942a7fSLouis Dionne# Once the necessary dependencies have been installed, you can switch
1818e21e68SLouis Dionne# to a non-administrator account and run the script again, passing the
1918e21e68SLouis Dionne# --setup-launchd argument. That will install a Launchd agent to run the
2018e21e68SLouis Dionne# BuildKite agent whenever the current user is logged in. You should enable
2118e21e68SLouis Dionne# automatic login for that user, so that if the CI node goes down, the user
2218e21e68SLouis Dionne# is logged back in automatically when the node goes up again, and the
2318e21e68SLouis Dionne# BuildKite agent starts automatically.
2418e21e68SLouis Dionne#
2518e21e68SLouis Dionne# Alternatively, you can simply run the BuildKite agent by hand using:
2604942a7fSLouis Dionne#
2704942a7fSLouis Dionne#   $ caffeinate -s buildkite-agent start --build-path /tmp/buildkite-builds
28370b7887SLouis Dionne
291ca74860SLouis Dionneset -e
301ca74860SLouis Dionne
3118e21e68SLouis Dionne# Install a Launchd agent that will automatically start the BuildKite agent at login
3218e21e68SLouis Dionneif [[ ${1} == "--setup-launchd" ]]; then
3318e21e68SLouis Dionne  HOMEBREW_PREFIX="$(brew --prefix)"
34fbc3e69fSLouis Dionne  mkdir -p ~/Library/LaunchAgents
3518e21e68SLouis Dionne  cat <<EOF > ~/Library/LaunchAgents/libcxx.buildkite-agent.plist
3618e21e68SLouis Dionne<?xml version="1.0" encoding="UTF-8"?>
3718e21e68SLouis Dionne<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3818e21e68SLouis Dionne<plist version="1.0">
3918e21e68SLouis Dionne<dict>
4018e21e68SLouis Dionne  <key>Label</key>
4118e21e68SLouis Dionne  <string>libcxx.buildkite-agent</string>
4218e21e68SLouis Dionne
4318e21e68SLouis Dionne  <key>ProgramArguments</key>
4418e21e68SLouis Dionne  <array>
4518e21e68SLouis Dionne    <string>${HOMEBREW_PREFIX}/bin/buildkite-agent</string>
4618e21e68SLouis Dionne    <string>start</string>
4718e21e68SLouis Dionne    <string>--build-path</string>
4818e21e68SLouis Dionne    <string>${HOME}/libcxx.buildkite-agent/builds</string>
4918e21e68SLouis Dionne  </array>
5018e21e68SLouis Dionne
5118e21e68SLouis Dionne  <key>EnvironmentVariables</key>
5218e21e68SLouis Dionne  <dict>
5318e21e68SLouis Dionne    <key>PATH</key>
5418e21e68SLouis Dionne    <string>${HOMEBREW_PREFIX}/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
5518e21e68SLouis Dionne  </dict>
5618e21e68SLouis Dionne
5718e21e68SLouis Dionne  <key>RunAtLoad</key>
5818e21e68SLouis Dionne  <true/>
5918e21e68SLouis Dionne
6018e21e68SLouis Dionne  <key>KeepAlive</key>
6118e21e68SLouis Dionne  <dict>
6218e21e68SLouis Dionne    <key>SuccessfulExit</key>
6318e21e68SLouis Dionne    <false/>
6418e21e68SLouis Dionne  </dict>
6518e21e68SLouis Dionne
6618e21e68SLouis Dionne  <key>ProcessType</key>
6718e21e68SLouis Dionne  <string>Interactive</string>
6818e21e68SLouis Dionne
6918e21e68SLouis Dionne  <key>ThrottleInterval</key>
7018e21e68SLouis Dionne  <integer>30</integer>
7118e21e68SLouis Dionne
7218e21e68SLouis Dionne  <key>StandardOutPath</key>
7318e21e68SLouis Dionne  <string>${HOME}/libcxx.buildkite-agent/stdout.log</string>
7418e21e68SLouis Dionne
7518e21e68SLouis Dionne  <key>StandardErrorPath</key>
7618e21e68SLouis Dionne  <string>${HOME}/libcxx.buildkite-agent/stderr.log</string>
7718e21e68SLouis Dionne</dict>
7818e21e68SLouis Dionne</plist>
7918e21e68SLouis DionneEOF
8018e21e68SLouis Dionne
8118e21e68SLouis Dionne  echo "Starting BuildKite agent"
8218e21e68SLouis Dionne  launchctl load ~/Library/LaunchAgents/libcxx.buildkite-agent.plist
8318e21e68SLouis Dionne
8418e21e68SLouis Dionneelse
8518e21e68SLouis Dionne  echo "Installing CI dependencies for macOS"
8618e21e68SLouis Dionne
87d9d20802SLouis Dionne  if [[ -z "${BUILDKITE_AGENT_TOKEN}" ]]; then
88d9d20802SLouis Dionne    echo "The BUILDKITE_AGENT_TOKEN environment variable must be set to a BuildKite Agent token when calling this script."
89d9d20802SLouis Dionne    exit 1
90d9d20802SLouis Dionne  fi
91370b7887SLouis Dionne
92370b7887SLouis Dionne  # Install Homebrew
939a460b84SLouis Dionne  if ! which -s brew; then
94370b7887SLouis Dionne    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
959a460b84SLouis Dionne  fi
96370b7887SLouis Dionne
97370b7887SLouis Dionne  # Install the required tools to run CI
989a460b84SLouis Dionne  brew update
999a460b84SLouis Dionne  for package in sphinx-doc python3 ninja cmake clang-format buildkite/buildkite/buildkite-agent; do
1009a460b84SLouis Dionne    if brew ls --versions "${package}" >/dev/null; then
1019a460b84SLouis Dionne      brew upgrade "${package}"
1029a460b84SLouis Dionne    else
1039a460b84SLouis Dionne      brew install "${package}"
1049a460b84SLouis Dionne    fi
1059a460b84SLouis Dionne  done
106*0a460416SLouis Dionne  python3 -m pip install --upgrade psutil
107370b7887SLouis Dionne
1089a460b84SLouis Dionne  echo "Setting up BuildKite Agent config"
1093fb83853SLouis Dionne  version="$(sw_vers -productVersion | sed -E 's/([0-9]+).([0-9]+).[0-9]+/\1.\2/')"
11087bfc49dSLouis Dionne  arch="$(uname -m)"
1119a460b84SLouis Dionne  cat <<EOF > "$(brew --prefix)/etc/buildkite-agent/buildkite-agent.cfg"
1129a460b84SLouis Dionnetoken="${BUILDKITE_AGENT_TOKEN}"
1139a460b84SLouis Dionnetags="queue=libcxx-builders,arch=${arch},os=macos,os=macos${version}"
1149a460b84SLouis Dionnebuild-path=/tmp/buildkite-builds # Note that this is actually overwritten when starting the agent with launchd
1159a460b84SLouis DionneEOF
11618e21e68SLouis Dionnefi
117