Skip to content

Commit 2e6b629

Browse files
committed
Füge Lizenzberichterstattung und SBOM-Generierung für example_app hinzu
1 parent 6a28816 commit 2e6b629

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

apps/example_app/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
22
load("//tools:c_copts.bzl", "C_COPTS")
3+
load("//tools/license:license_report.bzl", "generate_sbom", "license_report", "license_summary")
34

45
cc_binary(
56
name = "example_app",
@@ -12,3 +13,18 @@ cc_binary(
1213
"//apps/example_lib",
1314
],
1415
)
16+
17+
license_report(
18+
name = "example_app_licenses",
19+
deps = [":example_app"],
20+
)
21+
22+
license_summary(
23+
name = "example_app_license_summary",
24+
license_json = ":example_app_licenses",
25+
)
26+
27+
generate_sbom(
28+
name = "example_app_sbom",
29+
deps = [":example_app"],
30+
)

tools/license/README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,42 @@ license_manifest(
5858
)
5959
```
6060

61+
## Example: Using the tools for example_app
62+
63+
The template already defines license/SBOM targets in `apps/example_app/BUILD`:
64+
65+
```starlark
66+
license_report(
67+
name = "example_app_licenses",
68+
deps = [":example_app"],
69+
)
70+
71+
license_summary(
72+
name = "example_app_license_summary",
73+
license_json = ":example_app_licenses",
74+
)
75+
76+
generate_sbom(
77+
name = "example_app_sbom",
78+
deps = [":example_app"],
79+
)
80+
```
81+
82+
Build them with:
83+
84+
- `bazel build //apps/example_app:example_app_licenses`
85+
- `bazel build //apps/example_app:example_app_license_summary`
86+
- `bazel build //apps/example_app:example_app_sbom`
87+
6188
## API Compatibility
6289

6390
Our implementation maintains full API compatibility with `@rules_license`:
6491

65-
| Original | Our Implementation | Status |
66-
|----------|-------------------|---------|
67-
| `@rules_license//rules_gathering:generate_sbom.bzl` | `generate_sbom()` | ✅ Compatible |
92+
| Original | Our Implementation | Status |
93+
| -------------------------------------------------------------- | -------------------- | ------------ |
94+
| `@rules_license//rules_gathering:generate_sbom.bzl` | `generate_sbom()` | ✅ Compatible |
6895
| `@rules_license//rules_gathering:generate_sbom.bzl manifest()` | `license_manifest()` | ✅ Compatible |
69-
| `@rules_license//rules:gather_licenses_info.bzl` | `license_report()` | ✅ Compatible |
96+
| `@rules_license//rules:gather_licenses_info.bzl` | `license_report()` | ✅ Compatible |
7097

7198
## License Information
7299

tools/license/license_report.bzl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ visibility and integration capabilities.
1919
load(
2020
"@rules_license//rules:gather_licenses_info.bzl",
2121
"gather_licenses_info",
22-
"write_licenses_info",
22+
"licenses_info_to_json",
2323
)
2424
load(
2525
"@rules_license//rules_gathering:gather_metadata.bzl",
@@ -31,15 +31,39 @@ load(
3131
"TransitiveLicensesInfo",
3232
)
3333

34+
def _safe_write_licenses_info(ctx, deps, json_out):
35+
"""Writes license info as JSON, skipping targets without license data.
36+
37+
Some targets yield TransitiveLicensesInfo without target_under_license
38+
(when no licenses are found). The upstream write_licenses_info does not
39+
guard against that, so we skip such entries here.
40+
"""
41+
licenses_json = []
42+
licenses_files = []
43+
for dep in deps:
44+
if TransitiveLicensesInfo in dep:
45+
transitive_licenses_info = dep[TransitiveLicensesInfo]
46+
if not hasattr(transitive_licenses_info, "target_under_license"):
47+
continue
48+
lic_info, lic_files = licenses_info_to_json(transitive_licenses_info)
49+
licenses_json.extend(lic_info)
50+
licenses_files.extend(lic_files)
51+
52+
ctx.actions.write(
53+
output = json_out,
54+
content = "[\n%s\n]\n" % ",\n".join(licenses_json),
55+
)
56+
return licenses_files
57+
3458
def _license_report_impl(ctx):
3559
"""Implementation for license_report rule.
3660
3761
Collects license information from dependencies and writes it as JSON.
3862
Uses the official rules_license gather_licenses_info aspect.
3963
"""
4064

41-
# Use the official write_licenses_info function from rules_license
42-
write_licenses_info(ctx, ctx.attr.deps, ctx.outputs.out)
65+
# Use a safe writer to avoid failures when no licenses are present
66+
_safe_write_licenses_info(ctx, ctx.attr.deps, ctx.outputs.out)
4367

4468
return [DefaultInfo(files = depset([ctx.outputs.out]))]
4569

@@ -281,7 +305,7 @@ def _enhanced_license_report_impl(ctx):
281305

282306
# Create standard JSON report using the licenses_deps with gather_licenses_info aspect
283307
json_file = ctx.actions.declare_file(ctx.label.name + ".json")
284-
write_licenses_info(ctx, ctx.attr.licenses_deps, json_file)
308+
_safe_write_licenses_info(ctx, ctx.attr.licenses_deps, json_file)
285309

286310
# Create enhanced metadata report using the metadata_deps with gather_metadata_info aspect
287311
metadata_file = ctx.actions.declare_file(ctx.label.name + "_metadata.json")

0 commit comments

Comments
 (0)