[cfe-commits] r46501 - /cfe/trunk/utils/ccc

Anders Carlsson andersca at mac.com
Mon Jan 28 23:21:34 PST 2008


Author: andersca
Date: Tue Jan 29 01:21:34 2008
New Revision: 46501

URL: http://llvm.org/viewvc/llvm-project?rev=46501&view=rev
Log:
Improvements to ccc. Patch by Shantonu Sen.

Modified:
    cfe/trunk/utils/ccc

Modified: cfe/trunk/utils/ccc
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ccc?rev=46501&r1=46500&r2=46501&view=diff

==============================================================================
--- cfe/trunk/utils/ccc (original)
+++ cfe/trunk/utils/ccc Tue Jan 29 01:21:34 2008
@@ -15,11 +15,11 @@
 import subprocess
 
 def error(message):
-    print 'ccc: ' + message
+    print >> sys.stderr, 'ccc: ' + message
     sys.exit(1)
 
 def run(args):
-    print ' '.join(args)
+    print >> sys.stderr, ' '.join(args)
     code = subprocess.call(args)
     if code:
         sys.exit(code)
@@ -36,38 +36,102 @@
     command = 'llvm-ld -native'.split()
     run(command + args)
 
+def extension(path):
+    return path.rpartition(".")[2]
+
+def changeextension(path, newext):
+    components = path.rpartition(".")
+    return "".join([components[0], components[1], newext])
+
+def inferlanguage(extension):
+    if extension == "c":
+        return "c"
+    elif extension == "i":
+        return "c-cpp-output"
+    elif extension == "m":
+        return "objective-c"
+    elif extension == "mi":
+        return "objective-c-cpp-output"
+    else:
+        return "unknown"
+
 def main(args):
     action = 'link'
     output = ''
     compile_opts = []
     link_opts = []
     files = []
-
+    save_temps = 0
+    language = ''
+    
     i = 0
     while i < len(args):
         arg = args[i]
+
+        # Modes ccc supports
         if arg == '-E':
             action = 'preprocess'
         if arg == '-c':
             action = 'compile'
         if arg.startswith('-print-prog-name'):
             action = 'print-prog-name'
-        if arg == '-o':
-            output = args[i+1]
-            i += 1
-        if arg == '--param':
+        if arg == '-save-temps':
+            save_temps = 1
+
+        # Options with no arguments that should pass through
+        if arg in ['-v']:
+            compile_opts.append(arg)
+            link_opts.append(arg)
+        
+        # Options with one argument that should be ignored
+        if arg in ['--param', '-arch']:
             i += 1
-        if arg[:2] in ['-D', '-I', '-U']:
+
+        # Prefix matches for the compile mode
+        if arg[:2] in ['-D', '-I', '-U', '-F']:
             if not arg[2:]:
                 arg += args[i+1]
                 i += 1
             compile_opts.append(arg)
-        if arg[:2] in ['-l', '-L', '-O']:
+        if arg[:5] in ['-std=']:
+            compile_opts.append(arg)
+
+        # Options with one argument that should pass through
+        if arg in ['-include']:
+            compile_opts.append(arg)
+            compile_opts.append(args[i+1])
+            i += 1
+
+        # Prefix matches for the link mode
+        if arg[:2] in ['-l', '-L', '-O', '-F']:
             if arg == '-O': arg = '-O1'
             if arg == '-Os': arg = '-O2'
             link_opts.append(arg)
+
+        # Options with one argument that should pass through
+        if arg in ['-framework']:
+            link_opts.append(arg)
+            link_opts.append(args[i+1])
+            i += 1
+
+        # Input files
+        if arg == '-filelist':
+            f = open(args[i+1])
+            for line in f:
+                files.append(line.strip())
+            f.close()
+            i += 1
+        if arg == '-x':
+            language = args[i+1]
+            i += 1
         if arg[0] != '-':
             files.append(arg)
+
+        # Output file
+        if arg == '-o':
+            output = args[i+1]
+            i += 1
+
         i += 1
 
     if action == 'print-prog-name':
@@ -78,21 +142,50 @@
     if not files:
         error('no input files')
 
-    if action == 'preprocess':
-        args = compile_opts + files
-        preprocess(args)
+    if action == 'preprocess' or save_temps:
+        for i, file in enumerate(files):
+            if not language:
+                language = inferlanguage(extension(file))
+            if save_temps and action != 'preprocess':
+                # Need a temporary output file
+                if language == 'c':
+                    poutput = changeextension(file, "i");
+                elif language == 'objective-c':
+                    poutput = changeextension(file, "mi");
+                else:
+                    poutput = changeextension(file, "tmp." + extension(file))
+                files[i] = poutput
+            else:
+                poutput = output
+            if poutput:
+                args = ['-x', language, '-o', poutput, file] + compile_opts
+            else:
+                args = ['-x', language, file] + compile_opts
+            preprocess(args)
+            # Discard the explicit language after used once
+            language = ''
 
-    if action == 'compile':
-        if not output:
-            output = files[0].replace('.c', '.o')
-        args = ['-o', output] + compile_opts + files
-        compile(args)
+    if action == 'compile' or save_temps:
+        for i, file in enumerate(files):
+            if not language:
+                language = inferlanguage(extension(file))
+            if save_temps and action != "compile":
+                # Need a temporary output file
+                coutput = changeextension(file, "o");
+                files[i] = coutput
+            elif not output:
+                coutput = changeextension(file, "o")
+            else:
+                coutput = output
+            args = ['-x', language, '-o', coutput, file] + compile_opts
+            compile(args)
+            language = ''
 
     if action == 'link':
         for i, file in enumerate(files):
-            if '.c' in file:
-                out = file.replace('.c', '.o')
-                args = ['-o', out] + compile_opts + [file]
+            if extension(file) != "o":
+                out = changeextension(file, "o")
+                args = ['-o', out, file] + compile_opts
                 compile(args)
                 files[i] = out
         if not output:





More information about the cfe-commits mailing list