]> git.cameronkatri.com Git - cc-run.git/blob - cc-run.sh
Allow compiling stdin
[cc-run.git] / cc-run.sh
1 #!/bin/sh
2 compiler=$(basename "$0" | sed 's|-run$||')
3 src=""
4 args=""
5 exeargs=""
6
7 if ! command -v "$compiler" >/dev/null 2>&1; then
8 echo "$(basename "$0"): error: can't find compiler: '$compiler'" >&2
9 exit 1
10 fi
11
12 for arg in "$@"; do
13 if [ "$src" = "" -a -e "$arg" -o "$arg" = "-" ]; then
14 src="$arg"
15 elif [ "$src" != "" ]; then
16 exeargs="$exeargs $arg"
17 else
18 args="$args $arg"
19 fi
20 done
21
22 case $compiler in
23 cc|gcc|gcc-*|clang|clang-*|tcc)
24 if [ "$src" = '-' ]; then
25 args="$args -x c"
26 fi
27 args="$args $CPPFLAGS $CFLAGS $LDFLAGS";;
28 c++|g++|g++-*|clang++|clang++-*)
29 if [ "$src" = '-' ]; then
30 args="$args -x c++"
31 fi
32 args="$args $CPPFLAGS $CXXFLAGS $LDFLAGS"
33 esac
34
35 exe=$(mktemp -t cc-run) || exit 1
36 if ! $compiler $args $src -o $exe; then
37 rm -f $exe
38 exit 1
39 fi
40
41 $exe $exeargs
42 ret=$?
43 rm -f $exe
44 exit $ret