# Contributor Eric Molitor <eric@molitor.org>
# Contributor Patrick Gansterer <paroga@paroga.com>
# Contributor Travis Tilley <ttilley@gmail.com>
# Contributor omni <omni+alpine@hack.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=clang14
pkgver=14.0.6
pkgrel=1
_llvmver=${pkgver%%.*}
pkgdesc="A C language family front-end for LLVM"
arch="all"
url="https://llvm.org/"
license="Apache-2.0"
options="!check" # fail to build
makedepends="
	cmake
	help2man
	libxml2-dev
	llvm$_llvmver-dev
	llvm$_llvmver-static
	llvm$_llvmver-test-utils
	samurai
	python3
	"
checkdepends="diffutils bash gtest-dev"
depends_dev="$pkgname=$pkgver-r$pkgrel"
subpackages="
	$pkgname-static
	$pkgname-libclang
	$pkgname-libs
	$pkgname-dev
	"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/clang-$pkgver.src.tar.xz
	10-add-musl-triples.patch
	30-Enable-stack-protector-by-default-for-Alpine-Linux.patch
	"
builddir="$srcdir/clang-$pkgver.src"

# Whether is this package the default (latest) clang version.
_default_clang="no"

if [ "$_default_clang" = yes ]; then
	provides="clang=$pkgver-r$pkgrel"
	replaces="clang"
	depends_dev="$depends_dev $pkgname-extra-tools=$pkgver-r$pkgrel"
fi

build() {
	local python_version=$(python3 -V | sed 's/.*\([0-9]\{1,\}\.[0-9]\{1,\}\)\..*/\1/')

	if [ $_default_clang = yes ]; then
		local extras=ON
	else
		local extras=OFF
	fi

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_MODULE_PATH="$srcdir/cmake-$pkgver.src/Modules" \
		-DCLANG_BUILT_STANDALONE=ON \
		-DCLANG_ENABLE_ARCMT=$extras \
		-DCLANG_ENABLE_STATIC_ANALYZER=$extras \
		-DCLANG_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DCLANG_LINK_CLANG_DYLIB=ON \
		-DCLANG_PLUGIN_SUPPORT=ON \
		-DCLANG_PYTHON_BINDINGS_VERSIONS="$python_version" \
		-DCLANG_VENDOR=Alpine \
		-DENABLE_LINKER_BUILD_ID=ON \
		-DLIBCLANG_BUILD_STATIC=ON

	ninja -C build clang-tblgen
	ninja -C build

	help2man --no-info \
		--source "Alpine" \
		--name "Alpine Clang $pkgver-r$pkgrel" \
		--version-string "$pkgver-r$pkgrel" \
		--help-option "--help-hidden" \
		./build/bin/clang > clang.1
}

check() {
	ninja -C build check-all
}

package() {
	depends="libstdc++-dev"

	DESTDIR="$pkgdir" ninja -C build install

	install -Dm644 build/lib/libclang.a -t "$pkgdir"/usr/lib/llvm$_llvmver/lib/

	cd "$pkgdir"/usr/lib
	# the soname is the majorver but they kept the broken file version
	rm libclang.so.13
	ln -sfv libclang.so.$pkgver libclang.so.$_llvmver
	ln -svf libclang.so.$_llvmver libclang.so

	if [ $_default_clang = "yes" ]; then
		rm "$pkgdir"/usr/lib/share/clang/clang-format-bbedit.applescript \
			"$pkgdir"/usr/lib/share/clang/clang-doc-default-stylesheet.css \
			"$pkgdir"/usr/lib/share/clang/index.js

		install -Dm644 "$builddir"/clang.1 -t "$pkgdir"/usr/share/man/man1/

		mkdir -p "$pkgdir"/usr/share/bash-completion/completions
		mv "$pkgdir"/usr//share/clang/bash-autocomplete.sh \
			"$pkgdir"/usr/share/bash-completion/completions/clang

		mkdir -p "$pkgdir"/usr/share/emacs/site-lisp
		mv "$pkgdir"/usr/share/clang/clang-*.el \
			"$pkgdir"/usr/share/emacs/site-lisp
		rmdir -p usr/share/clang || true
	else
		# only has things we want to put in the main version
		rm -rf "$pkgdir"/usr/share
		rm -rf "$pkgdir"/usr/lib/python3*
	fi
}

