Skip to content

Commit baaa57a

Browse files
committed
Initial idea for a toolchain implementation
1 parent 5221aa7 commit baaa57a

5 files changed

Lines changed: 59 additions & 7 deletions

File tree

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ codechecker_extension = use_extension(
4040
"module_register_default_codechecker",
4141
)
4242
use_repo(codechecker_extension, "default_codechecker_tools")
43+
44+
register_toolchains(
45+
"//src:codechecker_local_toolchain",
46+
)

src/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
load(":codechecker_toolchain.bzl", "codechecker_toolchain")
1415

1516
# Tool filter compile_commands.json file
1617
# In bazel 6 we use our own python toolchain,
@@ -63,3 +64,18 @@ label_flag(
6364
build_setting_default = ":clang_tidy_additional_deps_default",
6465
visibility = ["//visibility:public"],
6566
)
67+
68+
# named this by convention
69+
# https://bazel.build/extending/toolchains#:~:text=%23%20By%20convention%2C%20toolchain%5Ftype%20targets%20are%20named%20%22toolchain%5Ftype%22%20and
70+
toolchain_type(name = "toolchain_type")
71+
72+
codechecker_toolchain(
73+
name = "codechecker_local",
74+
analyzer_binary = "CodeChecker", # Find it in PATH ?
75+
)
76+
77+
toolchain(
78+
name = "codechecker_local_toolchain",
79+
toolchain = "codechecker_local",
80+
toolchain_type = ":toolchain_type",
81+
)

src/codechecker_toolchain.bzl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
This file provides the toolchain rule for CodeChecker
3+
"""
4+
5+
CodeCheckerInfo = provider(
6+
doc = "This provider provides the executable path for CodeChecker",
7+
fields = [
8+
"executable",
9+
],
10+
)
11+
12+
def _codechecker_toolchain_impl(ctx):
13+
toolchain_info = platform_common.ToolchainInfo(
14+
codecheckerinfo = CodeCheckerInfo(
15+
executable = ctx.attr.analyzer_binary,
16+
),
17+
)
18+
return [toolchain_info]
19+
20+
codechecker_toolchain = rule(
21+
implementation = _codechecker_toolchain_impl,
22+
attrs = {
23+
"analyzer_binary": attr.string(),
24+
},
25+
)

src/per_file.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def _run_code_checker(
5959
content = "\n".join(ctx.attr.skip),
6060
)
6161

62+
info = ctx.toolchains["//src:toolchain_type"].codecheckerinfo
63+
6264
if "--ctu" in options:
6365
inputs = [
6466
compile_commands_json,
@@ -86,6 +88,7 @@ def _run_code_checker(
8688
outputs = outputs,
8789
executable = ctx.outputs.per_file_script,
8890
arguments = [
91+
info.executable,
8992
data_dir,
9093
src.path,
9194
codechecker_log.path,
@@ -155,6 +158,8 @@ def _create_wrapper_script(ctx, options, compile_commands_json, config_file):
155158
)
156159

157160
def _per_file_impl(ctx):
161+
info = ctx.toolchains["//src:toolchain_type"].codecheckerinfo
162+
print("CodeChecker path resolved:", info.executable)
158163
compile_commands = None
159164
for output in compile_commands_impl(ctx):
160165
if type(output) == "DefaultInfo":
@@ -258,4 +263,5 @@ per_file_test = rule(
258263
"test_script": "%{name}/test_script.sh",
259264
},
260265
test = True,
266+
toolchains = ["//src:toolchain_type"],
261267
)

src/per_file_script.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
COMPILE_COMMANDS_ABSOLUTE: str = f"{COMPILE_COMMANDS_JSON}.abs"
2929
CODECHECKER_ARGS: str = "{codechecker_args}"
3030
CONFIG_FILE: str = "{config_file}"
31-
SKIP_FILE: str = sys.argv[4]
31+
SKIP_FILE: str = sys.argv[5]
32+
CODECHECKER_BIN = sys.argv[1]
3233
# The output directory for CodeChecker
33-
DATA_DIR = sys.argv[1]
34+
DATA_DIR = sys.argv[2]
3435
# The file to be analyzed
35-
FILE_PATH = sys.argv[2]
36-
LOG_FILE = sys.argv[3]
36+
FILE_PATH = sys.argv[3]
37+
LOG_FILE = sys.argv[4]
3738
# List of pairs of analyzers and their plist files
38-
ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[5].split(";")]
39+
ANALYZER_PLIST_PATHS = [item.split(",") for item in sys.argv[6].split(";")]
3940

4041
EMPTY_PLIST = """<?xml version="1.0" encoding="UTF-8"?>
4142
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -86,7 +87,7 @@ def _run_codechecker() -> None:
8687
Runs CodeChecker analyze
8788
"""
8889
codechecker_cmd: list[str] = (
89-
["CodeChecker", "analyze"]
90+
[CODECHECKER_BIN, "analyze"]
9091
+ CODECHECKER_ARGS.split()
9192
+ ["--output=" + DATA_DIR]
9293
+ ["--file=*/" + FILE_PATH]
@@ -173,7 +174,7 @@ def main():
173174
"""
174175
Main function of CodeChecker wrapper
175176
"""
176-
if len(sys.argv) != 6:
177+
if len(sys.argv) != 7:
177178
print("Wrong amount of arguments")
178179
sys.exit(1)
179180
_create_compile_commands_json_with_absolute_paths()

0 commit comments

Comments
 (0)