blob: d4e9799b392611220c1256e6a1fd8edfc1180e73 [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:
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000172 return ""
Anders Carlssond125bb12008-01-29 07:21:34 +0000173
Daniel Dunbar869f8b62008-09-30 21:20:51 +0000174def inferaction(args):
175 if '-E' in args:
176 return 'preprocess'
177 if '-c' in args:
178 return 'compile'
179 for arg in args:
180 if arg.startswith('-print-prog-name'):
181 return 'pring-prog-name'
182 return 'link'
183
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000184def main(args):
Daniel Dunbar869f8b62008-09-30 21:20:51 +0000185 action = inferaction(args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000186 output = ''
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000187 compile_opts = []
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000188 link_opts = []
189 files = []
Anders Carlssond125bb12008-01-29 07:21:34 +0000190 save_temps = 0
191 language = ''
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000192 native = CCC_NATIVE
193
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000194 i = 0
195 while i < len(args):
196 arg = args[i]
Anders Carlssond125bb12008-01-29 07:21:34 +0000197
198 # Modes ccc supports
Anders Carlssond125bb12008-01-29 07:21:34 +0000199 if arg == '-save-temps':
200 save_temps = 1
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000201 if arg == '-emit-llvm' or arg == '--emit-llvm':
202 native = False
Anders Carlssond125bb12008-01-29 07:21:34 +0000203
204 # Options with no arguments that should pass through
Daniel Dunbard3d81412008-08-29 21:03:27 +0000205 if arg in ['-v', '-fobjc-gc', '-fobjc-gc-only', '-fnext-runtime',
206 '-fgnu-runtime']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000207 compile_opts.append(arg)
208 link_opts.append(arg)
209
210 # Options with one argument that should be ignored
Ted Kremenekd0eef022008-04-21 20:28:01 +0000211 if arg in ['--param', '-u']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000212 i += 1
Anders Carlssond125bb12008-01-29 07:21:34 +0000213
Ted Kremenek16833602008-07-24 03:49:15 +0000214 # Preprocessor options with one argument that should be ignored
215 if arg in ['-MT', '-MF']:
216 i += 1
217
Anders Carlssond125bb12008-01-29 07:21:34 +0000218 # Prefix matches for the compile mode
219 if arg[:2] in ['-D', '-I', '-U', '-F']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000220 if not arg[2:]:
221 arg += args[i+1]
222 i += 1
223 compile_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000224 if arg[:5] in ['-std=']:
225 compile_opts.append(arg)
226
Nuno Lopese5d12e82008-06-17 17:23:14 +0000227 # Options with one argument that should pass through to compiler
228 if arg in [ '-include', '-idirafter', '-iprefix',
229 '-iquote', '-isystem', '-iwithprefix',
230 '-iwithprefixbefore']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000231 compile_opts.append(arg)
232 compile_opts.append(args[i+1])
233 i += 1
234
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000235 # Options with no arguments that should pass through
Daniel Dunbar869f8b62008-09-30 21:20:51 +0000236 if arg in ('-dynamiclib','-bundle'):
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000237 link_opts.append(arg)
238
Nuno Lopese5d12e82008-06-17 17:23:14 +0000239 # Options with one argument that should pass through
Daniel Dunbar75e05712008-09-12 19:42:28 +0000240 if arg in ('-framework', '-multiply_defined', '-bundle_loader',
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000241 '-e', '-unexported_symbols_list', '-install_name',
242 '-compatibility_version', '-current_version'):
Nuno Lopese5d12e82008-06-17 17:23:14 +0000243 link_opts.append(arg)
244 link_opts.append(args[i+1])
245 i += 1
246
247 # Options with one argument that should pass through to both
248 if arg in ['-isysroot', '-arch']:
249 compile_opts.append(arg)
250 compile_opts.append(args[i+1])
251 link_opts.append(arg)
252 link_opts.append(args[i+1])
253 i += 1
Daniel Dunbar75e05712008-09-12 19:42:28 +0000254
255 # Options with three arguments that should pass through
256 if arg in ('-sectorder',):
257 link_opts.extend(args[i:i+4])
258 i += 3
Nuno Lopese5d12e82008-06-17 17:23:14 +0000259
Anders Carlssond125bb12008-01-29 07:21:34 +0000260 # Prefix matches for the link mode
Nuno Lopes9a184482008-08-10 21:58:01 +0000261 if arg[:2] in ['-l', '-L', '-F', '-R']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000262 link_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000263
Chris Lattnercf719b72008-06-21 17:46:11 +0000264 # Enable threads
265 if arg == '-pthread':
266 link_opts.append('-lpthread')
267
Anders Carlssond125bb12008-01-29 07:21:34 +0000268 # Input files
269 if arg == '-filelist':
270 f = open(args[i+1])
271 for line in f:
272 files.append(line.strip())
273 f.close()
274 i += 1
275 if arg == '-x':
276 language = args[i+1]
277 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000278 if arg[0] != '-':
279 files.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000280
281 # Output file
282 if arg == '-o':
283 output = args[i+1]
284 i += 1
285
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000286 i += 1
Daniel Dunbar869f8b62008-09-30 21:20:51 +0000287
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000288 if action == 'print-prog-name':
289 # assume we can handle everything
290 print sys.argv[0]
291 return
292
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000293 if not files:
294 error('no input files')
295
Anders Carlssond125bb12008-01-29 07:21:34 +0000296 if action == 'preprocess' or save_temps:
297 for i, file in enumerate(files):
298 if not language:
299 language = inferlanguage(extension(file))
300 if save_temps and action != 'preprocess':
301 # Need a temporary output file
302 if language == 'c':
303 poutput = changeextension(file, "i");
304 elif language == 'objective-c':
305 poutput = changeextension(file, "mi");
306 else:
307 poutput = changeextension(file, "tmp." + extension(file))
308 files[i] = poutput
309 else:
310 poutput = output
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000311 args = []
312 if language:
313 args.extend(['-x', language])
Anders Carlssond125bb12008-01-29 07:21:34 +0000314 if poutput:
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000315 args += ['-o', poutput, file] + compile_opts
Anders Carlssond125bb12008-01-29 07:21:34 +0000316 else:
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000317 args += [file] + compile_opts
Anders Carlssond125bb12008-01-29 07:21:34 +0000318 preprocess(args)
319 # Discard the explicit language after used once
320 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000321
Anders Carlssond125bb12008-01-29 07:21:34 +0000322 if action == 'compile' or save_temps:
323 for i, file in enumerate(files):
324 if not language:
325 language = inferlanguage(extension(file))
326 if save_temps and action != "compile":
327 # Need a temporary output file
328 coutput = changeextension(file, "o");
329 files[i] = coutput
330 elif not output:
331 coutput = changeextension(file, "o")
332 else:
333 coutput = output
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000334 args = []
335 if language:
336 args.extend(['-x', language])
337 args += ['-o', coutput, file] + compile_opts
Daniel Dunbard3d81412008-08-29 21:03:27 +0000338 checked_compile(args, native, language, save_temps)
Anders Carlssond125bb12008-01-29 07:21:34 +0000339 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000340
341 if action == 'link':
342 for i, file in enumerate(files):
Daniel Dunbard3d81412008-08-29 21:03:27 +0000343 if not language:
344 language = inferlanguage(extension(file))
Anders Carlssonc720d9b2008-01-31 23:48:19 +0000345 ext = extension(file)
Nuno Lopesc46cf492008-08-10 22:17:57 +0000346 if ext != "o" and ext != "a" and ext != "so":
Anders Carlssond125bb12008-01-29 07:21:34 +0000347 out = changeextension(file, "o")
Daniel Dunbar7000bf82008-09-30 16:18:31 +0000348 args = []
349 if language:
350 args.extend(['-x', language])
351 args = ['-o', out, file] + compile_opts
Daniel Dunbard3d81412008-08-29 21:03:27 +0000352 checked_compile(args, native, language, save_temps)
353 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000354 files[i] = out
355 if not output:
356 output = 'a.out'
357 args = ['-o', output] + link_opts + files
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000358 link(args, native)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000359
360if __name__ == '__main__':
361 main(sys.argv[1:])