Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # The LLVM Compiler Infrastructure |
| 4 | # |
| 5 | # This file is distributed under the University of Illinois Open Source |
| 6 | # License. See LICENSE.TXT for details. |
| 7 | # |
| 8 | ##===----------------------------------------------------------------------===## |
| 9 | # |
| 10 | # This script attempts to be a drop-in replacement for gcc. |
| 11 | # |
| 12 | ##===----------------------------------------------------------------------===## |
| 13 | |
| 14 | import sys |
| 15 | import subprocess |
| 16 | |
| 17 | def error(message): |
| 18 | print 'ccc: ' + message |
| 19 | sys.exit(1) |
| 20 | |
| 21 | def run(args): |
| 22 | print ' '.join(args) |
| 23 | code = subprocess.call(args) |
| 24 | if code: |
| 25 | sys.exit(code) |
| 26 | |
| 27 | def preprocess(args): |
| 28 | command = 'clang -E'.split() |
| 29 | run(command + args) |
| 30 | |
| 31 | def compile(args): |
| 32 | command = 'clang -emit-llvm-bc'.split() |
| 33 | run(command + args) |
| 34 | |
| 35 | def link(args): |
| 36 | command = 'llvm-ld -native'.split() |
| 37 | run(command + args) |
| 38 | |
| 39 | def main(args): |
| 40 | action = 'link' |
| 41 | output = '' |
| 42 | compile_opts = ['-U__GNUC__'] |
| 43 | link_opts = [] |
| 44 | files = [] |
| 45 | |
| 46 | i = 0 |
| 47 | while i < len(args): |
| 48 | arg = args[i] |
| 49 | if arg == '-E': |
| 50 | action = 'preprocess' |
| 51 | if arg == '-c': |
| 52 | action = 'compile' |
| 53 | if arg == '-o': |
| 54 | output = args[i+1] |
| 55 | i += 1 |
| 56 | if arg[:2] in ['-D', '-I', '-U']: |
| 57 | if not arg[2:]: |
| 58 | arg += args[i+1] |
| 59 | i += 1 |
| 60 | compile_opts.append(arg) |
| 61 | if arg[:2] in ['-l', '-L', '-O']: |
| 62 | if arg == '-O': arg = '-O1' |
| 63 | if arg == '-Os': arg = '-O2' |
| 64 | link_opts.append(arg) |
| 65 | if arg[0] != '-': |
| 66 | files.append(arg) |
| 67 | i += 1 |
| 68 | |
| 69 | if not files: |
| 70 | error('no input files') |
| 71 | |
| 72 | if action == 'preprocess': |
| 73 | args = compile_opts + files |
| 74 | preprocess(args) |
| 75 | |
| 76 | if action == 'compile': |
| 77 | if not output: |
| 78 | output = files[0].replace('.c', '.o') |
| 79 | args = ['-o', output] + compile_opts + files |
| 80 | compile(args) |
| 81 | |
| 82 | if action == 'link': |
| 83 | for i, file in enumerate(files): |
| 84 | if '.c' in file: |
| 85 | out = file.replace('.c', '.o') |
| 86 | args = ['-o', out] + compile_opts + [file] |
| 87 | compile(args) |
| 88 | files[i] = out |
| 89 | if not output: |
| 90 | output = 'a.out' |
| 91 | args = ['-o', output] + link_opts + files |
| 92 | link(args) |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | main(sys.argv[1:]) |