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