xref: /aosp_15_r20/external/executorch/.ci/scripts/setup-macos.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker#!/bin/bash
2*523fa7a6SAndroid Build Coastguard Worker# Copyright (c) Meta Platforms, Inc. and affiliates.
3*523fa7a6SAndroid Build Coastguard Worker# All rights reserved.
4*523fa7a6SAndroid Build Coastguard Worker#
5*523fa7a6SAndroid Build Coastguard Worker# This source code is licensed under the BSD-style license found in the
6*523fa7a6SAndroid Build Coastguard Worker# LICENSE file in the root directory of this source tree.
7*523fa7a6SAndroid Build Coastguard Worker
8*523fa7a6SAndroid Build Coastguard Workerset -exu
9*523fa7a6SAndroid Build Coastguard Worker
10*523fa7a6SAndroid Build Coastguard Worker# shellcheck source=/dev/null
11*523fa7a6SAndroid Build Coastguard Workersource "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
12*523fa7a6SAndroid Build Coastguard Worker
13*523fa7a6SAndroid Build Coastguard WorkerBUILD_TOOL=$1
14*523fa7a6SAndroid Build Coastguard Workerif [[ -z "${BUILD_TOOL:-}" ]]; then
15*523fa7a6SAndroid Build Coastguard Worker  echo "Missing build tool (require buck2 or cmake), exiting..."
16*523fa7a6SAndroid Build Coastguard Worker  exit 1
17*523fa7a6SAndroid Build Coastguard Workerelse
18*523fa7a6SAndroid Build Coastguard Worker  echo "Setup MacOS for ${BUILD_TOOL} ..."
19*523fa7a6SAndroid Build Coastguard Workerfi
20*523fa7a6SAndroid Build Coastguard Worker
21*523fa7a6SAndroid Build Coastguard Workerinstall_buck() {
22*523fa7a6SAndroid Build Coastguard Worker  if ! command -v zstd &> /dev/null; then
23*523fa7a6SAndroid Build Coastguard Worker    brew install zstd
24*523fa7a6SAndroid Build Coastguard Worker  fi
25*523fa7a6SAndroid Build Coastguard Worker
26*523fa7a6SAndroid Build Coastguard Worker  if ! command -v curl &> /dev/null; then
27*523fa7a6SAndroid Build Coastguard Worker    brew install curl
28*523fa7a6SAndroid Build Coastguard Worker  fi
29*523fa7a6SAndroid Build Coastguard Worker
30*523fa7a6SAndroid Build Coastguard Worker  pushd .ci/docker
31*523fa7a6SAndroid Build Coastguard Worker  # TODO(huydo): This is a one-off copy of buck2 2024-05-15 to unblock Jon and
32*523fa7a6SAndroid Build Coastguard Worker  # re-enable ShipIt. It’s not ideal that upgrading buck2 will require a manual
33*523fa7a6SAndroid Build Coastguard Worker  # update the cached binary on S3 bucket too. Let me figure out if there is a
34*523fa7a6SAndroid Build Coastguard Worker  # way to correctly implement the previous setup of installing a new version of
35*523fa7a6SAndroid Build Coastguard Worker  # buck2 only when it’s needed. AFAIK, the complicated part was that buck2
36*523fa7a6SAndroid Build Coastguard Worker  # --version doesn't say anything w.r.t its release version, i.e. 2024-05-15.
37*523fa7a6SAndroid Build Coastguard Worker  # See D53878006 for more details.
38*523fa7a6SAndroid Build Coastguard Worker  #
39*523fa7a6SAndroid Build Coastguard Worker  # If you need to upgrade buck2 version on S3, please reach out to Dev Infra
40*523fa7a6SAndroid Build Coastguard Worker  # team for help.
41*523fa7a6SAndroid Build Coastguard Worker  BUCK2_VERSION=$(cat ci_commit_pins/buck2.txt)
42*523fa7a6SAndroid Build Coastguard Worker  BUCK2=buck2-aarch64-apple-darwin-${BUCK2_VERSION}.zst
43*523fa7a6SAndroid Build Coastguard Worker  curl -s "https://ossci-macos.s3.amazonaws.com/${BUCK2}" -o "${BUCK2}"
44*523fa7a6SAndroid Build Coastguard Worker
45*523fa7a6SAndroid Build Coastguard Worker  zstd -d "${BUCK2}" -o buck2
46*523fa7a6SAndroid Build Coastguard Worker
47*523fa7a6SAndroid Build Coastguard Worker  chmod +x buck2
48*523fa7a6SAndroid Build Coastguard Worker  mv buck2 /opt/homebrew/bin
49*523fa7a6SAndroid Build Coastguard Worker
50*523fa7a6SAndroid Build Coastguard Worker  rm "${BUCK2}"
51*523fa7a6SAndroid Build Coastguard Worker  popd
52*523fa7a6SAndroid Build Coastguard Worker}
53*523fa7a6SAndroid Build Coastguard Worker
54*523fa7a6SAndroid Build Coastguard Workerfunction write_sccache_stub() {
55*523fa7a6SAndroid Build Coastguard Worker  OUTPUT=$1
56*523fa7a6SAndroid Build Coastguard Worker  BINARY=$(basename "${OUTPUT}")
57*523fa7a6SAndroid Build Coastguard Worker
58*523fa7a6SAndroid Build Coastguard Worker  printf "#!/bin/sh\nif [ \$(ps auxc \$(ps auxc -o ppid \$\$ | grep \$\$ | rev | cut -d' ' -f1 | rev) | tr '\\\\n' ' ' | rev | cut -d' ' -f2 | rev) != sccache ]; then\n  exec sccache %s \"\$@\"\nelse\n  exec %s \"\$@\"\nfi" "$(which "${BINARY}")" "$(which "${BINARY}")" > "${OUTPUT}"
59*523fa7a6SAndroid Build Coastguard Worker  chmod a+x "${OUTPUT}"
60*523fa7a6SAndroid Build Coastguard Worker}
61*523fa7a6SAndroid Build Coastguard Worker
62*523fa7a6SAndroid Build Coastguard Workerinstall_sccache() {
63*523fa7a6SAndroid Build Coastguard Worker  # Use existing S3 cache bucket for self-hosted MacOS runner
64*523fa7a6SAndroid Build Coastguard Worker  export SCCACHE_BUCKET=ossci-compiler-cache-circleci-v2
65*523fa7a6SAndroid Build Coastguard Worker  export SCCACHE_S3_KEY_PREFIX=executorch
66*523fa7a6SAndroid Build Coastguard Worker  export SCCACHE_IDLE_TIMEOUT=0
67*523fa7a6SAndroid Build Coastguard Worker  export SCCACHE_ERROR_LOG=/tmp/sccache_error.log
68*523fa7a6SAndroid Build Coastguard Worker  export RUST_LOG=sccache::server=error
69*523fa7a6SAndroid Build Coastguard Worker
70*523fa7a6SAndroid Build Coastguard Worker  SCCACHE_PATH="/usr/local/bin"
71*523fa7a6SAndroid Build Coastguard Worker  # NB: The function is adopted from PyTorch MacOS build workflow
72*523fa7a6SAndroid Build Coastguard Worker  # https://github.com/pytorch/pytorch/blob/main/.github/workflows/_mac-build.yml
73*523fa7a6SAndroid Build Coastguard Worker  if ! command -v sccache &> /dev/null; then
74*523fa7a6SAndroid Build Coastguard Worker    sudo curl --retry 3 "https://s3.amazonaws.com/ossci-macos/sccache/sccache-v0.4.1-${RUNNER_ARCH}" --output "${SCCACHE_PATH}/sccache"
75*523fa7a6SAndroid Build Coastguard Worker    sudo chmod +x "${SCCACHE_PATH}/sccache"
76*523fa7a6SAndroid Build Coastguard Worker  fi
77*523fa7a6SAndroid Build Coastguard Worker
78*523fa7a6SAndroid Build Coastguard Worker  export PATH="${SCCACHE_PATH}:${PATH}"
79*523fa7a6SAndroid Build Coastguard Worker
80*523fa7a6SAndroid Build Coastguard Worker  # Create temp directory for sccache shims if TMP_DIR doesn't exist
81*523fa7a6SAndroid Build Coastguard Worker  if [ -z "${TMP_DIR:-}" ]; then
82*523fa7a6SAndroid Build Coastguard Worker    TMP_DIR=$(mktemp -d)
83*523fa7a6SAndroid Build Coastguard Worker    trap 'rm -rfv ${TMP_DIR}' EXIT
84*523fa7a6SAndroid Build Coastguard Worker    export PATH="${TMP_DIR}:$PATH"
85*523fa7a6SAndroid Build Coastguard Worker  fi
86*523fa7a6SAndroid Build Coastguard Worker
87*523fa7a6SAndroid Build Coastguard Worker  write_sccache_stub "${TMP_DIR}/c++"
88*523fa7a6SAndroid Build Coastguard Worker  write_sccache_stub "${TMP_DIR}/cc"
89*523fa7a6SAndroid Build Coastguard Worker  write_sccache_stub "${TMP_DIR}/clang++"
90*523fa7a6SAndroid Build Coastguard Worker  write_sccache_stub "${TMP_DIR}/clang"
91*523fa7a6SAndroid Build Coastguard Worker
92*523fa7a6SAndroid Build Coastguard Worker  sccache --zero-stats || true
93*523fa7a6SAndroid Build Coastguard Worker}
94*523fa7a6SAndroid Build Coastguard Worker
95*523fa7a6SAndroid Build Coastguard Worker# This is the same rpath fix copied from PyTorch macos setup script
96*523fa7a6SAndroid Build Coastguard Worker# https://github.com/pytorch/pytorch/blob/main/.ci/pytorch/macos-common.sh
97*523fa7a6SAndroid Build Coastguard Workerprint_cmake_info() {
98*523fa7a6SAndroid Build Coastguard Worker  CMAKE_EXEC=$(which cmake)
99*523fa7a6SAndroid Build Coastguard Worker  echo "$CMAKE_EXEC"
100*523fa7a6SAndroid Build Coastguard Worker
101*523fa7a6SAndroid Build Coastguard Worker  export CMAKE_EXEC
102*523fa7a6SAndroid Build Coastguard Worker  # Explicitly add conda env lib folder to cmake rpath to address the flaky issue
103*523fa7a6SAndroid Build Coastguard Worker  # where cmake dependencies couldn't be found. This seems to point to how conda
104*523fa7a6SAndroid Build Coastguard Worker  # links $CMAKE_EXEC to its package cache when cloning a new environment
105*523fa7a6SAndroid Build Coastguard Worker  install_name_tool -add_rpath @executable_path/../lib "${CMAKE_EXEC}" || true
106*523fa7a6SAndroid Build Coastguard Worker  # Adding the rpath will invalidate cmake signature, so signing it again here
107*523fa7a6SAndroid Build Coastguard Worker  # to trust the executable. EXC_BAD_ACCESS (SIGKILL (Code Signature Invalid))
108*523fa7a6SAndroid Build Coastguard Worker  # with an exit code 137 otherwise
109*523fa7a6SAndroid Build Coastguard Worker  codesign -f -s - "${CMAKE_EXEC}" || true
110*523fa7a6SAndroid Build Coastguard Worker}
111*523fa7a6SAndroid Build Coastguard Worker
112*523fa7a6SAndroid Build Coastguard Workersetup_macos_env_variables() {
113*523fa7a6SAndroid Build Coastguard Worker  CMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
114*523fa7a6SAndroid Build Coastguard Worker  export CMAKE_PREFIX_PATH
115*523fa7a6SAndroid Build Coastguard Worker}
116*523fa7a6SAndroid Build Coastguard Worker
117*523fa7a6SAndroid Build Coastguard Workersetup_macos_env_variables
118*523fa7a6SAndroid Build Coastguard Worker# NB: we need buck2 in all cases because cmake build also depends on calling
119*523fa7a6SAndroid Build Coastguard Worker# buck2 atm
120*523fa7a6SAndroid Build Coastguard Workerinstall_buck
121*523fa7a6SAndroid Build Coastguard Workerinstall_pip_dependencies
122*523fa7a6SAndroid Build Coastguard Worker
123*523fa7a6SAndroid Build Coastguard Worker# TODO(huydhn): Unlike our self-hosted runner, GitHub runner doesn't have access
124*523fa7a6SAndroid Build Coastguard Worker# to our infra, so compiler caching needs to be setup differently using GitHub
125*523fa7a6SAndroid Build Coastguard Worker# cache. However, I need to figure out how to set that up for Nova MacOS job
126*523fa7a6SAndroid Build Coastguard Workerif [[ -z "${GITHUB_RUNNER:-}" ]]; then
127*523fa7a6SAndroid Build Coastguard Worker  install_sccache
128*523fa7a6SAndroid Build Coastguard Workerfi
129*523fa7a6SAndroid Build Coastguard Worker
130*523fa7a6SAndroid Build Coastguard Workerprint_cmake_info
131*523fa7a6SAndroid Build Coastguard Workerinstall_executorch
132*523fa7a6SAndroid Build Coastguard Workerbuild_executorch_runner "${BUILD_TOOL}"
133