1#
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  # getoutdir ensures paths are absolute. envsetup could be called from a
25  # directory other than the root of the source tree
26  local outdir=$(getoutdir)
27  if [[ -L "${outdir}" ]]; then
28    return
29  fi
30  if [ -d "${outdir}" ]; then
31    echo -e "\tOutput directory ${outdir} cannot be present in a Cog workspace."
32    echo -e "\tDelete \"${outdir}\" or create a symlink from \"${outdir}\" to a directory outside your workspace."
33    return 1
34  fi
35
36  DEFAULT_OUTPUT_DIR="${HOME}/.cog/android-build-out"
37  mkdir -p ${DEFAULT_OUTPUT_DIR}
38  ln -s ${DEFAULT_OUTPUT_DIR} ${outdir}
39}
40
41# This function sets up the build environment to be appropriate for Cog.
42function _setup_cog_env() {
43  _create_out_symlink_for_cog
44  if [ "$?" -eq "1" ]; then
45    echo -e "\e[0;33mWARNING:\e[00m Cog environment setup failed!"
46    return 1
47  fi
48
49  export ANDROID_BUILD_ENVIRONMENT_CONFIG="googler-cog"
50
51  # Running repo command within Cog workspaces is not supported, so override
52  # it with this function. If the user is running repo within a Cog workspace,
53  # we'll fail with an error, otherwise, we run the original repo command with
54  # the given args.
55  if ! ORIG_REPO_PATH=`which repo`; then
56    return 0
57  fi
58  function repo {
59    if [[ "${PWD}" == /google/cog/* ]]; then
60      echo "\e[01;31mERROR:\e[0mrepo command is disallowed within Cog workspaces."
61      return 1
62    fi
63    ${ORIG_REPO_PATH} "$@"
64  }
65}
66
67if [[ "${PWD}" != /google/cog/* ]]; then
68  echo -e "\e[01;31mERROR:\e[0m This script must be run from a Cog workspace."
69fi
70
71_setup_cog_env
72