1#!/bin/bash 2# Copyright (c) Meta Platforms, Inc. and affiliates. 3# All rights reserved. 4# 5# This source code is licensed under the BSD-style license found in the 6# LICENSE file in the root directory of this source tree. 7 8# NB: This script is adopted from PyTorch core repo at 9# https://github.com/pytorch/pytorch/blob/main/.ci/docker/common/install_cache.sh 10set -ex 11 12# shellcheck source=/dev/null 13source "$(dirname "${BASH_SOURCE[0]}")/utils.sh" 14 15install_ubuntu() { 16 echo "Preparing to build sccache from source" 17 apt-get update 18 # libssl-dev will not work as it is upgraded to libssl3 in Ubuntu-22.04. 19 # Instead use lib and headers from OpenSSL1.1 installed in `install_openssl.sh`` 20 apt-get install -y cargo 21 echo "Checking out sccache repo" 22 git clone https://github.com/mozilla/sccache -b v0.8.2 23 24 cd sccache 25 echo "Building sccache" 26 cargo build --release 27 cp target/release/sccache /opt/cache/bin 28 echo "Cleaning up" 29 cd .. 30 rm -rf sccache 31 apt-get remove -y cargo rustc 32 apt-get autoclean && apt-get clean 33} 34 35install_binary() { 36 echo "Downloading sccache binary from S3 repo" 37 curl --retry 3 https://s3.amazonaws.com/ossci-linux/sccache -o /opt/cache/bin/sccache 38 chmod +x /opt/cache/bin/sccache 39} 40 41mkdir -p /opt/cache/bin 42sed -e 's|PATH="\(.*\)"|PATH="/opt/cache/bin:\1"|g' -i /etc/environment 43export PATH="/opt/cache/bin:$PATH" 44 45install_ubuntu 46 47function write_sccache_stub() { 48 BINARY=$1 49 if [ $1 == "gcc" ]; then 50 # Do not call sccache recursively when dumping preprocessor argument 51 # For some reason it's very important for the first cached nvcc invocation 52 cat >"/opt/cache/bin/$1" <<EOF 53#!/bin/sh 54if [ "\$1" = "-E" ] || [ "\$2" = "-E" ]; then 55 exec $(which $1) "\$@" 56elif [ \$(env -u LD_PRELOAD ps -p \$PPID -o comm=) != sccache ]; then 57 exec sccache $(which $1) "\$@" 58else 59 exec $(which $1) "\$@" 60fi 61EOF 62 else 63 cat >"/opt/cache/bin/$1" <<EOF 64#!/bin/sh 65if [ \$(env -u LD_PRELOAD ps -p \$PPID -o comm=) != sccache ]; then 66 exec sccache $(which $1) "\$@" 67else 68 exec $(which $1) "\$@" 69fi 70EOF 71 fi 72 chmod a+x "/opt/cache/bin/${BINARY}" 73} 74 75init_sccache() { 76 # This is the remote cache bucket 77 export SCCACHE_BUCKET=ossci-compiler-cache-circleci-v2 78 export SCCACHE_S3_KEY_PREFIX=executorch 79 export SCCACHE_IDLE_TIMEOUT=0 80 export SCCACHE_ERROR_LOG=/tmp/sccache_error.log 81 export RUST_LOG=sccache::server=error 82 83 # NB: This function is adopted from PyTorch core at 84 # https://github.com/pytorch/pytorch/blob/main/.ci/pytorch/common-build.sh 85 as_ci_user sccache --stop-server >/dev/null 2>&1 || true 86 rm -f "${SCCACHE_ERROR_LOG}" || true 87 88 # Clear sccache stats before using it 89 as_ci_user sccache --zero-stats || true 90} 91 92write_sccache_stub cc 93write_sccache_stub c++ 94write_sccache_stub gcc 95write_sccache_stub g++ 96write_sccache_stub clang 97write_sccache_stub clang++ 98init_sccache 99