Merge "Add modules-utils-preconditions to allowed_deps."
diff --git a/MODULES_OWNERS b/MODULES_OWNERS
index fbf7d26..1db0ed0 100644
--- a/MODULES_OWNERS
+++ b/MODULES_OWNERS
@@ -3,7 +3,7 @@
 # classes of change within mainline modules and their build rules.
 #
 # It should be included in a module OWNERS file via:
-# include packages/modules/common:/MODULES_OWNERS
+# include platform/packages/modules/common:/MODULES_OWNERS
 #
 # See go/mainline-owners-policy for more details.
 
diff --git a/PREBUILTS_MODULE_OWNERS b/PREBUILTS_MODULE_OWNERS
new file mode 100644
index 0000000..5a2efe3
--- /dev/null
+++ b/PREBUILTS_MODULE_OWNERS
@@ -0,0 +1,14 @@
+# This file is intended to be included in the OWNERS file for mainline modules
+# prebuilts.  The goal is to allow the mainline modularization and release team
+# to update the prebuilts in bulk and on a regular basis without being blocked
+# on approvals.  classes of change within mainline modules and their build
+# rules.
+#
+# It should be included in a prebuilt module OWNERS file via:
+# include platform/packages/modules/common:/PREBUILTS_MODULES_OWNERS
+#
+# See go/mainline-owners-policy for more details.
+
+include platform/packages/modules/common:/MODULES_OWNERS
+aseaton@google.com #{LAST_RESORT_SUGGESTION}
+dariofreni@google.com #{LAST_RESORT_SUGGESTION}
diff --git a/build/build_unbundled_mainline_module.sh b/build/build_unbundled_mainline_module.sh
new file mode 100755
index 0000000..3c7d78b
--- /dev/null
+++ b/build/build_unbundled_mainline_module.sh
@@ -0,0 +1,147 @@
+#!/bin/bash -ex
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+function usage() {
+  cat <<END_OF_USAGE
+This script builds mainline modules. It is used from other build scripts that
+are run on build servers, and is meant to build both AOSP and internal
+variants of the modules.
+
+Basic usage:
+  \$ packages/modules/common/build/build_unbundled_mainline_module.sh \
+      --dist_dir out/dist/mainline_modules_arm64 \
+      --product module_arm64 \
+      -j8
+
+Arguments:
+   --dist_dir <dir>    a dist directory to store the outputs in.
+   --product <product> a target product to use when building.
+   \$@ all other arguments are passed through to soong_ui.bash verbatim.
+END_OF_USAGE
+}
+
+# List of AOSP modules to build if TARGET_BUILD_APPS is not set.
+readonly -a DEFAULT_MODULES=(
+  com.android.adbd
+  com.android.art
+  com.android.art.debug
+  com.android.art.testing
+  com.android.cellbroadcast
+  com.android.conscrypt
+  com.android.extservices
+  com.android.i18n
+  com.android.ipsec
+  com.android.media
+  com.android.mediaprovider
+  com.android.media.swcodec
+  com.android.neuralnetworks
+  # com.android.os.statsd
+  com.android.permission
+  com.android.resolv
+  com.android.runtime
+  com.android.sdkext
+  com.android.telephony
+  com.android.tethering
+  com.android.tzdata
+  com.android.wifi
+  test1_com.android.tzdata
+  test_com.android.conscrypt
+  test_com.android.media
+  test_com.android.media.swcodec
+  CaptivePortalLogin
+  DocumentsUI
+  ExtServices
+  NetworkPermissionConfig
+  NetworkStack
+  NetworkStackNext
+  PermissionController
+)
+
+# Initializes and parses the command line arguments and environment variables.
+#
+# Do not rely on environment global variables for DIST_DIT and PRODUCT, since
+# the script expects specific values for those, instead of anything that could
+# have been lunch'ed in the terminal.
+function init() {
+  declare -ga ARGV
+  while (($# > 0)); do
+    case $1 in
+    --dist_dir)
+      local -r dist_dir="$2"
+      shift 2
+      ;;
+    --product)
+      local -r product="$2"
+      shift 2
+      ;;
+    --help)
+      usage
+      exit
+      ;;
+    *)
+      ARGV+=("$1")
+      shift 1
+      ;;
+    esac
+  done
+  readonly ARGV
+
+  if [ -z "${dist_dir}" ]; then
+    echo "Expected --dist_dir arg is not provided."
+    exit 1
+  fi
+  if [ -z "${product}" ]; then
+    echo "Expected --product arg is not provided."
+    exit 1
+  fi
+
+  declare -grx DIST_DIR="${dist_dir}"
+  declare -grx TARGET_BUILD_APPS="${TARGET_BUILD_APPS:-${DEFAULT_MODULES[*]}}"
+  declare -grx TARGET_BUILD_DENSITY="${TARGET_BUILD_DENSITY:-alldpi}"
+  declare -grx TARGET_BUILD_TYPE="${TARGET_BUILD_TYPE:-release}"
+  declare -grx TARGET_BUILD_VARIANT="${TARGET_BUILD_VARIANT:-user}"
+  declare -grx TARGET_PRODUCT="${product}"
+
+  # This script cannot handle compressed apexes
+  declare -grx OVERRIDE_PRODUCT_COMPRESSED_APEX=false
+  # Unset to build using PreBuilt SDK.
+  declare -grx UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true
+}
+
+function main() {
+  if [ ! -e "build/make/core/Makefile" ]; then
+    echo "$0 must be run from the top of the Android source tree."
+    exit 1
+  fi
+
+  # Run installclean to remove previous artifacts, so they don't accumulate on
+  # the buildbots.
+  build/soong/soong_ui.bash --make-mode installclean
+
+  build/soong/soong_ui.bash --make-mode "$@" \
+    ALWAYS_EMBED_NOTICES=true \
+    MODULE_BUILD_FROM_SOURCE=true \
+    "${RUN_ERROR_PRONE:+"RUN_ERROR_PRONE=true"}" \
+    apps_only \
+    dist \
+    lint-check
+}
+
+init "$@"
+# The wacky ${foo[@]+"${foo[@]}"}, makes bash correctly pass nothing when an
+# array is empty (necessary prior to bash 4.4).
+main ${ARGV[@]+"${ARGV[@]}"}
diff --git a/build/mainline_modules_arm.sh b/build/mainline_modules_arm.sh
new file mode 100644
index 0000000..a7ba6eb
--- /dev/null
+++ b/build/mainline_modules_arm.sh
@@ -0,0 +1,26 @@
+#!/bin/bash -ex
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Assign to a variable and eval that, since bash ignores any error status from
+# the command substitution if it's directly on the eval line.
+readonly vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \
+  --vars="DIST_DIR")"
+eval "${vars}"
+
+packages/modules/common/build/build_unbundled_mainline_module.sh \
+  --product module_arm \
+  --dist_dir "${DIST_DIR}/mainline_modules_arm"
diff --git a/build/mainline_modules_arm64.sh b/build/mainline_modules_arm64.sh
new file mode 100644
index 0000000..34115ab
--- /dev/null
+++ b/build/mainline_modules_arm64.sh
@@ -0,0 +1,26 @@
+#!/bin/bash -ex
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Assign to a variable and eval that, since bash ignores any error status from
+# the command substitution if it's directly on the eval line.
+readonly vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \
+  --vars="DIST_DIR")"
+eval "${vars}"
+
+packages/modules/common/build/build_unbundled_mainline_module.sh \
+  --product module_arm64 \
+  --dist_dir "${DIST_DIR}/mainline_modules_arm64"
diff --git a/build/mainline_modules_x86.sh b/build/mainline_modules_x86.sh
new file mode 100755
index 0000000..5c7847d
--- /dev/null
+++ b/build/mainline_modules_x86.sh
@@ -0,0 +1,26 @@
+#!/bin/bash -ex
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Assign to a variable and eval that, since bash ignores any error status from
+# the command substitution if it's directly on the eval line.
+readonly vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \
+  --vars="DIST_DIR")"
+eval "${vars}"
+
+packages/modules/common/build/build_unbundled_mainline_module.sh \
+  --product module_x86 \
+  --dist_dir "${DIST_DIR}/mainline_modules_x86"
diff --git a/build/mainline_modules_x86_64.sh b/build/mainline_modules_x86_64.sh
new file mode 100755
index 0000000..ade992b
--- /dev/null
+++ b/build/mainline_modules_x86_64.sh
@@ -0,0 +1,26 @@
+#!/bin/bash -ex
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Assign to a variable and eval that, since bash ignores any error status from
+# the command substitution if it's directly on the eval line.
+readonly vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \
+  --vars="DIST_DIR")"
+eval "${vars}"
+
+packages/modules/common/build/build_unbundled_mainline_module.sh \
+  --product module_x86_64 \
+  --dist_dir "${DIST_DIR}/mainline_modules_x86_64"