package llvm-project {
	description = "LLVM monorepo with Rosa OS patches";
	anitya = 1830;

	version# = "22.1.8";
	output = remoteGitHub {
		suffix = "llvm/llvm-project";
		tag = "llvmorg-"+version;
		checksum = "_QzoDE0W6cv0vfIWeLPDvqG_vKhz6IGWl1nqUUvhlKWiRzf5dMGqeC3tyS2XgI6j";
	};
	patches = [
		"increase-stack-size-unconditional.patch",
		"add-rosa-vendor.patch",
		"xfail-broken-tests.patch",
		"path-system-include.patch",
		"path-system-libraries.patch",
	];
}

package early-compiler-rt {
	description = "early LLVM runtime: compiler-rt";
	source = llvm-project;

	exclusive = true;
	early = `
LLVM_VERSION_MAJOR="$(cmake -P /dev/stdin <<< '
include(cmake/Modules/LLVMVersion.cmake)
message(NOTICE "${LLVM_VERSION_MAJOR}")
' 2>&1)"
`;
	exec = cmake {
		append = [ "compiler-rt" ];
		cache = {
			"ENABLE_LINKER_BUILD_ID": "ON";

			// libc++ not yet available
			"CMAKE_CXX_COMPILER_TARGET": "";

			"LLVM_HOST_TRIPLE": `"${ROSA_TRIPLE}"`;
			"LLVM_DEFAULT_TARGET_TRIPLE": `"${ROSA_TRIPLE}"`;
			"LLVM_ENABLE_RTTI": "ON";
			"LLVM_BUILD_LLVM_DYLIB": "ON";
			"LLVM_LINK_LLVM_DYLIB": "ON";
			"LLVM_ENABLE_PER_TARGET_RUNTIME_DIR": "ON";

			"COMPILER_RT_BUILD_BUILTINS": "ON";
			"COMPILER_RT_DEFAULT_TARGET_ONLY": "ON";
			"COMPILER_RT_USE_BUILTINS_LIBRARY": "ON";
			"COMPILER_RT_SANITIZERS_TO_BUILD": "asan";
			"COMPILER_RT_BUILD_GWP_ASAN": "OFF";

			// does not work without libunwind
			"COMPILER_RT_BUILD_CTX_PROFILE": "OFF";
			"COMPILER_RT_BUILD_LIBFUZZER": "OFF";
			"COMPILER_RT_BUILD_MEMPROF": "OFF";
			"COMPILER_RT_BUILD_PROFILE": "OFF";
			"COMPILER_RT_BUILD_XRAY": "OFF";
		};
		test = nil;
		postInstall = `
mkdir -p "/work/system/lib/clang/$LLVM_VERSION_MAJOR/lib/"
ln -s \
	"../../../${ROSA_TRIPLE}" \
	"/work/system/lib/clang/$LLVM_VERSION_MAJOR/lib/"

ln -s \
	"clang_rt.crtbegin-`+linuxArch+`.o" \
	"/work/system/lib/${ROSA_TRIPLE}/crtbeginS.o"
ln -s \
	"clang_rt.crtend-`+linuxArch+`.o" \
	"/work/system/lib/${ROSA_TRIPLE}/crtendS.o"
`;
	};

	inputs = [
		python-early,

		musl-headers,
		kernel-headers,
	];

	runtime = [ musl ];
}

package early-runtimes {
	description = "early LLVM runtimes: libunwind, libcxx, libcxxabi";
	source = llvm-project;

	exclusive = true;
	exec = cmake {
		append = [ "runtimes" ];
		cache = {
			"ENABLE_LINKER_BUILD_ID": "ON";

			// libc++ not yet available
			"CMAKE_CXX_COMPILER_WORKS": "ON";

			"LLVM_HOST_TRIPLE": `"${ROSA_TRIPLE}"`;
			"LLVM_DEFAULT_TARGET_TRIPLE": `"${ROSA_TRIPLE}"`;
			"LLVM_ENABLE_RTTI": "ON";
			"LLVM_BUILD_LLVM_DYLIB": "ON";
			"LLVM_LINK_LLVM_DYLIB": "ON";
			"LLVM_ENABLE_RUNTIMES": "'libunwind;libcxx;libcxxabi'";

			"LIBUNWIND_ENABLE_ASSERTIONS": "OFF";
			"LIBUNWIND_USE_COMPILER_RT": "ON";
			"LIBCXX_HAS_MUSL_LIBC": "ON";
			"LIBCXX_USE_COMPILER_RT": "ON";
			"LIBCXX_CXX_ABI": "libcxxabi";
			"LIBCXX_ENABLE_STATIC_ABI_LIBRARY": "OFF";
			"LIBCXX_HARDENING_MODE": "fast";
			"LIBCXX_HAS_ATOMIC_LIB": "OFF";
			"LIBCXXABI_USE_COMPILER_RT": "ON";
			"LIBCXXABI_USE_LLVM_UNWINDER": "ON";
			"LIBCXXABI_ENABLE_STATIC_UNWINDER": "OFF";
			"LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL": "OFF";

			"LLVM_ENABLE_ZLIB": "FORCE_ON";
			"LLVM_ENABLE_ZSTD": "FORCE_ON";
			"LLVM_ENABLE_LIBXML2": "OFF";
		};
		test = nil;
	};

	inputs = [
		python-early,

		zlib,
		zstd,
		early-compiler-rt,
		kernel-headers,
	];

	runtime = [ early-compiler-rt ];
}
