summaryrefslogtreecommitdiff
path: root/wrappers/install-wrappers.sh
blob: 461e69a47c85c431eaa6809e80aca2e486df23b4 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash

set -eu -o pipefail

wrapper_dir=$(cd "$(dirname "$0")"; pwd)

orig_bin_dir=$(cd "$1"; pwd)
shadow_bin_dir=$(cd "$2"; pwd)
top=$(cd "$3"; pwd)
shadow_top=$(mkdir -p "$4"; cd "$4"; pwd)
shift 4

cd "$orig_bin_dir"

if [ $# = 0 ]; then
    # wrap all files
    readarray -t to_be_wrapped < <(find ./ ! -type d | sed -e "s#^\./##")
else
    # wrap only listed files
    to_be_wrapped=("$@")
fi

bin=$(basename "$(pwd)")

for exe in "${to_be_wrapped[@]}"; do
    if ! [ -x "$exe" ]; then
	continue
    fi

    mkdir -p "../$bin.orig/$(dirname "$exe")"
    mkdir -p "../$bin.wrapper/$(dirname "$exe")"

    # Install or update the wrapper
    case "$exe" in
	"clang") wrapper="shadow-cc.sh" ;;
	"clang++") wrapper="shadow-cc.sh" ;;
	"llvm-ar") wrapper="shadow-ar.sh" ;;
        "strip.sh") wrapper="shadow-strip.sh" ;;
	*) wrapper="count-wrapper.sh" ;;
    esac

    exe_hash=$(git rev-parse HEAD)
    ccache="CCACHE_COMPILERCHECK=string:$exe_hash /usr/bin/ccache"

    # We copy wrappers to bin.wrapper directory so that we can re-run install
    # procedure multiple times without side-effects.
    cp "$wrapper_dir/$wrapper" "../$bin.wrapper/$exe"
    sed -i \
	-e "s%#TOP#%$top%" \
	-e "s%#SHADOW_TOP#%$shadow_top%" \
	-e "s%#ORIG_TOOL#%$orig_bin_dir/../$bin.orig/$exe%" \
	-e "s%#SHADOW_TOOL#%$shadow_bin_dir/$exe%" \
	-e "s%#CCACHE#%$ccache%" \
	"../$bin.wrapper/$exe"

    # Calculate path relative to $exe's directory -- for ln.
    ln_rel_path="$bin.wrapper/$exe"
    exe_path="$exe"
    while true; do
	ln_rel_path="../$ln_rel_path"
	exe_path=$(dirname "$exe_path")
	if [ "$exe_path" = "." ]; then
	    break
	fi
    done

    # Check if executable was already moved to ../bin.orig
    if [ -L "$exe" ] \
	   && [ x"$(readlink "$exe")" = x"$ln_rel_path" ]; then
	continue
    fi

    # Move the executable and symlink to the wrapper.
    mv "$exe" "../$bin.orig/$exe"
    ln -s "$ln_rel_path" "$exe"
done

mkdir -p "${shadow_top}-counts"
echo "benchmark,symbol,size" > $shadow_top.size