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