pkgver=21.1.3
pkgname=llvm
runtime_subpkgs="libunwind libunwind-dev libcxx libcxx-dev libclang-rt-dev"
library_subpkgs="liblld-dev libclang libclang-dev libllvm libllvm-dev"
toolchain_subpkgs="clang llvm-binutils lld"
subpkgs="$runtime_subpkgs $library_subpkgs $toolchain_subpkgs llvm"
desc="LLVM Toolchain"
bad=""
ext="dev"
mkdeps="cmake:samurai:python"

fetch() {
	curl -L "https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/llvm-project-$pkgver.src.tar.xz" -o $pkgname-$pkgver.tar.gz
	tar -xf $pkgname-$pkgver.tar.gz
	mv llvm-project-$pkgver.src $pkgname-$pkgver
}

build() {
	cd $pkgname-$pkgver

	mkdir -p build
	cd build
	cmake -G Ninja -Wno-dev \
		-DCMAKE_C_COMPILER=$CC \
		-DCMAKE_CXX_COMPILER=$CXX \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=Release \
		-DLLVM_USE_HOST_TOOLS=OFF \
		-DLLVM_VERSION_SUFFIX="" \
		-DLLVM_APPEND_VC_REV=OFF \
		-DLLVM_ENABLE_PROJECTS="llvm;lld;clang" \
		-DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;libunwind" \
		-DLLVM_ENABLE_LLD=ON \
		-DLLVM_TARGETS_TO_BUILD="X86;AArch64;RISCV" \
		-DLLVM_INSTALL_BINUTILS_SYMLINKS=ON \
		-DLLVM_INSTALL_CCTOOLS_SYMLINKS=ON \
		-DLLVM_INCLUDE_EXAMPLES=OFF \
		-DLLVM_ENABLE_PIC=ON \
		-DLLVM_ENABLE_LTO=OFF \
		-DLLVM_INCLUDE_GO_TESTS=OFF \
		-DLLVM_INCLUDE_TESTS=OFF \
		-DLLVM_HOST_TRIPLE=$TRIPLE \
		-DLLVM_DEFAULT_TARGET_TRIPLE=$TRIPLE \
		-DLLVM_ENABLE_LIBXML2=OFF \
		-DLLVM_ENABLE_ZLIB=OFF \
		-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
		-DLLVM_ENABLE_BACKTRACES=OFF \
		-DLLVM_BUILD_LLVM_DYLIB=ON \
		-DLLVM_LINK_LLVM_DYLIB=ON \
		-DLLVM_OPTIMIZED_TABLEGEN=OFF \
		-DLLVM_INCLUDE_BENCHMARKS=OFF \
		-DLLVM_INCLUDE_DOCS=ON \
		-DLLVM_TOOL_LLVM_ITANIUM_DEMANGLE_FUZZER_BUILD=OFF \
		-DLLVM_TOOL_LLVM_MC_ASSEMBLE_FUZZER_BUILD=OFF \
		-DLLVM_TOOL_LLVM_MC_DISASSEMBLE_FUZZER_BUILD=OFF \
		-DLLVM_TOOL_LLVM_OPT_FUZZER_BUILD=OFF \
		-DLLVM_TOOL_LLVM_MICROSOFT_DEMANGLE_FUZZER_BUILD=OFF \
		-DLLVM_TOOL_LLVM_GO_BUILD=OFF \
		-DLLVM_INSTALL_UTILS=ON \
		-DLLVM_ENABLE_LIBCXX=ON \
		-DLLVM_STATIC_LINK_CXX_STDLIB=ON \
		-DLLVM_ENABLE_ZSTD=OFF \
		-DLLVM_ENABLE_LIBEDIT=OFF \
		-DLLVM_ENABLE_TERMINFO=OFF \
		-DLLVM_INSTALL_TOOLCHAIN_ONLY=OFF \
		-DLIBCXX_ENABLE_FILESYSTEM=ON \
		-DLIBCXX_USE_COMPILER_RT=ON \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
		-DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=ON \
		-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON \
		-DLIBCXX_INSTALL_LIBRARY=ON \
		-DLIBCXXABI_ENABLE_ASSERTIONS=ON \
		-DLIBCXXABI_USE_COMPILER_RT=ON \
		-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
		-DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON \
		-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY=YES \
		-DLIBCXXABI_ENABLE_SHARED=OFF \
		-DLIBCXXABI_ENABLE_STATIC=ON \
		-DLIBCXXABI_INSTALL_LIBRARY=ON \
		-DLIBUNWIND_ENABLE_SHARED=ON \
		-DLIBUNWIND_ENABLE_STATIC=ON \
		-DLIBUNWIND_INSTALL_LIBRARY=ON \
		-DLIBUNWIND_USE_COMPILER_RT=ON \
		-DCLANG_DEFAULT_LINKER=lld \
		-DCLANG_DEFAULT_CXX_STDLIB='libc++' \
		-DCLANG_DEFAULT_RTLIB=compiler-rt \
		-DCLANG_DEFAULT_UNWINDLIB=libunwind \
		-DCLANG_VENDOR="Iglunix" \
		-DCLANG_ENABLE_STATIC_ANALYZER=OFF \
		-DCLANG_ENABLE_ARCMT=OFF \
		-DCLANG_LINK_CLANG_DYLIB=OFF \
		-DCLANG_TOOLING_BUILD_AST_INTROSPECTION=OFF \
		-DCOMPILER_RT_USE_BUILTINS_LIBRARY=OFF \
		-DCOMPILER_RT_DEFAULT_TARGET_ONLY=OFF \
		-DCOMPILER_RT_INCLUDE_TESTS=OFF \
		-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
		-DCOMPILER_RT_BUILD_XRAY=OFF \
		-DCOMPILER_RT_BUILD_MEMPROF=OFF \
		-DCOMPILER_RT_INCLUDE_TESTS=OFF \
		-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
		-DENABLE_EXPERIMENTAL_NEW_PASS_MANAGER=TRUE \
		-DHAVE_CXX_ATOMICS_WITHOUT_LIB=ON \
		-DHAVE_CXX_ATOMICS64_WITHOUT_LIB=ON \
		-DHAVE_BACKTRACE=OFF \
		../llvm

	samu -j$JOBS
}

