]> git.cameronkatri.com Git - cc-run.git/commitdiff
Allow compiling stdin main
authorCameron Katri <me@cameronkatri.com>
Fri, 8 Apr 2022 23:39:57 +0000 (19:39 -0400)
committerCameron Katri <me@cameronkatri.com>
Fri, 8 Apr 2022 23:40:50 +0000 (19:40 -0400)
cc-run.sh

index 7a59ed697ef0d0ff7f88f2165ed4635818e99a0d..ee1d5fd74c7f414a71d8a79db230666a2ea58afb 100755 (executable)
--- a/cc-run.sh
+++ b/cc-run.sh
@@ -1,6 +1,5 @@
 #!/bin/sh
 compiler=$(basename "$0" | sed 's|-run$||')
-exe=$(mktemp -t cc-run) || exit 1
 src=""
 args=""
 exeargs=""
@@ -11,8 +10,8 @@ if ! command -v "$compiler" >/dev/null 2>&1; then
 fi
 
 for arg in "$@"; do
-       if [ -e "$arg" ] && [ "$src" = "" ]; then
-               src=$arg
+       if [ "$src" = "" -a -e "$arg" -o "$arg" = "-" ]; then
+               src="$arg"
        elif [ "$src" != "" ]; then
                exeargs="$exeargs $arg"
        else
@@ -20,14 +19,24 @@ for arg in "$@"; do
        fi
 done
 
-case "$compiler" in
-       cc|gcc|gcc-*|clang|clang-*)
+case $compiler in
+       cc|gcc|gcc-*|clang|clang-*|tcc)
+               if [ "$src" = '-' ]; then
+                       args="$args -x c"
+               fi
                args="$args $CPPFLAGS $CFLAGS $LDFLAGS";;
        c++|g++|g++-*|clang++|clang++-*)
+               if [ "$src" = '-' ]; then
+                       args="$args -x c++"
+               fi
                args="$args $CPPFLAGS $CXXFLAGS $LDFLAGS"
 esac
 
-$compiler $args $src -o $exe || exit 1
+exe=$(mktemp -t cc-run) || exit 1
+if ! $compiler $args $src -o $exe; then
+       rm -f $exe
+       exit 1
+fi
 
 $exe $exeargs
 ret=$?