blob: 02fcc8fec169ed5abee87c62e1e1d7476707fd9b [file] [log] [blame]
Kousik Kumarec5416c2023-09-14 17:11:45 +00001#
2# Copyright (C) 2023 The Android Open Source Project
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
17# This file is executed by build/envsetup.sh, and can use anything
18# defined in envsetup.sh.
19function _create_out_symlink_for_cog() {
20 if [[ "${OUT_DIR}" == "" ]]; then
21 OUT_DIR="out"
22 fi
23
24 if [[ -L "${OUT_DIR}" ]]; then
25 return
26 fi
27 if [ -d "${OUT_DIR}" ]; then
28 echo -e "\tOutput directory ${OUT_DIR} cannot be present in a Cog workspace."
29 echo -e "\tDelete \"${OUT_DIR}\" or create a symlink from \"${OUT_DIR}\" to a directory outside your workspace."
30 return 1
31 fi
32
33 DEFAULT_OUTPUT_DIR="${HOME}/.cog/android-build-out"
34 mkdir -p ${DEFAULT_OUTPUT_DIR}
35 ln -s ${DEFAULT_OUTPUT_DIR} `pwd`/out
36}
37
38# This function moves the reclient binaries into a directory that exists in a
39# non-cog part of the overall filesystem. This is to workaround the problem
40# described in b/289391270.
41function _copy_reclient_binaries_from_cog() {
42 local NONCOG_RECLIENT_BIN_DIR="${HOME}/.cog/reclient/bin"
43 if [ ! -d "$NONCOG_RECLIENT_BIN_DIR" ]; then
44 # Create the non cog directory if it doesn't exist.
45 mkdir -p ${NONCOG_RECLIENT_BIN_DIR}
46 else
47 # Clear out the non cog directory if it does exist.
48 rm -f ${NONCOG_RECLIENT_BIN_DIR}/*
49 fi
50
51 local TOP=$(gettop)
52
53 # Copy the binaries out of live.
54 cp $TOP/prebuilts/remoteexecution-client/live/* $NONCOG_RECLIENT_BIN_DIR
55
56 # Finally set the RBE_DIR env var to point to the out-of-cog directory.
57 export RBE_DIR=$NONCOG_RECLIENT_BIN_DIR
58}
59
60# This function sets up the build environment to be appropriate for Cog.
61function _setup_cog_env() {
62 _create_out_symlink_for_cog
63 if [ "$?" -eq "1" ]; then
64 echo -e "\e[0;33mWARNING:\e[00m Cog environment setup failed!"
65 return 1
66 fi
67 _copy_reclient_binaries_from_cog
68
69 export ANDROID_BUILD_ENVIRONMENT_CONFIG="googler-cog"
70
71 # Running repo command within Cog workspaces is not supported, so override
72 # it with this function. If the user is running repo within a Cog workspace,
73 # we'll fail with an error, otherwise, we run the original repo command with
74 # the given args.
75 ORIG_REPO_PATH=`which repo`
76 function repo {
77 if [[ "${PWD}" == /google/cog/* ]]; then
78 echo "\e[01;31mERROR:\e[0mrepo command is disallowed within Cog workspaces."
79 return 1
80 fi
81 ${ORIG_REPO_PATH} "$@"
82 }
83}
84
85if [[ "${PWD}" != /google/cog/* ]]; then
86 echo -e "\e[01;31mERROR:\e[0m This script must be run from a Cog workspace."
87fi
88
89_setup_cog_env