File PKGBUILD of Package llvm-minimal-git

# Maintainer: Lone_Wolf <lone_wolf@klaas-de-kat.nl>
# Contributor:	blackmoon3  <https://github.com/blacksky3>
# Contributor: yurikoles <root@yurikoles.com>
# Contributor: bearoso <bearoso@gmail.com>
# Contributor: Luchesar V. ILIEV <luchesar%2eiliev%40gmail%2ecom>
# Contributor: Anders Bergh <anders@archlinuxppc.org>
# Contributor: Armin K. <krejzi at email dot com>
# Contributor: Christian Babeux <christian.babeux@0x80.ca>
# Contributor: Jan "heftig" Steffens <jan.steffens@gmail.com>
# Contributor: Evangelos Foutras <evangelos@foutrelis.com>
# Contributor: Hesiod (https://github.com/hesiod)
# Contributor: Roberto Alsina <ralsina@kde.org>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: Tomas Lindquist Olsen <tomas@famolsen.dk>
# Contributor: Tomas Wilhelmsson <tomas.wilhelmsson@gmail.com>


pkgbase=llvm-minimal-git
pkgname=(llvm-minimal-git llvm-libs-minimal-git clang-minimal-git clang-libs-minimal-git clang-opencl-headers-minimal-git)
pkgver=21~++20250523083553+672e9263cb58
pkgrel=1
arch=('x86_64')
url="https://llvm.org/"
license=('Apache-2.0 WITH LLVM-exception')
makedepends=(git cmake libffi libedit ncurses libxml2
             libxcrypt python python-setuptools zstd ninja)

_debdir="llvm-toolchain-snapshot_${pkgver}"
source=("https://apt.llvm.org/oracular/pool/main/l/llvm-toolchain-snapshot/${_debdir}.orig.tar.xz")
sha512sums=('SKIP')

# explicitly disable lto & debug to reduce number of build hangs , resources needed and runtime issues
options=(!lto !debug)

# Ninja & LIT by default use all available cores. this can lead to heavy stress on systems making them unresponsive.
# It can also cause the kernel oom killer to interfere and kill important tasks.
# A reasonable value to avoid these issues appears to be 75% of available cores.
# This PKGBUILD uses the env vars NINJAFLAGS & LITFLAGS to achieve this. They should be set on command line or in files read by your shell on login (like .bashrc ) .
# example for systems with 24 cores
# export NINJAFLAGS="-j 18 -l 18"
# export LITFLAGS="-j 18"
# NOTE: It's your responbility to validate the value of LITFLAGS & NINJAFLAGS . If unsure, don't set them.

_get_distribution_components() {
local target
local include

# make help | grep -Po 'install-\K.*(?=-stripped)' | while read -r target; do
ninja -t targets | grep -Po 'install-\K.*(?=-stripped:)' | while read -r target; do
    case $target in
        llvm-libraries|clang-libraries|clang-tidy-headers|distribution)
            include=0
            ;;
        # don't build functionality conflicting with llvm-libs
        LLVMgold)
            include=0
            ;;
        # shared libraries
        LLVM)
            include=1
            ;;
        # static libraries needed for clang-tblgen
        LLVMDemangle|LLVMSupport|LLVMTableGen)
            include=1
            ;;
        # testing libraries
        LLVMTestingAnnotations|LLVMTestingSupport)
            include=1
            ;;
        # exclude static libraries
        LLVM*)
            include=0
            ;;
        # exclude llvm-exegesis (doesn't seem useful without libpfm)
        llvm-exegesis)
            include=0
            ;;
        clang|clangd|clang-*)
            include=1
            ;;
        clang*|findAllSymbols)
            include=0
            ;;
        # Anything not covered above is included
        * )
            include=1
    esac
    if [ $include -eq 1 ]; then
        echo $target
    fi
done
}

