From 7ea30ecf3c217a9f85835dcc1a5748a06cbb17b8 Mon Sep 17 00:00:00 2001 From: "F.Tibor" Date: Mon, 1 Dec 2025 11:18:47 +0100 Subject: [PATCH 1/2] Add conditional argumant creation --- src/codechecker.bzl | 10 +++++----- src/compile_commands.bzl | 13 ++++++++----- src/tools.bzl | 1 + 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/codechecker.bzl b/src/codechecker.bzl index a3ebd009..f37066e7 100644 --- a/src/codechecker.bzl +++ b/src/codechecker.bzl @@ -9,6 +9,7 @@ load( load( "@default_codechecker_tools//:defs.bzl", "CODECHECKER_BIN_PATH", + "BAZEL_VERSION" ) load( "per_file.bzl", @@ -338,10 +339,6 @@ _codechecker_test = rule( cfg = platforms_transition, doc = "List of compilable targets which should be checked.", ), - "_whitelist_function_transition": attr.label( - default = "@bazel_tools//tools/whitelists/function_transition_whitelist", - doc = "needed for transitions", - ), "_compile_commands_filter": attr.label( allow_files = True, executable = True, @@ -373,7 +370,10 @@ _codechecker_test = rule( default = [], doc = "List of analyze command arguments, e.g. --ctu", ), - }, + } | ({"_whitelist_function_transition": attr.label( + default = "@bazel_tools//tools/whitelists/function_transition_whitelist", + doc = "needed for transitions", + )} if BAZEL_VERSION.split(".")[0] in "0123456" else {}), outputs = { "compile_commands": "%{name}/compile_commands.json", "codechecker_commands": "%{name}/codechecker_commands.json", diff --git a/src/compile_commands.bzl b/src/compile_commands.bzl index 60262819..bf637182 100644 --- a/src/compile_commands.bzl +++ b/src/compile_commands.bzl @@ -39,6 +39,10 @@ load( "CPP_COMPILE_ACTION_NAME", "C_COMPILE_ACTION_NAME", ) +load( + "@default_codechecker_tools//:defs.bzl", + "BAZEL_VERSION" +) SourceFilesInfo = provider( doc = "Source files and corresponding compilation database (or compile commands)", @@ -411,11 +415,10 @@ _compile_commands = rule( cfg = platforms_transition, doc = "List of compilable targets which should be checked.", ), - "_whitelist_function_transition": attr.label( - default = "@bazel_tools//tools/whitelists/function_transition_whitelist", - doc = "needed for transitions", - ), - }, + } | ({"_whitelist_function_transition": attr.label( + default = "@bazel_tools//tools/whitelists/function_transition_whitelist", + doc = "needed for transitions", + )} if BAZEL_VERSION.split(".")[0] in "0123456" else {}), outputs = { "compile_commands": "%{name}/compile_commands.json", }, diff --git a/src/tools.bzl b/src/tools.bzl index 93629beb..f7b53d87 100644 --- a/src/tools.bzl +++ b/src/tools.bzl @@ -83,6 +83,7 @@ def _codechecker_local_repository_impl(repository_ctx): fail("ERROR! CodeChecker is not detected") defs = "CODECHECKER_BIN_PATH = '{}'\n".format(codechecker_bin_path) + defs += "BAZEL_VERSION = '{}'\n".format(native.bazel_version) repository_ctx.file( repository_ctx.path("defs.bzl"), content = defs, From a7430bff52a00d903298292aabdc3d0b77daa974 Mon Sep 17 00:00:00 2001 From: "F.Tibor" Date: Mon, 1 Dec 2025 12:46:42 +0100 Subject: [PATCH 2/2] Fix monolithic rule for bazel 8 --- MODULE.bazel | 20 ++++++++++++++++++++ src/codechecker.bzl | 37 ++++++++++++++++++++++++++++--------- src/compile_commands.bzl | 10 +++++----- src/extensions.bzl | 16 ++++++++++++++++ src/tools.bzl | 2 +- 5 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 MODULE.bazel create mode 100644 src/extensions.bzl diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 00000000..ae928862 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,20 @@ +""" +Module docs string +""" +module( + name = "bazel_codechecker", + version = "0.0.0", # Update this to your release version +) +ext = use_extension("//src:extensions.bzl", "initialize_tools") +use_repo(ext, "default_codechecker_tools") +bazel_dep(name = "rules_cc", version = "0.1.1") +bazel_dep(name = "rules_python", version = "0.40.0") +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + configure_coverage_tool = True, + python_version = "3.11", +) +use_repo(python, "python_3_11", "python_versions") +register_toolchains( + "@python_3_11//:all", +) diff --git a/src/codechecker.bzl b/src/codechecker.bzl index f37066e7..ab7bfbb6 100644 --- a/src/codechecker.bzl +++ b/src/codechecker.bzl @@ -90,8 +90,15 @@ def _copy_config_to_default(config_file, ctx): ) def _codechecker_impl(ctx): - py_runtime_info = ctx.attr._python_runtime[PyRuntimeInfo] - python_path = py_runtime_info.interpreter_path + py_toolchain = ctx.toolchains["@rules_python//python:toolchain_type"] + py_runtime_info = py_toolchain.py3_runtime + if py_runtime_info.interpreter: + python_path = py_runtime_info.interpreter.path + else: + python_path = py_runtime_info.interpreter_path + #py_runtime_info = ctx.attr._python_runtime[PyRuntimeInfo] + #python_path = py_runtime_info.interpreter_path + print(python_path) # Get compile_commands.json file and source files compile_commands = None @@ -200,8 +207,8 @@ def _codechecker_impl(ctx): codechecker_files, ctx.outputs.codechecker_log, ], - executable = ctx.outputs.codechecker_script, - arguments = [], + executable = py_runtime_info.interpreter, + arguments = [ctx.outputs.codechecker_script.path], mnemonic = "CodeChecker", progress_message = "CodeChecker %s" % str(ctx.label), # use_default_shell_env = True, @@ -281,8 +288,15 @@ codechecker = rule( ) def _codechecker_test_impl(ctx): - py_runtime_info = ctx.attr._python_runtime[PyRuntimeInfo] - python_path = py_runtime_info.interpreter_path + py_toolchain = ctx.toolchains["@rules_python//python:toolchain_type"] + py_runtime_info = py_toolchain.py3_runtime + if py_runtime_info.interpreter: + python_path = py_runtime_info.interpreter.path + else: + python_path = py_runtime_info.interpreter_path + #py_runtime_info = ctx.attr._python_runtime[PyRuntimeInfo] + #python_path = py_runtime_info.interpreter_path + print(python_path) # Run CodeChecker at build step info = _codechecker_impl(ctx) @@ -327,6 +341,7 @@ def _codechecker_test_impl(ctx): _codechecker_test = rule( implementation = _codechecker_test_impl, + toolchains = ["@rules_python//python:toolchain_type"], attrs = { "platform": attr.string( default = "", #"@platforms//os:linux", @@ -349,9 +364,9 @@ _codechecker_test = rule( default = ":codechecker_script.py", allow_single_file = True, ), - "_python_runtime": attr.label( - default = "@default_python_tools//:py3_runtime", - ), + #"_python_runtime": attr.label( + # default = "@default_python_tools//:py3_runtime", + #), "severities": attr.string_list( default = ["HIGH"], doc = "List of defect severities: HIGH, MEDIUM, LOW, STYLE etc", @@ -370,6 +385,10 @@ _codechecker_test = rule( default = [], doc = "List of analyze command arguments, e.g. --ctu", ), + #"_python_runtime": attr.label( + # default = Label("@rules_python//python:current_py_toolchain"), + # providers = [PyRuntimeInfo], + #), } | ({"_whitelist_function_transition": attr.label( default = "@bazel_tools//tools/whitelists/function_transition_whitelist", doc = "needed for transitions", diff --git a/src/compile_commands.bzl b/src/compile_commands.bzl index bf637182..73961964 100644 --- a/src/compile_commands.bzl +++ b/src/compile_commands.bzl @@ -345,11 +345,11 @@ def _check_source_files(source_files, compilation_db): fail("File: %s\nNot available in collected source files" % src) def _compile_commands_json(compilation_db): - json = "[\n" - entries = [entry.to_json() for entry in compilation_db] - json += ",\n".join(entries) - json += "]\n" - return json + json_cont = "[\n" + entries = [json.encode(entry) for entry in compilation_db] + json_cont += ",\n".join(entries) + json_cont += "]\n" + return json_cont def compile_commands_impl(ctx): """ Creates compile_commands.json file for given targets and platform diff --git a/src/extensions.bzl b/src/extensions.bzl new file mode 100644 index 00000000..5f3bd75b --- /dev/null +++ b/src/extensions.bzl @@ -0,0 +1,16 @@ +# src/extensions.bzl +load( + "//src:tools.bzl", + "register_default_codechecker", + "register_default_python_toolchain", +) + +def _initialize_tools_impl(ctx): + # WARNING: See note below about legacy macros! + register_default_python_toolchain() + register_default_codechecker() + +# Define the extension here +initialize_tools = module_extension( + implementation = _initialize_tools_impl, +) diff --git a/src/tools.bzl b/src/tools.bzl index f7b53d87..96553694 100644 --- a/src/tools.bzl +++ b/src/tools.bzl @@ -69,7 +69,7 @@ default_python_tools = repository_rule( def register_default_python_toolchain(): default_python_tools(name = "default_python_tools") - native.register_toolchains("@default_python_tools//:python_toolchain") + #native.register_toolchains("@default_python_tools//:python_toolchain") def _codechecker_local_repository_impl(repository_ctx): repository_ctx.file(