-
Notifications
You must be signed in to change notification settings - Fork 5
/
cc-wrapper.sh
94 lines (73 loc) · 2.09 KB
/
cc-wrapper.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! @shell@
set -eu
if [[ ! -v NIX_REMOTE ]]; then
echo "$0: warning: recursive Nix is disabled" >&2
exec @next@/bin/@program@ "$@"
fi
isCompilation=
cppFlags=()
compileFlags=()
sources=()
dest=
args=("$@")
for ((n = 0; n < $#; n++)); do
arg="${args[$n]}"
if [[ $arg = -c ]]; then
isCompilation=1
elif [[ $arg = -o ]]; then
: $((n++))
dest="${args[$n]}"
elif [[ $arg = --param ]]; then
: $((n++))
cppFlags+=("$arg" "${args[$n]}")
compileFlags+=("$arg" "${args[$n]}")
elif [[ $arg = -idirafter || $arg = -I || $arg = -isystem || $arg = -include || $arg = -MF ]]; then
: $((n++))
cppFlags+=("$arg" "${args[$n]}")
elif [[ $arg =~ ^-D.* || $arg =~ ^-I.* ]]; then
cppFlags+=("$arg")
elif [[ ! $arg =~ ^- ]]; then
sources+=("$arg")
else
cppFlags+=("$arg")
compileFlags+=("$arg")
fi
done
if [[ ! $isCompilation || ${#sources[*]} != 1 || ${sources[0]} =~ conftest ]]; then
#echo "SKIP: ${sources[@]}" >&2
exec @next@/bin/@program@ "$@"
fi
source="${sources[0]}"
if [[ -z $dest ]]; then
dest="$(basename "$source" .c).o" # FIXME
fi
if [[ $source =~ \.c$ ]]; then
ext=i
else
ext=ii
fi
#echo "NIX-CCACHE: $@" >&2
#echo "preprocessing to $dest.$ext..." >&2
@next@/bin/@program@ -o "$dest.$ext" -E "$source" "${cppFlags[@]}"
#echo "compiling to $dest..."
escapedArgs='"-o" "${placeholder "out"}" "-c" '$(readlink -f "$dest.$ext")' '
for arg in "${compileFlags[@]}"; do
escapedArgs+='"'
escapedArgs+="$arg" # FIXME: escape
escapedArgs+='" '
done
#echo "FINAL: @next@/bin/@program@ $escapedArgs"
# FIXME: add any store paths mentioned in the arguments (e.g. -B
# flags) to the input closure, or filter them?
@nix@/bin/nix-build --quiet -o "$dest.link" -E '(
derivation {
name = "cc";
system = "@system@";
builder = builtins.storePath "@next@/bin/@program@";
extra = builtins.storePath "@binutils@";
args = [ '"$escapedArgs"' "-B@libexec@/" "-B@binutils@/bin" ];
}
)' > /dev/null
cp "$dest.link" "$dest"
rm -f "$dest.$ext" "$dest.link"
exit 0