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 usage() {
19  cat <<END_OF_USAGE
20This script builds mainline modules. It is used from other build scripts that
21are run on build servers, and is meant to build both AOSP and internal
22variants of the modules.
23
24Basic usage:
25  \$ packages/modules/common/build/build_unbundled_mainline_module.sh \
26      --dist_dir out/dist/mainline_modules_arm64 \
27      --product module_arm64 \
28      -j8
29
30Arguments:
31   --dist_dir <dir>    a dist directory to store the outputs in.
32   --product <product> a target product to use when building.
33   \$@ all other arguments are passed through to soong_ui.bash verbatim.
34END_OF_USAGE
35}
36
37# List of AOSP modules to build if TARGET_BUILD_APPS is not set.
38readonly -a DEFAULT_MODULES=(
39  com.android.adbd
40  com.android.art
41  com.android.art.debug
42  com.android.art.testing
43  com.android.cellbroadcast
44  com.android.conscrypt
45  com.android.extservices
46  com.android.i18n
47  # TODO(b/210694291): include ipsec module in the build
48  # com.android.ipsec
49  com.android.media
50  com.android.mediaprovider
51  com.android.media.swcodec
52  com.android.neuralnetworks
53  # com.android.os.statsd
54  com.android.permission
55  com.android.resolv
56  com.android.runtime
57  com.android.sdkext
58  # TODO(b/210694291): include tethering module in the build
59  # com.android.tethering
60  com.android.tzdata
61  com.android.wifi
62  test1_com.android.tzdata
63  test_com.android.conscrypt
64  test_com.android.media
65  test_com.android.media.swcodec
66  CaptivePortalLogin
67  DocumentsUI
68  ExtServices-tplus
69  NetworkStack
70  NetworkStackNext
71  PermissionController
72)
73
74# Initializes and parses the command line arguments and environment variables.
75#
76# Do not rely on environment global variables for DIST_DIT and PRODUCT, since
77# the script expects specific values for those, instead of anything that could
78# have been lunch'ed in the terminal.
79function init() {
80  declare -ga ARGV
81  while (($# > 0)); do
82    case $1 in
83    --dist_dir)
84      local -r dist_dir="$2"
85      shift 2
86      ;;
87    --product)
88      local -r product="$2"
89      shift 2
90      ;;
91    --help)
92      usage
93      exit
94      ;;
95    *)
96      ARGV+=("$1")
97      shift 1
98      ;;
99    esac
100  done
101  readonly ARGV
102
103  if [ -z "${dist_dir}" ]; then
104    echo "Expected --dist_dir arg is not provided."
105    exit 1
106  fi
107  if [ -z "${product}" ]; then
108    echo "Expected --product arg is not provided."
109    exit 1
110  fi
111
112  DIST_DIR="${dist_dir}"
113  declare -grx TARGET_BUILD_APPS="${TARGET_BUILD_APPS:-${DEFAULT_MODULES[*]}}"
114  declare -grx TARGET_BUILD_DENSITY="${TARGET_BUILD_DENSITY:-alldpi}"
115  declare -grx TARGET_BUILD_TYPE="${TARGET_BUILD_TYPE:-release}"
116  declare -grx TARGET_BUILD_VARIANT="${TARGET_BUILD_VARIANT:-user}"
117  declare -grx TARGET_PRODUCT="${product}"
118  declare -grx BUILD_PRE_S_APEX="${BUILD_PRE_S_APEX:-false}"
119
120  # This script cannot handle compressed apexes
121  declare -grx OVERRIDE_PRODUCT_COMPRESSED_APEX=false
122
123  # UNBUNDLED_BUILD_SDKS_FROM_SOURCE defaults to false, which is necessary to
124  # use prebuilt SDKs on thin branches that may not have the sources (e.g.
125  # frameworks/base).
126}
127
128function build_modules() {
129
130  build/soong/soong_ui.bash --make-mode "$@" \
131    ALWAYS_EMBED_NOTICES=true \
132    MODULE_BUILD_FROM_SOURCE=true \
133    ${extra_build_params} \
134    "${RUN_ERROR_PRONE:+"RUN_ERROR_PRONE=true"}" \
135    "${CHECK_API:+"checkapi"}" \
136    apps_only \
137    dist \
138    lint-check
139}
140
141function main() {
142  if [ ! -e "build/make/core/Makefile" ]; then
143    echo "$0 must be run from the top of the Android source tree."
144    exit 1
145  fi
146
147  # Run installclean to remove previous artifacts, so they don't accumulate on
148  # the buildbots.
149  build/soong/soong_ui.bash --make-mode installclean
150
151  DIST_DIR="${DIST_DIR}" build_modules
152}
153
154init "$@"
155# The wacky ${foo[@]+"${foo[@]}"}, makes bash correctly pass nothing when an
156# array is empty (necessary prior to bash 4.4).
157main ${ARGV[@]+"${ARGV[@]}"}
158