blob: eff184392da8ae81d40b85afde5a7c22bbc953c2 [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
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000014import os
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000015import sys
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000016
17def error(message):
Anders Carlssond125bb12008-01-29 07:21:34 +000018 print >> sys.stderr, 'ccc: ' + message
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000019 sys.exit(1)
20
21def run(args):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000022 cmd = ' '.join(args)
23 print >> sys.stderr, cmd
24 code = os.system(cmd)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000025 if code:
26 sys.exit(code)
27
28def preprocess(args):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000029 run(['clang -E'] + args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000030
31def compile(args):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000032 run(['clang -emit-llvm-bc'] + args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000033
34def link(args):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000035 run(['llvm-ld -native'] + args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000036
Anders Carlssond125bb12008-01-29 07:21:34 +000037def extension(path):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000038 return path.split(".")[-1]
Anders Carlssond125bb12008-01-29 07:21:34 +000039
40def changeextension(path, newext):
Seo Sanghyeon795aaed2008-02-03 03:40:41 +000041 i = path.rfind('.')
42 if i < 0:
43 return path
44 return path[:i] + newext
Anders Carlssond125bb12008-01-29 07:21:34 +000045
46def inferlanguage(extension):
47 if extension == "c":
48 return "c"
49 elif extension == "i":
50 return "c-cpp-output"
51 elif extension == "m":
52 return "objective-c"
53 elif extension == "mi":
54 return "objective-c-cpp-output"
55 else:
56 return "unknown"
57
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000058def main(args):
59 action = 'link'
60 output = ''
Seo Sanghyeon96b99f72008-01-25 14:57:54 +000061 compile_opts = []
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000062 link_opts = []
63 files = []
Anders Carlssond125bb12008-01-29 07:21:34 +000064 save_temps = 0
65 language = ''
66
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000067 i = 0
68 while i < len(args):
69 arg = args[i]
Anders Carlssond125bb12008-01-29 07:21:34 +000070
71 # Modes ccc supports
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000072 if arg == '-E':
73 action = 'preprocess'
74 if arg == '-c':
75 action = 'compile'
Seo Sanghyeon96b99f72008-01-25 14:57:54 +000076 if arg.startswith('-print-prog-name'):
77 action = 'print-prog-name'
Anders Carlssond125bb12008-01-29 07:21:34 +000078 if arg == '-save-temps':
79 save_temps = 1
80
81 # Options with no arguments that should pass through
82 if arg in ['-v']:
83 compile_opts.append(arg)
84 link_opts.append(arg)
85
86 # Options with one argument that should be ignored
Anders Carlssonc720d9b2008-01-31 23:48:19 +000087 if arg in ['--param', '-arch', '-u']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000088 i += 1
Anders Carlssond125bb12008-01-29 07:21:34 +000089
90 # Prefix matches for the compile mode
91 if arg[:2] in ['-D', '-I', '-U', '-F']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000092 if not arg[2:]:
93 arg += args[i+1]
94 i += 1
95 compile_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +000096 if arg[:5] in ['-std=']:
97 compile_opts.append(arg)
98
99 # Options with one argument that should pass through
100 if arg in ['-include']:
101 compile_opts.append(arg)
102 compile_opts.append(args[i+1])
103 i += 1
104
105 # Prefix matches for the link mode
106 if arg[:2] in ['-l', '-L', '-O', '-F']:
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000107 if arg == '-O': arg = '-O1'
108 if arg == '-Os': arg = '-O2'
109 link_opts.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000110
111 # Options with one argument that should pass through
112 if arg in ['-framework']:
113 link_opts.append(arg)
114 link_opts.append(args[i+1])
115 i += 1
116
117 # Input files
118 if arg == '-filelist':
119 f = open(args[i+1])
120 for line in f:
121 files.append(line.strip())
122 f.close()
123 i += 1
124 if arg == '-x':
125 language = args[i+1]
126 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000127 if arg[0] != '-':
128 files.append(arg)
Anders Carlssond125bb12008-01-29 07:21:34 +0000129
130 # Output file
131 if arg == '-o':
132 output = args[i+1]
133 i += 1
134
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000135 i += 1
136
Seo Sanghyeon96b99f72008-01-25 14:57:54 +0000137 if action == 'print-prog-name':
138 # assume we can handle everything
139 print sys.argv[0]
140 return
141
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000142 if not files:
143 error('no input files')
144
Anders Carlssond125bb12008-01-29 07:21:34 +0000145 if action == 'preprocess' or save_temps:
146 for i, file in enumerate(files):
147 if not language:
148 language = inferlanguage(extension(file))
149 if save_temps and action != 'preprocess':
150 # Need a temporary output file
151 if language == 'c':
152 poutput = changeextension(file, "i");
153 elif language == 'objective-c':
154 poutput = changeextension(file, "mi");
155 else:
156 poutput = changeextension(file, "tmp." + extension(file))
157 files[i] = poutput
158 else:
159 poutput = output
160 if poutput:
161 args = ['-x', language, '-o', poutput, file] + compile_opts
162 else:
163 args = ['-x', language, file] + compile_opts
164 preprocess(args)
165 # Discard the explicit language after used once
166 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000167
Anders Carlssond125bb12008-01-29 07:21:34 +0000168 if action == 'compile' or save_temps:
169 for i, file in enumerate(files):
170 if not language:
171 language = inferlanguage(extension(file))
172 if save_temps and action != "compile":
173 # Need a temporary output file
174 coutput = changeextension(file, "o");
175 files[i] = coutput
176 elif not output:
177 coutput = changeextension(file, "o")
178 else:
179 coutput = output
180 args = ['-x', language, '-o', coutput, file] + compile_opts
181 compile(args)
182 language = ''
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000183
184 if action == 'link':
185 for i, file in enumerate(files):
Anders Carlssonc720d9b2008-01-31 23:48:19 +0000186 ext = extension(file)
187 if ext != "o" and ext != "a":
Anders Carlssond125bb12008-01-29 07:21:34 +0000188 out = changeextension(file, "o")
189 args = ['-o', out, file] + compile_opts
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000190 compile(args)
191 files[i] = out
192 if not output:
193 output = 'a.out'
194 args = ['-o', output] + link_opts + files
195 link(args)
196
197if __name__ == '__main__':
198 main(sys.argv[1:])