blob: 6569588ad0d53b28c384007294b182764d4f3a68 [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):
18 print 'ccc: ' + message
19 sys.exit(1)
20
21def run(args):
22 print ' '.join(args)
23 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
39def main(args):
40 action = 'link'
41 output = ''
42 compile_opts = ['-U__GNUC__']
43 link_opts = []
44 files = []
45
46 i = 0
47 while i < len(args):
48 arg = args[i]
49 if arg == '-E':
50 action = 'preprocess'
51 if arg == '-c':
52 action = 'compile'
53 if arg == '-o':
54 output = args[i+1]
55 i += 1
Seo Sanghyeon42599552008-01-17 01:08:43 +000056 if arg == '--param':
57 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000058 if arg[:2] in ['-D', '-I', '-U']:
59 if not arg[2:]:
60 arg += args[i+1]
61 i += 1
62 compile_opts.append(arg)
63 if arg[:2] in ['-l', '-L', '-O']:
64 if arg == '-O': arg = '-O1'
65 if arg == '-Os': arg = '-O2'
66 link_opts.append(arg)
67 if arg[0] != '-':
68 files.append(arg)
69 i += 1
70
71 if not files:
72 error('no input files')
73
74 if action == 'preprocess':
75 args = compile_opts + files
76 preprocess(args)
77
78 if action == 'compile':
79 if not output:
80 output = files[0].replace('.c', '.o')
81 args = ['-o', output] + compile_opts + files
82 compile(args)
83
84 if action == 'link':
85 for i, file in enumerate(files):
86 if '.c' in file:
87 out = file.replace('.c', '.o')
88 args = ['-o', out] + compile_opts + [file]
89 compile(args)
90 files[i] = out
91 if not output:
92 output = 'a.out'
93 args = ['-o', output] + link_opts + files
94 link(args)
95
96if __name__ == '__main__':
97 main(sys.argv[1:])