#/bin/bash

# Copyright 2024, 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.

set -x

source build/make/shell_utils.sh

export TARGET_PRODUCT=aosp_arm64
export TARGET_RELEASE=trunk_staging
export TARGET_BUILD_VARIANT=eng

import_build_vars \
        OUT_DIR \
        DIST_DIR \
        HOST_OUT_EXECUTABLES \
    || exit $?

TARGETS=(
    all_teams
    source_tree_size
    release_config_metadata
)

# Build modules
build/soong/bin/m dist ${TARGETS[@]} || exit $?

# Generate all of the release configs for all of the named products.
(
  PATH=$(gettop)/build/soong/bin:${PATH}
  args=(--dist --intermediate-dir=${OUT_DIR}/soong/.intermediates/build/soong/scripts/product-configs --json --textproto)
  if [[ -r vendor/google/build/broken_products_allowlist.txt ]]; then
      args+=(--skip-file vendor/google/build/broken_products_allowlist.txt)
  fi
  if [[ -r vendor/google/build/broken_projects_rw_dirs.txt ]]; then
      args+=(--rw-dirs-file vendor/google/build/broken_projects_rw_dirs.txt)
  fi
  ./build/soong/scripts/product-configs ${args[@]}
) || exit $?

# List all source files in the tree
(
    $HOST_OUT_EXECUTABLES/source_tree_size -o $DIST_DIR/all_source_tree_files.pb \
        && gzip -fn $DIST_DIR/all_source_tree_files.pb \
) || exit $?
