#!/bin/mkpkg
# description: Open-source web browser
# url: https://www.chromium.org/
#
# Patches:
#   copium     — musl basics (Alpine/selfisekai)
#   gentoo     — system toolchain support (Matt Jolly)
#   patches/   — musl deep fixes (Chimera cports + flux)

name=chromium
version=146.0.7680.164
release=1
_copium=146.4
_gentoo=146-1
depends=(gtk3 nss nspr harfbuzz icu libpng libjpeg-turbo libwebp
  zlib libxml2 fontconfig freetype libffi libxkbcommon alsa-lib
  wayland mesa libdrm dbus)
makedeps=(gn ninja rust rust-bindgen python3 nodejs flatbuffers gperf
  bsd-headers gcc-compat)
source=(https://github.com/chromium-linux-tarballs/chromium-tarballs/releases/download/$version/chromium-$version-linux.tar.xz
  https://registry.npmjs.org/@rollup/wasm-node/-/wasm-node-4.53.3.tgz
  https://codeberg.org/selfisekai/copium/archive/$_copium.tar.gz
  https://gitlab.com/Matt.Jolly/chromium-patches/-/archive/$_gentoo/chromium-patches-$_gentoo.tar.gz)

patch() {
  cd chromium-$version

  # Replace bundled rollup with wasm-node (pure WASM — no native binary needed on musl)
  rm -rf third_party/devtools-frontend/src/node_modules/rollup
  cp -a "$SRC"/package \
    third_party/devtools-frontend/src/node_modules/rollup

  # Copium patches (musl basics)
  for p in "$SRC"/copium/cr*.patch; do
    case "$p" in *rust-pre1.8*|*rust-pre1.90*|*no-unrar*) continue ;; esac
    patch -Np1 -i "$p"
  done

  # Gentoo patches (system toolchain)
  patch -Np1 -i "$SRC"/chromium-patches-$_gentoo/chromium-145-compiler.patch
  patch -Np1 -i "$SRC"/chromium-patches-$_gentoo/chromium-144-bindgen-custom-toolchain.patch
  patch -Np1 -i "$SRC"/chromium-patches-$_gentoo/chromium-117-material-color-include.patch
  patch -Np1 -i "$SRC"/chromium-patches-$_gentoo/chromium-145-revert-rustfmt.patch || true

  # flux musl patches
  for p in \
    008-fstatat64 \
    016-musl-sandbox \
    017-musl-tid-caching \
    019-musl-no-execinfo \
    020-musl-no-mallinfo \
    021-musl-no-res-ninit \
    022-no-sandbox-settls \
    027-temp-failure-retry \
    033-perfetto-get-thread-name \
    035-breakpad-no-getcontext \
    disable-dns_config_service \
    noclanglibs \
    sanitizer-no-death-callback \
    terminal-fdset \
    llvm21-fixes \
  ; do
    patch -Np1 -i "$PKGMK_SOURCE_DIR"/patches/$p.patch
  done

  # Rust target triple
  sed -i 's/unknown-linux-gnu/unknown-linux-musl/g' build/config/rust.gni
  echo 'x86_64-unknown-linux-musl' >> build/rust/known-target-triples.txt

  # Enable nightly Rust features
  sed -i '1a import os; os.environ["RUSTC_BOOTSTRAP"] = "1"' \
    build/rust/gni_impl/rustc_wrapper.py

  # Strip -Werror
  sed -i 's/-Werror//g' build/config/compiler/BUILD.gn

  # musl: libc++ rune table
  sed -i '1i #define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE' \
    third_party/libc++/src/include/__config

  # musl: guard glibc-only stack trace helper
  sed -i 's/OutputToStreamWithPrefixImpl(os, prefix_string);/#if defined(__GLIBC__)\n  OutputToStreamWithPrefixImpl(os, prefix_string);\n#endif/' \
    base/debug/stack_trace.cc

  # Fix dbus::Bus incomplete type
  sed -i '/chrome_browser_main_linux.h/a #include "dbus/bus.h"' \
    chrome/browser/chrome_browser_main_linux.cc
}

sha256sums=(
    "ea7350702cb827c94b786fc1fa51c24c0401e782c9c08dd7ca25a676da4a8f8c"
    "04b101456db24503a6facaade777f4bfb58909195208d4dbbf7dc4217bf71289"
    "bd7bd0984d048d6844195f4aa18b24a0f56603814c243ed32190746d8ba8d7c5"
    "96d6562120964b0c9e1a2bedf4335e0bf20f5e7d67a443bb26c651b0167a6a6b"
)

