blob: d4a3ee7e7b89985cd722e9ff90199d951ac6762f [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
14import sys
15import subprocess
16
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):
Anders Carlssond125bb12008-01-29 07:21:34 +000022 print >> sys.stderr, ' '.join(args)
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000023 code = subprocess.call(args)
24 if code:
25 sys.exit(code)
26
27def preprocess(args):
28 command = 'clang -E'.split()
29 run(command + args)
30
31def compile(args):
32 command = 'clang -emit-llvm-bc'.split()
33 run(command + args)
34
35def link(args):
36 command = 'llvm-ld -native'.split()
37 run(command + args)
38
Anders Carlssond125bb12008-01-29 07:21:34 +000039def extension(path):
40 return path.rpartition(".")[2]
41
42def changeextension(path, newext):
43 components = path.rpartition(".")
44 return "".join([components[0], components[1], newext])
45
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
87 if arg in ['--param', '-arch']:
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 Carlssond125bb12008-01-29 07:21:34 +0000186 if extension(file) != "o":
187 out = changeextension(file, "o")
188 args = ['-o', out, file] + compile_opts
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +0000189 compile(args)
190 files[i] = out
191 if not output:
192 output = 'a.out'
193 args = ['-o', output] + link_opts + files
194 link(args)
195
196if __name__ == '__main__':
197 main(sys.argv[1:])