dev() {
	default_dev
	_default_replace

	# move back usr/lib/clang/x.y.z/include with files such as altivec.h, msa.h etc
	# these are needed to build programs *with* clang, not *against* it.
	mv "$subpkgdir"/usr/lib/clang "$pkgdir"/usr/lib/
}

static() {
	default_static
	_default_replace
}

bash() {
	default_bashcomp
	_default_replace
}

libs() {
	default_libs
	_default_replace
}

libclang() {
	_default_replace

	# we add an extra subpackage for this so things that link to libclang don't
	# have to pull libclang-cpp too (separate)
	amove usr/lib/libclang.so.*
}

analyzer() {
	pkgdesc="Clang source code analysis framework"
	depends="$pkgname=$pkgver-r$pkgrel perl python3"
	_default_replace

	amove \
		usr/bin/scan* \
		usr/bin/analyze-build* \
		usr/bin/intercept-build* \
		usr/libexec/*-analyzer* \
		usr/libexec/analyze-* \
		usr/libexec/intercept-* \
		usr/share/scan-* \
		usr/lib/libear*
}

extra() {
	pkgdesc="Extra tools built using Clang’s tooling APIs"
	_default_replace

	amove \
		usr/bin/clang-apply-replacements* \
		usr/bin/clang-change-namespace* \
		usr/bin/clang-check* \
		usr/bin/clang-doc* \
		usr/bin/clang-extdef-mapping* \
		usr/bin/clang-format* \
		usr/bin/clang-include-fixer* \
		usr/bin/clang-move* \
		usr/bin/clang-offload-bundler* \
		usr/bin/clang-offload-wrapper* \
		usr/bin/clang-query* \
		usr/bin/clang-refactor* \
		usr/bin/clang-rename* \
		usr/bin/clang-reorder-fields* \
		usr/bin/clang-repl* \
		usr/bin/clang-scan-deps* \
		usr/bin/clang-tidy* \
		usr/bin/clangd* \
		usr/bin/c-index-test* \
		usr/bin/diagtool* \
		usr/bin/find-all-symbols* \
		usr/bin/git-clang-format* \
		usr/bin/hmaptool* \
		usr/bin/modularize* \
		usr/bin/pp-trace* \
		usr/bin/run-clang-tidy* \
		usr/share/clang/clang-include-fixer* \
		usr/share/clang/clang-format*.py \
		usr/share/clang/*clang-tidy* \
		usr/share/clang/run-find-all-symbols.py

	amove usr/share/emacs
}

python() {
	pkgdesc="Clang Python Bindings"
	depends="$pkgname-libs=$pkgver-r$pkgrel python3"
	provides="py3-clang=$pkgver-r$pkgrel"
	replaces="py3-clang"

	local sitedir=$(python3 -c "import site; print(site.getsitepackages()[0])")

	amove usr/lib/python*
	amove usr/share/clang/clang-rename.py

	# actually a python module..
	mv "$pkgdir"/usr/lib/libscanbuild "$subpkgdir"/"$sitedir"
}

_default_replace() {
	if [ "$_default_clang" = yes ]; then
		replaces="clang${subpkgname#"$pkgname"}"
		provides="$replaces=$pkgver-r$pkgrel"
	fi
}

sha512sums="
5f5497b57dd116225e90f321902f1015beb50c0b1bb90d0fc6c026f13aa748feabe44f98eb75e86f98f3b1785ad5e850210732f3eae75b942ecd520527dcd30b  clang-14.0.6.src.tar.xz
5809d931656cac13b91bc91aac63df072b0403708ac718bf01546d9d520ff2c4163ee11411439dce5bb683a3bfa044b62443b6f66a1419c9086d20e79a018071  10-add-musl-triples.patch
b9b5529bcf460767796ddbc6f7d8c479b62d63c74d39297b3c7c79a2f6898272ac52a2f2d1ea2bd601e869cecf23c0cd69fbcd613b8c6299b14a1be8203fd089  30-Enable-stack-protector-by-default-for-Alpine-Linux.patch
"