Skip to content

Commit

Permalink
[analyzer] Add support the CC_ANALYZER_BIN env var
Browse files Browse the repository at this point in the history
This environmental variable can be used the specify the absolute path of
an analyzer. Similar to Ericsson#4041, but this is the only solution we can
realistically get though the door.
  • Loading branch information
Szelethus committed Oct 24, 2023
1 parent 7af389a commit 98ca58f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
17 changes: 17 additions & 0 deletions analyzer/codechecker_analyzer/arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
'AnalyzerConfig', ["analyzer", "option", "value"])
CheckerConfig = collections.namedtuple(
"CheckerConfig", ["analyzer", "checker", "option", "value"])
AnalyzerBinary = collections.namedtuple(
"AnalyzerBinary", ["analyzer", "path"])


class OrderedCheckersAction(argparse.Action):
Expand Down Expand Up @@ -133,3 +135,18 @@ def checker_config(arg: str) -> CheckerConfig:
return CheckerConfig(
m.group("analyzer"), m.group("checker"),
m.group("option"), m.group("value"))


def analyzer_binary(arg: str) -> AnalyzerBinary:
"""
This function can be used at "type" argument of argparse.add_argument().
It checks the format of --analyzer_binary flag: <analyzer>:<path>
"""
m = re.match(r"(?P<analyzer>.+):(?P<path>.+)", arg)

if not m:
raise argparse.ArgumentTypeError(
f"Analyzer binary in wrong format: {arg}, should be "
"<analyzer>:</path/to/bin/>")

return AnalyzerBinary(m.group("analyzer"), m.group("path"))
37 changes: 36 additions & 1 deletion analyzer/codechecker_analyzer/cmd/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from codechecker_analyzer.analyzers import analyzer_types, clangsa
from codechecker_analyzer.arg import \
OrderedCheckersAction, OrderedConfigAction, existing_abspath, \
analyzer_config, checker_config
analyzer_config, checker_config, analyzer_binary
from codechecker_analyzer.buildlog import log_parser

from codechecker_common import arg, logger, cmd_config
Expand Down Expand Up @@ -899,6 +899,40 @@ def __get_result_source_files(metadata):
return result_src_files


def __parse_CC_ANALYZER_BIN():
context = analyzer_context.get_context()
if 'CC_ANALYZER_BIN' in context.analyzer_env:
had_error = False
for value in context.analyzer_env['CC_ANALYZER_BIN'].split(';'):
try:
analyzer_name, path = analyzer_binary(value)
except argparse.ArgumentTypeError as e:
LOG.error(e)
had_error = True
continue

if analyzer_name not in analyzer_types.supported_analyzers:
LOG.error(f"Unsupported analyzer_name name '{analyzer_name}' "
"given to CC_ANALYZER_BIN!")
had_error = True
if not os.path.isfile(path):
LOG.error(f"'{path}' is not a path to an analyzer binary "
"given to CC_ANALYZER_BIN!")
had_error = True

if had_error:
continue

LOG.info(f"Using '{path}' for analyzer '{analyzer_name}'")
context.analyzer_binaries[analyzer_name] = path

if had_error:
LOG.info("The value of CC_ANALYZER_BIN should be in the format of "
"CC_ANALYZER_BIN='<analyzer1>:/path/to/bin1"
"<analyzer2>:/path/to/bin2'")
sys.exit(1)


def main(args):
"""
Perform analysis on the given inputs. Possible inputs are a compilation
Expand Down Expand Up @@ -981,6 +1015,7 @@ def main(args):
ctu_or_stats_enabled = True

context = analyzer_context.get_context()
__parse_CC_ANALYZER_BIN()

# Number of all the compilation commands in the parsed log files,
# logged by the logger.
Expand Down

0 comments on commit 98ca58f

Please sign in to comment.