# Contributor: Adam Jensen <adam@acj.sh>
# Maintainer: Adam Jensen <adam@acj.sh>
pkgname=bcc
pkgver=0.31.0
pkgrel=0
pkgdesc="A toolkit for creating efficient kernel tracing and manipulation programs"
url="https://github.com/iovisor/bcc/"
arch="all"
license="Apache-2.0"
_llvmver=17
makedepends="
	bison
	clang$_llvmver-dev
	cmake
	elfutils-dev
	flex-dev
	iperf
	libbpf-dev
	linux-headers
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	py3-setuptools
	python3
	samurai
	zlib-dev
	libxml2-dev
	"
subpackages="
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-tools
	py3-$pkgname-pyc
	py3-$pkgname:_py:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/iovisor/bcc/archive/v$pkgver.tar.gz"

case "$CARCH" in
	ppc64le | riscv64 | loongarch64) ;;
	*) makedepends="$makedepends luajit-dev"
	   subpackages="$subpackages $pkgname-lua:_lua"
	   ;;
esac

prepare() {
	rm -rf tests/* && touch tests/CMakeLists.txt
	rm -rf examples/* && touch examples/CMakeLists.txt

	default_prepare

	sed -i "s|^#!/usr/bin/python$|#!/usr/bin/python3|" \
		tools/*.py tools/*/*.py
}

build() {
	# use libbpf installed uapi headers as we need something
	# newer than linux-headers provides
	export CFLAGS="$CFLAGS -I/usr/include/bpf/uapi"
	export CXXFLAGS="$CXXFLAGS -I/usr/include/bpf/uapi"

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_USE_LIBBPF_PACKAGE=ON \
		-DENABLE_LLVM_SHARED=ON \
		-DPYTHON_CMD=python3 \
		-DREVISION="$pkgver"
	cmake --build build
}

check() {
	PYTHONPATH="$builddir/build/src/python/bcc-python3" \
		LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$builddir/build/src/cc" \
		python3 -c "import bcc"
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

doc() {
	default_doc

	amove usr/share/bcc/man
	amove usr/share/bcc/tools/doc
}

tools() {
	pkgdesc="Command line tools for BPF Compiler Collection (BCC)"
	depends="py3-$pkgname=$pkgver-r$pkgrel"

	amove usr/share/bcc/tools
	amove usr/share/bcc/introspection
}

_py() {
	pkgdesc="Python3 bindings for BPF Compiler Collection (BCC)"
	depends="$pkgname=$pkgver-r$pkgrel python3"

	amove usr/lib/python3*
}

_lua() {
	pkgdesc="Standalone tool to run BCC tracers written in Lua"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/bcc-lua
}

sha512sums="
a1fe855c316d0508814dc02382b13897ff565bee52de0fc9d7521eabd304227e41bf7d322b10ab29fd5b13aeac85cc75ee76ac348cf1f4bb6499b8853bac10ab  bcc-0.31.0.tar.gz
"
