#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2021 Arm Limited or its affiliates and Contributors. All rights reserved.

[[ "$TRACE" ]] && set -x
set -euo pipefail

LLVM_SRCDIR="${1-../llvm}"
LLVM_TGTDIR="${2-.}"

if [[ ! -d "$LLVM_SRCDIR" ]]
then
    echo 'usage: manyclangs-cmake <path/to/llvm-project/llvm> <build_dir>

Configure

This script probably makes assumptions that the build directory
is inside the llvm-project directory.' 1>&2
    exit 1
fi

# Abspath to specified LLVM source directory.
FILE_PREFIX="$(dirname "$(realpath "$LLVM_SRCDIR")")"

# Version number of clang used to build the pack. Chosen because it's what is
# easily available in the universe repository on Ubuntu 20.04 which is a recent
# but not-too-new distribution with long term support, and therefore easy for
# others to reproduce by running this script.
# Defaults to "-12".
BUILDER_CLANG_VERSION_SUFFIX=${BUILDER_CLANG_VERSION_SUFFIX--12}

COMPILER_FLAGS=(
    # Flags which improve the compression ratio.
    -ffunction-sections
    -fdata-sections
    -mno-outline
    # Flags to make resulting build not depend on abspath of sources.
    -no-canonical-prefixes
    "-ffile-prefix-map=${FILE_PREFIX}="
    "-fmacro-prefix-map=${FILE_PREFIX}="
    # Don't embed the compiler ident into the objects; this allows using an
    # LTO/PGO build of the compiler which is faster and produces identical
    # binaries aside from the ident string.
    -fno-ident
)

FLAGS=(
    "-DCMAKE_EXPORT_COMPILE_COMMANDS=On"

    # Rel + assertions build.
    "-DCMAKE_BUILD_TYPE=Release"
    "-DLLVM_ENABLE_ASSERTIONS=On"
    "-DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra"
    "-DCLANG_ENABLE_CLANGD=Off"

    # VC revision is written into binaries with manyclangs-build. This is not
    # used because it invalidates tablegen which causes a lot of rebuilding.
    "-DLLVM_APPEND_VC_REV=Off"

    # Compiler + linker.
    "-DCMAKE_C_COMPILER=clang${BUILDER_CLANG_VERSION_SUFFIX}"
    "-DCMAKE_CXX_COMPILER=clang++${BUILDER_CLANG_VERSION_SUFFIX}"
    "-DCMAKE_C_FLAGS=${COMPILER_FLAGS[*]}"
    "-DCMAKE_CXX_FLAGS=${COMPILER_FLAGS[*]}"
    # Full path is required due to -no-canonical-prefixes
    "-DLLVM_USE_LINKER=${LLVM_USE_LINKER-/usr/bin/ld.lld"${BUILDER_CLANG_VERSION_SUFFIX}"}"

    # These are necessary for reproducibility in conjunction with
    # CCACHE_BASEDIR, search for other comments referencing this variable.
    "-DCMAKE_C_COMPILER_LAUNCHER=ccache"
    "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"

    # Bindings can slow down configuration-time, and we don't use them.
    "-DLLVM_ENABLE_BINDINGS=Off"
    # We don't care about warnings for this use-case.
    "-DLLVM_ENABLE_WARNINGS=Off"
    # Try to minimize dependencies, at the expense of minor features.
    "-DLLVM_ENABLE_LIBXML2=Off"
    "-DLLVM_ENABLE_ZLIB=Off"
    "-DLLVM_ENABLE_Z3_SOLVER=Off"
    # Faster builds(?)
    "-DCLANG_TOOLING_BUILD_AST_INTROSPECTION=Off"

    # Shrink ninja files.
    "-DCLANG_INCLUDE_TESTS=Off"
    "-DLLVM_BUILD_TESTS=Off"
    "-DLLVM_INCLUDE_BENCHMARKS=Off"
    "-DLLVM_INCLUDE_EXAMPLES=Off"
    "-DLLVM_INCLUDE_TESTS=Off"
)

cmake -GNinja "${FLAGS[@]}" -S "$LLVM_SRCDIR" -B "$LLVM_TGTDIR"
