# Maintainer: Rodrigo Hernandez <kwizatz@aeongames.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=v8
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=14.0.210
pkgrel=2
pkgdesc="Fast and modern Javascript engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://v8.dev"
msys2_references=(
  "cpe: cpe:/a:google:v8"
)
msys2_repository_url='https://github.com/v8/v8'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
source=("https://github.com/v8/v8/archive/refs/tags/${pkgver}.tar.gz"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=cb592905b1f94a0c315dccb59939f91ef869592c"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=d6072980974f9a4922d2eb27e4fd244ca3017031"
        "bare-clones/abseil-cpp::git+https://chromium.googlesource.com/chromium/src/third_party/abseil-cpp.git#commit=cae4b6a3990e1431caa09c7b2ed1c76d0dfeab17"
        "bare-clones/dragonbox::git+https://chromium.googlesource.com/external/github.com/jk-jeon/dragonbox.git#commit=6c7c925b571d54486b9ffae8d9d18a822801cbda"
        "bare-clones/fast_float::git+https://chromium.googlesource.com/external/github.com/fastfloat/fast_float.git#commit=cb1d42aaa1e14b09e1452cfdef373d051b8c02a4"
        "bare-clones/fp16::git+https://chromium.googlesource.com/external/github.com/Maratyszcza/FP16.git#commit=0a92994d729ff76a58f692d3028ca1b64b145d91"
        "bare-clones/googletest::git+https://chromium.googlesource.com/external/github.com/google/googletest.git#commit=309dab8d4bbfcef0ef428762c6fec7172749de0f"
        "bare-clones/highway::git+https://chromium.googlesource.com/external/github.com/google/highway.git#commit=00fe003dac355b979f36157f9407c7c46448958e"
        "bare-clones/jinja2::git+https://chromium.googlesource.com/chromium/src/third_party/jinja2.git#commit=c3027d884967773057bf74b957e3fea87e5df4d7"
        "bare-clones/markupsafe::git+https://chromium.googlesource.com/chromium/src/third_party/markupsafe.git#commit=4256084ae14175d38a3ff7d739dca83ae49ccec6"
        "bare-clones/simdutf::git+https://chromium.googlesource.com/chromium/src/third_party/simdutf.git#commit=a1046f20f7099b4f7dd72d7127bb4dc05252ec5c"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=bf1e8de6cc305fdab82db1b65bed76b70bd96c8b"
        "001-add-mingw-toolchain.patch"
        "002-buildflags-fixes.patch"
        "003-fix-macros-and-functions.patch"
        "004-fix-static-assert-implementations.patch"
        "005-fix-conflicting-macros.patch"
        "006-support-clang-in-mingw-mode.patch"
        "007-snapshot-use-system-zlib-header.patch"
        "008-prioritized-native-thread-on-windows.patch"
        "009-unicode-for-wide-char-functions.patch"
        "010-disable-msvc-hack.patch"
        "011-make-sure-that-__rdtsc-is-declared.patch"
        "012-remove-dllimport-attributes.patch"
        "013-builtin-deps-fixes.patch"
        "014-heap-use-proper-sources.patch"
        "015-abseil-build-as-static-lib.patch"
        "016-zlib-use-system-lib.patch"
        "icu.gn"
        "zlib.gn"
        "v8_libbase.pc"
        "v8_libplatform.pc"
        "v8.pc")
