From ac2ca1dbbafbc33337794e1507128e92d31985f1 Mon Sep 17 00:00:00 2001 From: David Benoit Date: Fri, 5 Nov 2021 15:15:14 -0400 Subject: [PATCH] Add llvm-lto-elf-check script The brp-llvm-compile-lto-elf script uses PCRE in grep to match for the -flto flag in bitcode object dumps, using negative lookahead to exclude the case where -fno-lto is specified after. When lines in the bitcode dump exceed the length that PCRE can match against, grep will error out causing brp-llvm-compile-lto-elf to fail. This script implements an equivalent regex match in python to avoid the limit in PCRE grep. Resolves: rhbz#2017193 --- brp-llvm-compile-lto-elf | 7 ++++++- llvm-lto-elf-check | 39 +++++++++++++++++++++++++++++++++++++++ macros | 3 ++- redhat-rpm-config.spec | 10 +++++++++- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100755 llvm-lto-elf-check diff --git a/brp-llvm-compile-lto-elf b/brp-llvm-compile-lto-elf index 19783fa..a7c55da 100755 --- a/brp-llvm-compile-lto-elf +++ b/brp-llvm-compile-lto-elf @@ -1,5 +1,10 @@ #!/usr/bin/bash -eu +# first argument is the path to llvm-lto-elf-check +MATCH_LTO_SCRIPT=$1 +shift + +# remaining args are cflags CLANG_FLAGS=$@ if [ -z "$RPM_BUILD_ROOT" ] || [ "$RPM_BUILD_ROOT" = "/" ]; then @@ -15,7 +20,7 @@ check_convert_bitcode () { 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 + llvm-bcanalyzer -dump ${file_name} | ${MATCH_LTO_SCRIPT} 2>&1 > /dev/null if [ $? -eq 0 ]; then echo "Compiling LLVM bitcode file ${file_name}." # create path to file in temp dir diff --git a/llvm-lto-elf-check b/llvm-lto-elf-check new file mode 100755 index 0000000..8e5ae14 --- /dev/null +++ b/llvm-lto-elf-check @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +# Check an llvm bcanalyzer dump for -flto + +# This script implements the following command in python +# to avoid PCRE backtrace limits in grep. +# grep -xP '.*\-flto((?!-fno-lto).)*' + +import re +import sys + +def open_input_file(): + if len(sys.argv) > 1: + f = open(sys.argv[1], "r") + else: + f = sys.stdin + if not f: + raise Exception("Could not open input file") + return f + +def match_lto(input_lines): + match_found = False + for line in input_lines: + match = re.match(r"^.*\-flto((?!-fno-lto).)*$", line) + if match: + match_found = True + print(f"{match.group(0)}") + return match_found + +def main(): + input_file = open_input_file() + input_lines = input_file.readlines() + if input_file != sys.stdin: + input_file.close() + if match_lto(input_lines): + sys.exit(0) + sys.exit(1) + +main() diff --git a/macros b/macros index 2bd19c7..a30a755 100644 --- a/macros +++ b/macros @@ -257,7 +257,8 @@ 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} +%__llvm_lto_elf_check /usr/lib/rpm/redhat/llvm-lto-elf-check +%__brp_llvm_compile_lto_elf /usr/lib/rpm/redhat/brp-llvm-compile-lto-elf %{__llvm_lto_elf_check} %{build_cflags} %{build_ldflags} # note: %%__os_install_post_python is defined in python-srpm-macros and contains several policies # redhat-rpm-config maintainers, don't remove it from %%__os_install_post unless coordinating the change with Python maintainers diff --git a/redhat-rpm-config.spec b/redhat-rpm-config.spec index 316f576..c8942e8 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: 201 +Version: 202 Release: 1%{?dist} # No version specified. License: GPL+ @@ -67,6 +67,7 @@ Source401: rpmsort Source402: symset-table Source403: kmodtool Source404: gpgverify +Source405: llvm-lto-elf-check # 2016-10-02 snapshots from http://git.savannah.gnu.org/gitweb/?p=config.git Source500: config.guess @@ -173,6 +174,8 @@ mkdir -p %{buildroot}%{_rpmluadir}/fedora/{rpm,srpm} install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora common.lua install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua +install -p -m 755 -t %{buildroot}%{rrcdir} llvm-lto-elf-check + %files %dir %{rrcdir} %{rrcdir}/macros @@ -186,6 +189,7 @@ install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua %{rrcdir}/find-provides %{rrcdir}/find-requires %{rrcdir}/brp-ldconfig +%{rrcdir}/llvm-lto-elf-check %{_fileattrsdir}/*.attr %{_rpmconfigdir}/macros.d/macros.*-srpm %{_rpmconfigdir}/macros.d/macros.build-constraints @@ -214,6 +218,10 @@ install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua %{_rpmconfigdir}/macros.d/macros.kmp %changelog +* Wed Nov 03 2021 David Benoit - 202-1 +- Add llvm-lto-elf-check script +- Resolves: rhbz#2017193 + * Mon Nov 01 2021 Jason L Tibbitts III - 201-1 - Better error handling for %%constrain_build.