1#!/bin/bash
2# Copyright 2022 The gRPC Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Source this rc script to create symlinks to ccache
17
18if [ "${GRPC_BUILD_ENABLE_CCACHE}" != "" ] && [ "${GRPC_BUILD_ENABLE_CCACHE}" != "false" ] && [ "${GRPC_BUILD_ENABLE_CCACHE}" != "0" ]
19then
20  if [ -x "$(command -v ccache)" ]
21  then
22    SUPPORTED_COMPILERS=(
23      # common compiler binaries
24      "gcc"
25      "g++"
26      "clang"
27      "clang++"
28      "cc"
29      "c++"
30      # ruby artifacts: rake-compiler-dock crosscompilers
31      # TODO(jtattermusch): ensure that the list of ruby crosscompilers stays up to date.
32      "x86_64-redhat-linux-gcc"
33      "x86_64-redhat-linux-g++"
34      "i686-redhat-linux-gcc"
35      "i686-redhat-linux-g++"
36      "x86_64-w64-mingw32-gcc"
37      "x86_64-w64-mingw32-g++"
38      "i686-w64-mingw32-gcc"
39      "i686-w64-mingw32-g++"
40      "x86_64-apple-darwin-clang"
41      "x86_64-apple-darwin-clang++"
42      "aarch64-apple-darwin-clang"
43      "aarch64-apple-darwin-clang++"
44      # python artifacts: dockcross crosscompilers
45      "aarch64-unknown-linux-gnueabi-gcc"
46      "aarch64-unknown-linux-gnueabi-g++"
47      "armv7-unknown-linux-gnueabi-gcc"
48      "armv7-unknown-linux-gnueabi-g++"
49    )
50    CCACHE_BINARY_PATH="$(command -v ccache)"
51    TEMP_CCACHE_BINDIR="$(mktemp -d)"
52
53    for compiler in "${SUPPORTED_COMPILERS[@]}"
54    do
55      # create a symlink pointing to ccache if compiler binary exists
56      if [ -x "$(command -v $compiler)" ]
57      then
58        ln -s "${CCACHE_BINARY_PATH}" "${TEMP_CCACHE_BINDIR}/${compiler}"
59        echo "Creating symlink $compiler pointing to ${CCACHE_BINARY_PATH}"
60      fi
61    done
62    echo "Adding ${TEMP_CCACHE_BINDIR} to PATH"
63    export PATH="${TEMP_CCACHE_BINDIR}:$PATH"
64  fi
65fi
66