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 |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 15 | import subprocess |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 16 | |
| 17 | def error(message): |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 18 | print >> sys.stderr, 'ccc: ' + message |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 19 | sys.exit(1) |
| 20 | |
| 21 | def run(args): |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 22 | print >> sys.stderr, ' '.join(args) |
| 23 | code = subprocess.call(args) |
Bill Wendling | 550ce0f | 2008-02-03 21:27:46 +0000 | [diff] [blame] | 24 | if code > 255: |
| 25 | code = 1 |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 26 | if code: |
| 27 | sys.exit(code) |
| 28 | |
| 29 | def preprocess(args): |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 30 | command = 'clang -E'.split() |
| 31 | run(command + args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 32 | |
| 33 | def compile(args): |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 34 | command = 'clang -emit-llvm-bc'.split() |
| 35 | run(command + args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 36 | |
| 37 | def link(args): |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 38 | command = 'llvm-ld -native'.split() |
| 39 | run(command + args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 40 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 41 | def extension(path): |
Seo Sanghyeon | 795aaed | 2008-02-03 03:40:41 +0000 | [diff] [blame] | 42 | return path.split(".")[-1] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 43 | |
| 44 | def changeextension(path, newext): |
Seo Sanghyeon | 795aaed | 2008-02-03 03:40:41 +0000 | [diff] [blame] | 45 | i = path.rfind('.') |
| 46 | if i < 0: |
| 47 | return path |
Bill Wendling | 550ce0f | 2008-02-03 21:27:46 +0000 | [diff] [blame] | 48 | j = path.rfind('/', 0, i) |
| 49 | print path |
| 50 | if j < 0: |
| 51 | return path[:i] + "." + newext |
| 52 | return path[j+1:i] + "." + newext |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 53 | |
| 54 | def inferlanguage(extension): |
| 55 | if extension == "c": |
| 56 | return "c" |
| 57 | elif extension == "i": |
| 58 | return "c-cpp-output" |
| 59 | elif extension == "m": |
| 60 | return "objective-c" |
| 61 | elif extension == "mi": |
| 62 | return "objective-c-cpp-output" |
| 63 | else: |
| 64 | return "unknown" |
| 65 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 66 | def main(args): |
| 67 | action = 'link' |
| 68 | output = '' |
Seo Sanghyeon | 96b99f7 | 2008-01-25 14:57:54 +0000 | [diff] [blame] | 69 | compile_opts = [] |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 70 | link_opts = [] |
| 71 | files = [] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 72 | save_temps = 0 |
| 73 | language = '' |
| 74 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 75 | i = 0 |
| 76 | while i < len(args): |
| 77 | arg = args[i] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 78 | |
| 79 | # Modes ccc supports |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 80 | if arg == '-E': |
| 81 | action = 'preprocess' |
| 82 | if arg == '-c': |
| 83 | action = 'compile' |
Seo Sanghyeon | 96b99f7 | 2008-01-25 14:57:54 +0000 | [diff] [blame] | 84 | if arg.startswith('-print-prog-name'): |
| 85 | action = 'print-prog-name' |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 86 | if arg == '-save-temps': |
| 87 | save_temps = 1 |
| 88 | |
| 89 | # Options with no arguments that should pass through |
| 90 | if arg in ['-v']: |
| 91 | compile_opts.append(arg) |
| 92 | link_opts.append(arg) |
| 93 | |
| 94 | # Options with one argument that should be ignored |
Anders Carlsson | c720d9b | 2008-01-31 23:48:19 +0000 | [diff] [blame] | 95 | if arg in ['--param', '-arch', '-u']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 96 | i += 1 |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 97 | |
| 98 | # Prefix matches for the compile mode |
| 99 | if arg[:2] in ['-D', '-I', '-U', '-F']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 100 | if not arg[2:]: |
| 101 | arg += args[i+1] |
| 102 | i += 1 |
| 103 | compile_opts.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 104 | if arg[:5] in ['-std=']: |
| 105 | compile_opts.append(arg) |
| 106 | |
| 107 | # Options with one argument that should pass through |
| 108 | if arg in ['-include']: |
| 109 | compile_opts.append(arg) |
| 110 | compile_opts.append(args[i+1]) |
| 111 | i += 1 |
| 112 | |
| 113 | # Prefix matches for the link mode |
| 114 | if arg[:2] in ['-l', '-L', '-O', '-F']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 115 | if arg == '-O': arg = '-O1' |
| 116 | if arg == '-Os': arg = '-O2' |
| 117 | link_opts.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 118 | |
| 119 | # Options with one argument that should pass through |
| 120 | if arg in ['-framework']: |
| 121 | link_opts.append(arg) |
| 122 | link_opts.append(args[i+1]) |
| 123 | i += 1 |
| 124 | |
| 125 | # Input files |
| 126 | if arg == '-filelist': |
| 127 | f = open(args[i+1]) |
| 128 | for line in f: |
| 129 | files.append(line.strip()) |
| 130 | f.close() |
| 131 | i += 1 |
| 132 | if arg == '-x': |
| 133 | language = args[i+1] |
| 134 | i += 1 |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 135 | if arg[0] != '-': |
| 136 | files.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 137 | |
| 138 | # Output file |
| 139 | if arg == '-o': |
| 140 | output = args[i+1] |
| 141 | i += 1 |
| 142 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 143 | i += 1 |
| 144 | |
Seo Sanghyeon | 96b99f7 | 2008-01-25 14:57:54 +0000 | [diff] [blame] | 145 | if action == 'print-prog-name': |
| 146 | # assume we can handle everything |
| 147 | print sys.argv[0] |
| 148 | return |
| 149 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 150 | if not files: |
| 151 | error('no input files') |
| 152 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 153 | if action == 'preprocess' or save_temps: |
| 154 | for i, file in enumerate(files): |
| 155 | if not language: |
| 156 | language = inferlanguage(extension(file)) |
| 157 | if save_temps and action != 'preprocess': |
| 158 | # Need a temporary output file |
| 159 | if language == 'c': |
| 160 | poutput = changeextension(file, "i"); |
| 161 | elif language == 'objective-c': |
| 162 | poutput = changeextension(file, "mi"); |
| 163 | else: |
| 164 | poutput = changeextension(file, "tmp." + extension(file)) |
| 165 | files[i] = poutput |
| 166 | else: |
| 167 | poutput = output |
| 168 | if poutput: |
| 169 | args = ['-x', language, '-o', poutput, file] + compile_opts |
| 170 | else: |
| 171 | args = ['-x', language, file] + compile_opts |
| 172 | preprocess(args) |
| 173 | # Discard the explicit language after used once |
| 174 | language = '' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 176 | if action == 'compile' or save_temps: |
| 177 | for i, file in enumerate(files): |
| 178 | if not language: |
| 179 | language = inferlanguage(extension(file)) |
| 180 | if save_temps and action != "compile": |
| 181 | # Need a temporary output file |
| 182 | coutput = changeextension(file, "o"); |
| 183 | files[i] = coutput |
| 184 | elif not output: |
| 185 | coutput = changeextension(file, "o") |
| 186 | else: |
| 187 | coutput = output |
| 188 | args = ['-x', language, '-o', coutput, file] + compile_opts |
| 189 | compile(args) |
| 190 | language = '' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 191 | |
| 192 | if action == 'link': |
| 193 | for i, file in enumerate(files): |
Anders Carlsson | c720d9b | 2008-01-31 23:48:19 +0000 | [diff] [blame] | 194 | ext = extension(file) |
| 195 | if ext != "o" and ext != "a": |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 196 | out = changeextension(file, "o") |
| 197 | args = ['-o', out, file] + compile_opts |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 198 | compile(args) |
| 199 | files[i] = out |
| 200 | if not output: |
| 201 | output = 'a.out' |
| 202 | args = ['-o', output] + link_opts + files |
| 203 | link(args) |
| 204 | |
| 205 | if __name__ == '__main__': |
| 206 | main(sys.argv[1:]) |