# Template file for 'bazel'
pkgname=bazel
version=8.3.1
revision=1
hostmakedepends="protobuf python3 unzip openjdk21 which perl grpc zip tar"
makedepends="protobuf-devel libprotoc-devel grpc-devel libarchive-devel
 zlib-devel"
depends="openjdk21"
short_desc="Google's build system"
maintainer="Đoàn Trần Công Danh <congdanhqx@gmail.com>"
license="Apache-2.0"
homepage="https://bazel.build/"
distfiles="https://github.com/bazelbuild/bazel/releases/download/${version}/bazel-${version}-dist.zip"
checksum=79da863df05fa4de79a82c4f9d4e710766f040bc519fd8b184a4d4d51345d5ba
nostrip=yes
python_version=3

nocross=yes

case "$XBPS_TARGET_MACHINE" in
	aarch64*) _cpu=aarch64 ;;
	armv7*) _cpu=armv7 ;;
	arm*) _cpu=arm ;;
	x86_64*) _cpu=x86_64 ;;
	i686*) _cpu=i386 ;;
	ppc64le*) _cpu=ppc64le ;;
	ppc64*) _cpu=ppc ;;
	ppc*) _cpu=ppc32 ;;
	riscv64*) _cpu=riscv64 ;;
	*) broken="not supported" ;;
esac

do_build() {
	local flag
	local _bazel_opts="-s --verbose_failures"
	for flag in $CFLAGS; do
		_bazel_opts+=" --conlyopt=$flag"
		# For -fstack-protector ...
		_bazel_opts+=" --linkopt=$flag"
	done
	for flag in $CXXFLAGS; do
		_bazel_opts+=" --cxxopt=$flag"
		_bazel_opts+=" --linkopt=$flag"
	done
	for flag in $LDFLAGS; do
		_bazel_opts+=" --linkopt=$flag"
	done

	_bazel_opts+=" --java_runtime_version=local_jdk"
	_bazel_opts+=" --tool_java_runtime_version=local_jdk"

	export JAVA_HOME="/usr/lib/jvm/openjdk21"

	EMBED_LABEL=${version} \
	EXTRA_BAZEL_ARGS="$_bazel_opts" \
	./compile.sh
	scripts/generate_bash_completion.sh \
		--prepend=scripts/bazel-complete-header.bash \
		--prepend=scripts/bazel-complete-template.bash \
		--bazel=output/bazel \
		--output=output/bazel-complete.bash
	PYTHONPATH="$wrksrc/third_party/py/abseil" \
	python3 scripts/generate_fish_completion.py \
		--bazel=output/bazel \
		--output=output/bazel.fish

	cp scripts/zsh_completion/_bazel output/
	cat >output/bazel.wrapper <<-EOF
	#!/bin/sh
	export JAVA_HOME="$JAVA_HOME"
	exec /usr/libexec/bazel/bazel "\$@"
	EOF
	chmod -R u+rwX /tmp
}

do_install() {
	vinstall output/bazel 755 usr/libexec/bazel/
	vbin output/bazel.wrapper bazel
	vcompletion output/bazel-complete.bash bash
	vcompletion output/bazel.fish fish
	vcompletion output/_bazel zsh
}
