From 2cc6285dfd32b786e92239d4038dcdea3abfb63b Mon Sep 17 00:00:00 2001 From: Daniel Krupp Date: Thu, 12 Sep 2024 17:59:29 +0200 Subject: [PATCH] Configuring the env for some additional tools --- .../analyzers/clangsa/ctu_autodetection.py | 11 ++++++----- .../analyzers/clangtidy/analyzer.py | 8 ++++++-- .../analyzers/cppcheck/analyzer.py | 7 +++++-- .../codechecker_analyzer/analyzers/gcc/analyzer.py | 8 +++++++- analyzer/codechecker_analyzer/cmd/analyzers.py | 8 ++++++-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/analyzer/codechecker_analyzer/analyzers/clangsa/ctu_autodetection.py b/analyzer/codechecker_analyzer/analyzers/clangsa/ctu_autodetection.py index dacf46fa90..d418b45419 100644 --- a/analyzer/codechecker_analyzer/analyzers/clangsa/ctu_autodetection.py +++ b/analyzer/codechecker_analyzer/analyzers/clangsa/ctu_autodetection.py @@ -13,6 +13,7 @@ import subprocess from codechecker_common.logger import get_logger +from codechecker_analyzer import analyzer_context from codechecker_analyzer import host_check from codechecker_analyzer.analyzers.clangsa import version @@ -75,7 +76,7 @@ def ctu_mapping(clang_version_info): return None, None -def invoke_binary_checked(binary_path, args=None, environ=None): +def invoke_binary_checked(binary_path, args=None): """ Invoke the binary with the specified args, and return the output if the command finished running with zero exit code. Return False otherwise. @@ -91,6 +92,7 @@ def invoke_binary_checked(binary_path, args=None, environ=None): args = args or [] invocation = [binary_path] invocation.extend(args) + environ = analyzer_context.get_context().get_env_for_bin(binary_path) try: output = subprocess.check_output( invocation, @@ -123,7 +125,7 @@ def __init__(self, analyzer_binary, environ): return analyzer_version = invoke_binary_checked( - self.__analyzer_binary, ['--version'], self.environ) + self.__analyzer_binary, ['--version']) if analyzer_version is False: LOG.debug('Failed to invoke command to get Clang version!') @@ -222,7 +224,7 @@ def is_ctu_capable(self): if not tool_path: return False - return invoke_binary_checked(tool_path, ['-version'], self.environ) \ + return invoke_binary_checked(tool_path, ['-version']) \ is not False @property @@ -233,8 +235,7 @@ def is_on_demand_ctu_available(self): """ analyzer_options = invoke_binary_checked( - self.__analyzer_binary, ['-cc1', '-analyzer-config-help'], - self.environ) + self.__analyzer_binary, ['-cc1', '-analyzer-config-help']) if analyzer_options is False: return False diff --git a/analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py b/analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py index 13441f7e1e..cbc803edfc 100644 --- a/analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py +++ b/analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py @@ -143,11 +143,12 @@ def get_diagtool_bin(): return None -def get_warnings(environment=None): +def get_warnings(): """ Returns list of warning flags by using diagtool. """ diagtool_bin = get_diagtool_bin() + environment = analyzer_context.get_context().get_env_for_bin(diagtool_bin) if not diagtool_bin: return [] @@ -285,7 +286,7 @@ def get_analyzer_checkers(cls): checker_description.extend( ("clang-diagnostic-" + warning, "") - for warning in get_warnings(environ)) + for warning in get_warnings()) cls.__analyzer_checkers = checker_description @@ -315,6 +316,9 @@ def get_analyzer_config(cls): """ Return the analyzer configuration with all checkers enabled. """ + if not cls.analyzer_binary(): + return [] + try: result = subprocess.check_output( [cls.analyzer_binary(), "-dump-config", "-checks=*"], diff --git a/analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py b/analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py index efd4c2eef9..4ad42a296e 100644 --- a/analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py +++ b/analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py @@ -244,10 +244,13 @@ def get_analyzer_checkers(cls): """ Return the list of the supported checkers. """ + if not cls.analyzer_binary(): + return [] command = [cls.analyzer_binary(), "--errorlist"] - + environ = analyzer_context.get_context().get_env_for_bin( + command[0]) try: - result = subprocess.check_output(command) + result = subprocess.check_output(command, env=environ) return parse_checkers(result) except (subprocess.CalledProcessError) as e: LOG.error(e.stderr) diff --git a/analyzer/codechecker_analyzer/analyzers/gcc/analyzer.py b/analyzer/codechecker_analyzer/analyzers/gcc/analyzer.py index 505194353f..d99e60cf5b 100644 --- a/analyzer/codechecker_analyzer/analyzers/gcc/analyzer.py +++ b/analyzer/codechecker_analyzer/analyzers/gcc/analyzer.py @@ -55,6 +55,8 @@ def construct_analyzer_cmd(self, result_handler): # unforeseen exceptions where a general catch is justified? config = self.config_handler + if not Gcc.analyzer_binary(): + return None # We don't want GCC do start linking, but -fsyntax-only stops the # compilation process too early for proper diagnostics generation. analyzer_cmd = [Gcc.analyzer_binary(), '-fanalyzer', '-c', @@ -91,10 +93,14 @@ def get_analyzer_checkers(cls): Return the list of the supported checkers. """ command = [cls.analyzer_binary(), "--help=warning"] + if not cls.analyzer_binary(): + return [] + environ = analyzer_context.get_context().get_env_for_bin( + command[0]) checker_list = [] try: - output = subprocess.check_output(command) + output = subprocess.check_output(command, env=environ) # Still contains the help message we need to remove. for entry in output.decode().split('\n'): diff --git a/analyzer/codechecker_analyzer/cmd/analyzers.py b/analyzer/codechecker_analyzer/cmd/analyzers.py index 26d0ddba3b..e235a08234 100644 --- a/analyzer/codechecker_analyzer/cmd/analyzers.py +++ b/analyzer/codechecker_analyzer/cmd/analyzers.py @@ -118,10 +118,13 @@ def main(args): if args.dump_config: binary = context.analyzer_binaries.get(args.dump_config) + environ = analyzer_context.get_context().get_env_for_bin( + binary) if args.dump_config == 'clang-tidy': subprocess.call([binary, '-dump-config', '-checks=*'], - encoding="utf-8", errors="ignore") + encoding="utf-8", errors="ignore", + env=environ) elif args.dump_config == 'clangsa': ret = subprocess.call([binary, '-cc1', @@ -129,7 +132,8 @@ def main(args): '-analyzer-checker-option-help-alpha'], stderr=subprocess.PIPE, encoding="utf-8", - errors="ignore") + errors="ignore", + env=environ) if ret: # This flag is supported from Clang 9.