Index: utils/ccc =================================================================== --- utils/ccc (revision 46878) +++ utils/ccc (working copy) @@ -11,15 +11,18 @@ # ##===----------------------------------------------------------------------===## +import os import sys import subprocess +import tempfile def error(message): print >> sys.stderr, 'ccc: ' + message sys.exit(1) -def run(args): - print >> sys.stderr, ' '.join(args) +def run(args, silent=False): + if not silent: + print >> sys.stderr, ' '.join(args) code = subprocess.call(args) if code > 255: code = 1 @@ -30,12 +33,37 @@ command = 'clang -E'.split() run(command + args) -def compile(args): - command = 'clang -emit-llvm-bc'.split() - run(command + args) +def compile(args, obj_name): + # First, compile the c file into a temporary .bc file + bc_fd, bc_name = tempfile.mkstemp('.bc', 'ccc') + command = ['clang', '-emit-llvm-bc'] + args + ['-o', bc_name] + run(command) + # Next, perform some necessary optimizations + bcopt_fd, bcopt_name = tempfile.mkstemp('.bc', 'ccc') + command = ['opt', '-globaldce', bc_name, '-f', '-o', bcopt_name] + run(command) + + os.unlink(bc_name) + os.close(bc_fd) + + # Compile the .bc file into an .s file + asm_fd, asm_name = tempfile.mkstemp('.s', 'ccc') + command = ['llc', bcopt_name, '-f', '-o', asm_name] + run(command) + + os.unlink(bcopt_name) + os.close(bcopt_fd) + + # Assemble the .s file + command = ['gcc', '-c', asm_name, '-o', obj_name] + run(command) + + os.unlink(asm_name) + os.close(asm_fd) + def link(args): - command = 'llvm-ld -native'.split() + command = 'gcc'.split() run(command + args) def extension(path): @@ -92,7 +120,7 @@ link_opts.append(arg) # Options with one argument that should be ignored - if arg in ['--param', '-arch', '-u']: + if arg in ['--param', '-arch']: i += 1 # Prefix matches for the compile mode @@ -117,7 +145,7 @@ link_opts.append(arg) # Options with one argument that should pass through - if arg in ['-framework']: + if arg in ['-framework', '-u', '-undefined']: link_opts.append(arg) link_opts.append(args[i+1]) i += 1 @@ -185,8 +213,8 @@ coutput = changeextension(file, "o") else: coutput = output - args = ['-x', language, '-o', coutput, file] + compile_opts - compile(args) + args = ['-x', language, file] + compile_opts + compile(args, coutput) language = '' if action == 'link': @@ -194,8 +222,8 @@ ext = extension(file) if ext != "o" and ext != "a": out = changeextension(file, "o") - args = ['-o', out, file] + compile_opts - compile(args) + args = [file] + compile_opts + compile(args, out) files[i] = out if not output: output = 'a.out'