# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

pkgname=static-compat-qt6-tools
_qtver=6.10.0
pkgver=${_qtver/-/}
pkgrel=2
arch=(x86_64)
url='https://www.qt.io'
license=(GPL-3.0-only
         LGPL-3.0-only
         LicenseRef-Qt-Commercial
         Qt-GPL-exception-1.0)
pkgdesc='A cross-platform application and UI framework (Development Tools, QtHelp)'
depends=(static-compat-qt6-base hicolor-icon-theme)
makedepends=(static-compat-cmake static-compat-qt6-declarative static-compat-vulkan-headers llvm clang ninja)
optdepends=('clang: for qdoc and lupdate')
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
_pkgfqn="qttools-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"
        '0001-Enable-only-SQL-plugins-which-are-known-to-work.patch')
sha256sums=('d86d5098cf3e3e599f37e18df477e65908fc8f036e10ea731b3469ec4fdbd02a'
            '82b7b7405760e302465536afd62311105cf4e2a001040ce552b628e3f90bcd9c')

prepare () {
  cd $_pkgfqn

  # apply patches; further descriptions can be found in patch files itself
  for patch in "$srcdir/"*.patch; do
    msg2 "Applying patch $patch"
    patch -p1 -i "$patch"
  done
}

build() {
  check_buildoption ccache y && ccache_args='
    -DCMAKE_C_COMPILER_LAUNCHER=ccache
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache'
  source static-compat-environment
  export PATH=$PWD:$PATH
  printf '#!/usr/bin/bash\nexec /usr/bin/pkg-config --static "$@"\n' > pkg-config
  chmod +x pkg-config
  # use llvm/clang from regular prefix (by linking into a custom prefix as we must avoid `-isystem /usr/include`)
  # note: Depending on Arch's normal LLVM/Clang is ok here as those tools are only used during build time and not
  #       distributed.
  [[ -e prefix ]] && rm -r prefix
  mkdir -p prefix/bin
  pacman -Ql llvm clang | grep /usr/bin | cut -d ' ' -f 2 | xargs ln -frs --target-directory=prefix/bin
  mkdir -p prefix/include
  ln -frs --target-directory=prefix/include /usr/include/{llvm,clang}{,-c}
  mkdir -p prefix/lib/cmake
  cp -r   --target-directory=prefix/lib/cmake /usr/lib/cmake/llvm
  ln -frs --target-directory=prefix/lib/cmake /usr/lib/cmake/clang
  ln -frs --target-directory=prefix/lib /usr/lib/*LLVM*
  ln -frs --target-directory=prefix/lib /usr/lib/libllvm*
  ln -frs --target-directory=prefix/lib /usr/lib/libLTO*
  ln -frs --target-directory=prefix/lib /usr/lib/libRemarks*
  ln -frs --target-directory=prefix/lib /usr/lib/libclang*
  ln -frs --target-directory=prefix/lib /usr/lib/clang
  # ensure targets linking against system LLVM are also linked against system libc/libdl/libm
  echo 'set_target_properties(LLVM PROPERTIES
  INTERFACE_LINK_LIBRARIES "/usr/lib/libpthread.so.0;/usr/lib/libm.so.6;/usr/lib/libmvec.so.1;/usr/lib/libdl.so.2;/usr/lib64/ld-linux-x86-64.so.2;/usr/lib/libc.so.6;/usr/lib/libc_nonshared.a;/usr/lib/ld-linux-x86-64.so.2")' \
    >> prefix/lib/cmake/llvm/LLVMExports-release.cmake
  static-compat-cmake -G Ninja -B build -S $_pkgfqn \
    $ccache_args \
    -DCMAKE_SKIP_INSTALL_RPATH=ON \
    -DGLIB2_USE_PKG_CONFIG=ON \
    -DWAYLAND_USE_PKG_CONFIG=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_harfbuzz=TRUE \
    -DQT_FEATURE_static_runtime=ON \
    -DFEATURE_pkg_config=ON \
    -DLLVM_DIR="$PWD/prefix/lib/cmake/llvm" \
    -DClang_DIR="$PWD/prefix/lib/cmake/clang"
  # ensure system libc/libdl/libm is present on the linker-line before other libs
  sed -i -E 's|(LINK_LIBRARIES = )(.*libLLVM.*)|\1/usr/lib/libpthread.so.0 /usr/lib/libm.so.6 /usr/lib/libmvec.so.1 /usr/lib/libdl.so.2 /usr/lib64/ld-linux-x86-64.so.2 /usr/lib/libc.so.6 \2|g' build/build.ninja
  VERBOSE=1 cmake --build build
}

package() {
  source static-compat-environment
  depends+=('libLLVM.so')
  DESTDIR="$pkgdir" cmake --install build

  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "$pkgdir/$static_compat_prefix/lib" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/static-compat-qt6-base "$pkgdir"/usr/share/licenses/$pkgname
}