package() {
	cd $pkgname-$pkgver
	cd build
	DESTDIR=$pkgdir samu install
	ln -s clang $pkgdir/usr/bin/cc
	ln -s clang $pkgdir/usr/bin/c89
	ln -s clang $pkgdir/usr/bin/c99
	ln -s clang $pkgdir/usr/bin/c17
	ln -s clang++ $pkgdir/usr/bin/c++
	ln -s ld.lld $pkgdir/usr/bin/ld
}

libunwind() {
	shlibs="libunwind.so.1"
	find usr/lib/libunwind.so.*
}

libunwind-dev() {
	deps=libunwind
	find usr/lib/libunwind.so
	find usr/include/*unwind* usr/include/mach-o/compact_unwind_encoding.h
}

libcxx() {
	shlibs="libc++.so.1"
	find usr/lib/libc++.so.*
	set -x
}

libcxx-dev() {
	deps="libcxx musl-dev"
	find usr/lib/libc++.so usr/lib/libc++experimental.a
	find usr/lib/libc++.modules.json
	find usr/include/c++
	find usr/share/libc++
}

libclang-rt-dev() {
	find usr/lib/clang
}

libllvm() {
	shlibs="libLLVM.so.21.1"
	find usr/lib/libLLVM.so.*
}

libllvm-dev() {
	find usr/include/llvm usr/include/llvm-c
	find usr/lib/libLLVM*.a
	find usr/lib/libLLVM.so usr/lib/libLLVM-*
	find usr/lib/cmake/llvm
}

lld() {
	find usr/bin/lld usr/bin/ld.lld usr/bin/ld
}

liblld-dev() {
	find usr/lib/liblld*
	find usr/include/lld
	find usr/lib/cmake/lld
}

clang() {
	deps=libclang-rt-dev
	find usr/bin/clang-*
	find usr/bin/clang usr/bin/cc usr/bin/c89 usr/bin/c99 usr/bin/c17
	find usr/bin/clang++ usr/bin/c++
}

libclang() {
	find usr/lib/libclang.so.* usr/lib/libclang-cpp.so.*
}

libclang-dev() {
	find usr/include/clang usr/include/clang-c
	find usr/lib/cmake/clang
	find usr/lib/libclang*.a
	find usr/lib/libclang.so usr/lib/libclang-cpp.so
}

llvm-binutils() {
	find usr/bin/llvm-addr2line
	find usr/bin/llvm-ar
	find usr/bin/llvm-bitcode-strip
	find usr/bin/llvm-cxxfilt
	find usr/bin/llvm-dlltool
	find usr/bin/llvm-dwp
	find usr/bin/llvm-install-name-tool
	find usr/bin/llvm-lib
	find usr/bin/llvm-nm
	find usr/bin/llvm-objcopy
	find usr/bin/llvm-objdump
	find usr/bin/llvm-otool
	find usr/bin/llvm-ranlib
	find usr/bin/llvm-rc
	find usr/bin/llvm-readelf
	find usr/bin/llvm-readobj
	find usr/bin/llvm-size
	find usr/bin/llvm-strings
	find usr/bin/llvm-strip
	find usr/bin/llvm-symbolizer
	find usr/bin/llvm-windres
	find usr/bin/addr2line
	find usr/bin/ar
	find usr/bin/bitcode_strip
	find usr/bin/c++filt
	find usr/bin/dlltool
	find usr/bin/dwp
	find usr/bin/install_name_tool
	find usr/bin/nm
	find usr/bin/objcopy
	find usr/bin/objdump
	find usr/bin/otool
	find usr/bin/ranlib
	find usr/bin/readelf
	find usr/bin/size
	find usr/bin/strings
	find usr/bin/strip
	find usr/bin/windres
}

llvm() {
	find usr/bin/llc
}

backup() {
	return
}

license() {
	cd $pkgname-$pkgver
	cat */LICENSE.TXT
}