build() {
  cd chromium-$version

  export CC=clang CXX=clang++ AR=llvm-ar NM=llvm-nm RANLIB=llvm-ranlib LD=ld.lld
  export CFLAGS="-O2 -pipe -Wno-unknown-warning-option -Wno-builtin-macro-redefined -Wno-deprecated-declarations"
  export CXXFLAGS="$CFLAGS -Wno-invalid-constexpr"
  export LDFLAGS="-fuse-ld=lld"
  export RUSTC_BOOTSTRAP=1
  export DEPOT_TOOLS_UPDATE=0
  export VPYTHON_BYPASS="manually managed python not supported by chrome operations"
  export PKG_CONFIG_PATH=/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
  export HOME="$PWD"/fake-home && mkdir -p "$HOME"

  # System node + toolchain stubs
  mkdir -p third_party/node/linux/node-linux-x64/bin
  ln -sf /usr/bin/node third_party/node/linux/node-linux-x64/bin/node
  mkdir -p third_party/llvm-build/Release+Asserts/bin
  ln -sf /usr/bin/clang   third_party/llvm-build/Release+Asserts/bin/
  ln -sf /usr/bin/clang++ third_party/llvm-build/Release+Asserts/bin/

  # Stub node version check
  printf '#!/usr/bin/env python3\nimport sys; open(sys.argv[sys.argv.index("--out_file")+1], "w").write("ok")\n' \
    > third_party/node/check_version.py

  mkdir -p out/Release
  cat > out/Release/args.gn << EOF
is_clang = true
use_lld = true
is_debug = false
is_official_build = true
symbol_level = 0
is_musl = true
custom_toolchain = "//build/toolchain/linux/unbundle:default"
host_toolchain = "//build/toolchain/linux/unbundle:default"
rust_sysroot_absolute = "$(rustc --print sysroot)"
rust_bindgen_root = "/usr"
bindgen_libclang_path = "/usr/lib"
clang_base_path = "/usr"
rustc_version = "0"
use_sysroot = false
treat_warnings_as_errors = false
fatal_linker_warnings = false
clang_use_chrome_plugins = false
use_custom_libcxx = true
use_safe_libstdcxx = false
chrome_pgo_phase = 0
is_cfi = false
use_thin_lto = false
use_clang_modules = false
blink_enable_generated_code_formatting = false
use_system_harfbuzz = true
use_system_libffi = true
use_bundled_fontconfig = false
use_ozone = true
ozone_platform_wayland = true
ozone_platform_x11 = false
ozone_platform_headless = true
use_xkbcommon = true
use_alsa = true
use_pulseaudio = false
rtc_use_pipewire = false
use_vaapi = true
use_dbus = true
use_cups = false
use_kerberos = false
enable_widevine = false
enable_hangout_services_extension = false
enable_nocompile_tests = false
safe_browsing_use_unrar = false
proprietary_codecs = true
ffmpeg_branding = "Chrome"
enable_ml_internal = false
target_cpu = "x64"
EOF

  gn gen out/Release || return 1

  # Remove bogus -I/include from generated ninja files
  find out/Release -name '*.ninja' -exec sed -i 's| -I/include||g; s| -isystem/include||g' {} +

  # System flatbuffers
  ln -sf /usr/bin/flatc out/Release/flatc

  ulimit -n 4096
  ninja -C out/Release -j8 chrome chrome_crashpad_handler || return 1

  # Install
  d="$PKG/usr/lib/chromium"
  install -d "$d" "$PKG/usr/bin"

  install -m755 out/Release/chrome "$d/"
  install -m755 out/Release/chrome_crashpad_handler "$d/"

  # Data files
  install -m644 out/Release/*.pak "$d/"
  install -m644 out/Release/*.bin "$d/" 2>/dev/null || true
  [ -f out/Release/icudtl.dat ] && install -m644 out/Release/icudtl.dat "$d/"

  # Shared libraries (skip .TOC build artifacts)
  for f in out/Release/*.so out/Release/*.so.*; do
    [ -f "$f" ] && case "$f" in *.TOC) continue ;; esac && install -m644 "$f" "$d/"
  done

  # Vulkan SwiftShader ICD
  [ -f out/Release/vk_swiftshader_icd.json ] && install -m644 out/Release/vk_swiftshader_icd.json "$d/"

  # XDG helpers
  for f in xdg-mime xdg-settings; do
    [ -f out/Release/$f ] && install -m755 out/Release/$f "$d/"
  done

  # Locales — only en-US and el (Greek)
  install -d "$d/locales"
  for loc in en-US el; do
    install -m644 out/Release/locales/${loc}.pak "$d/locales/"
    for v in FEMININE MASCULINE NEUTER; do
      [ -f out/Release/locales/${loc}_${v}.pak ] && \
        install -m644 out/Release/locales/${loc}_${v}.pak "$d/locales/"
    done
  done

  # Wrapper
  printf '#!/bin/sh\nexec /usr/lib/chromium/chrome --ozone-platform-hint=auto "$@"\n' > "$PKG/usr/bin/chromium"
  chmod 755 "$PKG/usr/bin/chromium"
}

signify() {
    untrusted comment: public key
    RWTZ9IduCSQ/mL8337TEUinPwT92xFEUpD92hkS7IxcOnzTt9QdpohT3
}

# vim: filetype=sh
