# 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=clang15
pkgver=15.0.7
pkgrel=22
_llvmver=${pkgver%%.*}
pkgdesc="C language family front-end for LLVM"
arch="all !loongarch64"
url="https://llvm.org/"
license="Apache-2.0 WITH LLVM-exception"
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="fortify-headers>=1.1-r2"
depends_dev="$pkgname=$pkgver-r$pkgrel"
subpackages="
	$pkgname-static
	$pkgname-headers
	$pkgname-libclang
	$pkgname-libs
	$pkgname-dev
	$pkgname-ccache
	$pkgname-extra-tools:extra
	"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/clang-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/clang-tools-extra-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/cmake-$pkgver.src.tar.xz
	10-add-musl-triples.patch
	30-Enable-stack-protector-by-default-for-Alpine-Linux.patch

	clang-default-fno-semantic-interposition.patch
	clang-001-fortify-include.patch
	clang-002-fortify-enable.patch
	clang-003-as-needed.patch
	"
builddir="$srcdir/clang-$pkgver.src"

prepare() {
	mv "$srcdir/clang-tools-extra-$pkgver.src" "$builddir/tools/extra"
	default_prepare
}

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

if [ "$_default_clang" = yes ]; then
	subpackages="
		$subpackages
		$pkgname-doc
		$pkgname-analyzer::noarch
		$pkgname-bash-completion:bash:noarch
		$pkgname-emacs::noarch
		py3-$pkgname:python:noarch
		"
	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/lib/llvm$_llvmver \
		-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

	if [ "$_default_clang" = yes ]; then
		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
	fi
}

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

package() {
	# the libraries should always exactly match, not just by soname
	depends="
		$pkgname-headers=$pkgver-r$pkgrel
		$pkgname-libs=$pkgver-r$pkgrel
		gcc
		libstdc++-dev
		"

	DESTDIR="$pkgdir" cmake --install build
	mkdir -p "$pkgdir"/usr/bin

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

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

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

		mkdir -p "$pkgdir"/usr/share/emacs/site-lisp
		mv "$pkgdir"/usr/lib/llvm$_llvmver/share/clang/clang-*.el \
			"$pkgdir"/usr/share/emacs/site-lisp
		rmdir -p usr/lib/llvm$_llvmver/share/emacs || true

		mv "$pkgdir"/usr/lib/llvm$_llvmver/share/man "$pkgdir"/usr/share/
		# XXX: scan-build specifically needs to stay in llvm path, but scan-view has to not be
		# everything uses different path lookup logic..
		mv "$pkgdir"/usr/lib/llvm$_llvmver/share/scan-view "$pkgdir"/usr/share/

		local sitedir=$(python3 -c "import site; print(site.getsitepackages()[0])")
		mv "$pkgdir"/usr/lib/llvm$_llvmver/lib/python3* "$pkgdir"/usr/lib
		mkdir -p "$pkgdir"/"$sitedir"
		mv "$pkgdir"/usr/lib/llvm$_llvmver/lib/libscanbuild "$pkgdir"/"$sitedir"

		# symlink everything in the bin dir,
		# into /usr/bin.
		local clangexes="
			analyze-build
			c-index-test
			clang
			clang-cl
			clang-cpp
			clang-linker-wrapper
			clang-nvlink-wrapper
			clang-pseudo
			clang++
			clang-$_llvmver
			clang-apply-replacements
			clang-change-namespace
			clang-check
			clang-doc
			clang-extdef-mapping
			clang-format
			clang-include-fixer
			clang-move
			clang-offload-bundler
			clang-offload-packager
			clang-offload-wrapper
			clang-query
			clang-refactor
			clang-rename
			clang-reorder-fields
			clang-repl
			clang-scan-deps
			clang-tidy
			clangd
			diagtool
			find-all-symbols
			git-clang-format
			hmaptool
			intercept-build
			modularize
			pp-trace
			run-clang-tidy
			scan-build
			scan-build-py
			scan-view
			"
		for clangexe in $clangexes; do
			ln -sfv ../lib/llvm$_llvmver/bin/$clangexe "$pkgdir"/usr/bin/$clangexe
		done
		ln -sfv ../lib/llvm$_llvmver/bin/clang-$_llvmver "$pkgdir"/usr/bin/clang++-$_llvmver

		install -Dm644 "$builddir"/clang.1 -t "$pkgdir"/usr/share/man/man1/
	else
		ln -sfv ../lib/llvm$_llvmver/bin/clang-$_llvmver "$pkgdir"/usr/bin/clang-$_llvmver
		ln -sfv ../lib/llvm$_llvmver/bin/clang-$_llvmver "$pkgdir"/usr/bin/clang++-$_llvmver

		# delete things we don't want non-latest of
		rm -r "$pkgdir"/usr/lib/llvm$_llvmver/share
		rm -r "$pkgdir"/usr/lib/llvm$_llvmver/lib/python3*
	fi

	local libs="
		libclang-cpp.so
		libclang-cpp.so.$_llvmver
		libclang.so
		libclang.so.$_llvmver
		libclang.so.$pkgver
		"
	for lib in $libs; do
		ln -sfv ../lib/llvm$_llvmver/lib/$lib "$pkgdir"/usr/lib/$lib
	done

	# compat symlink back to fix clang with -no-canonical-prefixes
	mkdir -p "$pkgdir"/usr/lib/clang
	ln -sfv ../../lib/llvm$_llvmver/lib/clang/$pkgver "$pkgdir"/usr/lib/clang/$pkgver
}

