1#!/bin/bash -ex
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18function init() {
19  declare -ga ARGV
20  while (($# > 0)); do
21    case $1 in
22    --py3script)
23      declare -gr py3script="$2"
24      shift 2
25      ;;
26    *)
27      ARGV+=("$1")
28      shift 1
29      ;;
30    esac
31  done
32  readonly ARGV
33
34  if [ -z "${py3script}" ]; then
35    declare -gr py3script="packages/modules/common/build/mainline_modules_sdks.py"
36  fi
37}
38
39function main() {
40  if [ ! -e "build/make/core/Makefile" ]; then
41    echo "$0 must be run from the top of the tree"
42    exit 1
43  fi
44
45  # Assign to a variable and eval that, since bash ignores any error status from
46  # the command substitution if it's directly on the eval line.
47  vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \
48    --vars="DIST_DIR OUT_DIR")"
49  eval "${vars}"
50
51  # Building with --soong-only and module products requires build_number.txt for
52  # some targets.
53  # Command to populate {OUT_DIR}/soong/build_number.txt.
54  build/soong/soong_ui.bash --make-mode nothing
55
56  # Delegate the SDK generation to the python script. Use the python version
57  # provided by the build to ensure consistency across build environments.
58  export DIST_DIR OUT_DIR
59
60  # Make sure that the ANDROID_BUILD_TOP (which is the same as the current
61  # directory as checked above) is exported to Python.
62  export ANDROID_BUILD_TOP="${PWD}"
63
64  # The path to this tool is the .sh script that lives alongside the .py script.
65  TOOL_PATH="${py3script%.py}.sh"
66  prebuilts/build-tools/linux-x86/bin/py3-cmd -u "${py3script}" \
67      --tool-path "${TOOL_PATH}" \
68      "$@"
69}
70
71init "$@"
72# The wacky ${foo[@]+"${foo[@]}"}, makes bash correctly pass nothing when an
73# array is empty (necessary prior to bash 4.4).
74main ${ARGV[@]+"${ARGV[@]}"}
75