# Maintainer: Mateusz Mikuła <mati865@gmail.com>

_realname=compiler-rt
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=21.1.1
pkgrel=1
pkgdesc="Compiler runtime libraries for cross clang"
arch=('i686' 'x86_64')
url="https://llvm.org"
license=('custom:Apache 2.0 with LLVM Exception')
msys2_references=(
  'archlinux: compiler-rt'
  'cpe: cpe:/a:llvm:llvm'
)
depends=('clang')
makedepends=("cmake" "lld" "llvm" "ninja" "${_mingw_suff}-clang-headers" 'gcc' 'python')
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver}
source=("${_url}/${_realname}-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/cmake-${pkgver}.src.tar.xz"{,.sig}
        "${_url}/third-party-${pkgver}.src.tar.xz"{,.sig})
sha256sums=('f5e46d1734be28bc8ea83d0a5621a43388d349def91e37e6907609e5b67d1f8e'
            'SKIP'
            '9c0b9064b7d0f2a3004f1d034aadf84d2af4e5dca2135ebf697b0a1eb85ef769'
            'SKIP'
            'e9b7299b0b32aa827231ed35101a3b5ea37dd5a45719225cb2c08ac571791233'
            'SKIP')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

_targets="x86_64-w64-mingw32 i686-w64-mingw32 armv7-w64-mingw32 aarch64-w64-mingw32"

prepare() {
  cd ${srcdir}
  rm -rf cmake
  mv cmake-$pkgver.src cmake
  mv third-party-$pkgver.src third-party
}

build() {
  for _target in ${_targets}; do
    cd "${srcdir}"
    _llvmtarget=${_target%%-*}
    case "${_target}" in
      x86_64*)
        _sizeof_void_p=8
        ;;
      i?86*)
        _sizeof_void_p=4
        _llvmtarget=i386
        ;;
      aarch64*)
        _sizeof_void_p=8
        ;;
      armv7*)
        _sizeof_void_p=4
        ;;
    esac

    [[ -d build-${_target} ]] && rm -rf build-${_target}
    mkdir build-${_target} && cd build-${_target}

    LDFLAGS+=" -fuse-ld=lld"

    cmake \
      -G'Ninja' \
      -DCMAKE_AR="$(which llvm-ar)" \
      -DCMAKE_ASM_COMPILER="clang" \
      -DCMAKE_C_COMPILER="clang" \
      -DCMAKE_CXX_COMPILER="clang++" \
      -DCMAKE_RANLIB="$(which llvm-ranlib)" \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_{ASM,C,CXX}_COMPILER_TARGET=${_llvmtarget}-pc-windows-gnu \
      -DCMAKE_{ASM,C,CXX}_COMPILER_WORKS=ON \
      -DCMAKE_INSTALL_PREFIX="$(clang -print-resource-dir)" \
      -DCMAKE_SIZEOF_VOID_P=${_sizeof_void_p} \
      -DCMAKE_SYSROOT="/opt/${_target}" \
      -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
      -DCMAKE_FIND_ROOT_PATH_MODE_{LIBRARY,INCLUDE,PACKAGE}=ONLY \
      -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
      -DCMAKE_SYSTEM_NAME=Windows \
      -DCOMPILER_RT_BUILD_{LIBFUZZER,MEMPROF,ORC,PROFILE,SANITIZERS,XRAY}=OFF \
      -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
      -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON \
      -DLLVM_DIR="/usr/lib/cmake/llvm" \
      ../${_realname}-${pkgver}.src

    cmake --build .
  done
}

package() {
  # ninja install needs working compiler detection to find correct directory
  for _target in ${_targets}; do
    DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${_target}"
  done
  rm -rf "${pkgdir}$(clang -print-resource-dir)/include"
}
