PKGNAME=qt-6
PKGSEC=x11
PKGSUG=qt-6-doc
PKGDES="Qt version 6"

# FIXME: qdoc-qt6 needs to specify llvm dir, so using specific version of llvm
PKGDEP="alsa-lib bluez cups dbus desktop-file-utils double-conversion ffmpeg \
        fontconfig geoclue2 glib gst-plugins-base-1-0 harfbuzz llvm-runtime-20 \
        hicolor-icon-theme hyphen icu lcms2 libb2 libdrm libevent libinput \
        libglvnd libjpeg-turbo libmng libpng libproxy libvpx libwebp \
        libxkbcommon libxml2 libxslt mesa minizip opus pciutils pcre2 perl \
        protobuf pipewire postgresql-runtime pulseaudio re2 snappy sqlite \
        srtp systemd tslib vulkan x11-lib xcb-util xcb-util-cursor \
        xcb-util-image xcb-util-keysyms xcb-util-renderutil xcb-util-wm \
        xdg-utils zlib libxkbcommon vulkan html5lib-python"
PKGDEP__LOONGSON3="${PKGDEP/llvm-runtime-20/llvm-runtime-21}"

# FIXME: nodejs-24 at loongarch64 will get RuntimeError:
# Command '/usr/bin/node ../../../../../../qtwebengine/src/3rdparty/chromium/
# third_party/devtools-frontend/src/node_modules/@rollup/wasm-node/dist/bin/
# rollup --silent --config ../../../../../../qtwebengine/src/3rdparty/chromium/
# third_party/devtools-frontend/src/scripts/build/rollup.config.mjs --input
# gen/third_party/devtools-frontend/src/front_end/third_party/intl-messageformat/
# intl-messageformat.prebundle.js --file gen/third_party/devtools-frontend/src/
# front_end/third_party/intl-messageformat/intl-messageformat.js' failed
BUILDDEP="gperf gtk-3 mariadb nodejs-22 openssl postgresql python-3 \
          unixodbc ninja cmake llvm-20"
BUILDDEP__NOWEBENGINE=" \
          ${BUILDDEP/nodejs-22/}"
BUILDDEP__LOONGSON3="${BUILDDEP__NOWEBENGINE}"
BUILDDEP__PPC64EL="${BUILDDEP__NOWEBENGINE}"
BUILDDEP__RISCV64="${BUILDDEP__NOWEBENGINE}"
BUILDDEP__LOONGSON3="${BUILDDEP/llvm-20/llvm-21}"

PKGDEP__NOWEBENGINE="alsa-lib bluez cups dbus desktop-file-utils \
        double-conversion fontconfig geoclue2 gst-plugins-base-1-0 glib \
        harfbuzz hicolor-icon-theme hyphen icu libb2 libdrm libinput \
        libglvnd libjpeg-turbo libmng libpng libproxy libxkbcommon libxslt \
        mesa pcre2 perl protobuf pulseaudio re2 snappy sqlite srtp systemd \
        tslib vulkan x11-lib xcb-util xcb-util-cursor xcb-util-renderutil \
        xcb-util-image xcb-util-keysyms xcb-util-wm xdg-utils zlib \
        libxkbcommon vulkan"
PKGDEP__LOONGSON3="${PKGDEP__NOWEBENGINE}"
PKGDEP__PPC64EL="${PKGDEP__NOWEBENGINE}"
PKGDEP__RISCV64="${PKGDEP__NOWEBENGINE}"


