-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfix_lto_intermediates_in_static_libraries
executable file
·46 lines (42 loc) · 1.64 KB
/
fix_lto_intermediates_in_static_libraries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/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 <[email protected]>
# (C) 2023 Bernhard "bero" Rosenkränzer <[email protected]>
if [ -z "$RPM_BUILD_ROOT" ] || [ "$RPM_BUILD_ROOT" = "/" ]; then
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