b1dfa8
#!/usr/bin/bash -eu
b1dfa8
b1dfa8
b1dfa8
if [ -z "$RPM_BUILD_ROOT" ] || [ "$RPM_BUILD_ROOT" = "/" ]; then
b1dfa8
	exit 0
b1dfa8
fi
b1dfa8
b1dfa8
CLANG_FLAGS=$@
b1dfa8
NCPUS=${RPM_BUILD_NCPUS:-1}
b1dfa8
b1dfa8
check_convert_bitcode () {
b1dfa8
  local file_name=$(realpath ${1})
b1dfa8
  local file_type=$(file ${file_name})
b1dfa8
b1dfa8
  shift
b1dfa8
  CLANG_FLAGS="$@"
b1dfa8
b1dfa8
  if [[ "${file_type}" == *"LLVM IR bitcode"* ]]; then
b1dfa8
    # Check the output of llvm-strings for the command line, which is in the LLVM bitcode because
b1dfa8
    # we pass -frecord-gcc-switches.
b1dfa8
    # Check for a line that has "-flto" after (or without) "-fno-lto".
b1dfa8
    llvm-strings ${file_name} | while read line ; do
b1dfa8
      flto=$(echo $line   | grep -o -b -e -flto     | tail -n 1 | cut -d : -f 1)
b1dfa8
      fnolto=$(echo $line | grep -o -b -e -fno-lto  | tail -n 1 | cut -d : -f 1)
b1dfa8
b1dfa8
      if test -n "$flto" && { test -z "$fnolto" || test "$flto" -gt "$fnolto"; } ; then
b1dfa8
        echo "Compiling LLVM bitcode file ${file_name}."
b1dfa8
        clang ${CLANG_FLAGS} -fno-lto -Wno-unused-command-line-argument \
b1dfa8
          -x ir ${file_name} -c -o ${file_name}
b1dfa8
        break
b1dfa8
      fi
b1dfa8
      done
b1dfa8
  elif [[ "${file_type}" == *"current ar archive"* ]]; then
b1dfa8
    echo "Unpacking ar archive ${file_name} to check for LLVM bitcode components."
b1dfa8
    # create archive stage for objects
b1dfa8
    local archive_stage=$(mktemp -d)
b1dfa8
    local archive=${file_name}
b1dfa8
    pushd ${archive_stage}
b1dfa8
    ar x ${archive}
b1dfa8
    for archived_file in $(find -not -type d); do
b1dfa8
      check_convert_bitcode ${archived_file} ${CLANG_FLAGS}
b1dfa8
      echo "Repacking ${archived_file} into ${archive}."
b1dfa8
      ar r ${archive} ${archived_file}
b1dfa8
    done
b1dfa8
    popd
b1dfa8
  fi
b1dfa8
}
b1dfa8
b1dfa8
echo "Checking for LLVM bitcode artifacts"
b1dfa8
export -f check_convert_bitcode
b1dfa8
find "$RPM_BUILD_ROOT" -type f -name "*.[ao]" -print0 | \
b1dfa8
  xargs -0 -r -n1 -P$NCPUS sh -c "check_convert_bitcode \$@ $CLANG_FLAGS" ARG0