From 391eca35086717a713c3f622a2e58ded465ce282 Mon Sep 17 00:00:00 2001 From: David Benoit Date: Wed, 10 Feb 2021 11:59:25 -0500 Subject: [PATCH] add brp-llvm-compile-lto-elf --- brp-llvm-compile-lto-elf | 47 ++++++++++++++++++++++++++++++++++++++++ macros | 3 +++ redhat-rpm-config.spec | 12 +++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 brp-llvm-compile-lto-elf diff --git a/brp-llvm-compile-lto-elf b/brp-llvm-compile-lto-elf new file mode 100755 index 0000000..19783fa --- /dev/null +++ b/brp-llvm-compile-lto-elf @@ -0,0 +1,47 @@ +#!/usr/bin/bash -eu + +CLANG_FLAGS=$@ + +if [ -z "$RPM_BUILD_ROOT" ] || [ "$RPM_BUILD_ROOT" = "/" ]; then + exit 0 +fi + +TMPDIR=`mktemp -d` + +check_convert_bitcode () { + local file_name=`realpath ${1}` + local file_type=`file ${file_name}` + + if [[ "${file_type}" == *"LLVM IR bitcode"* ]]; then + # check for an indication that the bitcode was + # compiled with -flto + llvm-bcanalyzer -dump ${file_name} | grep -xP '.*\-flto((?!-fno-lto).)*' 2>&1 > /dev/null + if [ $? -eq 0 ]; then + echo "Compiling LLVM bitcode file ${file_name}." + # create path to file in temp dir + # move file to temp dir with llvm .bc extension for clang + mkdir -p ${TMPDIR}/`dirname ${file_name}` + mv $file_name ${TMPDIR}/${file_name}.bc + clang -c ${CLANG_FLAGS} -fno-lto -Wno-unused-command-line-argument ${TMPDIR}/${file_name}.bc -o ${file_name} + fi + elif [[ "${file_type}" == *"current ar archive"* ]]; then + echo "Unpacking ar archive ${file_name} to check for LLVM bitcode components." + # create archive stage for objects + local archive_stage=`mktemp -d` + local archive=${file_name} + pushd ${archive_stage} + ar x ${archive} + for archived_file in `find -not -type d`; do + check_convert_bitcode ${archived_file} + echo "Repacking ${archived_file} into ${archive}." + ar r ${archive} ${archived_file} + done + popd + fi +} + +echo "Checking for LLVM bitcode artifacts" +for i in `find $RPM_BUILD_ROOT -type f -name "*.[ao]"`; do + check_convert_bitcode ${i} +done + diff --git a/macros b/macros index f062f94..b946438 100644 --- a/macros +++ b/macros @@ -253,7 +253,10 @@ print(result) # __brp_mangle_shebangs_exclude_from_file - file from which to get files to ignore %__brp_mangle_shebangs /usr/lib/rpm/redhat/brp-mangle-shebangs %{?__brp_mangle_shebangs_exclude:--shebangs "%{?__brp_mangle_shebangs_exclude}"} %{?__brp_mangle_shebangs_exclude_file:--shebangs-from "%{__brp_mangle_shebangs_exclude_file}"} %{?__brp_mangle_shebangs_exclude_from:--files "%{?__brp_mangle_shebangs_exclude_from}"} %{?__brp_mangle_shebangs_exclude_from_file:--files-from "%{__brp_mangle_shebangs_exclude_from_file}"} +%__brp_llvm_compile_lto_elf /usr/lib/rpm/redhat/brp-llvm-compile-lto-elf %{build_cflags} %{build_ldflags} + %__os_install_post \ + %[ "%{toolchain}" == "clang" ? "%{?__brp_llvm_compile_lto_elf}" : "%{nil}" ] \ %{?__brp_ldconfig} \ %{?__brp_compress} \ %{!?__debug_package:\ diff --git a/redhat-rpm-config.spec b/redhat-rpm-config.spec index bc0910d..8752e3d 100644 --- a/redhat-rpm-config.spec +++ b/redhat-rpm-config.spec @@ -6,7 +6,7 @@ Summary: Red Hat specific rpm configuration files Name: redhat-rpm-config -Version: 182 +Version: 183 Release: 1%{?dist} # No version specified. License: GPL+ @@ -57,6 +57,9 @@ Source202: brp-python-bytecompile # https://github.com/fedora-python/marshalparser Source203: brp-fix-pyc-reproducibility +# for converting llvm LTO bitcode objects into ELF +Source204: brp-llvm-compile-lto-elf + # Dependency generator scripts (deprecated) Source300: find-provides Source301: find-provides.ksyms @@ -124,6 +127,9 @@ Requires: %{_bindir}/grep Requires: %{_bindir}/sed Requires: %{_bindir}/xargs +# for brp-llvm-compile-lto-elf +Requires: (llvm if clang) + # -fstack-clash-protection and -fcf-protection require GCC 8. Conflicts: gcc < 8.0.1-0.22 @@ -213,6 +219,10 @@ install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua %{_rpmconfigdir}/macros.d/macros.kmp %changelog +* Tue Apr 6 2021 David Benoit - 183-1 +- BRP: LLVM Compile LTO Bitcode to ELF +- Add Requires: (llvm if clang) + * Mon Mar 22 2021 Lumír Balhar - 182-1 - Fix handling of files without newlines in brp-mangle-shebang