changeset 5020:fb86bf0b485a

<oerjan> fetch http://oerjan.nvg.org/gcccomp
author HackBot
date Mon, 06 Oct 2014 05:38:14 +0000
parents 917b2cefb4c5
children f7331c9f689f
files gcccomp
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcccomp	Mon Oct 06 05:38:14 2014 +0000
@@ -0,0 +1,54 @@
+#!/bin/bash
+LANG="$1"
+echo >>"$2"
+
+case "$LANG" in
+    c)
+        HEAD='#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <sys/types.h>\n#include <unistd.h>\nint main(int argc, char **argv) {\nint main(void) { printf("Calling main from itself not supported.\n"); exit(1); }\n'
+        TAIL='; return 0; }'
+        EXT='c'
+        GCC='gcc'
+        FLAGS='-std=gnu99'
+    ;;
+
+    c++)
+        HEAD='#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\nusing namespace std;\nint main(int argc, char **argv) {'
+        TAIL='; return 0; }'
+        EXT='cc'
+        GCC='g++'
+        FLAGS='-std=gnu++0x'
+    ;;
+
+    assembler)
+        HEAD='.globl main; main: pushq %rbp; movq %rsp, %rbp;'
+        TAIL='movl $0, %eax; leave; ret;'
+        EXT='s'
+        GCC='gcc'
+        FLAGS=''
+    ;;
+
+    java)
+        HEAD='class Main { public static void main(String[] args) {'
+        TAIL='; } }'
+        EXT='java'
+        GCC='gcj'
+        FLAGS='--main=Main'
+    ;;
+esac
+
+SOURCE="/tmp/source.$$.$EXT"
+
+(
+    echo -e "$HEAD"
+    cat "$2"
+    echo "$TAIL"
+) > "$SOURCE"
+
+"$GCC" -x "$LANG" "$SOURCE" $FLAGS -o /tmp/compiled.$$ 2> /dev/null ||
+"$GCC" -x "$LANG" "$2" $FLAGS -o /tmp/compiled.$$ 2> /dev/null ||
+echo 'Does not compile.'
+rm -f "$SOURCE"
+
+#ulimit -u 3
+if [ -x /tmp/compiled.$$ ] ; then /tmp/compiled.$$; fi 2>&1
+rm -f /tmp/compiled.$$