Skip to content
Draft
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
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ codechecker_extension = use_extension(
"module_register_default_codechecker",
)
use_repo(codechecker_extension, "default_codechecker_tools")

register_toolchains(
"//src:codechecker_local_toolchain",
)
5 changes: 4 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ load(

register_default_python_toolchain()

register_toolchains("@default_python_tools//:python_toolchain")
register_toolchains(
"@default_python_tools//:python_toolchain",
"//src:codechecker_local_toolchain",
)

register_default_codechecker()

Expand Down
16 changes: 16 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load(":codechecker_toolchain.bzl", "codechecker_toolchain")

# Tool filter compile_commands.json file
# In bazel 6 we use our own python toolchain,
Expand Down Expand Up @@ -63,3 +64,18 @@ label_flag(
build_setting_default = ":clang_tidy_additional_deps_default",
visibility = ["//visibility:public"],
)

# named this by convention
# https://bazel.build/extending/toolchains#:~:text=%23%20By%20convention%2C%20toolchain%5Ftype%20targets%20are%20named%20%22toolchain%5Ftype%22%20and
toolchain_type(name = "toolchain_type")

codechecker_toolchain(
name = "codechecker_local",
analyzer_binary = "CodeChecker", # Find it in PATH ?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should find a way how to make it flexible. At least we should be able to pass CodeChecker binary by absolute path, try to find in the PATH (current implementation), as a Label, maybe via env var, maybe somehow else...

)

toolchain(
name = "codechecker_local_toolchain",
toolchain = "codechecker_local",
toolchain_type = ":toolchain_type",
)
25 changes: 25 additions & 0 deletions src/codechecker_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
This file provides the toolchain rule for CodeChecker
"""

CodeCheckerInfo = provider(
doc = "This provider provides the executable path for CodeChecker",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should list all parameters including clang, clang-tidy and maybe more

fields = [
"executable",
],
Comment on lines +7 to +9
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip: I think this should be a dict

)

def _codechecker_toolchain_impl(ctx):
toolchain_info = platform_common.ToolchainInfo(
codecheckerinfo = CodeCheckerInfo(
executable = ctx.attr.analyzer_binary,
),
)
return [toolchain_info]

codechecker_toolchain = rule(
implementation = _codechecker_toolchain_impl,
attrs = {
"analyzer_binary": attr.string(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess "analyzer" is not a good name here

},
)
6 changes: 6 additions & 0 deletions src/per_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def _run_code_checker(
content = "\n".join(ctx.attr.skip),
)

info = ctx.toolchains["//src:toolchain_type"].codecheckerinfo
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we discussed that to get rid of "src" you may try alias. (I havent tried myself though :) )


if "--ctu" in options:
inputs = [
compile_commands_json,
Expand Down Expand Up @@ -86,6 +88,7 @@ def _run_code_checker(
outputs = outputs,
executable = ctx.outputs.per_file_script,
arguments = [
info.executable,
data_dir,
src.path,
codechecker_log.path,
Expand Down Expand Up @@ -155,6 +158,8 @@ def _create_wrapper_script(ctx, options, compile_commands_json, config_file):
)

def _per_file_impl(ctx):
info = ctx.toolchains["//src:toolchain_type"].codecheckerinfo
print("CodeChecker path resolved:", info.executable)
compile_commands = None
for output in compile_commands_impl(ctx):
if type(output) == "DefaultInfo":
Expand Down Expand Up @@ -258,4 +263,5 @@ per_file_test = rule(
"test_script": "%{name}/test_script.sh",
},
test = True,
toolchains = ["//src:toolchain_type"],
)
15 changes: 8 additions & 7 deletions src/per_file_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
COMPILE_COMMANDS_ABSOLUTE: str = f"{COMPILE_COMMANDS_JSON}.abs"
CODECHECKER_ARGS: str = "{codechecker_args}"
CONFIG_FILE: str = "{config_file}"
SKIP_FILE: str = sys.argv[4]
SKIP_FILE: str = sys.argv[5]
CODECHECKER_BIN = sys.argv[1]
# The output directory for CodeChecker
DATA_DIR = sys.argv[1]
DATA_DIR = sys.argv[2]
# The file to be analyzed
FILE_PATH = sys.argv[2]
LOG_FILE = sys.argv[3]
FILE_PATH = sys.argv[3]
LOG_FILE = sys.argv[4]
# List of pairs of analyzers and their plist files
ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[5].split(";")]
ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[6].split(";")]

EMPTY_PLIST = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Expand Down Expand Up @@ -86,7 +87,7 @@ def _run_codechecker() -> None:
Runs CodeChecker analyze
"""
codechecker_cmd: list[str] = (
["CodeChecker", "analyze"]
[CODECHECKER_BIN, "analyze"]
+ CODECHECKER_ARGS.split()
+ ["--output=" + DATA_DIR]
+ ["--file=*/" + FILE_PATH]
Expand Down Expand Up @@ -173,7 +174,7 @@ def main():
"""
Main function of CodeChecker wrapper
"""
if len(sys.argv) != 6:
if len(sys.argv) != 7:
print("Wrong amount of arguments")
sys.exit(1)
_create_compile_commands_json_with_absolute_paths()
Expand Down
5 changes: 4 additions & 1 deletion test/foss/templates/WORKSPACE.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ load(
)

register_default_python_toolchain()
register_toolchains("@default_python_tools//:python_toolchain")
register_toolchains(
"@default_python_tools//:python_toolchain",
"@rules_codechecker//src:codechecker_local_toolchain",
)

register_default_codechecker()

Expand Down
Loading