blob: 308544ecbca86daadd20c7f1cea8888b3e98eb38 [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 = ''
Seo Sanghyeon96b99f72008-01-25 14:57:54 +000042 compile_opts = []
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000043 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'
Seo Sanghyeon96b99f72008-01-25 14:57:54 +000053 if arg.startswith('-print-prog-name'):
54 action = 'print-prog-name'
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000055 if arg == '-o':
56 output = args[i+1]
57 i += 1
Seo Sanghyeon42599552008-01-17 01:08:43 +000058 if arg == '--param':
59 i += 1
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000060 if arg[:2] in ['-D', '-I', '-U']:
61 if not arg[2:]:
62 arg += args[i+1]
63 i += 1
64 compile_opts.append(arg)
65 if arg[:2] in ['-l', '-L', '-O']:
66 if arg == '-O': arg = '-O1'
67 if arg == '-Os': arg = '-O2'
68 link_opts.append(arg)
69 if arg[0] != '-':
70 files.append(arg)
71 i += 1
72
Seo Sanghyeon96b99f72008-01-25 14:57:54 +000073 if action == 'print-prog-name':
74 # assume we can handle everything
75 print sys.argv[0]
76 return
77
Seo Sanghyeon2bfa5332008-01-10 01:43:47 +000078 if not files:
79 error('no input files')
80
81 if action == 'preprocess':
82 args = compile_opts + files
83 preprocess(args)
84
85 if action == 'compile':
86 if not output:
87 output = files[0].replace('.c', '.o')
88 args = ['-o', output] + compile_opts + files
89 compile(args)
90
91 if action == 'link':
92 for i, file in enumerate(files):
93 if '.c' in file:
94 out = file.replace('.c', '.o')
95 args = ['-o', out] + compile_opts + [file]
96 compile(args)
97 files[i] = out
98 if not output:
99 output = 'a.out'
100 args = ['-o', output] + link_opts + files
101 link(args)
102
103if __name__ == '__main__':
104 main(sys.argv[1:])