# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_compiler=clang

_realname=flang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-rt")
_pkgver=21.1.8
pkgver=${_pkgver/-/}
pkgrel=1
pkgdesc="Fortran frontend for LLVM (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://flang.llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
  "aur: flang"
  "gentoo: "
)
license=("spdx:Apache-2.0 WITH LLVM-exception")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-mlir")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "gawk"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-lld" \
               "${MINGW_PACKAGE_PREFIX}-compiler-rt" \
               "${MINGW_PACKAGE_PREFIX}-libc++")
             $([[ "$_compiler" == "gcc" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-gcc")
             )
_pkgfn=llvm-project-${_pkgver}.src
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
source=("${_url}/${_pkgfn}.tar.xz"{,.sig}
        "0002-cmake-22162.patch"
        "0005-Fix-c_long_double-value-on-mingw-aarch64.patch"
        "0101-add-municode-flag-on-mingw.patch"
        "0102-do-not-use-clock_gettime-on-mingw.patch"
        "0103-fix-build-on-mingw.patch")
sha256sums=('4633a23617fa31a3ea51242586ea7fb1da7140e426bd62fc164261fe036aa142'
            'SKIP'
            '77fb0612217b6af7a122f586a9d0d334cd48bb201509bf72e8f8e6244616e895'
            'a1811227e2a26c2786b0c8577528580e679111f6c0889afea57d08333e2d5e4f'
            'cb7c7f8f00076c904263a1aeb1bdd68625f18b2d0246adb6f38588cd6e1c9d09'
            '215941eb952b3bc054d5f21e4db06d13aca44fb18f7a7800eb1d3e89cf9845da'
            'f4efcbd6baf9d26652b0111dafd5686a03ff167b466b5d84da94965d56ff2f4e')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              '71046D1E9C6656BDD61171873E83BABF4A4F9E85'  # Cullen Rhodes
              'FFB3368980F3E6BB5737145A316C56D064CACBA5'  # Douglas Yung
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
noextract=("llvm-project-${_pkgver}.src.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  plain "Extracting llvm-project-${_pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  tar -xJf "${srcdir}"/llvm-project-${_pkgver}.src.tar.xz -C "${srcdir}" || true

  # Patch flang
  cd "${srcdir}"/llvm-project-${_pkgver}.src/flang

  apply_patch_with_msg \
    "0002-cmake-22162.patch" \
    "0005-Fix-c_long_double-value-on-mingw-aarch64.patch"

  # Patch flang-rt
  cd "${srcdir}"/llvm-project-${_pkgver}.src/flang-rt

  apply_patch_with_msg \
    "0101-add-municode-flag-on-mingw.patch" \
    "0102-do-not-use-clock_gettime-on-mingw.patch" \
    "0103-fix-build-on-mingw.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${_compiler}" == "clang" ]; then
    export CC='clang'
    export CXX='clang++'
    _extra_config+=(-DLLVM_ENABLE_LLD=ON)
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DBUILD_SHARED_LIBS=OFF \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DCLANG_DIR=${MINGW_PREFIX}/lib/cmake/clang \
    -DLLVM_DIR=${MINGW_PREFIX}/lib/cmake/llvm \
    -DMLIR_DIR=${MINGW_PREFIX}/lib/cmake/mlir \
    -DFLANG_INCLUDE_TESTS=OFF \
    "${_extra_config[@]}" \
    -Wno-dev \
    ../${_pkgfn}/flang

  ${MINGW_PREFIX}/bin/cmake.exe --build .

  mkdir -p "${srcdir}"/build-${MSYSTEM}-rt && cd "${srcdir}"/build-${MSYSTEM}-rt

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DCMAKE_Fortran_COMPILER="${srcdir}"/build-${MSYSTEM}/bin/flang.exe \
    -DCMAKE_Fortran_COMPILER_WORKS=ON \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DLLVM_ENABLE_RUNTIMES="flang-rt" \
    -DFLANG_RT_INCLUDE_TESTS=OFF \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "${_extra_config[@]}" \
    -Wno-dev \
    ../${_pkgfn}/runtimes

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake.exe -DFLANG_INCLUDE_TESTS=ON ../${_pkgfn}
  ${MINGW_PREFIX}/bin/cmake.exe --build .
  ${MINGW_PREFIX}/bin/cmake.exe --build . -- check-flang || true
}

package_flang() {
  pkgdesc="Fortran frontend for LLVM (Compiler) (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-flang-rt")
  optdepends=("${MINGW_PACKAGE_PREFIX}-llvm-openmp")
  provides=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc"))

  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_pkgfn}/${_realname}/LICENSE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}

package_flang-rt() {
  pkgdesc="Fortran frontend for LLVM (Runtime) (mingw-w64)"
  depends=()
  #provides=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc-libs"))

  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}-rt

  mkdir -p lib
  cp "${pkgdir}"${MINGW_PREFIX}/lib/clang/${pkgver%.[0-9].*}/lib/${CARCH}-w64-windows-gnu/libflang_rt.runtime.static.a \
    "${pkgdir}"${MINGW_PREFIX}/lib/libflang_rt.runtime.a

  install -Dm644 "${srcdir}"/${_pkgfn}/${_realname}-rt/LICENSE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-rt/LICENSE
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