dev() {
	default_dev
	_default_replace

	# move cmake to -dev
	amove usr/lib/llvm$_llvmver/lib/cmake/clang

	# create cmakedir symlinks, so find_package works
	# llvm does the same thing- versioned, plus unversioned default.
	mkdir -p "$subpkgdir"/usr/lib/cmake
	ln -sfv ../llvm$_llvmver/lib/cmake/clang "$subpkgdir"/usr/lib/cmake/clang$_llvmver
	if [ $_default_clang = yes ]; then
		ln -sfv clang$_llvmver "$subpkgdir"/usr/lib/cmake/clang
	fi
}

static() {
	default_static
	_default_replace
}

bash() {
	default_bashcomp
	_default_replace
}

libs() {
	default_libs
	# this can fail depending on the symlinks and is already moved
	amove usr/lib/libclang-cpp.so.* || true
	amove usr/lib/llvm$_llvmver/lib/libclang-cpp.so.*
	_default_replace
}

libclang() {
	_default_replace
	replaces="$replaces clang-libs"
	depends="$pkgname-headers=$pkgver-r$pkgrel"

	# 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.*
	amove usr/lib/llvm$_llvmver/lib/libclang.so.*
}

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

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

headers() {
	_default_replace
	amove usr/lib/llvm$_llvmver/lib/clang/$pkgver/include
}

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

	if [ "$_default_clang" = "yes" ]; then
		amove \
			usr/bin/amdgpu-arch \
			usr/bin/clang-apply-replacements* \
			usr/bin/clang-change-namespace* \
			usr/bin/clang-check* \
			usr/bin/clang-doc* \
			usr/bin/clang-include-fixer* \
			usr/bin/clang-move* \
			usr/bin/clang-offload-bundler* \
			usr/bin/clang-pseudo* \
			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/hmaptool* \
			usr/bin/modularize* \
			usr/bin/nvptx-arch \
			usr/bin/pp-trace* \
			usr/bin/run-clang-tidy*
	fi

	amove \
		usr/lib/llvm$_llvmver/bin/clang-apply-replacements* \
		usr/lib/llvm$_llvmver/bin/clang-change-namespace* \
		usr/lib/llvm$_llvmver/bin/clang-doc* \
		usr/lib/llvm$_llvmver/bin/clang-format* \
		usr/lib/llvm$_llvmver/bin/clang-include-fixer* \
		usr/lib/llvm$_llvmver/bin/clang-move* \
		usr/lib/llvm$_llvmver/bin/clang-offload-bundler* \
		usr/lib/llvm$_llvmver/bin/clang-pseudo* \
		usr/lib/llvm$_llvmver/bin/clang-query* \
		usr/lib/llvm$_llvmver/bin/clang-refactor* \
		usr/lib/llvm$_llvmver/bin/clang-rename* \
		usr/lib/llvm$_llvmver/bin/clang-reorder-fields* \
		usr/lib/llvm$_llvmver/bin/clang-repl* \
		usr/lib/llvm$_llvmver/bin/clang-scan-deps* \
		usr/lib/llvm$_llvmver/bin/clang-tidy* \
		usr/lib/llvm$_llvmver/bin/clangd* \
		usr/lib/llvm$_llvmver/bin/diagtool* \
		usr/lib/llvm$_llvmver/bin/find-all-symbols* \
		usr/lib/llvm$_llvmver/bin/git-clang-format* \
		usr/lib/llvm$_llvmver/bin/hmaptool* \
		usr/lib/llvm$_llvmver/bin/modularize* \
		usr/lib/llvm$_llvmver/bin/pp-trace* \
		usr/lib/llvm$_llvmver/bin/run-clang-tidy*
}

