# Maintainer: Rodrigo Hernandez <kwizatz@aeongames.com>

SRCEXT='.src.tar.zst'

_realname=v8
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.0.257.17
pkgrel=1
pkgdesc="Fast and modern Javascript engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://v8.dev"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-icu")
source=("bare-clones/gn::git+https://gn.googlesource.com/gn#tag=5da62d5e9d0f10cb8ece7c30563a6a214c78b68d"
        "https://github.com/v8/v8/archive/refs/tags/${pkgver}.zip"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=acacc4cc0668cb4dc7f44a3f4430635f438d7478"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=156be8c52f80cde343088b4a69a80579101b6e67"
        "bare-clones/common::git+https://chromium.googlesource.com/chromium/src/base/trace_event/common.git#commit=dab187b372fc17e51f5b9fad8201813d0aed5129"
        "bare-clones/googletest::git+https://chromium.googlesource.com/external/github.com/google/googletest.git#commit=10b1902d893ea8cc43c69541d70868f91af3646b"
        "bare-clones/jinja2::git+https://chromium.googlesource.com/chromium/src/third_party/jinja2.git#commit=b41863e42637544c2941b574c7877d3e1f663e25"
        "bare-clones/markupsafe::git+https://chromium.googlesource.com/chromium/src/third_party/markupsafe.git#commit=8f45f5cfa0009d2a70589bcda0349b8cb2b72783"
        "0000-add-mingw-main-code-changes.patch"
        "0001-add-mingw-toolchain.patch"
        "0002-gn-ninja-deletes-objs-workaround.patch"
        "icu.gn"
        "zlib.gn"
        "v8_libbase.pc"
        "v8_libplatform.pc"
        "v8.pc")
sha256sums=('SKIP'
            '03dadee15a911aeb356c04e291e634ec7b99d5c206bdc733b3c560937f6af454'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'f07e9391edd5fd25b061d1bc34bf5c56c45076b186fcc3176d4c7e8166eeed09'
            '63d759de8bad9b8ac8879f8e69c24e62bf71fe14396cbdd190d48cabf8335800'
            'b831576af25b275fca72851ec275c9d21bee0be29b4eb973a3e2a2580deac003'
            'c10169e7a34e212b1a2bf0eaf45131485719f32a7c34d9a666b692f05a2aa830'
            'b99874ea904724911528a5530c14f3051c358805bd36df9d205df31458ebf8b9'
            'fbddbe4849b623afb320472aab316c9a08b54182161b6b4911cb7ef7d65e2b0e'
            'ca81528892f0e02fe7ecd159568610221fb8d0df109d9d0dbdfa00c1284dd0fc'
            '579e199a798ff76b72a5b17380a3cd09d361cb7cee99403090c3a266d1b2e527')

prepare() {
  echo "Preparing GN source"
  cd "${srcdir}"/gn
  git apply ${srcdir}/0002-gn-ninja-deletes-objs-workaround.patch
  python build/gen.py --out-path=out/${CARCH} --platform=mingw

  echo "Patching v8 source"
  cd "${srcdir}"/v8-${pkgver}
  patch -p1 < ${srcdir}/0000-add-mingw-main-code-changes.patch

  echo "Patching build directory"
  cd "${srcdir}"/build
  git apply ${srcdir}/0001-add-mingw-toolchain.patch
  python "${srcdir}"/build/util/lastchange.py -o "${srcdir}"/build/util/LASTCHANGE
  rm -rf "${srcdir}"/v8-${pkgver}/build
  ln -sf "${srcdir}"/build "${srcdir}"/v8-${pkgver}
  echo "checkout_google_benchmark = false" > "${srcdir}"/v8-${pkgver}/build/config/gclient_args.gni

  echo "Patching zlib directory"
  mkdir -p "${srcdir}"/v8-${pkgver}/third_party/zlib
  ln -sf "${srcdir}"/zlib.gn "${srcdir}"/v8-${pkgver}/third_party/zlib/BUILD.gn
  rm -rf "${srcdir}"/v8-${pkgver}/third_party/zlib/google
  ln -sf "${srcdir}"/zlib/google "${srcdir}"/v8-${pkgver}/third_party/zlib
  
  echo "Patching icu directory"
  mkdir -p "${srcdir}"/v8-${pkgver}/third_party/icu
  ln -sf "${srcdir}"/icu.gn "${srcdir}"/v8-${pkgver}/third_party/icu/BUILD.gn

  echo "Soft linking extra repos"
  mkdir -p ${srcdir}/v8-${pkgver}/base/trace_event
  rm -rf ${srcdir}/v8-${pkgver}/base/trace_event/common
  ln -sf "${srcdir}"/common ${srcdir}/v8-${pkgver}/base/trace_event
  rm -rf "${srcdir}"/v8-${pkgver}/third_party/googletest/src
  ln -sf "${srcdir}"/googletest "${srcdir}"/v8-${pkgver}/third_party/googletest/src
  rm -rf "${srcdir}"/v8-${pkgver}/third_party/jinja2
  ln -sf "${srcdir}"/jinja2 "${srcdir}"/v8-${pkgver}/third_party
  rm -rf "${srcdir}"/v8-${pkgver}/third_party/markupsafe
  ln -sf "${srcdir}"/markupsafe "${srcdir}"/v8-${pkgver}/third_party

  _cflags=""
  case ${CARCH} in
    x86_64)
      _cflags="$_cflags -DV8_COMPRESS_POINTERS -DV8_31BIT_SMIS_ON_64BIT_ARCH"
    ;;
  esac

  sed "s/@VERSION@/$pkgver/g" -i "$srcdir/v8_libbase.pc"
  sed "s/@CFLAGS@/$_cflags/g" -i "$srcdir/v8_libbase.pc"
  sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/v8_libbase.pc"

  sed "s/@VERSION@/$pkgver/g" -i "$srcdir/v8_libplatform.pc"
  sed "s/@CFLAGS@/$_cflags/g" -i "$srcdir/v8_libplatform.pc"
  sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/v8_libplatform.pc"

  sed "s/@VERSION@/$pkgver/g" -i "$srcdir/v8.pc"
  sed "s/@CFLAGS@/$_cflags/g" -i "$srcdir/v8.pc"
  sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/v8.pc"
}

