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

_realname=emscripten
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Sadly, upstream currently suggests bundling a binaryen version for the time being:
# https://github.com/emscripten-core/emscripten/issues/12252
# I'm obviously unhappy about that but it appears to be the only practical solution for the time being.
pkgver=4.0.16
# NOTE: You need to run ./get-compatible-versions.sh after changing the pkgver!
_binaryen_revision=4201437a34698649abe8863270e17aba9d6df8eb
_llvm_project_revision=375f48942b9a3f3fbd82133390af25b6c96f1460
pkgrel=1
pkgdesc="Compile C and C++ into highly-optimizable JavaScript for the web (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://emscripten.org"
msys2_repository_url="https://github.com/emscripten-core/emscripten"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-nodejs"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "git")
optdepends=("${MINGW_PACKAGE_PREFIX}-ruby: for using websockify addon"
            "${MINGW_PACKAGE_PREFIX}-cmake: for emcc --show-ports")
conflicts=("${MINGW_PACKAGE_PREFIX}-binaryen")
provides=("${MINGW_PACKAGE_PREFIX}-binaryen")
install="emscripten-${MSYSTEM}.install"
source=("git+$msys2_repository_url#tag=$pkgver"
        "llvm-project-$_llvm_project_revision.tar.gz::https://github.com/llvm/llvm-project/archive/$_llvm_project_revision.tar.gz"
        "binaryen-$_binaryen_revision.tar.gz::https://github.com/WebAssembly/binaryen/archive/$_binaryen_revision.tar.gz")
sha256sums=('3ea37993b3b14f354e608edc381ef2ff3d383b7a82e044c053c0cbc284809796'
            '4b874fd3a2abc8115786d90de8ab8e087eec0b23cd7016373cccabc265dc8f70'
            '6bea2de2dc30332f608cfb6d94eab31871dcedee0a9166f738b148a34440949f')
noextract=("llvm-project-$_llvm_project_revision.tar.gz")

prepare() {
  tar -xf llvm-project-$_llvm_project_revision.tar.gz 2> /dev/null || true
  mv "llvm-project-$_llvm_project_revision" llvm-project
  mv "binaryen-$_binaryen_revision" binaryen

  # Bootstrap launcher scripts. For more info see https://github.com/emscripten-core/emscripten/pull/23761
  cd "$srcdir"/emscripten
  ./bootstrap
}

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

  # Build binaryen
  mkdir -p "$srcdir"/build-${MSYSTEM}-binaryen && cd "$srcdir"/build-${MSYSTEM}-binaryen
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -GNinja \
    "${_extra_config[@]}" \
    -DENABLE_WERROR=OFF \
    -DBUILD_TESTS=OFF \
    ../binaryen
  ${MINGW_PREFIX}/bin/cmake --build .

  # Inspired from https://github.com/WebAssembly/waterfall/blob/db2ea5eeb11b74cce9b9459be0cc88807744b1b5/src/build.py#L868
  # Build llvm
  mkdir -p "$srcdir"/build-${MSYSTEM}-llvm && cd "$srcdir"/build-${MSYSTEM}-llvm
  _archs="WebAssembly"
  if [[ "${CARCH}" == "aarch64" ]]; then
    _archs+=";AArch64"
  else
    _archs+=";X86"
  fi
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}/opt/emscripten-llvm \
    "${_extra_config[@]}" \
    -DLLVM_ENABLE_LIBXML2=OFF \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_BUILD_RUNTIME=OFF \
    -DLLVM_TOOL_LTO_BUILD=ON \
    -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_INCLUDE_TESTS=OFF \
    -DLLVM_TARGETS_TO_BUILD="${_archs}" \
    -DLLVM_ENABLE_PROJECTS="lld;clang" \
    -DCLANG_INCLUDE_TESTS=OFF \
    ../llvm-project/llvm
  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "$srcdir"/build-${MSYSTEM}-binaryen
  DESTDIR="$pkgdir"  ${MINGW_PREFIX}/bin/cmake --install .

  # Install LLVM stuff according to
  # https://github.com/emscripten-core/emscripten/blob/master/docs/packaging.md
  # and
  # https://github.com/WebAssembly/waterfall/blob/d4a504ffee488a68d09b336897c00d404544601d/src/build.py#L915
  cd "$srcdir"/build-${MSYSTEM}-llvm
  DESTDIR="$pkgdir"  ${MINGW_PREFIX}/bin/cmake --install .

  cd "$pkgdir"${MINGW_PREFIX}/opt/emscripten-llvm/bin

  # Clean up some unnecessary bins and libs
  rm clang-check.exe clang-cl.exe clang-cpp.exe clang-extdef-mapping.exe clang-format.exe \
      clang-offload-bundler.exe clang-refactor.exe clang-scan-deps.exe \
      lld-link.exe ld.lld.exe llvm-lib.exe libclang.dll
  cd ..
  rm -r share

  # Copy some stuff that we need but that wasn't installed by default
  for bin in llvm-as.exe llvm-dis.exe FileCheck.exe llc.exe llvm-link.exe llvm-mc.exe llvm-readobj.exe opt.exe llvm-dwarfdump.exe; do
      install -Dm755 "$srcdir"/build-${MSYSTEM}-llvm/bin/$bin "$pkgdir"${MINGW_PREFIX}/opt/emscripten-llvm/bin/$bin
  done

  # Install emscripten
  cd "$srcdir"/emscripten
  DESTDIR="$pkgdir"${MINGW_PREFIX}/lib/emscripten make install

  # install profile script
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d
  echo "export PATH=\$PATH:${MINGW_PREFIX}/lib/emscripten" > "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/emscripten.sh
  cp "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/emscripten.{sh,zsh}

  install -Dm644 LICENSE "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
