# Description: A cross-platform application and UI framework
# URL: https://www.qt.io/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: at-spi2-core harfbuzz libb2 libtiff libxkbcommon mesa xorg-xcb-util-cursor xorg-xcb-util-keysyms xorg-xcb-util-wm
# Optional: brotli cups double-conversion gtk3 icu krb5 libinput libproxy mariadb md4c pipewire postgresql pulseaudio tslib unixodbc xdg-utils xmlstarlet vulkan-headers

name=qt6-base
version=6.7.0
release=1
source=(https://download.qt.io/official_releases/qt/${version%.*}/$version/submodules/qtbase-everywhere-src-$version.tar.xz
  qt6-base-cflags.patch
  qt6-base-nostrip.patch
  qtbase-6.5.3-xkbcommon160.patch)

build() {
  patch -d qtbase-everywhere-src-$version -p1 -i $SRC/qt6-base-cflags.patch
  patch -d qtbase-everywhere-src-$version -p1 -i $SRC/qt6-base-nostrip.patch
  patch -d qtbase-everywhere-src-$version -p1 -i $SRC/qtbase-6.5.3-xkbcommon160.patch

  prt-get isinst ccache && PKGMK_QT6+=' -D QT_USE_CCACHE=ON' && PATH="$(echo ${PATH} | awk -v RS=: -v ORS=: '/ccache/ {next} {print}' | sed 's/:*$//')"

  if [[ -e /usr/include/xkbcommon/xkbcommon-x11.h ]]; then
    PKGMK_QT6+=' -D QT_FEATURE_xcb=ON -D QT_FEATURE_xkbcommon_x11=ON'
  else
    PKGMK_QT6+=' -D QT_FEATURE_xcb=OFF -D QT_FEATURE_xkbcommon_x11=OFF'
    printf '\e[33m%s\e[m\n' "
Your currently installed version of opt/libxkbcommon is not providing
support for the x11-platform.
This is not mandatory at this point, but depending on your needs, you may
want to install xorg/xkeyboard-config, rebuild opt/libxkbcommon and then
build opt/qt6-base again.
"
  fi

  cmake -S qtbase-everywhere-src-$version -B build -G Ninja $PKGMK_QT6 \
    -D BUILD_SHARED_LIBS=ON \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_CXX_FLAGS_RELEASE="$CXXFLAGS -flto=auto" \
    -D CMAKE_C_FLAGS_RELEASE="$CFLAGS -flto=auto" \
    -D CMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
    -D BUILD_WITH_PCH=OFF \
    -D INSTALL_BINDIR=lib/qt6/bin \
    -D INSTALL_PUBLICBINDIR=usr/bin \
    -D INSTALL_LIBEXECDIR=lib/qt6 \
    -D INSTALL_DOCDIR=share/doc/qt6 \
    -D INSTALL_ARCHDATADIR=lib/qt6 \
    -D INSTALL_DATADIR=share/qt6 \
    -D INSTALL_INCLUDEDIR=include/qt6 \
    -D INSTALL_MKSPECSDIR=lib/qt6/mkspecs \
    -D INSTALL_PLUGINSDIR=lib/qt6/plugins \
    -D INSTALL_QMLDIR=lib/qt6/qml \
    -D INSTALL_SYSCONFDIR=/etc/xdg \
    -D INSTALL_TRANSLATIONSDIR=share/qt6/translations \
    -D INSTALL_EXAMPLESDIR=share/doc/qt6/examples \
    -D QT_FEATURE_no_direct_extern_access=ON \
    -D QT_FEATURE_openssl_linked=ON \
    -D QT_FEATURE_system_sqlite=ON \
    -D QT_BUILD_TESTS_BY_DEFAULT=OFF \
    -D QT_FEATURE_reduce_relocations=OFF \
    -D QT_FEATURE_androiddeployqt=OFF \
    -D QT_FEATURE_rpath=OFF \
    -Wno-dev

  cmake --build build
  DESTDIR=$PKG cmake --install build

  rm -r $PKG/usr/share/doc

  # Install useful symlinks
  cd $PKG
  mkdir -p usr/bin
  while read -r _line; do
    ln -sv $_line
  done < $SRC/build/user_facing_tool_links.txt
}
