blob: 794c166b87951518e04e701c005781c24f5fbc00 [file] [log] [blame]
Seo Sanghyeon40f0b762008-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
14import sys
Anders Carlsson5ec4dfe2008-02-06 19:03:27 +000015import subprocess
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000016
17def error(message):
Anders Carlssona93e51d2008-01-29 07:21:34 +000018 print >> sys.stderr, 'ccc: ' + message
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000019 sys.exit(1)
20
Seo Sanghyeon877866b2008-04-04 11:02:21 +000021def quote(arg):
22 if '"' in arg:
23 return repr(arg)
24 return arg
25
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000026def run(args):
Seo Sanghyeon877866b2008-04-04 11:02:21 +000027 print ' '.join(map(quote, args))
Anders Carlsson5ec4dfe2008-02-06 19:03:27 +000028 code = subprocess.call(args)
Bill Wendlingabb9ea02008-02-03 21:27:46 +000029 if code > 255:
30 code = 1
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000031 if code:
32 sys.exit(code)
33
34def preprocess(args):
Anders Carlsson5ec4dfe2008-02-06 19:03:27 +000035 command = 'clang -E'.split()
36 run(command + args)
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000037
38def compile(args):
Anders Carlsson5ec4dfe2008-02-06 19:03:27 +000039 command = 'clang -emit-llvm-bc'.split()
40 run(command + args)
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000041
42def link(args):
Lauro Ramos Venanciobfdf0172008-02-27 18:46:32 +000043 command = 'llvm-ld -native -disable-internalize'.split()
Anders Carlsson5ec4dfe2008-02-06 19:03:27 +000044 run(command + args)
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000045
Anders Carlssona93e51d2008-01-29 07:21:34 +000046def extension(path):
Seo Sanghyeon85282482008-02-03 03:40:41 +000047 return path.split(".")[-1]
Anders Carlssona93e51d2008-01-29 07:21:34 +000048
49def changeextension(path, newext):
Seo Sanghyeon85282482008-02-03 03:40:41 +000050 i = path.rfind('.')
51 if i < 0:
52 return path
Bill Wendlingabb9ea02008-02-03 21:27:46 +000053 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 Carlssona93e51d2008-01-29 07:21:34 +000058
59def inferlanguage(extension):
60 if extension == "c":
61 return "c"
Lauro Ramos Venancio9b9eec52008-02-15 22:35:25 +000062 elif extension in ["cpp", "cc"]:
63 return "c++"
Anders Carlssona93e51d2008-01-29 07:21:34 +000064 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 Sanghyeon40f0b762008-01-10 01:43:47 +000073def main(args):
74 action = 'link'
75 output = ''
Seo Sanghyeon365b3ba2008-01-25 14:57:54 +000076 compile_opts = []
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000077 link_opts = []
78 files = []
Anders Carlssona93e51d2008-01-29 07:21:34 +000079 save_temps = 0
80 language = ''
81
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000082 i = 0
83 while i < len(args):
84 arg = args[i]
Anders Carlssona93e51d2008-01-29 07:21:34 +000085
86 # Modes ccc supports
Seo Sanghyeon40f0b762008-01-10 01:43:47 +000087 if arg == '-E':
88 action = 'preprocess'
89 if arg == '-c':
90 action = 'compile'
Seo Sanghyeon365b3ba2008-01-25 14:57:54 +000091 if arg.startswith('-print-prog-name'):
92 action = 'print-prog-name'
Anders Carlssona93e51d2008-01-29 07:21:34 +000093 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 Kremenekaaa2a0f2008-04-21 20:28:01 +0000102 if arg in ['--param', '-u']:
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000103 i += 1
Anders Carlssona93e51d2008-01-29 07:21:34 +0000104
105 # Prefix matches for the compile mode
106 if arg[:2] in ['-D', '-I', '-U', '-F']:
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000107 if not arg[2:]:
108 arg += args[i+1]
109 i += 1
110 compile_opts.append(arg)
Anders Carlssona93e51d2008-01-29 07:21:34 +0000111 if arg[:5] in ['-std=']:
112 compile_opts.append(arg)
113
Nuno Lopes33ef4322008-06-17 17:23:14 +0000114 # Options with one argument that should pass through to compiler
115 if arg in [ '-include', '-idirafter', '-iprefix',
116 '-iquote', '-isystem', '-iwithprefix',
117 '-iwithprefixbefore']:
Anders Carlssona93e51d2008-01-29 07:21:34 +0000118 compile_opts.append(arg)
119 compile_opts.append(args[i+1])
120 i += 1
121
Nuno Lopes33ef4322008-06-17 17:23:14 +0000122 # Options with one argument that should pass through
123 if arg in ['-framework']:
124 link_opts.append(arg)
125 link_opts.append(args[i+1])
126 i += 1
127
128 # Options with one argument that should pass through to both
129 if arg in ['-isysroot', '-arch']:
130 compile_opts.append(arg)
131 compile_opts.append(args[i+1])
132 link_opts.append(arg)
133 link_opts.append(args[i+1])
134 i += 1
135
Anders Carlssona93e51d2008-01-29 07:21:34 +0000136 # Prefix matches for the link mode
137 if arg[:2] in ['-l', '-L', '-O', '-F']:
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000138 if arg == '-O': arg = '-O1'
139 if arg == '-Os': arg = '-O2'
140 link_opts.append(arg)
Anders Carlssona93e51d2008-01-29 07:21:34 +0000141
Chris Lattnerc3a2aa52008-06-21 17:46:11 +0000142 # Enable threads
143 if arg == '-pthread':
144 link_opts.append('-lpthread')
145
Anders Carlssona93e51d2008-01-29 07:21:34 +0000146 # Input files
147 if arg == '-filelist':
148 f = open(args[i+1])
149 for line in f:
150 files.append(line.strip())
151 f.close()
152 i += 1
153 if arg == '-x':
154 language = args[i+1]
Chris Lattnerc3a2aa52008-06-21 17:46:11 +0000155 compile_opts.append(arg)
156 compile_opts.append(args[i+1])
Anders Carlssona93e51d2008-01-29 07:21:34 +0000157 i += 1
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000158 if arg[0] != '-':
159 files.append(arg)
Anders Carlssona93e51d2008-01-29 07:21:34 +0000160
161 # Output file
162 if arg == '-o':
163 output = args[i+1]
164 i += 1
165
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000166 i += 1
167
Seo Sanghyeon365b3ba2008-01-25 14:57:54 +0000168 if action == 'print-prog-name':
169 # assume we can handle everything
170 print sys.argv[0]
171 return
172
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000173 if not files:
174 error('no input files')
175
Anders Carlssona93e51d2008-01-29 07:21:34 +0000176 if action == 'preprocess' or save_temps:
177 for i, file in enumerate(files):
178 if not language:
179 language = inferlanguage(extension(file))
180 if save_temps and action != 'preprocess':
181 # Need a temporary output file
182 if language == 'c':
183 poutput = changeextension(file, "i");
184 elif language == 'objective-c':
185 poutput = changeextension(file, "mi");
186 else:
187 poutput = changeextension(file, "tmp." + extension(file))
188 files[i] = poutput
189 else:
190 poutput = output
191 if poutput:
192 args = ['-x', language, '-o', poutput, file] + compile_opts
193 else:
194 args = ['-x', language, file] + compile_opts
195 preprocess(args)
196 # Discard the explicit language after used once
197 language = ''
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000198
Anders Carlssona93e51d2008-01-29 07:21:34 +0000199 if action == 'compile' or save_temps:
200 for i, file in enumerate(files):
201 if not language:
202 language = inferlanguage(extension(file))
203 if save_temps and action != "compile":
204 # Need a temporary output file
205 coutput = changeextension(file, "o");
206 files[i] = coutput
207 elif not output:
208 coutput = changeextension(file, "o")
209 else:
210 coutput = output
211 args = ['-x', language, '-o', coutput, file] + compile_opts
212 compile(args)
213 language = ''
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000214
215 if action == 'link':
216 for i, file in enumerate(files):
Anders Carlssondfcf69a2008-01-31 23:48:19 +0000217 ext = extension(file)
218 if ext != "o" and ext != "a":
Anders Carlssona93e51d2008-01-29 07:21:34 +0000219 out = changeextension(file, "o")
220 args = ['-o', out, file] + compile_opts
Seo Sanghyeon40f0b762008-01-10 01:43:47 +0000221 compile(args)
222 files[i] = out
223 if not output:
224 output = 'a.out'
225 args = ['-o', output] + link_opts + files
226 link(args)
227
228if __name__ == '__main__':
229 main(sys.argv[1:])