Skip to content
ccc 2.38 KiB
Newer Older
Seo Sanghyeon's avatar
Seo Sanghyeon committed
#!/usr/bin/env python
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
#
# This script attempts to be a drop-in replacement for gcc.
#
##===----------------------------------------------------------------------===##

import sys
import subprocess

def error(message):
    print 'ccc: ' + message
    sys.exit(1)

def run(args):
    print ' '.join(args)
    code = subprocess.call(args)
    if code:
        sys.exit(code)

def preprocess(args):
    command = 'clang -E'.split()
    run(command + args)

def compile(args):
    command = 'clang -emit-llvm-bc'.split()
    run(command + args)

def link(args):
    command = 'llvm-ld -native'.split()
    run(command + args)

def main(args):
    action = 'link'
    output = ''
    compile_opts = ['-U__GNUC__']
    link_opts = []
    files = []

    i = 0
    while i < len(args):
        arg = args[i]
        if arg == '-E':
            action = 'preprocess'
        if arg == '-c':
            action = 'compile'
        if arg == '-o':
            output = args[i+1]
            i += 1
Seo Sanghyeon's avatar
Seo Sanghyeon committed
        if arg == '--param':
            i += 1
Seo Sanghyeon's avatar
Seo Sanghyeon committed
        if arg[:2] in ['-D', '-I', '-U']:
            if not arg[2:]:
                arg += args[i+1]
                i += 1
            compile_opts.append(arg)
        if arg[:2] in ['-l', '-L', '-O']:
            if arg == '-O': arg = '-O1'
            if arg == '-Os': arg = '-O2'
            link_opts.append(arg)
        if arg[0] != '-':
            files.append(arg)
        i += 1

    if not files:
        error('no input files')

    if action == 'preprocess':
        args = compile_opts + files
        preprocess(args)

    if action == 'compile':
        if not output:
            output = files[0].replace('.c', '.o')
        args = ['-o', output] + compile_opts + files
        compile(args)

    if action == 'link':
        for i, file in enumerate(files):
            if '.c' in file:
                out = file.replace('.c', '.o')
                args = ['-o', out] + compile_opts + [file]
                compile(args)
                files[i] = out
        if not output:
            output = 'a.out'
        args = ['-o', output] + link_opts + files
        link(args)

if __name__ == '__main__':
    main(sys.argv[1:])