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

_realname=clang
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=21.1.1
pkgrel=1
pkgdesc="Cross Clang for the MinGW-w64"
groups=("${_mingw_suff}-${_realname}-toolchain")
arch=('i686' 'x86_64')
url="https://llvm.org"
license=("spdx:Apache-2.0 WITH LLVM-exception")
depends=('llvm' 'mingw-w64-cross-clang-crt' 'mingw-w64-cross-compiler-rt')
makedepends=("clang" "cmake" "lld" "ninja" "python")
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
msys2_references=(
  'archlinux: clang'
  'cpe: cpe:/a:llvm:llvm'
)
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver}
source=("${_url}/llvm-project-${pkgver}.src.tar.xz"{,.sig})
sha256sums=('8863980e14484a72a9b7d2c80500e1749054d74f08f8c5102fd540a3c5ac9f8a'
            'SKIP')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
noextract=(llvm-project-${pkgver}.src.tar.xz)

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

prepare() {
  cd ${srcdir}
  [ -d llvm-project-${pkgver}.src ] && rm -rf llvm-project-${pkgver}.src
  plain "Extracting llvm-project-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  tar -Jxf llvm-project-${pkgver}.src.tar.xz || true
}

build() {
  CFLAGS+=" --start-no-unused-arguments -rtlib=compiler-rt --end-no-unused-arguments"
  CXXFLAGS+=" --start-no-unused-arguments -rtlib=compiler-rt --end-no-unused-arguments"
  LDFLAGS+=" -fuse-ld=lld"
  export CC="clang" CXX="clang++" ASM="clang" AR="llvm-ar" RANLIB="llvm-ranlib" DLLTOOL="llvm-dlltool" LD="clang"
  for _target in ${_targets}; do
    echo building $_target
    cd "${srcdir}"
    [[ -d build-${_target} ]] && rm -rf build-${_target}
    mkdir build-${_target} && cd build-${_target}
    cmake -GNinja \
      -DCMAKE_INSTALL_PREFIX=/opt/${_target} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_AR="$(which llvm-ar)" \
      -DCMAKE_ASM_COMPILER="clang" \
      -DCMAKE_C_COMPILER="clang" \
      -DCMAKE_CXX_COMPILER="clang++" \
      -DCMAKE_{ASM,C,CXX}_COMPILER_TARGET="${_target}" \
      -DCMAKE_{ASM,C,CXX}_COMPILER_WORKS=ON \
      -DCMAKE_RANLIB="$(which llvm-ranlib)" \
      -DCMAKE_SYSROOT=/opt/${_target} \
      -DCMAKE_CROSSCOMPILING=ON \
      -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 \
      -DCXX_SUPPORTS_CUSTOM_LINKER=ON \
      -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=ON \
      -DLIBCXX_ENABLE_SHARED=ON \
      -DLIBCXX_ENABLE_STATIC=ON \
      -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
      -DLIBCXX_HAS_WIN32_THREAD_API=ON \
      -DLIBCXX_INSTALL_MODULES=ON \
      -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
      -DLIBCXX_USE_COMPILER_RT=ON \
      -DLIBCXXABI_ENABLE_SHARED=OFF \
      -DLIBCXXABI_ENABLE_STATIC=ON \
      -DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON \
      -DLIBCXXABI_HAS_WIN32_THREAD_API=ON \
      -DLIBCXXABI_USE_COMPILER_RT=ON \
      -DLIBCXXABI_USE_LLVM_UNWINDER=ON \
      -DLIBUNWIND_ENABLE_FRAME_APIS=ON \
      -DLIBUNWIND_ENABLE_SHARED=ON \
      -DLIBUNWIND_ENABLE_STATIC=ON \
      -DLIBUNWIND_USE_COMPILER_RT=ON \
      -DENABLE_TIME_ZONE_DATABASE_DEFAULT=ON \
      -DLLVM_ENABLE_LIBCXX=ON \
      -DLLVM_ENABLE_LLD=ON \
      -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
      -DLLVM_INCLUDE_TESTS=OFF \
      ../llvm-project-${pkgver}.src/runtimes

    cmake --build . -- unwind cxxabi cxx cxx_experimental
  done
}

package() {
  for _target in ${_targets}; do
    cd "${srcdir}"
    DESTDIR="${pkgdir}" cmake --build build-${_target} --target install-cxx install-cxxabi install-unwind
    install -Dm644 "${srcdir}"/llvm-project-${pkgver}.src/libcxx/LICENSE.TXT \
      "${pkgdir}"/usr/share/licenses/libc++/LICENSE
    install -Dm644 "${srcdir}"/llvm-project-${pkgver}.src/libunwind/LICENSE.TXT \
      "${pkgdir}"/usr/share/licenses/libunwind/LICENSE

    _prefix="${pkgdir}/opt/${_target}/bin"
    for tool in addr2line ar dlltool nm objcopy objdump ranlib rc readelf readobj size strings strip; do
      printf "#!/bin/bash\n\nexec -a \"${_target}-${tool}\" /usr/bin/llvm-${tool} \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
    for tool in windres; do
      printf "#!/bin/bash\n\nexec /usr/bin/llvm-${tool} --target ${_target} --preprocessor-arg --sysroot=/opt/${_target} \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
    for tool in as cc clang; do
      printf "#!/bin/bash\n\nexec /usr/bin/clang --start-no-unused-arguments -target ${_target} -fuse-ld=lld -rtlib=compiler-rt -stdlib=libc++ -unwindlib=libunwind --sysroot=/opt/${_target} --end-no-unused-arguments \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
    for tool in c++ clang++; do
      printf "#!/bin/bash\n\nexec /usr/bin/clang++ --start-no-unused-arguments -target ${_target} -fuse-ld=lld -rtlib=compiler-rt -stdlib=libc++ -unwindlib=libunwind --sysroot=/opt/${_target} --end-no-unused-arguments \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
    for tool in ld ld.lld; do
      printf "#!/bin/bash\n\nexec /usr/bin/ld.lld \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
    for tool in lld; do
      printf "#!/bin/bash\n\nexec /usr/bin/lld \"\$@\"" | tee "${_prefix}/${tool}" "${_prefix}/${_target}-${tool}" > /dev/null
    done
  done
}
