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 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 14 | import os |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 15 | import sys |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 16 | import subprocess |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 17 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 18 | def checkenv(name, alternate=None): |
| 19 | """checkenv(var, alternate=None) - Return the given environment var, |
| 20 | or alternate if it is undefined or empty.""" |
| 21 | v = os.getenv(name) |
| 22 | if v and v.strip(): |
| 23 | return v.strip() |
| 24 | return alternate |
| 25 | |
Daniel Dunbar | 1bf3f73 | 2008-10-01 01:10:22 +0000 | [diff] [blame] | 26 | def checkbool(name, default=False): |
| 27 | v = os.getenv(name) |
| 28 | if v: |
| 29 | try: |
| 30 | return bool(int(v)) |
| 31 | except: |
| 32 | pass |
| 33 | return default |
| 34 | |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 35 | CCC_LOG = checkenv('CCC_LOG') |
Daniel Dunbar | 1bf3f73 | 2008-10-01 01:10:22 +0000 | [diff] [blame] | 36 | CCC_ECHO = checkbool('CCC_ECHO') |
| 37 | CCC_NATIVE = checkbool('CCC_NATIVE','1') |
| 38 | CCC_FALLBACK = checkbool('CCC_FALLBACK') |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 39 | CCC_LANGUAGES = checkenv('CCC_LANGUAGES','c,c++,c-cpp-output,objective-c,objective-c++,objective-c-cpp-output,assembler-with-cpp') |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 40 | if CCC_LANGUAGES: |
| 41 | CCC_LANGUAGES = set([s.strip() for s in CCC_LANGUAGES.split(',')]) |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 42 | |
| 43 | # We want to support use as CC or LD, so we need different defines. |
| 44 | CLANG = checkenv('CLANG', 'clang') |
| 45 | LLC = checkenv('LLC', 'llc') |
| 46 | AS = checkenv('AS', 'as') |
| 47 | CC = checkenv('CCC_CC', 'cc') |
| 48 | LD = checkenv('CCC_LD', 'c++') |
| 49 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 50 | def error(message): |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 51 | print >> sys.stderr, 'ccc: ' + message |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 52 | sys.exit(1) |
| 53 | |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 54 | def quote(arg): |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 55 | if '"' in arg or ' ' in arg: |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 56 | return repr(arg) |
| 57 | return arg |
| 58 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 59 | def stripoutput(args): |
| 60 | """stripoutput(args) -> (output_name, newargs) |
| 61 | |
| 62 | Remove the -o argument from the arg list and return the output |
| 63 | filename and a new argument list. Assumes there will be at most |
| 64 | one -o option. If no output argument is found the result is (None, |
| 65 | args).""" |
| 66 | for i,a in enumerate(args): |
| 67 | if a.startswith('-o'): |
| 68 | if a=='-o': |
| 69 | if i+1<len(args): |
| 70 | return args[i+1],args[:i]+args[i+2:] |
| 71 | elif a.startswith('-o='): |
| 72 | opt,arg = a.split('=',1) |
| 73 | return arg,args[:i]+args[i+1:] |
| 74 | return None,args |
| 75 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 76 | def run(args): |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 77 | if CCC_ECHO: |
| 78 | print ' '.join(map(quote, args)) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 79 | sys.stdout.flush() |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 80 | code = subprocess.call(args) |
Bill Wendling | 550ce0f | 2008-02-03 21:27:46 +0000 | [diff] [blame] | 81 | if code > 255: |
| 82 | code = 1 |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 83 | if code: |
| 84 | sys.exit(code) |
| 85 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 86 | def remove(path): |
| 87 | """remove(path) -> bool - Attempt to remove the file at path (if any). |
| 88 | |
| 89 | The result indicates if the remove was successful. A warning is |
| 90 | printed if there is an error removing the file.""" |
| 91 | if os.path.exists(path): |
| 92 | try: |
| 93 | os.remove(path) |
| 94 | except: |
| 95 | print >>sys.stderr, 'WARNING: Unable to remove temp "%s"'%(path,) |
| 96 | return False |
| 97 | return True |
| 98 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 99 | def preprocess(args): |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 100 | command = [CLANG,'-E'] |
Anders Carlsson | dac2b54 | 2008-02-06 19:03:27 +0000 | [diff] [blame] | 101 | run(command + args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 103 | def syntaxonly(args): |
| 104 | command = [CLANG,'-fsyntax-only'] |
| 105 | run(command + args) |
| 106 | |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 107 | def compile_fallback(args): |
| 108 | command = [CC,'-c'] |
| 109 | run(command + args) |
| 110 | |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 111 | def compile(args, native, save_temps=False, asm_opts=[]): |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 112 | if native: |
| 113 | output,args = stripoutput(args) |
| 114 | if not output: |
| 115 | raise ValueError,'Expected to always have explicit -o in compile()' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 117 | # I prefer suffixing these to changing the extension, which is |
| 118 | # more likely to overwrite other things. We could of course |
| 119 | # use temp files. |
| 120 | bc_output = output + '.bc' |
| 121 | s_output = output + '.s' |
| 122 | command = [CLANG,'-emit-llvm-bc'] |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 123 | try: |
| 124 | run(command + args + ['-o', bc_output]) |
| 125 | # FIXME: What controls relocation model? |
| 126 | run([LLC, '-relocation-model=pic', '-f', '-o', s_output, bc_output]) |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 127 | run([AS, '-o', output, s_output] + asm_opts) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 128 | finally: |
| 129 | if not save_temps: |
| 130 | remove(bc_output) |
| 131 | remove(s_output) |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 132 | else: |
| 133 | command = [CLANG,'-emit-llvm-bc'] |
| 134 | run(command + args) |
| 135 | |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 136 | def checked_compile(args, native, language, save_temps, asm_opts): |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 137 | if CCC_LANGUAGES and language and language not in CCC_LANGUAGES: |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 138 | log('fallback', args) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 139 | print >>sys.stderr, 'NOTE: ccc: Using fallback compiler for: %s'%(' '.join(map(quote, args)),) |
| 140 | compile_fallback(args) |
| 141 | elif CCC_FALLBACK: |
| 142 | try: |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 143 | compile(args, native, save_temps, asm_opts) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 144 | except: |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 145 | log('fallback-on-fail', args) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 146 | print >>sys.stderr, 'WARNING: ccc: Using fallback compiler for: %s'%(' '.join(map(quote, args)),) |
| 147 | compile_fallback(args) |
| 148 | else: |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 149 | compile(args, native, save_temps, asm_opts) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 151 | def link(args, native): |
| 152 | if native: |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 153 | run([LD] + args) |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 154 | else: |
| 155 | command = ['llvm-ld', '-native', '-disable-internalize'] |
| 156 | run(command + args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 157 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 158 | def extension(path): |
Seo Sanghyeon | 795aaed | 2008-02-03 03:40:41 +0000 | [diff] [blame] | 159 | return path.split(".")[-1] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 160 | |
| 161 | def changeextension(path, newext): |
Seo Sanghyeon | 795aaed | 2008-02-03 03:40:41 +0000 | [diff] [blame] | 162 | i = path.rfind('.') |
| 163 | if i < 0: |
| 164 | return path |
Bill Wendling | 550ce0f | 2008-02-03 21:27:46 +0000 | [diff] [blame] | 165 | j = path.rfind('/', 0, i) |
Bill Wendling | 550ce0f | 2008-02-03 21:27:46 +0000 | [diff] [blame] | 166 | if j < 0: |
| 167 | return path[:i] + "." + newext |
| 168 | return path[j+1:i] + "." + newext |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 169 | |
| 170 | def inferlanguage(extension): |
| 171 | if extension == "c": |
| 172 | return "c" |
Lauro Ramos Venancio | 279876b | 2008-02-15 22:35:25 +0000 | [diff] [blame] | 173 | elif extension in ["cpp", "cc"]: |
| 174 | return "c++" |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 175 | elif extension == "i": |
| 176 | return "c-cpp-output" |
| 177 | elif extension == "m": |
| 178 | return "objective-c" |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 179 | elif extension == "mm": |
| 180 | return "objective-c++" |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 181 | elif extension == "mi": |
| 182 | return "objective-c-cpp-output" |
Nuno Lopes | 24653e8 | 2008-09-02 10:27:37 +0000 | [diff] [blame] | 183 | elif extension == "s": |
| 184 | return "assembler" |
| 185 | elif extension == "S": |
| 186 | return "assembler-with-cpp" |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 187 | else: |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 188 | return "" |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 190 | def log(name, item): |
| 191 | if CCC_LOG: |
| 192 | f = open(CCC_LOG,'a') |
| 193 | print >>f, (name, item) |
| 194 | f.close() |
| 195 | |
Daniel Dunbar | 869f8b6 | 2008-09-30 21:20:51 +0000 | [diff] [blame] | 196 | def inferaction(args): |
| 197 | if '-E' in args: |
| 198 | return 'preprocess' |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 199 | if '-fsyntax-only' in args: |
| 200 | return 'syntax-only' |
Daniel Dunbar | 869f8b6 | 2008-09-30 21:20:51 +0000 | [diff] [blame] | 201 | if '-c' in args: |
| 202 | return 'compile' |
| 203 | for arg in args: |
| 204 | if arg.startswith('-print-prog-name'): |
| 205 | return 'pring-prog-name' |
| 206 | return 'link' |
| 207 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 208 | def main(args): |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 209 | log('invoke', args) |
| 210 | |
Daniel Dunbar | 869f8b6 | 2008-09-30 21:20:51 +0000 | [diff] [blame] | 211 | action = inferaction(args) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 212 | output = '' |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 213 | asm_opts = [] |
Seo Sanghyeon | 96b99f7 | 2008-01-25 14:57:54 +0000 | [diff] [blame] | 214 | compile_opts = [] |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 215 | link_opts = [] |
| 216 | files = [] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 217 | save_temps = 0 |
| 218 | language = '' |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 219 | native = CCC_NATIVE |
| 220 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 221 | i = 0 |
| 222 | while i < len(args): |
| 223 | arg = args[i] |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | 319e792 | 2008-09-30 22:54:22 +0000 | [diff] [blame] | 225 | if '=' in arg: |
| 226 | argkey,argvalue = arg.split('=',1) |
| 227 | else: |
| 228 | argkey,argvalue = arg,None |
| 229 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 230 | # Modes ccc supports |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 231 | if arg == '-save-temps': |
| 232 | save_temps = 1 |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 233 | if arg == '-emit-llvm' or arg == '--emit-llvm': |
| 234 | native = False |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 235 | |
| 236 | # Options with no arguments that should pass through |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 237 | if arg in ['-v', '-fobjc-gc', '-fobjc-gc-only', '-fnext-runtime', |
| 238 | '-fgnu-runtime']: |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 239 | compile_opts.append(arg) |
| 240 | link_opts.append(arg) |
| 241 | |
| 242 | # Options with one argument that should be ignored |
Ted Kremenek | d0eef02 | 2008-04-21 20:28:01 +0000 | [diff] [blame] | 243 | if arg in ['--param', '-u']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 244 | i += 1 |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | 1683360 | 2008-07-24 03:49:15 +0000 | [diff] [blame] | 246 | # Preprocessor options with one argument that should be ignored |
| 247 | if arg in ['-MT', '-MF']: |
| 248 | i += 1 |
| 249 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 250 | # Prefix matches for the compile mode |
| 251 | if arg[:2] in ['-D', '-I', '-U', '-F']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 252 | if not arg[2:]: |
| 253 | arg += args[i+1] |
| 254 | i += 1 |
| 255 | compile_opts.append(arg) |
Daniel Dunbar | 8e45f73 | 2008-10-02 17:26:37 +0000 | [diff] [blame] | 256 | if argkey in ('-std', '-mmacosx-version-min'): |
| 257 | compile_opts.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | fa538e7 | 2008-10-19 02:41:16 +0000 | [diff] [blame] | 259 | # Special case debug options to only pass -g to clang. This is |
| 260 | # wrong. |
| 261 | if arg in ('-g', '-gdwarf-2'): |
| 262 | compile_opts.append('-g') |
Daniel Dunbar | fa538e7 | 2008-10-19 02:41:16 +0000 | [diff] [blame] | 263 | |
Nuno Lopes | e5d12e8 | 2008-06-17 17:23:14 +0000 | [diff] [blame] | 264 | # Options with one argument that should pass through to compiler |
Daniel Dunbar | 8e45f73 | 2008-10-02 17:26:37 +0000 | [diff] [blame] | 265 | if arg in [ '-include', '-idirafter', '-iprefix', |
Daniel Dunbar | 319e792 | 2008-09-30 22:54:22 +0000 | [diff] [blame] | 266 | '-iquote', '-isystem', '-iwithprefix', |
Daniel Dunbar | 8e45f73 | 2008-10-02 17:26:37 +0000 | [diff] [blame] | 267 | '-iwithprefixbefore']: |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 268 | compile_opts.append(arg) |
| 269 | compile_opts.append(args[i+1]) |
| 270 | i += 1 |
| 271 | |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 272 | # Options with no arguments that should pass through |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 273 | if (arg in ('-dynamiclib', '-bundle', '-headerpad_max_install_names', |
| 274 | '-nostdlib', '-static', '-dynamic', '-r') or |
Daniel Dunbar | 1bf3f73 | 2008-10-01 01:10:22 +0000 | [diff] [blame] | 275 | arg.startswith('-Wl,')): |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 276 | link_opts.append(arg) |
| 277 | |
Nuno Lopes | e5d12e8 | 2008-06-17 17:23:14 +0000 | [diff] [blame] | 278 | # Options with one argument that should pass through |
Daniel Dunbar | 75e0571 | 2008-09-12 19:42:28 +0000 | [diff] [blame] | 279 | if arg in ('-framework', '-multiply_defined', '-bundle_loader', |
Daniel Dunbar | fa538e7 | 2008-10-19 02:41:16 +0000 | [diff] [blame] | 280 | '-weak_framework', |
Daniel Dunbar | 319e792 | 2008-09-30 22:54:22 +0000 | [diff] [blame] | 281 | '-e', '-install_name', |
| 282 | '-unexported_symbols_list', '-exported_symbols_list', |
| 283 | '-compatibility_version', '-current_version', '-init', |
Anders Carlsson | e90b0ac | 2009-01-05 01:24:39 +0000 | [diff] [blame] | 284 | '-seg1addr', '-dylib_file', '-Xlinker', '-undefined'): |
Nuno Lopes | e5d12e8 | 2008-06-17 17:23:14 +0000 | [diff] [blame] | 285 | link_opts.append(arg) |
| 286 | link_opts.append(args[i+1]) |
| 287 | i += 1 |
| 288 | |
| 289 | # Options with one argument that should pass through to both |
| 290 | if arg in ['-isysroot', '-arch']: |
| 291 | compile_opts.append(arg) |
| 292 | compile_opts.append(args[i+1]) |
| 293 | link_opts.append(arg) |
| 294 | link_opts.append(args[i+1]) |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 295 | asm_opts.append(arg) |
| 296 | asm_opts.append(args[i+1]) |
Nuno Lopes | e5d12e8 | 2008-06-17 17:23:14 +0000 | [diff] [blame] | 297 | i += 1 |
Daniel Dunbar | 75e0571 | 2008-09-12 19:42:28 +0000 | [diff] [blame] | 298 | |
| 299 | # Options with three arguments that should pass through |
| 300 | if arg in ('-sectorder',): |
| 301 | link_opts.extend(args[i:i+4]) |
| 302 | i += 3 |
Nuno Lopes | e5d12e8 | 2008-06-17 17:23:14 +0000 | [diff] [blame] | 303 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 304 | # Prefix matches for the link mode |
Nuno Lopes | 9a18448 | 2008-08-10 21:58:01 +0000 | [diff] [blame] | 305 | if arg[:2] in ['-l', '-L', '-F', '-R']: |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 306 | link_opts.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 307 | |
Chris Lattner | cf719b7 | 2008-06-21 17:46:11 +0000 | [diff] [blame] | 308 | # Enable threads |
| 309 | if arg == '-pthread': |
| 310 | link_opts.append('-lpthread') |
| 311 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 312 | # Input files |
| 313 | if arg == '-filelist': |
| 314 | f = open(args[i+1]) |
| 315 | for line in f: |
| 316 | files.append(line.strip()) |
| 317 | f.close() |
| 318 | i += 1 |
| 319 | if arg == '-x': |
| 320 | language = args[i+1] |
Fariborz Jahanian | 9142bc9 | 2009-02-03 23:59:32 +0000 | [diff] [blame] | 321 | compile_opts.append(arg) |
| 322 | compile_opts.append(args[i+1]) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 323 | i += 1 |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 324 | if arg[0] != '-': |
| 325 | files.append(arg) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 326 | |
| 327 | # Output file |
| 328 | if arg == '-o': |
| 329 | output = args[i+1] |
| 330 | i += 1 |
| 331 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 332 | i += 1 |
Daniel Dunbar | 869f8b6 | 2008-09-30 21:20:51 +0000 | [diff] [blame] | 333 | |
Seo Sanghyeon | 96b99f7 | 2008-01-25 14:57:54 +0000 | [diff] [blame] | 334 | if action == 'print-prog-name': |
| 335 | # assume we can handle everything |
| 336 | print sys.argv[0] |
| 337 | return |
| 338 | |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 339 | if not files: |
| 340 | error('no input files') |
| 341 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 342 | if action == 'preprocess' or save_temps: |
| 343 | for i, file in enumerate(files): |
| 344 | if not language: |
| 345 | language = inferlanguage(extension(file)) |
| 346 | if save_temps and action != 'preprocess': |
| 347 | # Need a temporary output file |
| 348 | if language == 'c': |
| 349 | poutput = changeextension(file, "i"); |
| 350 | elif language == 'objective-c': |
| 351 | poutput = changeextension(file, "mi"); |
| 352 | else: |
| 353 | poutput = changeextension(file, "tmp." + extension(file)) |
| 354 | files[i] = poutput |
| 355 | else: |
| 356 | poutput = output |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 357 | args = [] |
| 358 | if language: |
| 359 | args.extend(['-x', language]) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 360 | if poutput: |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 361 | args += ['-o', poutput, file] + compile_opts |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 362 | else: |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 363 | args += [file] + compile_opts |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 364 | preprocess(args) |
| 365 | # Discard the explicit language after used once |
| 366 | language = '' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 367 | |
Daniel Dunbar | cb52925 | 2008-10-15 21:52:00 +0000 | [diff] [blame] | 368 | if action == 'syntax-only': |
| 369 | for i, file in enumerate(files): |
| 370 | if not language: |
| 371 | language = inferlanguage(extension(file)) |
| 372 | args = [] |
| 373 | if language: |
| 374 | args.extend(['-x', language]) |
| 375 | args += [file] + compile_opts |
| 376 | syntaxonly(args) |
| 377 | language = '' |
| 378 | |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 379 | if action == 'compile' or save_temps: |
| 380 | for i, file in enumerate(files): |
| 381 | if not language: |
| 382 | language = inferlanguage(extension(file)) |
| 383 | if save_temps and action != "compile": |
| 384 | # Need a temporary output file |
| 385 | coutput = changeextension(file, "o"); |
| 386 | files[i] = coutput |
| 387 | elif not output: |
| 388 | coutput = changeextension(file, "o") |
| 389 | else: |
| 390 | coutput = output |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 391 | args = [] |
| 392 | if language: |
| 393 | args.extend(['-x', language]) |
| 394 | args += ['-o', coutput, file] + compile_opts |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 395 | checked_compile(args, native, language, save_temps, asm_opts) |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 396 | language = '' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 397 | |
| 398 | if action == 'link': |
| 399 | for i, file in enumerate(files): |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 400 | if not language: |
| 401 | language = inferlanguage(extension(file)) |
Anders Carlsson | c720d9b | 2008-01-31 23:48:19 +0000 | [diff] [blame] | 402 | ext = extension(file) |
Nuno Lopes | c46cf49 | 2008-08-10 22:17:57 +0000 | [diff] [blame] | 403 | if ext != "o" and ext != "a" and ext != "so": |
Anders Carlsson | d125bb1 | 2008-01-29 07:21:34 +0000 | [diff] [blame] | 404 | out = changeextension(file, "o") |
Daniel Dunbar | 7000bf8 | 2008-09-30 16:18:31 +0000 | [diff] [blame] | 405 | args = [] |
| 406 | if language: |
| 407 | args.extend(['-x', language]) |
| 408 | args = ['-o', out, file] + compile_opts |
Daniel Dunbar | 91dce08 | 2009-01-07 00:03:20 +0000 | [diff] [blame] | 409 | checked_compile(args, native, language, save_temps, asm_opts) |
Daniel Dunbar | d3d8141 | 2008-08-29 21:03:27 +0000 | [diff] [blame] | 410 | language = '' |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 411 | files[i] = out |
| 412 | if not output: |
| 413 | output = 'a.out' |
Daniel Dunbar | 1b6ff6f | 2008-11-18 17:38:30 +0000 | [diff] [blame] | 414 | args = ['-o', output] + files + link_opts |
Daniel Dunbar | 4b5c4f2 | 2008-08-23 22:15:15 +0000 | [diff] [blame] | 415 | link(args, native) |
Seo Sanghyeon | 2bfa533 | 2008-01-10 01:43:47 +0000 | [diff] [blame] | 416 | |
| 417 | if __name__ == '__main__': |
| 418 | main(sys.argv[1:]) |