diff --git a/.ci/mise/config.toml b/.ci/mise/config.toml index 6691f135..8359b944 100644 --- a/.ci/mise/config.toml +++ b/.ci/mise/config.toml @@ -4,10 +4,10 @@ experimental = true [tools] # Environment tools: -"bazel" = "6.5" +"bazel" = "7" "python" = "3.11" "pipx" = "latest" -"pipx:codechecker" = "6.26" +"pipx:codechecker" = "6.27" # Clang tools: "conda:clang" = "latest" @@ -70,11 +70,17 @@ run = "# Installing tools" quiet = true -[tasks."test"] -depends = ["install:bazel-7", "install:codechecker-26", "install:codechecker-27"] +[tasks."legacy"] +depends = [ + "install:bazel-8", + "install:bazel-7", + "install:bazel-6", + "install:codechecker-27", +] run = [ - { task = "test:7-26" }, - { task = "test:7-27" }, + { task = "test:8-27 test/unit/legacy" }, + { task = "test:7-27 test/unit/legacy" }, + { task = "test:6-27 test/unit/legacy" }, ] [tasks."test:all"] diff --git a/MODULE.bazel b/MODULE.bazel index e6b4b5dc..3c5b5f14 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -16,18 +16,21 @@ limitations under the License. module(name = "rules_codechecker") +# C/C++ rules: cc_library, cc_binary +bazel_dep(name = "rules_cc", version = "0.2.14") + +# Python rules and toolchain +bazel_dep(name = "rules_python", version = "1.9.0") python_extension = use_extension( - "//src:tools.bzl", - "module_register_default_python_toolchain", + "@rules_python//python/extensions:python.bzl", + "default_python_tools", ) +python_extension.toolchain(python_version = "3.11", is_default = True) +use_repo(python_extension, "default_python_tools") +# CodeChecker support codechecker_extension = use_extension( "//src:tools.bzl", "module_register_default_codechecker", ) - -use_repo(python_extension, "default_python_tools") - use_repo(codechecker_extension, "default_codechecker_tools") - -register_toolchains("@default_python_tools//:python_toolchain") diff --git a/src/codechecker.bzl b/src/codechecker.bzl index 6030b201..86483716 100644 --- a/src/codechecker.bzl +++ b/src/codechecker.bzl @@ -104,7 +104,6 @@ def _codechecker_impl(ctx): substitutions = { "{Mode}": "Run", "{Verbosity}": "DEBUG", - "{PythonPath}": python_path(ctx), # "/usr/bin/env python3", "{codechecker_bin}": CODECHECKER_BIN_PATH, "{compile_commands}": ctx.outputs.codechecker_commands.path, "{codechecker_skipfile}": ctx.outputs.codechecker_skipfile.path, @@ -233,7 +232,6 @@ def _codechecker_test_impl(ctx): substitutions = { "{Mode}": "Test", "{Verbosity}": "INFO", - "{PythonPath}": python_path(ctx), # "/usr/bin/env python3", "{codechecker_bin}": CODECHECKER_BIN_PATH, "{codechecker_files}": codechecker_files.short_path, "{Severities}": " ".join(ctx.attr.severities), diff --git a/src/codechecker_script.py b/src/codechecker_script.py index dd87a74a..4860a283 100644 --- a/src/codechecker_script.py +++ b/src/codechecker_script.py @@ -1,4 +1,4 @@ -#!{PythonPath} +#!/usr/bin/env python3 # Copyright 2023 Ericsson AB # diff --git a/src/common.bzl b/src/common.bzl index 198b800e..4cc90038 100644 --- a/src/common.bzl +++ b/src/common.bzl @@ -53,6 +53,7 @@ def python_toolchain_type(): def python_path(ctx): """ Returns version specific Python path + NOTE: this function is not being used """ py_toolchain = ctx.toolchains[python_toolchain_type()] if hasattr(py_toolchain, "py3_runtime_info"): @@ -61,6 +62,8 @@ def python_path(ctx): elif hasattr(py_toolchain, "py3_runtime"): py_runtime = py_toolchain.py3_runtime python_path = py_runtime.interpreter_path + if not python_path: + python_path = py_runtime.interpreter.path else: fail("The resolved Python toolchain does not provide a Python3 runtime.") if not python_path: diff --git a/src/per_file.bzl b/src/per_file.bzl index 8a9be0fa..22223be5 100644 --- a/src/per_file.bzl +++ b/src/per_file.bzl @@ -20,7 +20,7 @@ for each translation unit. load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load("codechecker_config.bzl", "get_config_file") -load("common.bzl", "SOURCE_ATTR") +load("common.bzl", "python_toolchain_type", "SOURCE_ATTR") load( "compile_commands.bzl", "SourceFilesInfo", @@ -126,7 +126,6 @@ def _create_wrapper_script(ctx, options, compile_commands_json, config_file): output = ctx.outputs.per_file_script, is_executable = True, substitutions = { - "{PythonPath}": ctx.attr._python_runtime[PyRuntimeInfo].interpreter_path, "{compile_commands_json}": compile_commands_json.path, "{codechecker_args}": options_str, "{config_file}": config_file.path, @@ -225,14 +224,12 @@ per_file_test = rule( default = ":per_file_script.py", allow_single_file = True, ), - "_python_runtime": attr.label( - default = "@default_python_tools//:py3_runtime", - ), }, outputs = { "compile_commands": "%{name}/compile_commands.json", "test_script": "%{name}/test_script.sh", "per_file_script": "%{name}/per_file_script.py", }, + toolchains = [python_toolchain_type()], test = True, ) diff --git a/src/per_file_script.py b/src/per_file_script.py index 1cfad4b9..d64b7a6c 100644 --- a/src/per_file_script.py +++ b/src/per_file_script.py @@ -1,4 +1,4 @@ -#!{PythonPath} +#!/usr/bin/env python3 # Copyright 2023 Ericsson AB # diff --git a/test/foss/yaml-cpp/init.sh b/test/foss/yaml-cpp/init.sh index 80e769a0..92368c58 100755 --- a/test/foss/yaml-cpp/init.sh +++ b/test/foss/yaml-cpp/init.sh @@ -49,16 +49,21 @@ codechecker_test( ], ) +# per_file fails with the following error: +# ERROR: BUILD.bazel:31:17: output 'per_file_test/data/src-emitter.cpp_clangsa.plist' was not created +# ERROR: BUILD.bazel:31:17: CodeChecker analyze src/emitter.cpp failed: not all outputs were created or valid codechecker_test( name = "per_file_test", targets = [ ":yaml-cpp", ], - per_file = True, + #<<< per_file = True, ) #------------------------------------------------------- EOF -# Add rules_codechecker repo to WORKSPACE +# Add rules_codechecker repo to WORKSPACE for Bazel 6 cat ../templates/WORKSPACE.template >> "$1/WORKSPACE" +# And to MODULE.bazel for Bazel 7, 8 and higher +cat ../templates/MODULE.template >> "$1/MODULE.bazel" diff --git a/test/foss/zlib-module/init.sh b/test/foss/zlib-module/init.sh index eb818e63..68c13dbc 100644 --- a/test/foss/zlib-module/init.sh +++ b/test/foss/zlib-module/init.sh @@ -58,10 +58,7 @@ codechecker_test( #------------------------------------------------------- EOF -# Enable MODULE.bazel (in Bazel 6) -echo "common --enable_bzlmod" > "$1/.bazelrc" -# Add rules_codechecker repo MODULE.bazel +# Add rules_codechecker repo to WORKSPACE for Bazel 6 +cat ../templates/WORKSPACE.template >> "$1/WORKSPACE" +# And to MODULE.bazel for Bazel 7, 8 and higher cat ../templates/MODULE.template >> "$1/MODULE.bazel" -# An empty workspace file is required to keep Bazel versions older than 6.5.0 -# in the project directory -touch "$1/WORKSPACE" diff --git a/test/foss/zlib/init.sh b/test/foss/zlib/init.sh index 6d5f0126..991f6c6d 100644 --- a/test/foss/zlib/init.sh +++ b/test/foss/zlib/init.sh @@ -60,5 +60,7 @@ codechecker_test( #------------------------------------------------------- EOF -# Add rules_codechecker repo to WORKSPACE +# Add rules_codechecker repo to WORKSPACE for Bazel 6 cat ../templates/WORKSPACE.template >> "$1/WORKSPACE" +# And to MODULE.bazel for Bazel 7, 8 and higher +cat ../templates/MODULE.template >> "$1/MODULE.bazel" diff --git a/test/unit/external_repository/test_external_repo.py b/test/unit/external_repository/test_external_repo.py index 73622287..138e9db5 100644 --- a/test/unit/external_repository/test_external_repo.py +++ b/test/unit/external_repository/test_external_repo.py @@ -57,7 +57,10 @@ def setUpClass(cls): logging.debug("No bazel version set, using system default") _, stdout, _ = cls.run_command("bazel --version") cls.BAZEL_VERSION = stdout.split(" ")[2].strip() - logging.debug("Using Bazel %s", cls.BAZEL_VERSION) + logging.debug("Bazel version: %s", cls.BAZEL_VERSION) + major_version = int(cls.BAZEL_VERSION.split(".")[0]) + if major_version < 7: + raise unittest.SkipTest("Skip tests for Bazel older than 7") @final @classmethod @@ -84,11 +87,11 @@ def tearDownClass(cls): def test_compile_commands_external_lib(self): """ Test: bazel build :compile_commands_isystem " - "--experimental_cc_implementation_deps --enable_bzlmod + "--experimental_cc_implementation_deps """ ret, _, stderr = self.run_command( "bazel build :compile_commands_isystem " - "--experimental_cc_implementation_deps --enable_bzlmod" + "--experimental_cc_implementation_deps" ) self.assertEqual(ret, 0, stderr) comp_json_file = os.path.join( @@ -97,19 +100,11 @@ def test_compile_commands_external_lib(self): "compile_commands.json", ) - # The ~override part is a consquence of using Bzlmod. - if self.BAZEL_VERSION.startswith("6"): # type: ignore - pattern1 = "-isystem external/external_lib~override/include" - pattern2 = ( - "-isystem bazel-out/k8-fastbuild/bin/external/" - "external_lib~override/include" - ) - else: - pattern1 = "-isystem external/external_lib~/include" - pattern2 = ( - "-isystem bazel-out/k8-fastbuild/bin/external/" - "external_lib~/include" - ) + pattern1 = r"-isystem external/external_lib./include" + pattern2 = ( + r"-isystem bazel-out/k8-fastbuild/bin/external/" + "external_lib./include" + ) self.assertTrue(self.contains_regex_in_file(comp_json_file, pattern1)) self.assertTrue(self.contains_regex_in_file(comp_json_file, pattern2)) @@ -117,11 +112,11 @@ def test_compile_commands_external_lib(self): def test_codechecker_external_lib(self): """ Test: bazel build :codechecker_external_deps - --experimental_cc_implementation_deps --enable_bzlmod + --experimental_cc_implementation_deps """ ret, _, stderr = self.run_command( "bazel build :codechecker_external_deps " - "--experimental_cc_implementation_deps --enable_bzlmod" + "--experimental_cc_implementation_deps" ) self.assertEqual(ret, 0, stderr) @@ -130,7 +125,7 @@ def test_per_file_external_lib(self): "--experimental_cc_implementation_deps""" ret, _, stderr = self.run_command( "bazel build :per_file_external_deps " - "--experimental_cc_implementation_deps --enable_bzlmod" + "--experimental_cc_implementation_deps" ) self.assertEqual(ret, 0, stderr) diff --git a/test/unit/legacy/BUILD b/test/unit/legacy/BUILD index 3e263675..34f6fb00 100644 --- a/test/unit/legacy/BUILD +++ b/test/unit/legacy/BUILD @@ -1,3 +1,8 @@ +load("@rules_cc//cc:defs.bzl", + "cc_binary", + "cc_library", +) + # clang-tidy and clang -analyze rules load( "//src:clang.bzl",