build() {
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi

  case ${CARCH} in
    i686)
      _arch=x86
    ;;
    x86_64)
      _arch=x64
    ;;
  esac
  case ${BUILDTYPE} in
    Release)
      _is_debug=false
    ;;
    Debug)
      _is_debug=true
    ;;
  esac

  export DEPOT_TOOLS_WIN_TOOLCHAIN=0
  cd "${srcdir}"/gn
  ninja -C out/${CARCH}

  cd "${srcdir}"/v8-${pkgver}
  "${srcdir}"/gn/out/${CARCH}/gn gen "${PWD}/out/${BUILDTYPE}-${CARCH}" --args="is_debug=${_is_debug} is_component_build=true target_cpu=\"${_arch}\" is_clang=false use_custom_libcxx=false v8_enable_verify_heap=false icu_use_data_file=false enable_iterator_debugging=${_is_debug} v8_use_external_startup_data=true"
  ninja -C "${PWD}/out/${BUILDTYPE}-${CARCH}" :v8_libbase :v8_libplatform :v8 :d8 :torque :torque-language-server :mksnapshot
}

check() {
  cd "${_realname}-${pkgver}"
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi
}

package() {
  cd "${_realname}-${pkgver}"
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi

  install -d "$pkgdir"/${MINGW_PREFIX}/bin
  install -Dm755 out/${BUILDTYPE}-${CARCH}/d8.exe "$pkgdir"/${MINGW_PREFIX}/bin/d8.exe
  install -Dm755 out/${BUILDTYPE}-${CARCH}/torque.exe "$pkgdir"/${MINGW_PREFIX}/bin/torque.exe
  install -Dm755 out/${BUILDTYPE}-${CARCH}/torque-language-server.exe "$pkgdir"/${MINGW_PREFIX}/bin/torque-language-server.exe
  install -Dm755 out/${BUILDTYPE}-${CARCH}/mksnapshot.exe "$pkgdir"/${MINGW_PREFIX}/bin/mksnapshot.exe
  install -Dm755 out/${BUILDTYPE}-${CARCH}/*.dll "$pkgdir"/${MINGW_PREFIX}/bin/
  install -Dm644 out/${BUILDTYPE}-${CARCH}/snapshot_blob.bin "$pkgdir"/${MINGW_PREFIX}/bin/snapshot_blob.bin

  install -d "$pkgdir"/${MINGW_PREFIX}/lib
  install -Dm755 out/${BUILDTYPE}-${CARCH}/*.a "$pkgdir"/${MINGW_PREFIX}/lib/

  install -d "$pkgdir"/${MINGW_PREFIX}/include
  install -Dm644 include/*.h "$pkgdir"/${MINGW_PREFIX}/include
  install -d "$pkgdir"/${MINGW_PREFIX}/include/cppgc
  install -Dm644 include/cppgc/*.h "$pkgdir"/${MINGW_PREFIX}/include/cppgc
  install -d "$pkgdir"/${MINGW_PREFIX}/include/cppgc/internal
  install -Dm644 include/cppgc/internal/*.h "$pkgdir"/${MINGW_PREFIX}/include/cppgc/internal
  install -d "$pkgdir"/${MINGW_PREFIX}/include/libplatform
  install -Dm644 include/libplatform/*.h "$pkgdir"/${MINGW_PREFIX}/include/libplatform

  install -d "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig
  install -m644 "$srcdir/v8_libbase.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/
  install -m644 "$srcdir/v8_libplatform.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/
  install -m644 "$srcdir/v8.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/

  install -d "$pkgdir"/${MINGW_PREFIX}/share/licenses/v8
  install -m644 LICENSE* "$pkgdir"/${MINGW_PREFIX}/share/licenses/v8
}
