# Maintainer: Evangelos Foutras <foutrelis@archlinux.org>
# Contributor: Jan "heftig" Steffens <jan.steffens@gmail.com>

# Holo: Split the clang and clang-libs packages (https://gitlab.archlinux.org/archlinux/packaging/packages/clang/-/merge_requests/1)
# Holo: Disable check() to speed up the build

pkgbase=clang
pkgname=(clang clang-libs)
pkgver=21.1.8
pkgrel=3
pkgdesc="C language family frontend for LLVM"
arch=('x86_64' 'aarch64')
url="https://clang.llvm.org/"
license=('Apache-2.0 WITH LLVM-exception')
makedepends=("llvm=$pkgver" 'cmake' 'ninja' 'python-sphinx' 'python-myst-parser'
  'python>=3.13'
  'python<3.15'
)
# Holo: Avoid building debug packages
options=('!debug')
_source_base=https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver
source=($_source_base/clang-$pkgver.src.tar.xz{,.sig}
        $_source_base/clang-tools-extra-$pkgver.src.tar.xz{,.sig}
        $_source_base/llvm-$pkgver.src.tar.xz{,.sig}
        $_source_base/cmake-$pkgver.src.tar.xz{,.sig}
        $_source_base/third-party-$pkgver.src.tar.xz{,.sig}
        0001-Revert-clang-driver-When-fveclib-ArmPL-flag-is-in-us.patch
        enable-fstack-protector-strong-by-default.patch)
sha256sums=('6090e3f23720d003cdd84483a47d0eec6d01adbb5e0c714ac0c8b58de546aa62'
            'SKIP'
            '6ddb7e731bf09ab821132e2e8a3c706b6d9c52c92a9ce9a9ed0976a9c6c28829'
            'SKIP'
            'd9022ddadb40a15015f6b27e6549a7144704ded8828ba036ffe4b8165707de21'
            'SKIP'
            '85735f20fd8c81ecb0a09abb0c267018475420e93b65050cc5b7634eab744de9'
            'SKIP'
            '7fe99424384aea529ffaeec9cc9dfb8b451fd1852c03fc109e426fe208a1f1a7'
            'SKIP'
            '6f3cc7a4847e03d16ed90341bb71fec98427763ff2351f5344b2ca2ce62a604c'
            '65c2b77693b9380c4e9c08977bed163932fbd72eaaa4dc666bb7310d426f6887')
validpgpkeys=('474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard <tstellar@redhat.com>
              'D574BD5D1D0E98895E3BF90044F2485E45D59042'  # Tobias Hieta <tobias@hieta.se>
              'FFB3368980F3E6BB5737145A316C56D064CACBA5'  # Douglas Yung <douglas.yung@sony.com>
              '71046D1E9C6656BDD61171873E83BABF4A4F9E85'  # Cullen Rhodes <cullen.rhodes@arm.com>
)

# Utilizing LLVM_DISTRIBUTION_COMPONENTS to avoid
# installing static libraries; inspired by Gentoo
_get_distribution_components() {
  local target
  ninja -t targets | grep -Po 'install-\K.*(?=-stripped:)' | while read -r target; do
    case $target in
      clang-libraries|distribution)
        continue
        ;;
      clang|clangd|clang-*)
        ;;
      clang*|findAllSymbols)
        continue
        ;;
    esac
    echo $target
  done
}

prepare() {
  rename -v -- "-$pkgver.src" '' {cmake,clang-tools-extra}-$pkgver.src
  cd clang-$pkgver.src
  mkdir build
  patch -Np2 -i ../enable-fstack-protector-strong-by-default.patch

  # Revert always linking against libamath when -fveclib=ArmPL
  patch -Np2 -i ../0001-Revert-clang-driver-When-fveclib-ArmPL-flag-is-in-us.patch
}

