You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.1 KiB
85 lines
3.1 KiB
10 years ago
|
# Copyright (c) 2015 - present Facebook, Inc.
|
||
|
# All rights reserved.
|
||
|
#
|
||
|
# This source code is licensed under the BSD style license found in the
|
||
|
# LICENSE file in the root directory of this source tree. An additional grant
|
||
|
# of patent rights can be found in the PATENTS file in the same directory.
|
||
|
|
||
9 years ago
|
import logging
|
||
10 years ago
|
import os
|
||
|
import util
|
||
10 years ago
|
import tempfile
|
||
10 years ago
|
|
||
9 years ago
|
from inferlib import jwlib
|
||
|
|
||
10 years ago
|
MODULE_NAME = __name__
|
||
|
MODULE_DESCRIPTION = '''Run analysis of code built with a command like:
|
||
|
gradle [options] [task]
|
||
|
|
||
|
Analysis examples:
|
||
|
infer -- gradle build
|
||
|
infer -- ./gradlew build'''
|
||
|
|
||
9 years ago
|
|
||
10 years ago
|
|
||
|
def gen_instance(*args):
|
||
|
return GradleCapture(*args)
|
||
|
|
||
|
# This creates an empty argparser for the module, which provides only
|
||
|
# description/usage information and no arguments.
|
||
|
create_argparser = util.base_argparser(MODULE_DESCRIPTION, MODULE_NAME)
|
||
|
|
||
|
|
||
|
class GradleCapture:
|
||
|
|
||
|
def __init__(self, args, cmd):
|
||
|
self.args = args
|
||
10 years ago
|
# TODO: make the extraction of targets smarter
|
||
10 years ago
|
self.build_cmd = [cmd[0], '--debug'] + cmd[1:]
|
||
10 years ago
|
# That contains javac version as well
|
||
|
version_str = util.run_cmd_ignore_fail([cmd[0], '--version'])
|
||
9 years ago
|
path = os.path.join(self.args.infer_out, jwlib.FILELISTS)
|
||
10 years ago
|
if not os.path.exists(path):
|
||
|
os.mkdir(path)
|
||
10 years ago
|
logging.info("Running with:\n" + version_str)
|
||
10 years ago
|
|
||
9 years ago
|
def get_infer_commands(self, verbose_output):
|
||
10 years ago
|
argument_start_pattern = ' Compiler arguments: '
|
||
|
calls = []
|
||
9 years ago
|
seen_build_cmds = set([])
|
||
10 years ago
|
for line in verbose_output:
|
||
|
if argument_start_pattern in line:
|
||
|
content = line.partition(argument_start_pattern)[2].strip()
|
||
9 years ago
|
# if we're building both the debug and release configuration
|
||
|
# and the build commands are identical up to "release/debug",
|
||
|
# only do capture for one set of commands
|
||
|
build_agnostic_cmd = content.replace('release', 'debug')
|
||
|
if build_agnostic_cmd in seen_build_cmds:
|
||
|
continue
|
||
|
seen_build_cmds.add(build_agnostic_cmd)
|
||
10 years ago
|
javac_arguments = content.split(' ')
|
||
10 years ago
|
java_files = []
|
||
|
java_args = []
|
||
|
for java_arg in javac_arguments:
|
||
|
if java_arg.endswith('.java'):
|
||
|
java_files.append(java_arg)
|
||
|
else:
|
||
|
java_args.append(java_arg)
|
||
|
with tempfile.NamedTemporaryFile(
|
||
|
mode='w',
|
||
|
suffix='.txt',
|
||
|
prefix='gradle_',
|
||
9 years ago
|
dir=os.path.join(self.args.infer_out, jwlib.FILELISTS),
|
||
10 years ago
|
delete=False) as sources:
|
||
|
sources.write("\n".join(java_files))
|
||
|
sources.flush()
|
||
|
java_args.append("@" + sources.name)
|
||
9 years ago
|
capture = util.create_infer_command(self.args,
|
||
10 years ago
|
java_args)
|
||
|
calls.append(capture)
|
||
10 years ago
|
return calls
|
||
|
|
||
|
def capture(self):
|
||
9 years ago
|
cmds = self.get_infer_commands(util.get_build_output(self.build_cmd))
|
||
10 years ago
|
return util.run_commands(cmds)
|