• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15function gettop
16{
17    local TOPFILE=build/make/core/envsetup.mk
18    # The ${TOP-} expansion allows this to work even with set -u
19    if [ -n "${TOP:-}" -a -f "${TOP:-}/$TOPFILE" ] ; then
20        # The following circumlocution ensures we remove symlinks from TOP.
21        (cd "$TOP"; PWD= /bin/pwd)
22    else
23        if [ -f $TOPFILE ] ; then
24            # The following circumlocution (repeated below as well) ensures
25            # that we record the true directory name and not one that is
26            # faked up with symlink names.
27            PWD= /bin/pwd
28        else
29            local HERE=$PWD
30            local T=
31            while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
32                \cd ..
33                T=`PWD= /bin/pwd -P`
34            done
35            \cd "$HERE"
36            if [ -f "$T/$TOPFILE" ]; then
37                echo "$T"
38            fi
39        fi
40    fi
41}
42
43# Sets TOP, or if the root of the tree can't be found, prints a message and
44# exits.  Since this function exits, it should not be called from functions
45# defined in envsetup.sh.
46if [ -z "${IMPORTING_ENVSETUP:-}" ] ; then
47function require_top
48{
49    TOP=$(gettop)
50    if [[ ! $TOP ]] ; then
51        echo "Can not locate root of source tree. $(basename $0) must be run from within the Android source tree." >&2
52        exit 1
53    fi
54}
55fi
56
57function getoutdir
58{
59    local top=$(gettop)
60    local out_dir="${OUT_DIR:-}"
61    if [[ -z "${out_dir}" ]]; then
62        if [[ -n "${OUT_DIR_COMMON_BASE:-}" && -n "${top}" ]]; then
63            out_dir="${OUT_DIR_COMMON_BASE}/$(basename ${top})"
64        else
65            out_dir="out"
66        fi
67    fi
68    if [[ "${out_dir}" != /* ]]; then
69        out_dir="${top}/${out_dir}"
70    fi
71    echo "${out_dir}"
72}
73
74
75