build() {
    local cmake_args=(
        -G Ninja
        -D CMAKE_BUILD_TYPE=Release
        -D CMAKE_INSTALL_PREFIX=/usr
        -D LLVM_BINUTILS_INCDIR=/usr/include
        -D FFI_INCLUDE_DIR=$(pkg-config --variable=includedir libffi)
        -D LLVM_VERSION_SUFFIX=""
        -D LLVM_APPEND_VC_REV=ON
        -D LLVM_HOST_TRIPLE=$CHOST
        -D LLVM_TARGETS_TO_BUILD="AMDGPU;X86"
        -D LLVM_BUILD_LLVM_DYLIB=ON
        -D LLVM_LINK_LLVM_DYLIB=ON
        -D CLANG_LINK_CLANG_DYLIB=ON
        -D LLVM_DYLIB_COMPONENTS="all"
        -D LLVM_INSTALL_UTILS=ON
        -D LLVM_ENABLE_RTTI=ON
        -D LLVM_ENABLE_FFI=ON
        -D LLVM_USE_PERF=ON
        -D LLVM_INCLUDE_BENCHMARKS=OFF
        -D LLVM_INCLUDE_EXAMPLES=OFF
        -D LLVM_BUILD_DOCS=OFF
        -D LLVM_INCLUDE_DOCS=OFF
        -D LLVM_ENABLE_OCAMLDOC=OFF
        -D LLVM_ENABLE_SPHINX=OFF
        -D LLVM_ENABLE_DOXYGEN=OFF
        -D LLVM_ENABLE_BINDINGS=OFF
        -D LLVM_ENABLE_RUNTIMES=compiler-rt
        -D LLVM_ENABLE_PROJECTS="clang-tools-extra;clang"
        -D LLVM_ENABLE_DUMP=ON
        -D LLVM_LIT_ARGS="$LITFLAGS"" -sv --ignore-fail"
  )
    
    cmake -B _build -S "$srcdir"/"${_debdir}"/llvm "${cmake_args[@]}" -Wno-dev
    
    pushd "$srcdir"/_build
    local distribution_components=$(_get_distribution_components | paste -sd\;)
    popd
    test -n "$distribution_components"
    cmake_args+=(-D LLVM_DISTRIBUTION_COMPONENTS="$distribution_components")
    
    cmake -B _build -S "$srcdir"/"${_debdir}"/llvm "${cmake_args[@]}" -Wno-dev
   
    ninja $NINJAFLAGS -C _build
}

#check() {
#    ninja $NINJAFLAGS -C _build check-llvm
#    ninja $NINJAFLAGS -C _build check-clang
#    ninja $NINJAFLAGS -C _build check-clang-tools
#}