ABTYPE=cmakeninja
# To query possible build options, you may want to check following files in
# Qt 6 source tree: qtbase/configure, qtbase/config_help.txt,
# qtbase/cmake/configure-cmake-mapping.md and
# qtbase/cmake/QtProcessConfigureArgs.cmake.
# It's also helpful to refer to build options used by other distributions.
CMAKE_AFTER=(
    '-DINSTALL_BINDIR=lib/qt6/bin'
    '-DINSTALL_DOCDIR=share/doc/qt-6'
    '-DINSTALL_INCLUDEDIR=include/qt6'
    '-DINSTALL_ARCHDATADIR=lib/qt6'
    '-DINSTALL_MKSPECSDIR=lib/qt6/mkspecs'
    '-DINSTALL_DATADIR=share/qt6'
    '-DINSTALL_SYSCONFDIR=/etc/xdg'
    '-DLLVM_INSTALL_DIR=/usr/lib/llvm-20'
    '-DINSTALL_EXAMPLESDIR=share/doc/qt-6/examples'
    '-DBUILD_SHARED_LIB=ON'
    '-DCMAKE_SKIP_RPATH=OFF'
    '-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON'
    '-DQT_BUILD_TESTS=OFF'
    '-DQT_FEATURE_use_gold_linker=OFF'
    '-DQT_FEATURE_reduce_relocations=OFF'
    '-DQT_FEATURE_system_sqlite=ON'
    '-DQT_FEATURE_system_harfbuzz=ON'
    '-DQT_FEATURE_system_png=ON'
    '-DQT_FEATURE_system_pcre2=ON'
    '-DQT_FEATURE_system_jpeg=ON'
    '-DQT_FEATURE_system_zlib=ON'
    '-DINPUT_dbus=linked'
    '-DINPUT_openssl=linked'
    '-DQT_FEATURE_sql_psql=ON'
    '-DQT_FEATURE_sql_sqlite=ON'
    '-DQT_FEATURE_sql_odbc=ON'
    '-DQT_FEATURE_accessibility=ON'
    '-DQT_FEATURE_fontconfig=ON'
    '-DQT_FEATURE_glib=ON'
    '-DQT_FEATURE_icu=ON'
    '-DQT_FEATURE_journald=ON'
    '-DQT_FEATURE_libinput=ON'
    '-DQT_FEATURE_libproxy=ON'
    '-DQT_FEATURE_vulkan=ON'
    '-DQT_FEATURE_qtpdf_build=ON'
    '-DQT_FEATURE_webengine_full_debug_info=ON'
    '-DQT_FEATURE_webengine_extensions=ON'
    '-DQT_FEATURE_webengine_system_ffmpeg=OFF'
    '-DQT_FEATURE_webengine_system_libevent=ON'
    '-DQT_FEATURE_webengine_system_libxslt=ON'
    '-DQT_FEATURE_webengine_proprietary_codecs=ON'
    '-DQT_FEATURE_webengine_kerberos=ON'
    '-DQT_FEATURE_webengine_webrtc=ON'
    '-DQT_FEATURE_webengine_webrtc_pipewire=ON'
)

CMAKE_AFTER__AMD64=(
    "${CMAKE_AFTER[@]}"
    '-DQT_FEATURE_sse2=ON'
)
# Force the use of LLD and consistent LTO unit splitting on ARM64.
# 1. Use LLD to prevent "relocation truncated to fit: R_AARCH64_CALL26" errors, 
#    as GNU ld (bfd) cannot handle the massive jump offsets in QtWebEngine/Chromium.
# 2. Add -fsplit-lto-unit to CFLAGS/CXXFLAGS to match Chromium's internal LTO 
#    logic and avoid "ld.lld: error: inconsistent LTO Unit splitting" during linking.
CMAKE_AFTER__ARM64=(
    "${CMAKE_AFTER[@]}"
    '-DQT_FEATURE_use_lld_linker=ON'
    "-DCMAKE_C_FLAGS=${CFLAGS} -fsplit-lto-unit"
    "-DCMAKE_CXX_FLAGS=${CXXFLAGS} -fsplit-lto-unit"
)
CMAKE_AFTER__NOWEBENGINE=(
    "${CMAKE_AFTER[@]}"
    '-DBUILD_qtwebengine=OFF'
)
CMAKE_AFTER__LOONGSON3=(
    "${CMAKE_AFTER__NOWEBENGINE[@]/-DLLVM_INSTALL_DIR=\/usr\/lib\/llvm-20/-DLLVM_INSTALL_DIR=\/usr\/lib\/llvm-21}"
)
# FIXME: qtquick3dphysics' PhysX bindings is not supported on loongarch64, ppc*, riscv64.
CMAKE_AFTER__LOONGARCH64=(
    "${CMAKE_AFTER[@]}"
    '-DBUILD_qtquick3dphysics=OFF'
)
CMAKE_AFTER__LOONGARCH64_NOSIMD=(
    "${CMAKE_AFTER__LOONGARCH64[@]}"
)
CMAKE_AFTER__PPC64EL=(
    "${CMAKE_AFTER__NOWEBENGINE[@]}"
    '-DBUILD_qtquick3dphysics=OFF'
)
CMAKE_AFTER__RISCV64=(
    "${CMAKE_AFTER__NOWEBENGINE[@]}"
    '-DBUILD_qtquick3dphysics=OFF'
)

# Note: Needed for libQt6QmlDom.a
NOSTATIC=0

# Does not work if built with qt 6.6
PKGBREAK="fcitx5-qt<=1:5.1.3-1 fcitx-qt5<=1.2.7-3"

# NOTE: GNU ld (bfd) struggles with the massive size of QtWebEngine on arm64,
# causing R_AARCH64_CALL26 relocation truncation errors.
# Switching to clang + lld is required to successfully link the binaries with LTO enabled.
# Reference: https://bugs.gentoo.org/918897
USECLANG__ARM64=1

#NOTE: As of GCC 15.2.0, GCC does not support -flax-vector-conversions=all, use llvm instead.
USECLANG__LOONGARCH64=1
USECLANG__LOONGARCH64_NOSIMD=1
