File: llvm-compile-lto-elf.sh

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (59 lines) | stat: -rw-r--r-- 1,819 bytes parent folder | download | duplicates (11)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/bash -eu

# Initial version:
# https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/rawhide/f/brp-llvm-compile-lto-elf

CLANG_FLAGS=$@

if test -z $P_TO_LLVM; then
    echo "P_TO_LLVM isn't set"
    exit 1
fi

if test -z $NJOBS; then
    echo "NJOBS isn't set"
    exit 1
fi

if test -z $VERSION; then
    echo "VERSION isn't set"
    exit 1
fi

NCPUS=$NJOBS

check_convert_bitcode () {
  local file_name=$(realpath ${1})
  local file_type=$(file ${file_name})
  shift
  CLANG_FLAGS="$@"

  if [[ "${file_type}" == *"LLVM IR bitcode"* ]]; then
    # check for an indication that the bitcode was
    # compiled with -flto
    ${P_TO_LLVM}/debian/tmp/usr/bin/llvm-bcanalyzer-${VERSION} -dump ${file_name} | grep -xP '.*\-flto((?!-fno-lto).)*' 2>&1 > /dev/null
    if [ $? -eq 0 ]; then
      echo "Compiling LLVM bitcode file ${file_name}."
      ${P_TO_LLVM}/debian/tmp/usr/bin/clang-${VERSION} -fno-lto -opaque-pointers -Wno-unused-command-line-argument \
        -x ir ${file_name} -c -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} ${CLANG_FLAGS}
      echo "Repacking ${archived_file} into ${archive}."
      ${P_TO_LLVM}/debian/tmp/usr/bin/llvm-ar-${VERSION} r ${archive} ${archived_file}
    done
    popd
  fi
}

echo "Checking for LLVM bitcode artifacts"
export -f check_convert_bitcode
find "$P_TO_LLVM/debian/" -type f -name "*.[ao]" -print0 | \
  xargs -0 -r -n1 -P$NCPUS bash -c "check_convert_bitcode \$@ $CLANG_FLAGS" ARG0