sha256sums=('a8d553be4130bc5e95e4bb7635958e8f79ecd34aef32de23d3bd7db0b3203760'
            '5eb934ca43dfaccad43358820d8a84f1b8229d6330d2e0db1e827081b6858b99'
            'f58b2605f036b8c9c7992635ad5f2070abe4d527314915874766d654ae4eb429'
            '812f67ab2c4269b101df3af90a452867398a668b46c4997cbbde4fffe6c5cbfa'
            '7a6c373f8694e540f5a767b315670ccef5526baa41f5427f02db2e4bf261765c'
            'be05ebb53b7468e246aac2a22d1ce748c25e2e0cc5d0227e16272a00827092ff'
            '5d0c4f261d36707f926fa9ef9a39349f1cccac8ae6443a8f8571c1625eb90c41'
            '716f953b914f025dacce3860f0329a49b08c24c7d3ae9bcecd315ecd6781637b'
            '5f359dae10e80599f26483879b879d7a6808520d4a56c98ee0879373a6339619'
            '79aa2b70328acdbf62fa738743f32cd7a9eb13c7a236f63ec7b77e955893d163'
            '81daaa3fe38b4b5569a18bff4daf06dea57e46fb1fd3b4930f9f9c423243c7b5'
            '238f5601a483dd886c0200b3e5a5d5c78751288bc37de81680b73fdf982a33be'
            '6478d94a8295db87759f756c99a7c079d7f3710e4fb996f9851d7f9ca8982f11'
            '1d01845d0092582fe329b93e319e6338b17e8598c89ada70bfc9a4d407cdd3f9'
            'ebd7b8e2b91e02879cb6c83154cb276be2a820f939b6d166e98b39c175b0eb51'
            '274d4cfc5a8a4f99699e67fed36565f315dd3bc0d1335bbf5070ba7052d99541'
            'aa7255002773aa3944027e0aab19627eee089c2ec3893713c8c7ff082e214553'
            '75a0d6fbf04cb9bbb724230104cf28595cd9b1169a0e91a4090bd0d3b6f80a8c'
            'c860f43dfaeb77b7567044a45bcbc6d5508e1571fbb31cb49cfdfbe86a031916'
            '1fca982ec6ede3b29d2692231569944ce0ad33519b47fd6fb01b0975c10eb6bd'
            '3c7b480cd4c19e8120c9adb27d7fd45b8d98c5bfc13c153479afa0636c319153'
            '4edcd5f7d2f9b52c8783e81b7f06f6e5b386aa6c0143cc2f9054754db53919cf'
            'e4d8e57914cfcf6c0ea299d0556d7b747271e5a7ec66e1d8d52394fcd0da34b6'
            '1ef6854b6a68101ab177a7a95389e9b03e7b8c4e3223320dfc9ce4405cbf5bfe'
            '1d03e6c517528a31f6ff38d251b011e34a667f0b60968817b573677501dc20c1'
            '37a29668fb02d4fa60062c02f0d24dce4ab9b7fce4d7f5a8e6eca745fa125be2'
            '5b1c2a1c4faefdbb918640b394a100b08c5b9940547d4ce5a04b8d9dc40559a7'
            '7e3555a4128d8b69cd9733b14b114d27063667ae0687b693c51fd1881f847e93'
            'a4275f41723cc990f2f43559ce716100830a8ce3f38e4878f6bf36d70d47b646'
            'e1bc88af48143f29f5805b8ac5d77addef1b09318d1f5f127a9281872c00303b'
            '7e1e39a3dc69632f7bf9182c92405186ce709aca921cf0abefadb6fa8b825510'
            '71ac09cff81a983c63cbeee405a18b092655e6c608da1ede3aacc568edb80b39'
            'aaeb6759ccbe875babdc48ab394e1a0cc8485731c9311827b3981ba6e85ee688'
            '75a1f195164d830524b02801c82f39628cd51fa15ba59472e37dde4ce97aecd5')

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