package_llvm-minimal-git() {
    pkgdesc="Collection of modular and reusable compiler and toolchain technologies, trimmed down git version"
    depends=(llvm-libs-minimal-git="$pkgver-$pkgrel" zstd zlib glibc gcc-libs)
    provides=('llvm')
    conflicts=('llvm')
    optdepends=('python: for using lit (LLVM Integrated Tester)'
                'python-setuptools: for using lit')

    DESTDIR="$pkgdir" ninja $NINJAFLAGS -C _build install-distribution

    # Include lit for running lit-based tests in other projects
    pushd "$srcdir"/"${_debdir}"/llvm/utils/lit
    python3 setup.py install --root="$pkgdir" -O1
    # -O1 ensures the python files for lit will be optimized
    popd
    
    # Remove some symlinks to prevent a conflict with repo llvm-libs
    rm "$pkgdir"/usr/lib/lib{LLVM,LTO}.so
    rm "$pkgdir"/usr/lib/libRemarks.so

    # Some static libraries for clang are still built and !staticlibs is unable to remove them
    # force their removal
    find "$pkgdir"/usr/lib/clang -depth -type f -name "*.a" -delete
    
    # clang uses clang/LLVM_Major_version in it's folder structure
    # extract this from pkgver
    local _major_ver=$(echo $pkgver | cut -d~ -f1)
    
    # prepare folders in srcdir to store files that are placed in other package_*() functions
    mkdir -p "$srcdir"/llvm-libs/usr/lib
    mkdir -p "$srcdir"/clang/usr/{bin,include,lib,lib/cmake,share}
    mkdir -p "$srcdir"/clang-libs/usr/lib
    mkdir -p "$srcdir"/clang-opencl-headers/usr/{lib/clang/$_major_ver/include,include/clang/Basic}

    # The llvm runtime libraries go into llvm-libs-minimal-git
    mv -f "$pkgdir"/usr/lib/{libLLVM*.so*,libLTO.so.*,libRemarks.so.*} "$srcdir"/llvm-libs/usr/lib

    # The clang runtime libraries go into clang-libs-minimal-git
    mv -f "$pkgdir"/usr/lib/libclang{,-cpp}.so* "$srcdir"/clang-libs/usr/lib
    
    # clang opencl files go to clang-opencl-headers-git
    mv -f "$pkgdir"/usr/lib/clang/$_major_ver/include/opencl* "$srcdir"/clang-opencl-headers/usr/lib/clang/$_major_ver/include
    mv -f "$pkgdir"/usr/include/clang/Basic/OpenCL* "$srcdir"/clang-opencl-headers/usr/include/clang/Basic

    # clang-minimal-git files go to a separate package
    mv -f "$pkgdir"/usr/lib/{libear,libscanbuild,clang} "$srcdir"/clang/usr/lib
    mv -f "$pkgdir"/usr/lib/cmake/clang "$srcdir"/clang/usr/lib/cmake/
    mv -f "$pkgdir"/usr/include/{clang,clang-c} "$srcdir"/clang/usr/include/

    # llvm project uses /usr/libexec and setting CMAKE_INSTALL_LIBEXECDIR doesn't affect that.
    # to comply with archlinux packaging standards we have to move some files manually
    mv -f "$pkgdir"/usr/libexec/* "$srcdir"/clang/usr/lib/clang
    rmdir "$pkgdir"/usr/libexec
    mv -f "$pkgdir"/usr/bin/{amdgpu-arch,analyze-build,c-index-test,clang*,diagtool,find-all-symbols,git-clang-format,hmaptool,intercept-build,modularize,nvptx-arch,pp-trace,run-clang-tidy,scan-build,scan-build-py,scan-view} "$srcdir"/clang/usr/bin/
    mv -f "$pkgdir"/usr/share/{clang,clang-doc,man,opt-viewer,scan-build,scan-view} "$srcdir"/clang/usr/share/
    
    install -Dm644 "$srcdir"/"${_debdir}"/llvm/LICENSE.TXT "$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE.TXT
}

package_llvm-libs-minimal-git() {
    pkgdesc="LLVM runtime libraries, trimmed down trunk version"
    depends=(gcc-libs zlib libffi libedit ncurses libxml2 zstd glibc)

    cp --preserve --recursive "$srcdir"/llvm-libs/* "$pkgdir"/

    install -Dm644 "$srcdir"/"${_debdir}"/llvm/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.TXT
}

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

package_clang-minimal-git() {
  pkgdesc='C language family frontend for LLVM (trimmed down git version)'
  depends=(llvm-libs-minimal-git clang-libs-minimal-git gcc-libs glibc)
  optdepends=('openmp: OpenMP support in clang with -fopenmp'
              'python: for scan-view, scan-build, git-clang-format, clang-rename and python bindings'
              'llvm-minimal-git: referenced by some clang headers')
  conflicts=(compiler-rt clang)
  provides=(compiler-rt clang clang-analyzer clang-tools-extra)

    cp --preserve --recursive "$srcdir"/clang/* "$pkgdir"/

    # Move scanbuild-py into site-packages
    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/"

    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


    install -Dm644 "$srcdir"/"${_debdir}"/compiler-rt/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/compiler-rt-LICENSE.TXT
    install -Dm644 "$srcdir"/"${_debdir}"/clang-tools-extra/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/clang-tools-extra-LICENSE.TXT
    install -Dm644 "$srcdir"/"${_debdir}"/clang/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/clang-LICENSE.TXT
}

package_clang-libs-minimal-git() {
    pkgdesc="clang runtime libraries, trunk version"
    depends=(llvm-libs-minimal-git="$pkgver-$pkgrel" gcc-libs glibc)
    # the functionality offered by this package is part of the clang repo pacakge.
    # TODO: when/if this functionality is split off from repo clang, verify if changes are needed to this package

    provides=('clang-libs')
    conflicts=("clang<$pkgver-$pkgrel" 'clang-libs')
    
    cp --preserve --recursive "$srcdir"/clang-libs/* "$pkgdir"/

    install -Dm644 "$srcdir"/"${_debdir}"/llvm/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.TXT
}

package_clang-opencl-headers-minimal-git() {
    pkgdesc="clang headers & include files for OpenCL, trunk version"
    depends=(clang-libs-minimal-git="$pkgver-$pkgrel")
    # the functionality offered by this package is part of the clang repo package.
    # As far as I know rusticl from mesa is the only known user.
    # TODO: when/if this functionality is split off from repo clang, verify if changes are needed to this package
    arch=('any')
    provides=('clang-opencl-headers')
    conflicts=("clang<$pkgver-$pkgrel" 'clang-opencl-headers')
    
    cp --preserve --recursive "$srcdir"/clang-opencl-headers/* "$pkgdir"/

    install -Dm644 "$srcdir"/"${_debdir}"/llvm/LICENSE.TXT "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.TXT
}
# vim:set ts=2 sw=2 et:
openSUSE Build Service is sponsored by