blob: b070fdbee5f55c3e2d0d3163114a07268d47d02d [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')
29CCC_LANGUAGES = checkenv('CCC_LANGUAGES')
30if 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"
167 else:
168 return "unknown"
169
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000170def main(args):
171 action = 'link'
172 output = ''
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000173 compile_opts = []
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000174 link_opts = []
175 files = []
Anders Carlssond125bb12008-01-29 07:21:34 +0000176 save_temps = 0
177 language = ''
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000178 native = CCC_NATIVE
179
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000180 i = 0
181 while i < len(args):
182 arg = args[i]
Anders Carlssond125bb12008-01-29 07:21:34 +0000183
184 # Modes ccc supports
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000185 if arg == '-E':
186 action = 'preprocess'
187 if arg == '-c':
188 action = 'compile'
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000189 if arg.startswith('-print-prog-name'):
190 action = 'print-prog-name'
Anders Carlssond125bb12008-01-29 07:21:34 +0000191 if arg == '-save-temps':
192 save_temps = 1
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000193 if arg == '-emit-llvm' or arg == '--emit-llvm':
194 native = False
Anders Carlssond125bb12008-01-29 07:21:34 +0000195
196 # Options with no arguments that should pass through
Daniel Dunbard3d81412008-08-29 21:03:27 +0000197 if arg in ['-v', '-fobjc-gc', '-fobjc-gc-only', '-fnext-runtime',
198 '-fgnu-runtime']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000199 compile_opts.append(arg)
200 link_opts.append(arg)
201
202 # Options with one argument that should be ignored
Ted Kremenekd0eef022008-04-21 20:28:01 +0000203 if arg in ['--param', '-u']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000204 i += 1
Anders Carlssond125bb12008-01-29 07:21:34 +0000205
Ted Kremenek16833602008-07-24 03:49:15 +0000206 # Preprocessor options with one argument that should be ignored
207 if arg in ['-MT', '-MF']:
208 i += 1
209
Anders Carlssond125bb12008-01-29 07:21:34 +0000210 # Prefix matches for the compile mode
211 if arg[:2] in ['-D', '-I', '-U', '-F']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000212 if not arg[2:]:
213 arg += args[i+1]
214 i += 1
215 compile_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000216 if arg[:5] in ['-std=']:
217 compile_opts.append(arg)
218
Nuno Lopese5d12e82008-06-17 17:23:14 +0000219 # Options with one argument that should pass through to compiler
220 if arg in [ '-include', '-idirafter', '-iprefix',
221 '-iquote', '-isystem', '-iwithprefix',
222 '-iwithprefixbefore']:
Anders Carlssond125bb12008-01-29 07:21:34 +0000223 compile_opts.append(arg)
224 compile_opts.append(args[i+1])
225 i += 1
226
Nuno Lopese5d12e82008-06-17 17:23:14 +0000227 # Options with one argument that should pass through
Daniel Dunbard3d81412008-08-29 21:03:27 +0000228 if arg in ['-framework', '-multiply_defined', '-bundle_loader']:
Nuno Lopese5d12e82008-06-17 17:23:14 +0000229 link_opts.append(arg)
230 link_opts.append(args[i+1])
231 i += 1
232
233 # Options with one argument that should pass through to both
234 if arg in ['-isysroot', '-arch']:
235 compile_opts.append(arg)
236 compile_opts.append(args[i+1])
237 link_opts.append(arg)
238 link_opts.append(args[i+1])
239 i += 1
240
Anders Carlssond125bb12008-01-29 07:21:34 +0000241 # Prefix matches for the link mode
Nuno Lopes9a184482008-08-10 21:58:01 +0000242 if arg[:2] in ['-l', '-L', '-F', '-R']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000243 link_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000244
Chris Lattnercf719b72008-06-21 17:46:11 +0000245 # Enable threads
246 if arg == '-pthread':
247 link_opts.append('-lpthread')
248
Anders Carlssond125bb12008-01-29 07:21:34 +0000249 # Input files
250 if arg == '-filelist':
251 f = open(args[i+1])
252 for line in f:
253 files.append(line.strip())
254 f.close()
255 i += 1
256 if arg == '-x':
257 language = args[i+1]
258 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000259 if arg[0] != '-':
260 files.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000261
262 # Output file
263 if arg == '-o':
264 output = args[i+1]
265 i += 1
266
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000267 i += 1
268
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000269 if action == 'print-prog-name':
270 # assume we can handle everything
271 print sys.argv[0]
272 return
273
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000274 if not files:
275 error('no input files')
276
Anders Carlssond125bb12008-01-29 07:21:34 +0000277 if action == 'preprocess' or save_temps:
278 for i, file in enumerate(files):
279 if not language:
280 language = inferlanguage(extension(file))
281 if save_temps and action != 'preprocess':
282 # Need a temporary output file
283 if language == 'c':
284 poutput = changeextension(file, "i");
285 elif language == 'objective-c':
286 poutput = changeextension(file, "mi");
287 else:
288 poutput = changeextension(file, "tmp." + extension(file))
289 files[i] = poutput
290 else:
291 poutput = output
292 if poutput:
293 args = ['-x', language, '-o', poutput, file] + compile_opts
294 else:
295 args = ['-x', language, file] + compile_opts
296 preprocess(args)
297 # Discard the explicit language after used once
298 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000299
Anders Carlssond125bb12008-01-29 07:21:34 +0000300 if action == 'compile' or save_temps:
301 for i, file in enumerate(files):
302 if not language:
303 language = inferlanguage(extension(file))
304 if save_temps and action != "compile":
305 # Need a temporary output file
306 coutput = changeextension(file, "o");
307 files[i] = coutput
308 elif not output:
309 coutput = changeextension(file, "o")
310 else:
311 coutput = output
312 args = ['-x', language, '-o', coutput, file] + compile_opts
Daniel Dunbard3d81412008-08-29 21:03:27 +0000313 checked_compile(args, native, language, save_temps)
Anders Carlssond125bb12008-01-29 07:21:34 +0000314 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000315
316 if action == 'link':
317 for i, file in enumerate(files):
Daniel Dunbard3d81412008-08-29 21:03:27 +0000318 if not language:
319 language = inferlanguage(extension(file))
Anders Carlssonc720d9b2008-01-31 23:48:19 +0000320 ext = extension(file)
Nuno Lopesc46cf492008-08-10 22:17:57 +0000321 if ext != "o" and ext != "a" and ext != "so":
Anders Carlssond125bb12008-01-29 07:21:34 +0000322 out = changeextension(file, "o")
Daniel Dunbard3d81412008-08-29 21:03:27 +0000323 args = ['-x', language, '-o', out, file] + compile_opts
324 checked_compile(args, native, language, save_temps)
325 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000326 files[i] = out
327 if not output:
328 output = 'a.out'
329 args = ['-o', output] + link_opts + files
Daniel Dunbar4b5c4f22008-08-23 22:15:15 +0000330 link(args, native)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000331
332if __name__ == '__main__':
333 main(sys.argv[1:])