# 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 fontconfig 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 vulkan-headers xdg-utils xmlstarlet

name=qt6-base
version=6.11.1
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
)

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

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

  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

  rm -r qtbase-everywhere-src-$version/src/3rdparty/{harfbuzz-ng,freetype,libjpeg,libpng,zlib}

  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=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_UNITY_BUILD=ON \
    -D QT_FEATURE_testlib=ON \
    -D QT_INTERNAL_AVOID_OVERRIDING_SYNCQT_CONFIG=ON \
    -D QT_BUILD_TESTS_BY_DEFAULT=OFF \
    -D QT_FEATURE_reduce_relocations=OFF \
    -D QT_FEATURE_relocatable=OFF \
    -D QT_FEATURE_androiddeployqt=OFF \
    -D QT_FEATURE_rpath=OFF \
    -Wno-dev

  cmake --build build -j ${JOBS-1}
  DESTDIR=$PKG cmake --install build

  rm -r $PKG/usr/share/doc
}
