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
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ module(name = "rules_codechecker")
bazel_dep(name = "rules_cc", version = "0.2.3")
bazel_dep(name = "rules_python", version = "0.38.0")

bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
dev_dependency = True,
)
bazel_dep(
name = "buildifier_prebuilt",
version = "7.3.1",
Expand Down
28 changes: 23 additions & 5 deletions src/compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,30 @@ platforms_transition = transition(
],
)

def _check_source_files(source_files, compilation_db):
def check_source_files(source_files, compilation_db):
"""Check that all files in compilation_db are present in source_files.

Args:
source_files: list of source file objects.
compilation_db: list of compilation database entries.
Returns:
None if all files are present, or an error message string if a file is missing.
"""
available_sources = [src.path for src in source_files]
checking_sources = [item.file for item in compilation_db]
for src in checking_sources:
if src not in available_sources:
fail("File: %s\nNot available in collected source files" % src)
return "File: %s\nNot available in collected source files" % src
return None

def _compile_commands_json(compilation_db):
def compile_commands_json(compilation_db):
"""Generate a compile_commands.json string from a compilation database.

Args:
compilation_db: list of structs with file, command, directory fields.
Returns:
A JSON string representing the compilation database.
"""
json_file = "[\n"
entries = [json.encode(entry) for entry in compilation_db]
json_file += ",\n".join(entries)
Expand Down Expand Up @@ -426,10 +442,12 @@ def compile_commands_impl(ctx):
fail("Compilation database is empty!")

# Check that we collect all required source files
_check_source_files(source_files, compilation_db)
error = check_source_files(source_files, compilation_db)
if error:
fail(error)

# Generate compile_commands.json from compilation database info
compile_db_json = _compile_commands_json(compilation_db)
compile_db_json = compile_commands_json(compilation_db)

# Save compile_commands.json file
ctx.actions.write(
Expand Down
219 changes: 219 additions & 0 deletions test/unit/compile_commands/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# Copyright 2026 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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("@rules_cc//cc:defs.bzl", "cc_library")
load(
":compile_commands_analysis_test.bzl",
"cc_compiler_info_test_suite",
"collect_headers_test_suite",
"custom_ccinfo",
"get_compile_flags_no_duplicates_test",
"get_compile_flags_quote_includes_from_deps_test",
"get_compile_flags_test_suite",
"get_sources_test_suite",
)
load(
":compile_commands_unit_test.bzl",
"compile_commands_test_suite",
)

# =============================================================================
# Unit tests
# =============================================================================

compile_commands_test_suite(name = "compile_commands_tests")

# =============================================================================
# Analysis tests
# =============================================================================

# Shared test targets
# -------------------

cc_library(
name = "collect_headers_tests_foo",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
)

cc_library(
name = "collect_headers_tests_bar",
srcs = ["testdata/bar.cc"],
hdrs = ["testdata/bar.h"],
tags = ["manual"],
)

cc_library(
name = "collect_headers_tests_with_dep",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
deps = [":collect_headers_tests_bar"],
)

cc_library(
name = "collect_headers_tests_with_impl_dep",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
implementation_deps = [":collect_headers_tests_bar"],
tags = ["manual"],
)

cc_library(
name = "collect_headers_tests_no_hdrs",
srcs = ["testdata/foo.cc"],
tags = ["manual"],
)

# collect_headers
# ---------------

collect_headers_test_suite(name = "collect_headers_tests")

# get_sources
# -----------

get_sources_test_suite(name = "get_sources_tests")

# get_compile_flags
# -----------------

cc_library(
name = "compile_flags_tests_with_defines",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
defines = ["MY_DEFINE=1"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_local_defines",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
local_defines = ["LOCAL_DEF=1"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_includes",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
includes = ["my/include/path"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_copts",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
copts = [
"-Wall",
"-Wextra",
],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_dep_with_includes",
srcs = ["testdata/bar.cc"],
hdrs = ["testdata/bar.h"],
includes = ["dep/include"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_dep_includes",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
deps = [":compile_flags_tests_dep_with_includes"],
)

custom_ccinfo(
name = "compile_flags_tests_sys_include_provider",
system_includes = ["my/sys/include"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_system_includes",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
deps = [":compile_flags_tests_sys_include_provider"],
)

custom_ccinfo(
name = "compile_flags_tests_quote_include_provider",
quote_includes = ["my/quote/include"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_quote_includes",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
deps = [":compile_flags_tests_quote_include_provider"],
)

get_compile_flags_test_suite(name = "compile_flags_tests")

# BUG (manual): compile commands should not contain duplicate flags.
get_compile_flags_no_duplicates_test(
name = "get_compile_flags_no_duplicates",
tags = ["manual"],
target_under_test = ":compile_flags_tests_with_dep_includes",
)

# BUG (manual): quote_includes from implementation_deps are not collected.
custom_ccinfo(
name = "compile_flags_tests_dep_quote_include_provider",
quote_includes = ["dep/quote/path"],
tags = ["manual"],
)

cc_library(
name = "compile_flags_tests_with_dep_quote_includes",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
implementation_deps = [":compile_flags_tests_dep_quote_include_provider"],
tags = ["manual"],
)

get_compile_flags_quote_includes_from_deps_test(
name = "get_compile_flags_quote_includes_from_deps",
tags = ["manual"],
target_under_test = ":compile_flags_tests_with_dep_quote_includes",
)

# _cc_compiler_info
# -----------------

cc_library(
name = "cc_compiler_info_tests_cpp_target",
srcs = ["testdata/foo.cc"],
hdrs = ["testdata/foo.h"],
tags = ["manual"],
)

cc_library(
name = "cc_compiler_info_tests_c_target",
srcs = ["testdata/bar.c"],
hdrs = ["testdata/bar.h"],
tags = ["manual"],
)

cc_compiler_info_test_suite(name = "cc_compiler_info_tests")
Loading
Loading