build() {
  cd clang-$pkgver.src/build

  # Build only minimal debug info to reduce size
  CFLAGS=${CFLAGS/-g /-g1 }
  CXXFLAGS=${CXXFLAGS/-g /-g1 }

  local cmake_args=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_DOCDIR=share/doc
    -DCMAKE_SKIP_RPATH=ON
    -DCLANG_DEFAULT_PIE_ON_LINUX=ON
    -DCLANG_LINK_CLANG_DYLIB=ON
    -DENABLE_LINKER_BUILD_ID=ON
    -DLLVM_BUILD_DOCS=ON
    -DLLVM_BUILD_TESTS=ON
    -DLLVM_ENABLE_RTTI=ON
    -DLLVM_ENABLE_SPHINX=ON
    -DLLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR="$srcdir/clang-tools-extra"
    -DLLVM_EXTERNAL_LIT=/usr/bin/lit
    -DLLVM_INCLUDE_DOCS=ON
    -DLLVM_LINK_LLVM_DYLIB=ON
    -DLLVM_THIRD_PARTY_DIR="$srcdir/third-party-$pkgver.src"
    -DSPHINX_WARNINGS_AS_ERRORS=OFF
    -DLLVM_PACKAGE_BUGREPORT=https://gitlab.archlinux.org/archlinux/packaging/packages/clang/-/issues
  )

  cmake .. "${cmake_args[@]}"
  local distribution_components=$(_get_distribution_components | paste -sd\;)
  test -n "$distribution_components"
  cmake_args+=(-DLLVM_DISTRIBUTION_COMPONENTS="$distribution_components")

  cmake .. "${cmake_args[@]}"
  ninja
}

check() {
  return 0 # Holo: disable check to speed up build
  cd clang-$pkgver.src/build
  LD_LIBRARY_PATH=$PWD/lib ninja check-clang{,-tools}
}

_python_optimize() {
  python -m compileall "$@"
  python -O -m compileall "$@"
  python -OO -m compileall "$@"
}

package_clang() {
  depends=('clang-libs' 'llvm-libs' 'gcc' 'compiler-rt')
  optdepends=('openmp: OpenMP support in clang with -fopenmp'
              'python: for scan-view and git-clang-format'
              'llvm: referenced by some clang headers')
  provides=("clang-analyzer=$pkgver" "clang-tools-extra=$pkgver")
  conflicts=('clang-analyzer' 'clang-tools-extra')
  replaces=('clang-analyzer' 'clang-tools-extra')

  cd clang-$pkgver.src/build

  DESTDIR="$pkgdir" ninja install-distribution
  install -Dm644 ../LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  # Remove documentation sources
  rm -r "$pkgdir"/usr/share/doc/clang{,-tools}/html/{_sources,.buildinfo}

  # The python and runtime libraries go into clang-libs
  mv -f "$pkgdir"/usr/lib/libclang{,-cpp}.so* "$srcdir"
  mv -f "$pkgdir"/usr/lib/{libear,libscanbuild} "$srcdir"
  mv -f "$pkgdir"/usr/libexec "$srcdir"
  mv -f "$pkgdir"/usr/bin/scan-build "$srcdir"

  # Move bash completion
  local bash_completion_destdir="$pkgdir/usr/share/bash-completion/completions"
  install -d $bash_completion_destdir
  mv "$pkgdir/usr/share/clang/bash-autocomplete.sh" "$bash_completion_destdir/clang"
}

package_clang-libs() {
  depends=('llvm-libs')
  optdepends=('python: for the python bindings')

  cd "$srcdir/$pkgbase-$pkgver.src/build"

  # Move in libraries ejected at end of package_clang
  install -d "$pkgdir/usr/lib"
  install -d "$pkgdir/usr/lib/clang"
  install -d "$pkgdir/usr/bin"
  cp -a "$srcdir"/libclang{,-cpp}.so* "$pkgdir/usr/lib"
  # These three moved again below, but keeping those blocks untouched for cleaner merges with upstream monopackage.
  cp -a "$srcdir"/{libear,libscanbuild} "$pkgdir/usr/lib"
  cp -av "$srcdir"/libexec "$pkgdir"/usr/
  cp -av "$srcdir"/scan-build "$pkgdir"/usr/bin/

  # Move scanbuild-py into site-packages and install Python bindings
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir/$site_packages"
  mv "$pkgdir"/usr/lib/{libear,libscanbuild} "$pkgdir/$site_packages/"
  cp -a ../bindings/python/clang "$pkgdir/$site_packages/"

  # Move analyzer scripts out of /usr/libexec
  mv "$pkgdir"/usr/libexec/* "$pkgdir/usr/lib/clang/"
  rmdir "$pkgdir/usr/libexec"
  sed -i 's|libexec|lib/clang|' \
    "$pkgdir/usr/bin/scan-build" \
    "$pkgdir/$site_packages/libscanbuild/analyze.py"

  # Compile Python scripts
  _python_optimize "$pkgdir/usr/share" "$pkgdir/$site_packages"
}

# vim:set ts=2 sw=2 et:
