#!/usr/bin/env bash
# ===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===----------------------------------------------------------------------===##

set -e
set -o pipefail
unset LANG
unset LC_ALL
unset LC_COLLATE

PROGNAME="$(basename "${0}")"

function usage() {
cat <<EOF
Usage:
${PROGNAME} [options] <BUILDER>

[-h|--help]         Display this help and exit.

--llvm-root <DIR>   Path to the root of the LLVM monorepo. By default, we try
                    to figure it out based on the current working directory.

--build-dir <DIR>   The directory to use for building the library. By default,
                    this is '<llvm-root>/build/<builder>'.

Environment variables
CC                  The C compiler to use, this value is used by CMake. This
                    variable is mandatory.

CXX                 The C++ compiler to use, this value is used by CMake. This
                    variable is mandatory.

CCACHE              The ccache binary to use. This variable is optional and is only
                    used by the bootstrapping build.
EOF
}

function step() {
  endstep
  set +x
  if [[ ! -z ${GITHUB_ACTIONS+x} ]]; then
    echo "::group::$1"
    export IN_GROUP=1
  else
    echo "--- $1"
  fi
  set -x
}

function endstep() {
  set +x
  if [[ ! -z ${GITHUB_ACTIONS+x} ]] && [[ ! -z ${IN_GROUP+x} ]]; then
    echo "::endgroup::"
    unset IN_GROUP
  fi
  set -x
}

function error() {
    echo "::error::$1"
}

if [[ $# == 0 ]]; then
   usage
   exit 0
fi

while [[ $# -gt 0 ]]; do
    case ${1} in
        -h|--help)
            usage
            exit 0
            ;;
        --llvm-root)
            MONOREPO_ROOT="${2}"
            shift; shift
            ;;
        --build-dir)
            BUILD_DIR="${2}"
            shift; shift
            ;;
        *)
            BUILDER="${1}"
            shift
            ;;
    esac
done

MONOREPO_ROOT="${MONOREPO_ROOT:="$(git rev-parse --show-toplevel)"}"
BUILD_DIR="${BUILD_DIR:=${MONOREPO_ROOT}/build/${BUILDER}}"
INSTALL_DIR="${BUILD_DIR}/install"

if [ -z ${CC+x} ]; then
    error "Environment variable CC must be defined"
    exit 1
fi

if [ -z ${CXX+x} ]; then
    error "Environment variable CXX must be defined"
    exit 1
fi

# Print the version of a few tools to aid diagnostics in some cases
step "Diagnose tools in use"
cmake --version
ninja --version
${CC} --version
${CXX} --version

function clean() {
    rm -rf "${BUILD_DIR}"
}

function generate-cmake-base() {
    step "Generating CMake"

    # We can remove -DCMAKE_INSTALL_MESSAGE=NEVER once https://gitlab.kitware.com/cmake/cmake/-/issues/26085 is fixed.
    cmake \
          -S "${MONOREPO_ROOT}/runtimes" \
          -B "${BUILD_DIR}" \
          -GNinja \
          -DCMAKE_BUILD_TYPE=RelWithDebInfo \
          -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
          -DLIBCXX_ENABLE_WERROR=YES \
          -DLIBCXXABI_ENABLE_WERROR=YES \
          -DLIBUNWIND_ENABLE_WERROR=YES \
          -DCMAKE_INSTALL_MESSAGE=NEVER \
          -DLLVM_LIT_ARGS="-sv --xunit-xml-output test-results.xml --timeout=1500 --time-tests" \
          "${@}"
}

function generate-cmake() {
    generate-cmake-base \
          -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
          -DLIBCXX_CXX_ABI=libcxxabi \
          -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
          "${@}"
}

function generate-cmake-libcxx-win() {
    generate-cmake-base -DLLVM_ENABLE_RUNTIMES="libcxx" "${@}"
}

function generate-cmake-android() {
    generate-cmake-base \
          -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
          -DLIBCXX_CXX_ABI=libcxxabi \
          "${@}"
}

function check-runtimes() {
    step "Building libc++ test dependencies"
    ninja -vC "${BUILD_DIR}" cxx-test-depends

    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx

    step "Running the libc++abi tests"
    ninja -vC "${BUILD_DIR}" check-cxxabi

    step "Running the libunwind tests"
    ninja -vC "${BUILD_DIR}" check-unwind
}

# TODO: The goal is to test this against all configurations. We should also move
#       this to the Lit test suite instead of being a separate CMake target.
function check-abi-list() {
    step "Running the libc++ ABI list test"
    ninja -vC "${BUILD_DIR}" check-cxx-abilist || (
        error "Generating the libc++ ABI list after failed check"
        ninja -vC "${BUILD_DIR}" generate-cxx-abilist
        false
    )
}

function test-armv7m-picolibc() {
    clean

    # To make it easier to get this builder up and running, build picolibc
    # from scratch. Anecdotally, the build-picolibc script takes about 16 seconds.
    # This could be optimised by building picolibc into the Docker container.
    step "Building picolibc from source"
    ${MONOREPO_ROOT}/libcxx/utils/ci/build-picolibc.sh \
        --build-dir "${BUILD_DIR}" \
        --install-dir "${INSTALL_DIR}" \
        --target armv7m-none-eabi

    step "Generating CMake for compiler-rt"
    flags="--sysroot=${INSTALL_DIR}"
    # LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON means that we produce a file
    # libclang_rt.builtins.a that will be installed to
    # ${INSTALL_DIR}/lib/armv7m-unknown-none-eabi/.
    # With LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF, the filename includes the
    # architecture name, which is not what Clang's driver expects to find.
    # The install location will however be wrong with
    # LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON, so we correct that below.
    cmake \
        -S "${MONOREPO_ROOT}/compiler-rt" \
        -B "${BUILD_DIR}/compiler-rt" \
        -GNinja \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
        -DCMAKE_C_FLAGS="${flags}" \
        -DCMAKE_CXX_FLAGS="${flags}" \
        -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
        "${@}"

    step "Generating CMake for libc++"
    generate-cmake \
        -DLIBCXX_TEST_CONFIG="armv7m-picolibc-libc++.cfg.in" \
        -DLIBCXXABI_TEST_CONFIG="armv7m-picolibc-libc++abi.cfg.in" \
        -DLIBUNWIND_TEST_CONFIG="armv7m-picolibc-libunwind.cfg.in" \
        -DCMAKE_C_FLAGS="${flags}" \
        -DCMAKE_CXX_FLAGS="${flags}" \
        -DRUNTIMES_USE_LIBC=picolibc \
        "${@}"

    step "Installing compiler-rt"
    ninja -vC "${BUILD_DIR}/compiler-rt" install
    # Move compiler-rt libs into the same directory as all the picolib objects.
    mv "${INSTALL_DIR}/lib/armv7m-unknown-none-eabi"/* "${INSTALL_DIR}/lib"

    check-runtimes
}

case "${BUILDER}" in
check-generated-output)
    # `! foo` doesn't work properly with `set -e`, use `! foo || false` instead.
    # https://stackoverflow.com/questions/57681955/set-e-does-not-respect-logical-not
    clean
    generate-cmake

    # Reject patches that forgot to re-run the generator scripts.
    step "Making sure the generator scripts were run"
    set +x # Printing all the commands below just creates extremely confusing output
    ninja -vC "${BUILD_DIR}" libcxx-generate-files
    git diff | tee ${BUILD_DIR}/generated_output.patch
    git ls-files -o --exclude-standard | tee ${BUILD_DIR}/generated_output.status
    ! grep -q '^--- a' ${BUILD_DIR}/generated_output.patch || false
    if [ -s ${BUILD_DIR}/generated_output.status ]; then
        echo "It looks like not all the generator scripts were run,"
        echo "did you forget to build the libcxx-generate-files target?"
        echo "Did you add all new files it generated?"
        false
    fi

    # This depends on LC_COLLATE set at the top of this script.
    step "Reject patches that introduce non-ASCII characters or hard tabs."
    ! grep -rn '[^ -~]' libcxx/include libcxx/src libcxx/test \
           --exclude '*.dat' \
           --exclude '*unicode*.cpp' \
           --exclude '*print*.sh.cpp' \
           --exclude 'escaped_output.*.pass.cpp' \
           --exclude 'format_tests.h' \
           --exclude 'format.functions.tests.h' \
           --exclude 'formatter.*.pass.cpp' \
           --exclude 'grep.pass.cpp' \
           --exclude 'locale-specific_form.pass.cpp' \
           --exclude 'ostream.pass.cpp' \
           --exclude 'transcoding.pass.cpp' \
           --exclude 'underflow.pass.cpp' \
           || false
;;
#
# Various Standard modes
#
frozen-cxx03-headers)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx03-frozen.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx03)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx03.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx11)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx11.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx14)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx14.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx17)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx17.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx20)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx20.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx23)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx23.cmake"
    check-runtimes
    check-abi-list
;;
generic-cxx26)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx26.cmake"
    check-runtimes
    check-abi-list
;;
#
# Other compiler support
#
generic-gcc)
    clean
    generate-cmake -DLIBCXX_ENABLE_WERROR=NO \
                   -DLIBCXXABI_ENABLE_WERROR=NO \
                   -DLIBUNWIND_ENABLE_WERROR=NO
    check-runtimes
;;
generic-gcc-cxx11)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-cxx11.cmake" \
                   -DLIBCXX_ENABLE_WERROR=NO \
                   -DLIBCXXABI_ENABLE_WERROR=NO \
                   -DLIBUNWIND_ENABLE_WERROR=NO
    check-runtimes
;;
#
# Sanitizers
#
generic-asan)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-asan.cmake"
    check-runtimes
;;
generic-msan)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-msan.cmake"
    check-runtimes
;;
generic-tsan)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-tsan.cmake"
    check-runtimes
;;
generic-ubsan)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-ubsan.cmake"
    check-runtimes
;;
#
# Various build configurations
#
bootstrapping-build)
    clean

    if [ ! -z ${CCACHE+x} ]; then
        COMPILER_LAUNCHER="-DCMAKE_CXX_COMPILER_LAUNCHER=${CCACHE}"
    fi

    step "Generating CMake"
    cmake \
          -S "${MONOREPO_ROOT}/llvm" \
          -B "${BUILD_DIR}" \
          -GNinja \
          ${COMPILER_LAUNCHER} \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
          -DLLVM_ENABLE_PROJECTS="clang;lldb" \
          -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
          -DLLVM_RUNTIME_TARGETS="$(${CXX} --print-target-triple)" \
          -DLLVM_HOST_TRIPLE="$(${CXX} --print-target-triple)" \
          -DLLVM_TARGETS_TO_BUILD="host" \
          -DRUNTIMES_BUILD_ALLOW_DARWIN=ON \
          -DCOMPILER_RT_INCLUDE_TESTS=OFF \
          -DLLVM_ENABLE_ASSERTIONS=ON \
          -DLLVM_LIT_ARGS="-sv --xunit-xml-output test-results.xml --timeout=1500 --time-tests"

    step "Running the libc++ and libc++abi tests"
    ninja -vC "${BUILD_DIR}" check-runtimes

    step "Installing libc++ and libc++abi to a fake location"
    ninja -vC "${BUILD_DIR}" install-runtimes

    step "Running the LLDB libc++ data formatter tests"
    ninja -vC "${BUILD_DIR}" lldb-api-test-deps
    ${BUILD_DIR}/bin/llvm-lit -sv --param dotest-args='--category libc++' "${MONOREPO_ROOT}/lldb/test/API"

    ccache -s
;;
generic-static)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-static.cmake"
    check-runtimes
;;
generic-merged)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-merged.cmake" \
                   -DLIBCXX_TEST_CONFIG="llvm-libc++-shared.cfg.in" \
                   -DLIBCXXABI_TEST_CONFIG="llvm-libc++abi-merged.cfg.in" \
                   -DLIBUNWIND_TEST_CONFIG="llvm-libunwind-merged.cfg.in"
    check-runtimes
;;
generic-hardening-mode-fast)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardening-mode-fast.cmake"
    check-runtimes
    check-abi-list
;;
generic-hardening-mode-fast-with-abi-breaks)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardening-mode-fast-with-abi-breaks.cmake"
    check-runtimes
    # Not checking ABI list since we purposefully enable ABI breaking changes
;;
generic-hardening-mode-extensive)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardening-mode-extensive.cmake"
    check-runtimes
    check-abi-list
;;
generic-hardening-mode-extensive-observe-semantic)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardening-mode-extensive-observe-semantic.cmake"
    check-runtimes
    check-abi-list
;;
generic-hardening-mode-debug)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardening-mode-debug.cmake"
    check-runtimes
    check-abi-list
;;
#
# Module builds
#
generic-modules)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-modules.cmake"
    check-runtimes
    check-abi-list
;;
generic-modules-cxx17-lsv)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-modules-cxx17-lsv.cmake"
    check-runtimes
    check-abi-list
;;
#
# Parts removed
#
generic-no-threads)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-threads.cmake"
    check-runtimes
;;
generic-no-filesystem)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-filesystem.cmake"
    check-runtimes
;;
generic-no-random_device)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-random_device.cmake"
    check-runtimes
;;
generic-no-localization)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-localization.cmake"
    check-runtimes
;;
generic-no-terminal)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-terminal.cmake"
    check-runtimes
;;
generic-no-unicode)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-unicode.cmake"
    check-runtimes
;;
generic-no-wide-characters)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-wide-characters.cmake"
    check-runtimes
;;
generic-no-tzdb)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-tzdb.cmake"
    check-runtimes
;;
generic-no-experimental)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-experimental.cmake"
    check-runtimes
    check-abi-list
;;
generic-no-exceptions)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-exceptions.cmake"
    check-runtimes
    check-abi-list
;;
generic-no-rtti)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-no-rtti.cmake"
    check-runtimes
;;
#
# Other miscellaneous jobs
#
generic-abi-unstable)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-abi-unstable.cmake"
    check-runtimes
;;
generic-optimized-speed)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-optimized-speed.cmake"
    check-runtimes
;;
apple-configuration)
    clean

    step "Installing libc++ with the Apple system configuration"
    arch="$(uname -m)"
    xcrun --sdk macosx                                              \
        ${MONOREPO_ROOT}/libcxx/utils/ci/apple-install-libcxx.sh    \
            --llvm-root ${MONOREPO_ROOT}                            \
            --build-dir ${BUILD_DIR}                                \
            --install-dir ${INSTALL_DIR}                            \
            --symbols-dir "${BUILD_DIR}/symbols"                    \
            --architectures "${arch}"                               \
            --version "999.99"

    step "Running tests against Apple-configured libc++"
    # TODO: It would be better to run the tests against the fake-installed version of libc++ instead
    xcrun --sdk macosx ninja -vC "${BUILD_DIR}/${arch}" check-cxx check-cxxabi check-cxx-abilist
;;
apple-system|apple-system-hardened)
    clean

    arch="$(uname -m)"
    version="$(sw_vers --productVersion)"
    params="target_triple=${arch}-apple-macosx${version}"
    if [[ "${BUILDER}" == *-hardened ]]; then
        params+=";hardening_mode=fast"
    fi

    # In the Apple system configuration, we build libc++ and libunwind separately.
    step "Installing libc++ and libc++abi in Apple-system configuration"
    cmake \
        -S "${MONOREPO_ROOT}/runtimes" \
        -B "${BUILD_DIR}/cxx" \
        -GNinja \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}/cxx" \
        -DLLVM_LIT_ARGS="-sv --xunit-xml-output test-results.xml --timeout=1500 --time-tests" \
        -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
        -DLIBCXX_CXX_ABI=libcxxabi \
        -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Apple.cmake" \
        -DLIBCXX_TEST_CONFIG="apple-libc++-system.cfg.in" \
        -DLIBCXXABI_TEST_CONFIG="apple-libc++abi-system.cfg.in" \
        -DLIBCXX_TEST_PARAMS="${params}" \
        -DLIBCXXABI_TEST_PARAMS="${params}"

    step "Installing libunwind in Apple-system configuration"
    cmake \
        -S "${MONOREPO_ROOT}/runtimes" \
        -B "${BUILD_DIR}/unwind" \
        -GNinja \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}/unwind" \
        -DLLVM_LIT_ARGS="-sv --xunit-xml-output test-results.xml --timeout=1500 --time-tests" \
        -DLLVM_ENABLE_RUNTIMES="libunwind" \
        -DLIBUNWIND_TEST_CONFIG="apple-libunwind-system.cfg.in" \
        -DLIBUNWIND_TEST_PARAMS="${params}" \
        -DCMAKE_INSTALL_NAME_DIR="/usr/lib/system"

    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}/cxx" check-cxx

    step "Running the libc++abi tests"
    ninja -vC "${BUILD_DIR}/cxx" check-cxxabi

    step "Running the libunwind tests"
    ninja -vC "${BUILD_DIR}/unwind" check-unwind
;;
aarch64)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/AArch64.cmake"
    check-runtimes
;;
aarch64-no-exceptions)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/AArch64.cmake" \
                   -DLIBCXX_ENABLE_EXCEPTIONS=OFF \
                   -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF
    check-runtimes
;;
# Aka Armv8 32 bit
armv8)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv8Arm.cmake"
    check-runtimes
;;
armv8-no-exceptions)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv8Thumb-no-exceptions.cmake"
    check-runtimes
;;
# Armv7 32 bit. One building Arm only one Thumb only code.
armv7)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv7Arm.cmake"
    check-runtimes
;;
armv7-no-exceptions)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv7Thumb-no-exceptions.cmake"
    check-runtimes
;;
armv7m-picolibc)
    test-armv7m-picolibc \
        -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv7M-picolibc.cmake"
;;
armv7m-picolibc-no-exceptions)
    test-armv7m-picolibc \
        -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Armv7M-picolibc.cmake" \
        -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
        -DLIBCXXABI_ENABLE_STATIC_UNWINDER=OFF \
        -DLIBCXX_ENABLE_EXCEPTIONS=OFF \
        -DLIBCXX_ENABLE_RTTI=OFF
;;
clang-cl-dll)
    clean
    # TODO: Currently, building with the experimental library breaks running
    # tests (the test linking look for the c++experimental library with the
    # wrong name, and the statically linked c++experimental can't be linked
    # correctly when libc++ visibility attributes indicate dllimport linkage
    # anyway), thus just disable the experimental library. Remove this
    # setting when cmake and the test driver does the right thing automatically.
    generate-cmake-libcxx-win -DLIBCXX_TEST_PARAMS="enable_experimental=False"
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
;;
clang-cl-static)
    clean
    generate-cmake-libcxx-win -DLIBCXX_ENABLE_SHARED=OFF
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
;;
clang-cl-no-vcruntime)
    clean
    # Building libc++ in the same way as in clang-cl-dll above, but running
    # tests with -D_HAS_EXCEPTIONS=0, which users might set in certain
    # translation units while using libc++, even if libc++ is built with
    # exceptions enabled.
    generate-cmake-libcxx-win -DLIBCXX_TEST_PARAMS="enable_experimental=False" \
                              -DLIBCXX_TEST_CONFIG="llvm-libc++-shared-no-vcruntime-clangcl.cfg.in"
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
;;
clang-cl-debug)
    clean
    generate-cmake-libcxx-win -DLIBCXX_TEST_PARAMS="enable_experimental=False" \
                              -DCMAKE_BUILD_TYPE=Debug
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
;;
clang-cl-static-crt)
    clean
    # Test linking a static libc++ with the static CRT ("MultiThreaded" denotes
    # the static CRT, as opposed to "MultiThreadedDLL" which is the default).
    generate-cmake-libcxx-win -DLIBCXX_ENABLE_SHARED=OFF \
                              -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
;;
mingw-dll)
    clean
    generate-cmake \
          -C "${MONOREPO_ROOT}/libcxx/cmake/caches/MinGW.cmake"
    check-runtimes
;;
mingw-static)
    clean
    generate-cmake \
          -C "${MONOREPO_ROOT}/libcxx/cmake/caches/MinGW.cmake" \
          -DLIBCXX_ENABLE_SHARED=OFF \
          -DLIBUNWIND_ENABLE_SHARED=OFF
    check-runtimes
;;
mingw-incomplete-sysroot)
    # When bringing up a new cross compiler from scratch, we build
    # libunwind/libcxx in a setup where the toolchain is incomplete and
    # unable to perform the normal linker checks; this requires a few
    # special cases in the CMake files.
    #
    # Building in an incomplete setup requires setting CMAKE_*_COMPILER_WORKS,
    # as CMake fails to probe the compiler. This case also requires
    # setting CMAKE_CXX_COMPILER_TARGET, as LLVM's heuristics for setting
    # the triple fails when CMake hasn't been able to probe the environment.
    # (This is what one has to do when building the initial libunwind/libcxx
    # for a new toolchain.)
    clean
    generate-cmake \
          -DCMAKE_C_COMPILER_WORKS=TRUE \
          -DCMAKE_CXX_COMPILER_WORKS=TRUE \
          -DCMAKE_C_COMPILER_TARGET=x86_64-w64-windows-gnu \
          -DCMAKE_CXX_COMPILER_TARGET=x86_64-w64-windows-gnu \
          -C "${MONOREPO_ROOT}/libcxx/cmake/caches/MinGW.cmake"
    # Only test that building succeeds; there's not much extra value in running
    # the tests here, as it would be equivalent to the mingw-dll config above.
    step "Building the runtimes"
    ninja -vC "${BUILD_DIR}"
;;
aix)
    clean
    generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/AIX.cmake" \
                   -DLIBCXX_TEST_CONFIG="ibm-libc++-shared.cfg.in" \
                   -DLIBCXXABI_TEST_CONFIG="ibm-libc++abi-shared.cfg.in" \
                   -DLIBUNWIND_TEST_CONFIG="ibm-libunwind-shared.cfg.in"
    check-abi-list
    check-runtimes
;;
android-ndk-*)
    clean

    ANDROID_EMU_IMG="${BUILDER#android-ndk-}"
    . "${MONOREPO_ROOT}/libcxx/utils/ci/vendor/android/emulator-functions.sh"
    if ! validate_emu_img "${ANDROID_EMU_IMG}"; then
        error "android-ndk suffix must be a valid emulator image (${ANDROID_EMU_IMG})" >&2
        exit 1
    fi
    ARCH=$(arch_of_emu_img ${ANDROID_EMU_IMG})

    # The NDK libc++_shared.so is always built against the oldest supported API
    # level. When tests are run against a device with a newer API level, test
    # programs can be built for any supported API level, but building for the
    # newest API (i.e. the system image's API) is probably the most interesting.
    PARAMS="executor=${MONOREPO_ROOT}/libcxx/utils/adb_run.py;target_triple=$(triple_of_arch ${ARCH})$(api_of_emu_img ${ANDROID_EMU_IMG})"
    generate-cmake-android -C "${MONOREPO_ROOT}/runtimes/cmake/android/Arch-${ARCH}.cmake" \
                           -C "${MONOREPO_ROOT}/libcxx/cmake/caches/AndroidNDK.cmake" \
                           -DCMAKE_SYSROOT=/opt/android/ndk/sysroot \
                           -DLIBCXX_TEST_PARAMS="${PARAMS}" \
                           -DLIBCXXABI_TEST_PARAMS="${PARAMS}"
    check-abi-list
    ninja -vC "${BUILD_DIR}" install-cxx install-cxxabi

    # Start the emulator and make sure we can connect to the adb server running
    # inside of it.
    "${MONOREPO_ROOT}/libcxx/utils/ci/vendor/android/start-emulator.sh" ${ANDROID_EMU_IMG}
    trap "${MONOREPO_ROOT}/libcxx/utils/ci/vendor/android/stop-emulator.sh" EXIT
    . "${MONOREPO_ROOT}/libcxx/utils/ci/vendor/android/setup-env-for-emulator.sh"

    # Create adb_run early to avoid concurrent `mkdir -p` of common parent
    # directories.
    adb shell mkdir -p /data/local/tmp/adb_run
    adb push "${BUILD_DIR}/lib/libc++_shared.so" /data/local/tmp/libc++/libc++_shared.so
    step "Running the libc++ tests"
    ninja -vC "${BUILD_DIR}" check-cxx
    step "Running the libc++abi tests"
    ninja -vC "${BUILD_DIR}" check-cxxabi
;;
#################################################################
# Insert vendor-specific internal configurations below.
#
# This allows vendors to extend this file with their own internal
# configurations without running into merge conflicts with upstream.
#################################################################

#################################################################
*)
    echo "${BUILDER} is not a known configuration"
    exit 1
;;
esac

endstep # Make sure we close any still-open output group
