Compiler driver

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45809 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/ccc b/utils/ccc
new file mode 100755
index 0000000..083fd93
--- /dev/null
+++ b/utils/ccc
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+#
+# This script attempts to be a drop-in replacement for gcc.
+#
+##===----------------------------------------------------------------------===##
+
+import sys
+import subprocess
+
+def error(message):
+    print 'ccc: ' + message
+    sys.exit(1)
+
+def run(args):
+    print ' '.join(args)
+    code = subprocess.call(args)
+    if code:
+        sys.exit(code)
+
+def preprocess(args):
+    command = 'clang -E'.split()
+    run(command + args)
+
+def compile(args):
+    command = 'clang -emit-llvm-bc'.split()
+    run(command + args)
+
+def link(args):
+    command = 'llvm-ld -native'.split()
+    run(command + args)
+
+def main(args):
+    action = 'link'
+    output = ''
+    compile_opts = ['-U__GNUC__']
+    link_opts = []
+    files = []
+
+    i = 0
+    while i < len(args):
+        arg = args[i]
+        if arg == '-E':
+            action = 'preprocess'
+        if arg == '-c':
+            action = 'compile'
+        if arg == '-o':
+            output = args[i+1]
+            i += 1
+        if arg[:2] in ['-D', '-I', '-U']:
+            if not arg[2:]:
+                arg += args[i+1]
+                i += 1
+            compile_opts.append(arg)
+        if arg[:2] in ['-l', '-L', '-O']:
+            if arg == '-O': arg = '-O1'
+            if arg == '-Os': arg = '-O2'
+            link_opts.append(arg)
+        if arg[0] != '-':
+            files.append(arg)
+        i += 1
+
+    if not files:
+        error('no input files')
+
+    if action == 'preprocess':
+        args = compile_opts + files
+        preprocess(args)
+
+    if action == 'compile':
+        if not output:
+            output = files[0].replace('.c', '.o')
+        args = ['-o', output] + compile_opts + files
+        compile(args)
+
+    if action == 'link':
+        for i, file in enumerate(files):
+            if '.c' in file:
+                out = file.replace('.c', '.o')
+                args = ['-o', out] + compile_opts + [file]
+                compile(args)
+                files[i] = out
+        if not output:
+            output = 'a.out'
+        args = ['-o', output] + link_opts + files
+        link(args)
+
+if __name__ == '__main__':
+    main(sys.argv[1:])