1#!/bin/bash -e
2
3# This is a wrapper around "m" that builds the given modules in multi-arch mode
4# for all architectures supported by Mainline modules. The make (kati) stage is
5# skipped, so the build targets in the arguments can only be Soong modules or
6# intermediate output files - make targets and normal installed paths are not
7# supported.
8#
9# This script is typically used with "sdk" or "module_export" modules, which
10# Soong will install in $OUT_DIR/soong/mainline-sdks (cf
11# PathForMainlineSdksInstall in android/paths.go).
12
13export OUT_DIR=${OUT_DIR:-out}
14
15if [ -e ${OUT_DIR}/soong/.soong.in_make ]; then
16  # If ${OUT_DIR} has been created without --skip-make, Soong will create an
17  # ${OUT_DIR}/soong/build.ninja that leaves out many targets which are
18  # expected to be supplied by the .mk files, and that might cause errors in
19  # "m --skip-make" below. We therefore default to a different out dir
20  # location in that case.
21  AML_OUT_DIR=out/aml
22  echo "Avoiding in-make OUT_DIR '${OUT_DIR}' - building in '${AML_OUT_DIR}' instead"
23  OUT_DIR=${AML_OUT_DIR}
24fi
25
26if [ ! -e "build/envsetup.sh" ]; then
27  echo "$0 must be run from the top of the tree"
28  exit 1
29fi
30
31source build/envsetup.sh
32
33my_get_build_var() {
34  # get_build_var will run Soong in normal in-make mode where it creates
35  # .soong.in_make. That would clobber our real out directory, so we need to
36  # run it in a different one.
37  OUT_DIR=${OUT_DIR}/get_build_var get_build_var "$@"
38}
39
40readonly PLATFORM_SDK_VERSION="$(my_get_build_var PLATFORM_SDK_VERSION)"
41readonly PLATFORM_VERSION="$(my_get_build_var PLATFORM_VERSION)"
42PLATFORM_VERSION_ALL_CODENAMES="$(my_get_build_var PLATFORM_VERSION_ALL_CODENAMES)"
43
44# PLATFORM_VERSION_ALL_CODENAMES is a comma separated list like O,P. We need to
45# turn this into ["O","P"].
46PLATFORM_VERSION_ALL_CODENAMES="${PLATFORM_VERSION_ALL_CODENAMES/,/'","'}"
47PLATFORM_VERSION_ALL_CODENAMES="[\"${PLATFORM_VERSION_ALL_CODENAMES}\"]"
48
49# Logic from build/make/core/goma.mk
50if [ "${USE_GOMA}" = true ]; then
51  if [ -n "${GOMA_DIR}" ]; then
52    goma_dir="${GOMA_DIR}"
53  else
54    goma_dir="${HOME}/goma"
55  fi
56  GOMA_CC="${goma_dir}/gomacc"
57  export CC_WRAPPER="${CC_WRAPPER}${CC_WRAPPER:+ }${GOMA_CC}"
58  export CXX_WRAPPER="${CXX_WRAPPER}${CXX_WRAPPER:+ }${GOMA_CC}"
59  export JAVAC_WRAPPER="${JAVAC_WRAPPER}${JAVAC_WRAPPER:+ }${GOMA_CC}"
60else
61  USE_GOMA=false
62fi
63
64readonly SOONG_OUT=${OUT_DIR}/soong
65mkdir -p ${SOONG_OUT}
66readonly SOONG_VARS=${SOONG_OUT}/soong.variables
67
68# Aml_abis: true
69#   -  This flag configures Soong to compile for all architectures required for
70#      Mainline modules.
71# CrossHost: linux_bionic
72# CrossHostArch: x86_64
73#   -  Enable Bionic on host as ART needs prebuilts for it.
74cat > ${SOONG_VARS}.new << EOF
75{
76    "Platform_sdk_version": ${PLATFORM_SDK_VERSION},
77    "Platform_sdk_codename": "${PLATFORM_VERSION}",
78    "Platform_version_active_codenames": ${PLATFORM_VERSION_ALL_CODENAMES},
79
80    "DeviceName": "generic_arm64",
81    "HostArch": "x86_64",
82    "HostSecondaryArch": "x86",
83    "CrossHost": "linux_bionic",
84    "CrossHostArch": "x86_64",
85    "Aml_abis": true,
86
87    "UseGoma": ${USE_GOMA}
88}
89EOF
90
91if [ -f ${SOONG_VARS} ] && cmp -s ${SOONG_VARS} ${SOONG_VARS}.new; then
92  # Don't touch soong.variables if we don't have to, to avoid Soong rebuilding
93  # the ninja file when it isn't necessary.
94  rm ${SOONG_VARS}.new
95else
96  mv ${SOONG_VARS}.new ${SOONG_VARS}
97fi
98
99# We use force building LLVM components flag (even though we actually don't
100# compile them) because we don't have bionic host prebuilts
101# for them.
102export FORCE_BUILD_LLVM_COMPONENTS=true
103
104m --skip-make "$@"
105