Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
47 changes: 33 additions & 14 deletions src/codechecker.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
load(
"@default_codechecker_tools//:defs.bzl",
"CODECHECKER_BIN_PATH",
"BAZEL_VERSION"
)
load(
"per_file.bzl",
Expand Down Expand Up @@ -89,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
Expand Down Expand Up @@ -199,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,
Expand Down Expand Up @@ -280,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)
Expand Down Expand Up @@ -326,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",
Expand All @@ -338,10 +354,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,
Expand All @@ -352,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",
Expand All @@ -373,7 +385,14 @@ _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",
)} if BAZEL_VERSION.split(".")[0] in "0123456" else {}),
outputs = {
"compile_commands": "%{name}/compile_commands.json",
"codechecker_commands": "%{name}/codechecker_commands.json",
Expand Down
23 changes: 13 additions & 10 deletions src/compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -341,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
Expand Down Expand Up @@ -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",
},
Expand Down
16 changes: 16 additions & 0 deletions src/extensions.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
3 changes: 2 additions & 1 deletion src/tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
Loading