summaryrefslogtreecommitdiffstats
path: root/cc-run.sh
blob: 7a59ed697ef0d0ff7f88f2165ed4635818e99a0d (plain) (blame)
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
#!/bin/sh
compiler=$(basename "$0" | sed 's|-run$||')
exe=$(mktemp -t cc-run) || exit 1
src=""
args=""
exeargs=""

if ! command -v "$compiler" >/dev/null 2>&1; then
	echo "$(basename "$0"): error: can't find compiler: '$compiler'" >&2
	exit 1
fi

for arg in "$@"; do
	if [ -e "$arg" ] && [ "$src" = "" ]; then
		src=$arg
	elif [ "$src" != "" ]; then
		exeargs="$exeargs $arg"
	else
		args="$args $arg"
	fi
done

case "$compiler" in
	cc|gcc|gcc-*|clang|clang-*)
		args="$args $CPPFLAGS $CFLAGS $LDFLAGS";;
	c++|g++|g++-*|clang++|clang++-*)
		args="$args $CPPFLAGS $CXXFLAGS $LDFLAGS"
esac

$compiler $args $src -o $exe || exit 1

$exe $exeargs
ret=$?
rm -f $exe
exit $ret