blob: 9a858291d5e6f875394fa907ae01ba9d241fa007 [file] [log] [blame]
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +00001#!/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 Dunbar4b5c4f22008-08-23 22:15:15 +000014import os
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000015import sys
Anders Carlssondac2b542008-02-06 19:03:27 +000016import subprocess
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000017
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000018def 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
26CCC_ECHO = checkenv('CCC_ECHO','1')
27CCC_NATIVE = checkenv('CCC_NATIVE')
Daniel Dunbard3d81412008-08-29 21:03:27 +000028CCC_FALLBACK = checkenv('CCC_FALLBACK')
Nuno Lopes24653e82008-09-02 10:27:37 +000029CCC_LANGUAGES = checkenv('CCC_LANGUAGES','c,c++,c-cpp-output,objective-c,objective-c++,objective-c-cpp-output')
Daniel Dunbard3d81412008-08-29 21:03:27 +000030if CCC_LANGUAGES:
31 CCC_LANGUAGES = set([s.strip() for s in CCC_LANGUAGES.split(',')])
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000032
33# We want to support use as CC or LD, so we need different defines.
34CLANG = checkenv('CLANG', 'clang')
35LLC = checkenv('LLC', 'llc')
36AS = checkenv('AS', 'as')
37CC = checkenv('CCC_CC', 'cc')
38LD = checkenv('CCC_LD', 'c++')
39
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000040def error(message):
Anders Carlssond125bb12008-01-29 07:21:34 +000041 print >> sys.stderr, 'ccc: ' + message
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000042 sys.exit(1)
43
Seo Sanghyeond3894652008-04-04 11:02:21 +000044def quote(arg):
Daniel Dunbard3d81412008-08-29 21:03:27 +000045 if '"' in arg or ' ' in arg:
Seo Sanghyeond3894652008-04-04 11:02:21 +000046 return repr(arg)
47 return arg
48
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000049def stripoutput(args):
50 """stripoutput(args) -> (output_name, newargs)
51
52 Remove the -o argument from the arg list and return the output
53 filename and a new argument list. Assumes there will be at most
54 one -o option. If no output argument is found the result is (None,
55 args)."""
56 for i,a in enumerate(args):
57 if a.startswith('-o'):
58 if a=='-o':
59 if i+1<len(args):
60 return args[i+1],args[:i]+args[i+2:]
61 elif a.startswith('-o='):
62 opt,arg = a.split('=',1)
63 return arg,args[:i]+args[i+1:]
64 return None,args
65
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000066def run(args):
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000067 if CCC_ECHO:
68 print ' '.join(map(quote, args))
Daniel Dunbard3d81412008-08-29 21:03:27 +000069 sys.stdout.flush()
Anders Carlssondac2b542008-02-06 19:03:27 +000070 code = subprocess.call(args)
Bill Wendling550ce0f2008-02-03 21:27:46 +000071 if code > 255:
72 code = 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000073 if code:
74 sys.exit(code)
75
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000076def remove(path):
77 """remove(path) -> bool - Attempt to remove the file at path (if any).
78
79 The result indicates if the remove was successful. A warning is
80 printed if there is an error removing the file."""
81 if os.path.exists(path):
82 try:
83 os.remove(path)
84 except:
85 print >>sys.stderr, 'WARNING: Unable to remove temp "%s"'%(path,)
86 return False
87 return True
88
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000089def preprocess(args):
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000090 command = [CLANG,'-E']
Anders Carlssondac2b542008-02-06 19:03:27 +000091 run(command + args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000092
Daniel Dunbard3d81412008-08-29 21:03:27 +000093def compile_fallback(args):
94 command = [CC,'-c']
95 run(command + args)
96
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +000097def compile(args, native, save_temps=False):
98 if native:
99 output,args = stripoutput(args)
100 if not output:
101 raise ValueError,'Expected to always have explicit -o in compile()'
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000102
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000103 # I prefer suffixing these to changing the extension, which is
104 # more likely to overwrite other things. We could of course
105 # use temp files.
106 bc_output = output + '.bc'
107 s_output = output + '.s'
108 command = [CLANG,'-emit-llvm-bc']
Daniel Dunbard3d81412008-08-29 21:03:27 +0000109 try:
110 run(command + args + ['-o', bc_output])
111 # FIXME: What controls relocation model?
112 run([LLC, '-relocation-model=pic', '-f', '-o', s_output, bc_output])
113 run([AS, '-o', output, s_output])
114 finally:
115 if not save_temps:
116 remove(bc_output)
117 remove(s_output)
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000118 else:
119 command = [CLANG,'-emit-llvm-bc']
120 run(command + args)
121
Daniel Dunbard3d81412008-08-29 21:03:27 +0000122def checked_compile(args, native, language, save_temps):
123 if CCC_LANGUAGES and language and language not in CCC_LANGUAGES:
124 print >>sys.stderr, 'NOTE: ccc: Using fallback compiler for: %s'%(' '.join(map(quote, args)),)
125 compile_fallback(args)
126 elif CCC_FALLBACK:
127 try:
128 compile(args, native, save_temps)
129 except:
130 print >>sys.stderr, 'WARNING: ccc: Using fallback compiler for: %s'%(' '.join(map(quote, args)),)
131 compile_fallback(args)
132 else:
133 compile(args, native, save_temps)
134
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000135def link(args, native):
136 if native:
Daniel Dunbard3d81412008-08-29 21:03:27 +0000137 run([LD] + args)
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000138 else:
139 command = ['llvm-ld', '-native', '-disable-internalize']
140 run(command + args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000141
Anders Carlssond125bb12008-01-29 07:21:34 +0000142def extension(path):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +0000143 return path.split(".")[-1]
Anders Carlssond125bb12008-01-29 07:21:34 +0000144
145def changeextension(path, newext):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +0000146 i = path.rfind('.')
147 if i < 0:
148 return path
Bill Wendling550ce0f2008-02-03 21:27:46 +0000149 j = path.rfind('/', 0, i)
Bill Wendling550ce0f2008-02-03 21:27:46 +0000150 if j < 0:
151 return path[:i] + "." + newext
152 return path[j+1:i] + "." + newext
Anders Carlssond125bb12008-01-29 07:21:34 +0000153
154def inferlanguage(extension):
155 if extension == "c":
156 return "c"
Lauro Ramos Venancio279876b2008-02-15 22:35:25 +0000157 elif extension in ["cpp", "cc"]:
158 return "c++"
Anders Carlssond125bb12008-01-29 07:21:34 +0000159 elif extension == "i":
160 return "c-cpp-output"
161 elif extension == "m":
162 return "objective-c"
Daniel Dunbard3d81412008-08-29 21:03:27 +0000163 elif extension == "mm":
164 return "objective-c++"
Anders Carlssond125bb12008-01-29 07:21:34 +0000165 elif extension == "mi":
166 return "objective-c-cpp-output"
Nuno Lopes24653e82008-09-02 10:27:37 +0000167 elif extension == "s":
168 return "assembler"
169 elif extension == "S":
170 return "assembler-with-cpp"
Anders Carlssond125bb12008-01-29 07:21:34 +0000171 else:
172 return "unknown"
173
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000174def main(args):
175 action = 'link'
176 output = ''
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000177 compile_opts = []
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000178 link_opts = []
179 files = []
Anders Carlssond125bb12008-01-29 07:21:34 +0000180 save_temps = 0
181 language = ''
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000182 native = CCC_NATIVE
183
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000184 i = 0
185 while i < len(args):
186 arg = args[i]
Anders Carlssond125bb12008-01-29 07:21:34 +0000187
188 # Modes ccc supports
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000189 if arg == '-E':
190 action = 'preprocess'
191 if arg == '-c':
192 action = 'compile'
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000193 if arg.startswith('-print-prog-name'):
194 action = 'print-prog-name'
Anders Carlssond125bb12008-01-29 07:21:34 +0000195 if arg == '-save-temps':
196 save_temps = 1
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000197 if arg == '-emit-llvm' or arg == '--emit-llvm':
198 native = False
Anders Carlssond125bb12008-01-29 07:21:34 +0000199
200 # Options with no arguments that should pass through
Daniel Dunbard3d81412008-08-29 21:03:27 +0000201 if arg in ['-v', '-fobjc-gc', '-fobjc-gc-only', '-fnext-runtime',
202 '-fgnu-runtime']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000203 compile_opts.append(arg)
204 link_opts.append(arg)
205
206 # Options with one argument that should be ignored
Ted Kremenekd0eef022008-04-21 20:28:01 +0000207 if arg in ['--param', '-u']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000208 i += 1
Anders Carlssond125bb12008-01-29 07:21:34 +0000209
Ted Kremenek16833602008-07-24 03:49:15 +0000210 # Preprocessor options with one argument that should be ignored
211 if arg in ['-MT', '-MF']:
212 i += 1
213
Anders Carlssond125bb12008-01-29 07:21:34 +0000214 # Prefix matches for the compile mode
215 if arg[:2] in ['-D', '-I', '-U', '-F']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000216 if not arg[2:]:
217 arg += args[i+1]
218 i += 1
219 compile_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000220 if arg[:5] in ['-std=']:
221 compile_opts.append(arg)
222
Nuno Lopese5d12e82008-06-17 17:23:14 +0000223 # Options with one argument that should pass through to compiler
224 if arg in [ '-include', '-idirafter', '-iprefix',
225 '-iquote', '-isystem', '-iwithprefix',
226 '-iwithprefixbefore']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000227 compile_opts.append(arg)
228 compile_opts.append(args[i+1])
229 i += 1
230
Nuno Lopese5d12e82008-06-17 17:23:14 +0000231 # Options with one argument that should pass through
Daniel Dunbard3d81412008-08-29 21:03:27 +0000232 if arg in ['-framework', '-multiply_defined', '-bundle_loader']:
Nuno Lopese5d12e82008-06-17 17:23:14 +0000233 link_opts.append(arg)
234 link_opts.append(args[i+1])
235 i += 1
236
237 # Options with one argument that should pass through to both
238 if arg in ['-isysroot', '-arch']:
239 compile_opts.append(arg)
240 compile_opts.append(args[i+1])
241 link_opts.append(arg)
242 link_opts.append(args[i+1])
243 i += 1
244
Anders Carlssond125bb12008-01-29 07:21:34 +0000245 # Prefix matches for the link mode
Nuno Lopes9a184482008-08-10 21:58:01 +0000246 if arg[:2] in ['-l', '-L', '-F', '-R']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000247 link_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000248
Chris Lattnercf719b72008-06-21 17:46:11 +0000249 # Enable threads
250 if arg == '-pthread':
251 link_opts.append('-lpthread')
252
Anders Carlssond125bb12008-01-29 07:21:34 +0000253 # Input files
254 if arg == '-filelist':
255 f = open(args[i+1])
256 for line in f:
257 files.append(line.strip())
258 f.close()
259 i += 1
260 if arg == '-x':
261 language = args[i+1]
262 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000263 if arg[0] != '-':
264 files.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000265
266 # Output file
267 if arg == '-o':
268 output = args[i+1]
269 i += 1
270
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000271 i += 1
272
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000273 if action == 'print-prog-name':
274 # assume we can handle everything
275 print sys.argv[0]
276 return
277
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000278 if not files:
279 error('no input files')
280
Anders Carlssond125bb12008-01-29 07:21:34 +0000281 if action == 'preprocess' or save_temps:
282 for i, file in enumerate(files):
283 if not language:
284 language = inferlanguage(extension(file))
285 if save_temps and action != 'preprocess':
286 # Need a temporary output file
287 if language == 'c':
288 poutput = changeextension(file, "i");
289 elif language == 'objective-c':
290 poutput = changeextension(file, "mi");
291 else:
292 poutput = changeextension(file, "tmp." + extension(file))
293 files[i] = poutput
294 else:
295 poutput = output
296 if poutput:
297 args = ['-x', language, '-o', poutput, file] + compile_opts
298 else:
299 args = ['-x', language, file] + compile_opts
300 preprocess(args)
301 # Discard the explicit language after used once
302 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000303
Anders Carlssond125bb12008-01-29 07:21:34 +0000304 if action == 'compile' or save_temps:
305 for i, file in enumerate(files):
306 if not language:
307 language = inferlanguage(extension(file))
308 if save_temps and action != "compile":
309 # Need a temporary output file
310 coutput = changeextension(file, "o");
311 files[i] = coutput
312 elif not output:
313 coutput = changeextension(file, "o")
314 else:
315 coutput = output
316 args = ['-x', language, '-o', coutput, file] + compile_opts
Daniel Dunbard3d81412008-08-29 21:03:27 +0000317 checked_compile(args, native, language, save_temps)
Anders Carlssond125bb12008-01-29 07:21:34 +0000318 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000319
320 if action == 'link':
321 for i, file in enumerate(files):
Daniel Dunbard3d81412008-08-29 21:03:27 +0000322 if not language:
323 language = inferlanguage(extension(file))
Anders Carlssonc720d9b2008-01-31 23:48:19 +0000324 ext = extension(file)
Nuno Lopesc46cf492008-08-10 22:17:57 +0000325 if ext != "o" and ext != "a" and ext != "so":
Anders Carlssond125bb12008-01-29 07:21:34 +0000326 out = changeextension(file, "o")
Daniel Dunbard3d81412008-08-29 21:03:27 +0000327 args = ['-x', language, '-o', out, file] + compile_opts
328 checked_compile(args, native, language, save_temps)
329 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000330 files[i] = out
331 if not output:
332 output = 'a.out'
333 args = ['-o', output] + link_opts + files
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000334 link(args, native)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000335
336if __name__ == '__main__':
337 main(sys.argv[1:])