2008-03-26 01:35:32 +03:00
|
|
|
#!/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.
|
|
|
|
#
|
|
|
|
##===----------------------------------------------------------------------===##
|
|
|
|
#
|
|
|
|
# A reduced version of the 'ccc' script that is designed to handle off
|
|
|
|
# actual compilation to gcc, but run the code passed to gcc through the
|
|
|
|
# static analyzer.
|
|
|
|
#
|
|
|
|
##===----------------------------------------------------------------------===##
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
|
|
|
|
def error(message):
|
|
|
|
print >> sys.stderr, 'ccc: ' + message
|
|
|
|
sys.exit(1)
|
|
|
|
|
2008-04-04 15:02:21 +04:00
|
|
|
def quote(arg):
|
|
|
|
if '"' in arg:
|
|
|
|
return repr(arg)
|
|
|
|
return arg
|
|
|
|
|
2008-03-26 01:35:32 +03:00
|
|
|
def run(args):
|
2008-04-08 03:27:54 +04:00
|
|
|
# We MUST print to stderr. Some clients use the stdout output of
|
|
|
|
# gcc for various purposes.
|
2008-05-13 03:47:41 +04:00
|
|
|
#print >> sys.stderr, ' '.join(map(quote, args))
|
|
|
|
#print >> sys.stderr
|
2008-03-26 01:35:32 +03:00
|
|
|
code = subprocess.call(args)
|
|
|
|
if code > 255:
|
|
|
|
code = 1
|
|
|
|
if code:
|
|
|
|
sys.exit(code)
|
|
|
|
|
2008-04-22 01:58:05 +04:00
|
|
|
def compile(args):
|
2008-04-08 03:27:54 +04:00
|
|
|
# We MUST print to stderr. Some clients use the stdout output of
|
|
|
|
# gcc for various purposes.
|
2008-05-13 03:47:41 +04:00
|
|
|
#print >> sys.stderr, '\n'
|
2008-03-26 01:35:32 +03:00
|
|
|
command = 'gcc'.split()
|
|
|
|
run(command + args)
|
|
|
|
|
|
|
|
def remove_pch_extension(path):
|
|
|
|
i = path.rfind('.gch')
|
|
|
|
if i < 0:
|
|
|
|
return path
|
|
|
|
return path[:i]
|
|
|
|
|
2008-05-15 00:10:33 +04:00
|
|
|
def analyze(clang, args,language,output,files,verbose,htmldir,file,analysis_type):
|
2008-04-04 01:29:11 +04:00
|
|
|
if language.find("c++") > 0:
|
2008-03-26 01:35:32 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
print_args = []
|
|
|
|
|
2008-03-31 22:25:05 +04:00
|
|
|
if verbose:
|
2008-04-08 03:27:54 +04:00
|
|
|
# We MUST print to stderr. Some clients use the stdout output of
|
|
|
|
# gcc for various purposes.
|
2008-03-31 22:25:05 +04:00
|
|
|
print >> sys.stderr, ' '.join(['\n[LOCATION]:', os.getcwd(), '\n' ])
|
|
|
|
i = 0
|
|
|
|
while i < len(args):
|
2008-03-26 01:35:32 +03:00
|
|
|
print_args.append(''.join([ '\'', args[i], '\'' ]))
|
|
|
|
i += 1
|
2008-05-13 02:07:14 +04:00
|
|
|
|
|
|
|
|
|
|
|
RunAnalyzer = 0;
|
|
|
|
|
2008-03-26 01:35:32 +03:00
|
|
|
if language.find("header") > 0:
|
|
|
|
target = remove_pch_extension(output)
|
|
|
|
command = 'cp'.split()
|
|
|
|
args = command + files + target.split()
|
|
|
|
else:
|
2008-05-15 00:10:33 +04:00
|
|
|
command = clang.split() + analysis_type.split()
|
2008-04-01 01:20:32 +04:00
|
|
|
args = command + args;
|
2008-05-13 02:07:14 +04:00
|
|
|
RunAnalyzer = 1
|
|
|
|
|
|
|
|
if verbose == 2:
|
|
|
|
print >> sys.stderr, '#SHELL (cd ' + os.getcwd() + ' && ' + ' '.join(command + print_args) + ')\n'
|
|
|
|
|
|
|
|
if RunAnalyzer and htmldir is not None:
|
|
|
|
args.append('-o')
|
|
|
|
print_args.append('-o')
|
|
|
|
args.append(htmldir)
|
|
|
|
print_args.append(htmldir)
|
2008-03-31 22:25:05 +04:00
|
|
|
|
2008-04-08 03:27:54 +04:00
|
|
|
if verbose:
|
|
|
|
# We MUST print to stderr. Some clients use the stdout output of
|
|
|
|
# gcc for various purposes.
|
2008-03-31 22:25:05 +04:00
|
|
|
print >> sys.stderr, ' '.join(command+print_args)
|
|
|
|
print >> sys.stderr, '\n'
|
2008-05-13 02:07:14 +04:00
|
|
|
|
2008-05-13 21:10:28 +04:00
|
|
|
subprocess.call(args)
|
2008-03-26 01:35:32 +03:00
|
|
|
|
|
|
|
def link(args):
|
|
|
|
command = 'gcc'.split()
|
|
|
|
run(command + args)
|
|
|
|
|
|
|
|
def extension(path):
|
|
|
|
return path.split(".")[-1]
|
|
|
|
|
|
|
|
def changeextension(path, newext):
|
|
|
|
i = path.rfind('.')
|
|
|
|
if i < 0:
|
|
|
|
return path
|
|
|
|
j = path.rfind('/', 0, i)
|
|
|
|
print path
|
|
|
|
if j < 0:
|
|
|
|
return path[:i] + "." + newext
|
|
|
|
return path[j+1:i] + "." + newext
|
|
|
|
|
|
|
|
def inferlanguage(extension):
|
|
|
|
if extension == "c":
|
|
|
|
return "c"
|
|
|
|
elif extension in ["cpp", "cc"]:
|
|
|
|
return "c++"
|
|
|
|
elif extension == "i":
|
|
|
|
return "c-cpp-output"
|
|
|
|
elif extension == "m":
|
|
|
|
return "objective-c"
|
|
|
|
elif extension == "mi":
|
|
|
|
return "objective-c-cpp-output"
|
2008-05-15 00:20:46 +04:00
|
|
|
elif extension == "s":
|
2008-05-15 00:17:17 +04:00
|
|
|
return "skip"
|
2008-03-26 01:35:32 +03:00
|
|
|
else:
|
|
|
|
return "unknown"
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
old_args = args
|
|
|
|
action = 'link'
|
|
|
|
output = ''
|
|
|
|
compile_opts = [ ]
|
|
|
|
link_opts = [ ]
|
|
|
|
files = []
|
|
|
|
save_temps = 0
|
|
|
|
language = ''
|
|
|
|
|
2008-03-31 22:25:05 +04:00
|
|
|
verbose = 0
|
2008-05-13 03:47:41 +04:00
|
|
|
clang = "clang"
|
2008-03-31 22:25:05 +04:00
|
|
|
|
2008-05-13 03:47:41 +04:00
|
|
|
# Forward to GCC.
|
|
|
|
compile(args)
|
2008-05-15 00:10:33 +04:00
|
|
|
|
|
|
|
# Set the analyzer flag.
|
|
|
|
analysis_type = os.environ.get('CCC_ANALYZER_ANALYSIS')
|
|
|
|
|
|
|
|
if analysis_type is not None:
|
|
|
|
analysis_type = "-" + analysis_type
|
|
|
|
else:
|
|
|
|
analysis_type = "-checker-cfref"
|
|
|
|
|
|
|
|
# Determine the level of verbosity.
|
2008-03-31 22:25:05 +04:00
|
|
|
if os.environ.get('CCC_ANALYZER_VERBOSE') is not None:
|
2008-05-13 02:07:14 +04:00
|
|
|
verbose = 1
|
|
|
|
|
|
|
|
if os.environ.get('CCC_ANALYZER_LOG') is not None:
|
|
|
|
verbose = 2
|
2008-04-19 02:00:56 +04:00
|
|
|
|
2008-05-15 00:10:33 +04:00
|
|
|
# Determine what clang executable to use.
|
2008-04-19 02:00:56 +04:00
|
|
|
clang_env = os.environ.get('CLANG')
|
|
|
|
|
|
|
|
if clang_env is not None:
|
|
|
|
clang = clang_env
|
2008-03-31 22:25:05 +04:00
|
|
|
|
2008-05-15 00:10:33 +04:00
|
|
|
# Get the HTML output directory.
|
2008-05-15 00:26:52 +04:00
|
|
|
htmldir = None
|
|
|
|
|
|
|
|
if analysis_type == "-checker-cfref":
|
|
|
|
htmldir = os.environ.get('CCC_ANALYZER_HTML')
|
|
|
|
|
|
|
|
# Process the arguments.
|
2008-03-26 01:35:32 +03:00
|
|
|
i = 0
|
|
|
|
while i < len(args):
|
|
|
|
arg = args[i]
|
|
|
|
|
|
|
|
# Modes ccc supports
|
|
|
|
if arg == '-E':
|
|
|
|
action = 'preprocess'
|
|
|
|
if arg == '-c':
|
|
|
|
action = 'compile'
|
|
|
|
if arg.startswith('-print-prog-name'):
|
|
|
|
action = 'print-prog-name'
|
|
|
|
if arg == '-save-temps':
|
|
|
|
save_temps = 1
|
|
|
|
|
|
|
|
# Options with no arguments that should pass through
|
|
|
|
if arg in ['-v']:
|
|
|
|
compile_opts.append(arg)
|
|
|
|
link_opts.append(arg)
|
|
|
|
|
|
|
|
# Options with one argument that should be ignored
|
2008-04-22 00:28:01 +04:00
|
|
|
if arg in ['--param', '-u']:
|
2008-03-26 01:35:32 +03:00
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Prefix matches for the compile mode
|
2008-04-26 01:28:20 +04:00
|
|
|
if arg[:2] in ['-D', '-I', '-U', '-F' ]:
|
2008-03-26 01:35:32 +03:00
|
|
|
if not arg[2:]:
|
|
|
|
arg += args[i+1]
|
|
|
|
i += 1
|
|
|
|
compile_opts.append(arg)
|
2008-04-26 01:28:20 +04:00
|
|
|
|
2008-03-26 01:35:32 +03:00
|
|
|
if arg[:5] in ['-std=']:
|
|
|
|
compile_opts.append(arg)
|
|
|
|
|
2008-04-22 08:47:32 +04:00
|
|
|
# Options with one argument that should pass through to compiler
|
2008-04-26 01:28:20 +04:00
|
|
|
if arg in [ '-include', '-idirafter', '-iprefix',
|
|
|
|
'-iquote', '-isystem', '-iwithprefix',
|
|
|
|
'-iwithprefixbefore']:
|
2008-03-26 01:35:32 +03:00
|
|
|
compile_opts.append(arg)
|
|
|
|
compile_opts.append(args[i+1])
|
|
|
|
i += 1
|
2008-04-26 01:28:20 +04:00
|
|
|
|
|
|
|
# Options with no argument that should pass through to compiler
|
2008-05-02 01:26:22 +04:00
|
|
|
if arg in [ '-nostdinc', '-fobjc-gc-only', '-fobjc-gc' ]:
|
2008-04-26 01:28:20 +04:00
|
|
|
compile_opts.append(arg)
|
2008-03-26 01:35:32 +03:00
|
|
|
|
2008-04-22 08:47:32 +04:00
|
|
|
# Options with one argument that should pass through to linker
|
2008-05-02 01:26:22 +04:00
|
|
|
if arg == '-framework':
|
2008-03-26 01:35:32 +03:00
|
|
|
link_opts.append(arg)
|
2008-04-22 08:47:32 +04:00
|
|
|
link_opts.append(args[i+1])
|
|
|
|
i += 1
|
2008-03-26 01:35:32 +03:00
|
|
|
|
2008-04-22 08:47:32 +04:00
|
|
|
# Options with one argument that should pass through to both
|
|
|
|
if arg in ['-isysroot', '-arch']:
|
|
|
|
compile_opts.append(arg)
|
|
|
|
compile_opts.append(args[i+1])
|
2008-03-26 01:35:32 +03:00
|
|
|
link_opts.append(arg)
|
|
|
|
link_opts.append(args[i+1])
|
|
|
|
i += 1
|
|
|
|
|
2008-04-22 08:47:32 +04:00
|
|
|
# Prefix matches for the link mode
|
|
|
|
if arg[:2] in ['-l', '-L', '-O', '-F']:
|
|
|
|
if arg == '-O': arg = '-O1'
|
|
|
|
if arg == '-Os': arg = '-O2'
|
|
|
|
link_opts.append(arg)
|
|
|
|
|
2008-03-26 01:35:32 +03:00
|
|
|
# Input files
|
|
|
|
if arg == '-filelist':
|
|
|
|
f = open(args[i+1])
|
|
|
|
for line in f:
|
|
|
|
files.append(line.strip())
|
|
|
|
f.close()
|
|
|
|
i += 1
|
|
|
|
if arg == '-x':
|
|
|
|
language = args[i+1]
|
|
|
|
i += 1
|
|
|
|
if arg[0] != '-':
|
|
|
|
files.append(arg)
|
|
|
|
|
|
|
|
# Output file
|
|
|
|
if arg == '-o':
|
|
|
|
output = args[i+1]
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if action == 'print-prog-name':
|
|
|
|
# assume we can handle everything
|
|
|
|
print sys.argv[0]
|
|
|
|
return
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
error('no input files')
|
|
|
|
|
2008-05-13 03:47:41 +04:00
|
|
|
# if action == 'preprocess' or save_temps:
|
|
|
|
# compile(args)
|
2008-03-26 01:35:32 +03:00
|
|
|
|
|
|
|
if action == 'compile' or save_temps:
|
|
|
|
for i, file in enumerate(files):
|
|
|
|
if not language:
|
|
|
|
language = inferlanguage(extension(file))
|
2008-05-15 00:17:17 +04:00
|
|
|
if language == "skip":
|
|
|
|
continue
|
|
|
|
|
2008-03-26 01:35:32 +03:00
|
|
|
if save_temps and action != "compile":
|
|
|
|
# Need a temporary output file
|
|
|
|
coutput = changeextension(file, "o");
|
|
|
|
files[i] = coutput
|
|
|
|
elif not output:
|
|
|
|
coutput = changeextension(file, "o")
|
|
|
|
else:
|
|
|
|
coutput = output
|
|
|
|
analyze_args = [ file ]
|
|
|
|
if language != 'unknown':
|
2008-05-13 03:56:50 +04:00
|
|
|
analyze_args = [ '-x', language ] + analyze_args
|
2008-03-26 01:35:32 +03:00
|
|
|
analyze_args = analyze_args + compile_opts
|
2008-05-15 00:10:33 +04:00
|
|
|
analyze(clang, analyze_args, language, output, files, verbose, htmldir, file, analysis_type)
|
2008-05-13 03:47:41 +04:00
|
|
|
# compile(args)
|
2008-03-26 01:35:32 +03:00
|
|
|
|
|
|
|
|
2008-05-13 03:47:41 +04:00
|
|
|
# if action == 'link':
|
|
|
|
# link(args)
|
|
|
|
# # analyze(link_opts)
|
2008-03-26 01:35:32 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|