forked from ryanmjacobs/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c
executable file
·181 lines (156 loc) · 3.99 KB
/
c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env bash
# max cachesize in kilobytes
[[ -z "$C_CACHE_SIZE" ]] && C_CACHE_SIZE=$((5*1024))
help_msg() {
>&$1 echo "Usage: $0 [file.c ... | compiler_options ...] [program arguments]"
>&$1 echo "Execute C programs from the command line."
>&$1 echo
>&$1 echo " Ex: c main.c"
>&$1 echo " Ex: c main.c arg1 arg2"
>&$1 echo " Ex: c \"main.c other.c\" arg1 arg2"
>&$1 echo " Ex: c \"main.c -lncurses\" arg1 arg2"
>&$1 echo
}
cleanup() {
# remove $tmpdir, which holds source code
rm -rf "$tmpdir" "$tmproot/stdin.c"
# remove cache files until we are under $cachesize
while [[ "$(du -kc "$tmproot" | tail -1 | cut -f1)" -gt "$C_CACHE_SIZE" ]]; do
[[ -z "$(ls -A "$tmproot")" ]] && break
rm -rf "$(find "$tmproot" -type f | tail -n1)"
done
}
# help if we have no arguments and no stdin
if [[ $# -eq 0 && -t 0 ]]; then
help_msg 2
exit 1
fi
# help if we get the flags
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
help_msg 1
exit 0
fi
# ensure our $CC variable is set
[[ -z "$CC" ]] && CC=cc
if ! type "$CC" &>/dev/null; then
>&2 echo "error: \$CC ($CC) not found"
exit 1
fi
# $comp holds the files and options that will be passed to the compiler
# $fname will become the program's argv[0]
for arg in $1; do
if [[ "$arg" == "--" ]]; then
fname="$2"
comp+=("$2")
shift
else
comp+=("$arg")
fi
done
# If we don't have an fname yet, pick one out of $comp
# that doesn't start with a '-'
if [[ -z "$fname" ]]; then
for arg in $1; do
if [[ "$arg" != -* ]]; then
fname="$arg"
break
fi
done
fi
# get cache location
if [[ -n "$C_CACHE_PATH" ]]; then
tmproot="$C_CACHE_PATH"
else
[[ -z "$TMPDIR" ]] && TMPDIR="/tmp"
tmproot="$TMPDIR/c.cache"
fi
mkdir -p "$tmproot"
# create stdin file if we need it
if [[ ! -t 0 ]]; then
fname="stdin"
stdin="$tmproot/stdin.c"
comp+=("$stdin")
cat <&0 >$stdin # useless use of cat?
fi
0<&-
shasum="sha1sum"
if ! type "$shasum" &>/dev/null; then
shasum="shasum"
fi
# determine if we are C or C++, then use appropriate flags
for f in ${comp[@]}; do
if [[ -f "$f" && "$f" =~ \.(cc|c\+\+|cpp|cxx)$ ]]; then
comp+=($CXXFLAGS "-lstdc++")
type "$CXX" &>/dev/null && CC="$CXX"
break
else
comp+=($CFLAGS)
break
fi
done
comp+=($CPPFLAGS)
# create calculated biname
cachename="$("$shasum" <<< "$CC")"
for f in ${comp[@]}; do
# first, append sha1sums of all files and options into one long string
if [[ -f "$f" ]]; then
cachename+="$("$shasum" "$f" | cut -d' ' -f1)"
else
cachename+="$("$shasum" <<< "$f" | cut -d' ' -f1)"
fi
done
# now sha1sum this so that it fits into a filename
sha="$("$shasum" <<< $cachename | cut -d' ' -f1)"
tmpdir="$tmproot/dir.$sha"
binname="$tmproot/$sha"
# run binary
run() {
trap cleanup SIGINT
shift
(exec -a "$fname" "$binname" "$@")
ret=$?
trap - SIGINT
cleanup
exit $ret
}
# run cached file if it exists
if [[ -f "$binname" ]]; then
run "$@"
else
mkdir -p "$tmpdir"
fi
# assemble our includes, based on the original file locations
includes="-I$(pwd)"
for f in ${comp[@]}; do
[[ -f "$f" ]] && includes+=" -I$(dirname "$f")"
done
# copy source files to $tmpdir
i=0
for f in ${comp[@]}; do
if [[ -f "$f" && "$f" != $tmpdir* ]]; then
mkdir -p "$tmpdir/$(dirname "$f")"
cp "$f" "$tmpdir/$f"
# assume language is C, if no extension is given
base="$(basename "$f")"
ext="${base##*.}"
if [[ "$ext" == "$base" ]]; then
comp[$i]="-x c $tmpdir/$f -x none"
else
comp[$i]="$tmpdir/$f"
fi
fi
let i++
done
# remove shebangs
for f in ${comp[@]}; do
if [[ -f "$f" ]] && [[ "$(head -n1 "$f")" == \#\!* ]]; then
echo "$(tail -n +2 "$f")" > "$f"
fi
done
# compile and run
if "$CC" -O2 -o "$binname" ${comp[@]} $includes; then
run "$@"
else
cleanup
exit 1
fi