#!/bin/sh
# Compile LLVM IR bitcode in static libraries into object code
# so the resulting library is usable with any compiler
#
# (C) 2022-2023 Tomasz Paweł Gajc <tpgxyz@gmail.com>
# (C)      2023 Bernhard "bero" Rosenkränzer <bero@lindev.ch>

if [ -z "$RPM_BUILD_ROOT" ] || [ "$RPM_BUILD_ROOT" = "/" ]; then
	exit 0
fi

# Check if llvm-lto is available
if ! command -v llvm-lto >/dev/null 2>&1; then
    echo "llvm-lto not found, skipping LLVM IR bitcode compilation."
    exit 0
fi

check_convert_bitcode() {
	printf "Checking %s for LLVM IR bitcode\n" "${1}"
	llvm_file_name="$(realpath ${1})"
	llvm_file_type="$(LC_ALL=C file ${llvm_file_name})"

	if printf '%s' "${llvm_file_type}" | grep -q "LLVM IR bitcode"; then
		printf 'Compiling bitcode in %s to object code\n' "${llvm_file_name}"
		llvm-lto -o "${llvm_file_name}" $(llvm-nm "${llvm_file_name}" |while read r; do echo $r |awk '{print $3;}' |grep -vE '^$' |sed -e 's,^,--exported-symbol=,'; done) "${llvm_file_name}"
		return 0
	elif printf '%s' "${llvm_file_type}" | grep -q "current ar archive"; then
		printf '%s\n' "Unpacking ar archive ${llvm_file_name} to check for LLVM bitcode components."
		# create archive stage for objects
		archive_stage="$(mktemp -d)"
		[ -z "$archive_stage" ] && exit 1
		archive="${llvm_file_name}"
		cd "${archive_stage}"
		llvm-ar x "${archive}"
		find -not -type d |while read archived_file; do
			if check_convert_bitcode "${archived_file}"; then
				printf 'Repacking %s into %s.\n' "${archived_file}" "${archive}"
				llvm-ar r "${archive}" "${archived_file}"
				touch "${archive_stage}/modified"
			fi
		done
		[ -e "${archive_stage}/modified" ] && llvm-ranlib ${archive}
		cd ..
		rm -rf "${archive_stage}"
		return 0
	fi
	return 1
}

find "$RPM_BUILD_ROOT" -type f -name "*.[ao]" |while read i; do
	check_convert_bitcode "${i}" || :
done