prepare() {
  echo ":: Patching build directory"
  cd "${srcdir}"/build
  patch -p1 -i "${srcdir}"/001-add-mingw-toolchain.patch
  python "${srcdir}"/build/util/lastchange.py -o "${srcdir}"/build/util/LASTCHANGE
  rm -rf "${srcdir}"/${_realname}-${pkgver}/build
  ln -sf "${srcdir}"/build "${srcdir}"/${_realname}-${pkgver}
  echo "build_with_chromium = false" > "${srcdir}"/${_realname}-${pkgver}/build/config/gclient_args.gni

  echo ":: Patching abseil-cpp directory"
  cd "${srcdir}"/abseil-cpp
  patch -p1 -i "${srcdir}"/015-abseil-build-as-static-lib.patch
  ln -sf "${srcdir}"/abseil-cpp "${srcdir}"/${_realname}-${pkgver}/third_party

  echo ":: Patching zlib directory"
  cd "${srcdir}"/zlib
  patch -p1 -i "${srcdir}"/016-zlib-use-system-lib.patch
  mkdir -p "${srcdir}"/${_realname}-${pkgver}/third_party/zlib
  ln -sf "${srcdir}"/zlib.gn "${srcdir}"/${_realname}-${pkgver}/third_party/zlib/BUILD.gn
  ln -sf "${srcdir}"/zlib/google "${srcdir}"/${_realname}-${pkgver}/third_party/zlib

  echo ":: Patching icu directory"
  mkdir -p "${srcdir}"/${_realname}-${pkgver}/third_party/icu
  ln -sf "${srcdir}"/icu.gn "${srcdir}"/${_realname}-${pkgver}/third_party/icu/BUILD.gn
  echo "icu_use_data_file = false" > "${srcdir}"/${_realname}-${pkgver}/third_party/icu/config.gni

  echo ":: Soft linking extra repos"
  mkdir -p "${srcdir}"/${_realname}-${pkgver}/third_party/{dragonbox,fast_float,fp16,googletest,highway}/src
  ln -sf "${srcdir}"/dragonbox/* "${srcdir}"/${_realname}-${pkgver}/third_party/dragonbox/src
  ln -sf "${srcdir}"/fast_float/* "${srcdir}"/${_realname}-${pkgver}/third_party/fast_float/src
  ln -sf "${srcdir}"/fp16/* "${srcdir}"/${_realname}-${pkgver}/third_party/fp16/src
  ln -sf "${srcdir}"/googletest/* "${srcdir}"/${_realname}-${pkgver}/third_party/googletest/src
  ln -sf "${srcdir}"/jinja2 "${srcdir}"/${_realname}-${pkgver}/third_party
  ln -sf "${srcdir}"/markupsafe "${srcdir}"/${_realname}-${pkgver}/third_party
  ln -sf "${srcdir}"/highway/* "${srcdir}"/${_realname}-${pkgver}/third_party/highway/src
  ln -sf "${srcdir}"/simdutf "${srcdir}"/${_realname}-${pkgver}/third_party
  ln -sf "${srcdir}"/clang "${srcdir}"/${_realname}-${pkgver}/tools

  echo ":: Patching v8 source"
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    002-buildflags-fixes.patch \
    003-fix-macros-and-functions.patch \
    004-fix-static-assert-implementations.patch \
    005-fix-conflicting-macros.patch \
    006-support-clang-in-mingw-mode.patch \
    007-snapshot-use-system-zlib-header.patch \
    008-prioritized-native-thread-on-windows.patch \
    009-unicode-for-wide-char-functions.patch \
    010-disable-msvc-hack.patch \
    011-make-sure-that-__rdtsc-is-declared.patch \
    012-remove-dllimport-attributes.patch \
    013-builtin-deps-fixes.patch \
    014-heap-use-proper-sources.patch

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|@PREFIX@|${MINGW_PREFIX}|g" -i "${srcdir}/v8_libbase.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|@PREFIX@|${MINGW_PREFIX}|g" -i "${srcdir}/v8_libplatform.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|@PREFIX@|${MINGW_PREFIX}|g" -i "${srcdir}/v8.pc"
}

build() {
  cd ${_realname}-${pkgver}

  local _buildtype=Release
  local _debug=false
  local _symbol=0
  if check_option "debug" "y"; then
    _buildtype=Debug
    _debug=true
    _symbol=2
  fi

  local _arch=x64
  local _cfg=true
  if [[ ${CARCH} == aarch64 ]]; then
    _arch=arm64
    _cfg=false
  fi

  local _clang=false
  if [[ ${MSYSTEM} == CLANG* ]]; then
    _clang=true
  fi

  local _gn_args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_clang=${_clang}
    use_custom_libcxx=false
    use_libcxx_modules=false
    use_lld=false
    use_siso=false
    use_sysroot=false
    v8_enable_etw_stack_walking=false
    v8_enable_fuzztest=false
    v8_enable_system_instrumentation=false
    v8_enable_temporal_support=false
    v8_enable_verify_heap=false
    v8_symbol_level=${_symbol}
    v8_use_external_startup_data=false
    chrome_pgo_phase=0
    clang_use_chrome_plugins=false
    enable_iterator_debugging=${_debug}
    enable_rust=false
    win_enable_cfg_guards=${_cfg}
    treat_warnings_as_errors=false"

  # generate build files for shared library
  gn gen out/${_buildtype}-${MSYSTEM} --args="${_gn_args} is_component_build=true"

  ninja -C out/${_buildtype}-${MSYSTEM} v8 d8 torque torque-language-server mksnapshot

  # generate build files for static library
  gn gen out/${_buildtype}-${MSYSTEM}-static --args="${_gn_args} is_component_build=false v8_enable_i18n_support=false v8_monolithic=true"

  ninja -C out/${_buildtype}-${MSYSTEM}-static v8_monolith
}

package() {
  cd ${_realname}-${pkgver}

  local _buildtype=Release
  if check_option "debug" "y"; then
    _buildtype=Debug
  fi

  install -d "${pkgdir}"${MINGW_PREFIX}/{bin,include/{cppgc/internal,libplatform},lib/pkgconfig,share/licenses/${_realname}}

  install -Dm755 out/${_buildtype}-${MSYSTEM}/d8.exe                      "${pkgdir}"${MINGW_PREFIX}/bin/d8.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/torque.exe                  "${pkgdir}"${MINGW_PREFIX}/bin/torque.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/torque-language-server.exe  "${pkgdir}"${MINGW_PREFIX}/bin/torque-language-server.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/mksnapshot.exe              "${pkgdir}"${MINGW_PREFIX}/bin/mksnapshot.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.dll                       "${pkgdir}"${MINGW_PREFIX}/bin/

  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.a                         "${pkgdir}"${MINGW_PREFIX}/lib/
  install -Dm755 out/${_buildtype}-${MSYSTEM}-static/obj/libv8_monolith.a "${pkgdir}"${MINGW_PREFIX}/lib/
  install -Dm644 "${srcdir}"/*.pc                                         "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/

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

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