Skip to content
Open
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
103 changes: 71 additions & 32 deletions bazel/rules/rules_score/private/unit.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ following S-CORE process guidelines. A unit is the smallest testable
software element with associated design, implementation, and tests.
"""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@lobster//:lobster.bzl", "subrule_gtest_report")
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_rust//rust:defs.bzl", "rust_common")
load("//bazel/rules/rules_score:providers.bzl", "CcDependencyInfo", "CertifiedScope", "SphinxSourcesInfo", "UnitDesignInfo", "UnitInfo")
load("//cpp/libclang:cpp_parser.bzl", "cpp_parser_action_internal_attrs", "cpp_parser_target_aspects", "has_cpp_parser_inputs", "run_cpp_parser_action")
load(":cc_dependency_aspect.bzl", "cc_dependencies_aspect")

def _run_implementation_cpp_parser(ctx, impl, output_prefix):
return run_cpp_parser_action(
ctx,
target = impl,
output_prefix = output_prefix,
tool = ctx.attr._tool,
libclang = ctx.file._libclang,
llvm_cxx_builtin_include = ctx.attr._llvm_cxx_builtin_include,
llvm_extra_config_site = ctx.attr._llvm_extra_config_site,
log_level = ctx.attr._log_level[BuildSettingInfo].value,
)

def _target_output_prefix(ctx, target):
package_name = target.label.package.replace("/", "_")
target_name = target.label.name.replace("/", "_")
return "{}_{}_{}".format(ctx.label.name, package_name, target_name)

# ============================================================================
# Private Rule Implementation
# ============================================================================
Expand Down Expand Up @@ -59,28 +79,41 @@ def _unit_impl(ctx):
design_static_fbs_depset = depset(transitive = design_static_fbs)
design_dynamic_fbs_depset = depset(transitive = design_dynamic_fbs)

# Generate FBS files for implementation targets supported by the libclang parser.
implementation_class_fbs = []
collected_dependent_labels = []
for impl in ctx.attr.implementation:
if CcDependencyInfo in impl:
collected_dependent_labels.append(impl[CcDependencyInfo].dependencies)

if not has_cpp_parser_inputs(impl):
continue

output_prefix = _target_output_prefix(ctx, impl)
parser_outputs = _run_implementation_cpp_parser(ctx, impl, output_prefix)
implementation_class_fbs.append(parser_outputs.class_fbs)

# Run each test executable via subrule_gtest_report and collect the XML outputs
xml_files = []
for test_target in ctx.attr.tests:
pkg = test_target.label.package.replace("/", "_")
test_name = test_target.label.name.replace("/", "_")
unique_name = "{}_{}_{}_gtest_report".format(ctx.label.name, pkg, test_name)
xml = subrule_gtest_report(unique_name, test_target.files_to_run.executable, test_target.default_runfiles.files)
unique_name = "{}_gtest_report".format(
_target_output_prefix(ctx, test_target),
)
xml = subrule_gtest_report(
unique_name,
test_target.files_to_run.executable,
test_target.default_runfiles.files,
)
xml_files.append(xml)

tests_depset = depset(xml_files)

# Combine all files for DefaultInfo
all_files = depset(
xml_files,
xml_files + implementation_class_fbs,
transitive = [design_depset],
)

collected_dependent_labels = []
for impl in ctx.attr.implementation:
if CcDependencyInfo in impl:
collected_dependent_labels.append(impl[CcDependencyInfo].dependencies)

return [
DefaultInfo(files = all_files),
CertifiedScope(transitive_scopes = depset(ctx.attr.scope)),
Expand All @@ -103,32 +136,38 @@ def _unit_impl(ctx):
# Rule Definition
# ============================================================================

_unit_attrs = {
"unit_design": attr.label_list(
mandatory = True,
providers = [UnitDesignInfo],
doc = "Unit design artifacts (unit_design targets only)",
),
"implementation": attr.label_list(
mandatory = True,
providers = [[CcInfo], [rust_common.crate_info]],
aspects = [cc_dependencies_aspect] + cpp_parser_target_aspects(),
doc = "Implementation targets (cc_library, cc_binary, rust_library, rust_binary, etc.).",
),
"scope": attr.string_list(
default = [],
doc = "Additional not explicitly named targets which are needed for the unit implementation",
),
"tests": attr.label_list(
mandatory = True,
cfg = "exec",
doc = "Test targets that verify the unit (cc_test, py_test, rust_test, etc.)",
),
}

_unit_attrs.update(cpp_parser_action_internal_attrs())

_unit = rule(
implementation = _unit_impl,
doc = "Defines a software unit with design, implementation, and tests for S-CORE process compliance",
subrules = [subrule_gtest_report],
attrs = {
"unit_design": attr.label_list(
mandatory = True,
providers = [UnitDesignInfo],
doc = "Unit design artifacts (unit_design targets only)",
),
"implementation": attr.label_list(
mandatory = True,
providers = [[CcInfo], [rust_common.crate_info]],
aspects = [cc_dependencies_aspect],
doc = "Implementation targets (cc_library, cc_binary, rust_library, rust_binary, etc.)",
),
"scope": attr.string_list(
default = [],
doc = "Additional not explicitly named targets which are needed for the unit implementation",
),
"tests": attr.label_list(
mandatory = True,
cfg = "exec",
doc = "Test targets that verify the unit (cc_test, py_test, rust_test, etc.)",
),
},
attrs = _unit_attrs,
toolchains = use_cc_toolchain(),
fragments = ["cpp"],
)

# ============================================================================
Expand Down
3 changes: 0 additions & 3 deletions cpp/libclang/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ rust_binary(
data = [
"@llvm_toolchain_llvm//:libclang",
],
env = {
"LIBCLANG_PATH": "$(rootpath @llvm_toolchain_llvm//:libclang)",
},
visibility = ["//visibility:public"],
deps = [
"//cpp/libclang/src/utils",
Expand Down
9 changes: 4 additions & 5 deletions cpp/libclang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ cpp_parser(
extra_args = [
],
target = "//cpp/libclang/integration_test/cases/include_3rdparty",
tool = ":clang_rs_parser",
)
```

Expand All @@ -38,9 +37,9 @@ Where:
Expected result:

- Bazel creates parser output artifact:
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.fbs.bin`
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_class_diagram.fbs.bin`
- When `emit_debug_json = True`, the parser also writes:
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result/debug.json`
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_debug.json`

## Configure debug logging

Expand All @@ -55,6 +54,6 @@ Accepted values are: `error`, `warn`, `info`, `debug`, `trace`.
## Quick check (optional)

```bash
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.fbs.bin
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result/debug.json
ls -l bazel-bin/cpp/libclang/integration_test/cases/include_3rdparty/parser_class_diagram.fbs.bin
ls -l bazel-bin/cpp/libclang/integration_test/cases/include_3rdparty/parser_debug.json
```
Loading
Loading