emacs() {
	pkgdesc="clang plugin for emacs"
	install_if="$pkgname-extra-tools=$pkgver-r$pkgrel emacs"

	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"

	amove usr/lib/python*
}

ccache() {
	pkgdesc="$pkgdesc (ccache symlinks)"
	install_if="$pkgname=$pkgver-r$pkgrel ccache"

	mkdir -p "$subpkgdir"/usr/lib/ccache/bin
	ln -sfv /usr/bin/ccache "$subpkgdir"/usr/lib/ccache/bin/clang-$_llvmver
	ln -sfv /usr/bin/ccache "$subpkgdir"/usr/lib/ccache/bin/clang++-$_llvmver
}

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

sha512sums="
9fd2736a9f5993ddbb5b3c507fe497234a6def64f6f418f379d1ca56c9c361ad1ae9a5445ab938230fbc1671ec00b4f802a412b048569023863c20dc6bb46a1f  clang-15.0.7.src.tar.xz
dd95d261d3a1618967b698e97ac0b41204d872d7479c0c832fcf5a91fe447af6a97fa794de07b3e271285867b30b905688f090e98f516b9df4cdfb3bad9defc6  clang-tools-extra-15.0.7.src.tar.xz
85097a6eaee0df004567905f7e5ce8ca02e3b46c6fc66fa552fe99734642e6ee61685de4e5e6e8b505963334d7b91d98ecd9c5870e6bdd3f0018ee5b963ffa96  cmake-15.0.7.src.tar.xz
5809d931656cac13b91bc91aac63df072b0403708ac718bf01546d9d520ff2c4163ee11411439dce5bb683a3bfa044b62443b6f66a1419c9086d20e79a018071  10-add-musl-triples.patch
b9b5529bcf460767796ddbc6f7d8c479b62d63c74d39297b3c7c79a2f6898272ac52a2f2d1ea2bd601e869cecf23c0cd69fbcd613b8c6299b14a1be8203fd089  30-Enable-stack-protector-by-default-for-Alpine-Linux.patch
6896d6c5358b8b83c97c7cb31f60042f33a1326cfad093a0f23dcacd10ef418f845964f6de948a5873494dae5180e51a4b2b0cec937aa5597765362779da6f35  clang-default-fno-semantic-interposition.patch
aa2dff990278e0258763934eaa528827baa1733eeb7dc36cc7f1500a4919b8d11870a0522f20f210d454322e3faf5945e9afaeffc9f3a3c4fd422b706370a663  clang-001-fortify-include.patch
751f8acef2bfd1b50c065dea2ad0c3a20cadd4ff4c842ae519b5085c48372127b49a75d3fd0bd32291f5a69119340e5568a9fba6d94e6f354b14233986f61328  clang-002-fortify-enable.patch
5f98f147d7d66315e9a56ae6fee602a053d40e57a8b1ee4f2ef1764203bb6fdb9082c8e825c535a9388213a38b49d2702b1e936fcf56560c4a2a56b7e6dd232c  clang-003-as-needed.patch
"
