# 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.22
# NOTE: You need to run ./get-compatible-versions.sh after changing the pkgver!
_binaryen_revision=ccd2c82dde6c03f6de504e007c70b9cef1056be5
_llvm_project_revision=9cc1585b137c4b80f23af0d17e1502c76e430735
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"
        '0001-emscripten-4.0.22-do-unix-on-windows.patch')
sha256sums=('b3e7102e5e26295ca528641f13a5efd008436a9a4042d465b9581a04e249ba2c'
            'c959a23d96538619a48ac9012ba316802fbfc2a1830e70d70fc430c8f0fe7350'
            '96aa3338febafe917fbf5e0b767ccccb1232acf6f36337dee9a9346fd23b10e5'
            '37bf560196b6933735524035683a1d16d04cf83613ea111d9b2cefc7e0704691')
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

  cd "$srcdir"/emscripten

  # Restore launcher scripts. For more info see https://github.com/msys2/MINGW-packages/pull/26928
  patch -p1 -i "$srcdir"/0001-emscripten-4.0.22-do-unix-on-windows.patch

  # Bootstrap launcher scripts. For more info see https://github.com/emscripten-core/emscripten/pull/23761
  ./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
}
