Skip to content

Commit 81dc6d1

Browse files
authored
bazel: Split existing rules into reusable sub-rules (#540)
1 parent 216153d commit 81dc6d1

File tree

12 files changed

+547
-3476
lines changed

12 files changed

+547
-3476
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.1.0
1+
8.5.1

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ jobs:
177177
python3 -m pip install -r requirements_dev.txt
178178
sudo apt install cmake ninja-build graphviz
179179
- name: Setup bazel (for lobster-gtest)
180-
uses: jwlawson/actions-setup-bazel@v2
180+
uses: bazel-contrib/setup-bazel@0.15.0
181181
with:
182-
bazel-version: '7.1.0'
182+
bazelisk-cache: true
183+
disk-cache: ${{ github.workflow }}
184+
repository-cache: true
183185
- name: Run integration tests
184186
run: |
185187
make integration-tests

MODULE.bazel.lock

Lines changed: 291 additions & 3278 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/BUILD

Whitespace-only changes.

bazel/private/BUILD

Whitespace-only changes.

bazel/private/lobster_gtest.bzl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load("//bazel:providers.bzl", "LobsterProvider")
2+
3+
def _lobster_gtest_subrule_impl(ctx, tests, _lobster_gtest):
4+
lobster_gtest_trace = ctx.actions.declare_file(ctx.label.name + ".lobster")
5+
6+
args = ctx.actions.args()
7+
args.add_all(["--out", lobster_gtest_trace.path])
8+
args.add(".")
9+
10+
ctx.actions.run(
11+
executable = _lobster_gtest,
12+
inputs = tests,
13+
outputs = [lobster_gtest_trace],
14+
arguments = [args],
15+
progress_message = "lobster-gtest {}".format(lobster_gtest_trace.path),
16+
)
17+
18+
return [
19+
lobster_gtest_trace,
20+
LobsterProvider(lobster_input = {lobster_gtest_trace.basename: lobster_gtest_trace.path}),
21+
]
22+
23+
subrule_lobster_gtest = subrule(
24+
implementation = _lobster_gtest_subrule_impl,
25+
attrs = {
26+
"_lobster_gtest": attr.label(
27+
default = "//:lobster-gtest",
28+
executable = True,
29+
cfg = "exec",
30+
),
31+
},
32+
)
33+
34+
def _lobster_gtest(ctx):
35+
lobster_gtest_trace, lobster_provider = subrule_lobster_gtest(ctx.files.tests)
36+
return [DefaultInfo(files = depset([lobster_gtest_trace])), lobster_provider]
37+
38+
lobster_gtest = rule(
39+
implementation = _lobster_gtest,
40+
attrs = {
41+
"tests": attr.label_list(
42+
allow_empty = False,
43+
mandatory = True,
44+
),
45+
},
46+
subrules = [subrule_lobster_gtest],
47+
)

bazel/private/lobster_raw.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//bazel:providers.bzl", "LobsterProvider")
2+
3+
def _lobster_raw_impl(ctx):
4+
name = "{}.lobster".format(ctx.attr.name)
5+
6+
return [
7+
DefaultInfo(files = depset([ctx.file.src])),
8+
LobsterProvider(lobster_input = {name: ctx.file.src.path}),
9+
]
10+
11+
lobster_raw = rule(
12+
implementation = _lobster_raw_impl,
13+
attrs = {
14+
"src": attr.label(
15+
allow_single_file = [".lobster"],
16+
mandatory = True,
17+
),
18+
},
19+
)

bazel/private/lobster_test.bzl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
load("//bazel:providers.bzl", "LobsterProvider")
2+
3+
def _lobster_report_subrule_impl(ctx, inputs, lobster_config, _lobster_report):
4+
lobster_report = ctx.actions.declare_file(ctx.label.name + "_report.json")
5+
6+
args = ctx.actions.args()
7+
args.add_all(["--lobster-config", lobster_config.path])
8+
args.add_all(["--out", lobster_report.path])
9+
10+
ctx.actions.run(
11+
executable = _lobster_report,
12+
inputs = depset(inputs + [lobster_config]),
13+
outputs = [lobster_report],
14+
arguments = [args],
15+
progress_message = "lobster-report {}".format(lobster_report.path),
16+
)
17+
18+
return lobster_report
19+
20+
subrule_lobster_report = subrule(
21+
implementation = _lobster_report_subrule_impl,
22+
attrs = {
23+
"_lobster_report": attr.label(
24+
default = "//:lobster-report",
25+
executable = True,
26+
cfg = "exec",
27+
),
28+
},
29+
)
30+
31+
def _lobster_html_report_subrule_impl(ctx, lobster_report, _lobster_html_report):
32+
lobster_html_report = ctx.actions.declare_file("{}_report.html".format(ctx.label.name))
33+
34+
args = ctx.actions.args()
35+
args.add(lobster_report.path)
36+
args.add_all(["--out", lobster_html_report.path])
37+
38+
ctx.actions.run(
39+
executable = _lobster_html_report,
40+
inputs = [lobster_report],
41+
outputs = [lobster_html_report],
42+
arguments = [args],
43+
progress_message = "lobster-html-report {}".format(lobster_html_report.path),
44+
)
45+
46+
return lobster_html_report
47+
48+
subrule_lobster_html_report = subrule(
49+
implementation = _lobster_html_report_subrule_impl,
50+
attrs = {
51+
"_lobster_html_report": attr.label(
52+
default = "//:lobster-html-report",
53+
executable = True,
54+
cfg = "exec",
55+
),
56+
},
57+
)
58+
59+
def _lobster_test_impl(ctx):
60+
lobster_config_substitutions = {}
61+
for input_config in ctx.attr.inputs:
62+
lobster_config_substitutions.update(input_config[LobsterProvider].lobster_input)
63+
64+
# We have to adjust the paths from the config, with the paths used by Bazel
65+
lobster_config = ctx.actions.declare_file("{}_expanded_lobster.conf".format(ctx.attr.name))
66+
ctx.actions.expand_template(
67+
template = ctx.file.config,
68+
output = lobster_config,
69+
substitutions = lobster_config_substitutions,
70+
)
71+
72+
lobster_report = subrule_lobster_report(ctx.files.inputs, lobster_config)
73+
lobster_html_report = subrule_lobster_html_report(lobster_report)
74+
75+
test_executable = ctx.actions.declare_file("{}_lobster_ci_test_executable".format(ctx.attr.name))
76+
command = "set -o pipefail; {} {}".format(ctx.executable._lobster_ci_report.short_path, lobster_report.short_path)
77+
78+
ctx.actions.write(
79+
output = test_executable,
80+
content = command,
81+
)
82+
83+
return [DefaultInfo(
84+
runfiles = ctx.runfiles(
85+
files = [
86+
ctx.executable._lobster_ci_report,
87+
lobster_report,
88+
],
89+
).merge(ctx.attr._lobster_ci_report[DefaultInfo].default_runfiles),
90+
files = depset([lobster_report, lobster_html_report]),
91+
executable = test_executable,
92+
)]
93+
94+
lobster_test = rule(
95+
implementation = _lobster_test_impl,
96+
attrs = {
97+
"inputs": attr.label_list(
98+
providers = [LobsterProvider],
99+
mandatory = True,
100+
),
101+
"config": attr.label(
102+
mandatory = True,
103+
allow_single_file = True,
104+
),
105+
"_lobster_ci_report": attr.label(
106+
default = "//:lobster-ci-report",
107+
executable = True,
108+
cfg = "exec",
109+
),
110+
},
111+
test = True,
112+
subrules = [subrule_lobster_report, subrule_lobster_html_report],
113+
)

bazel/private/lobster_trlc.bzl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
load("@trlc//:trlc.bzl", "TrlcProviderInfo")
2+
load("//bazel:providers.bzl", "LobsterProvider")
3+
4+
def _lobster_trlc_subrule_impl(ctx, requirements, config, _lobster_trlc):
5+
lobster_trlc_trace = ctx.actions.declare_file(ctx.label.name + ".lobster")
6+
7+
args = ctx.actions.args()
8+
args.add_all(["--config", config.path])
9+
args.add_all(["--out", lobster_trlc_trace.path])
10+
11+
ctx.actions.run(
12+
executable = _lobster_trlc,
13+
inputs = requirements + [config],
14+
outputs = [lobster_trlc_trace],
15+
arguments = [args],
16+
progress_message = "lobster-trlc {}".format(lobster_trlc_trace.path),
17+
)
18+
19+
return [
20+
lobster_trlc_trace,
21+
LobsterProvider(lobster_input = {lobster_trlc_trace.basename: lobster_trlc_trace.path}),
22+
]
23+
24+
subrule_lobster_trlc = subrule(
25+
implementation = _lobster_trlc_subrule_impl,
26+
attrs = {
27+
"_lobster_trlc": attr.label(
28+
default = "//:lobster-trlc",
29+
executable = True,
30+
cfg = "exec",
31+
),
32+
},
33+
)
34+
35+
def _lobster_trlc_impl(ctx):
36+
lobster_trlc_trace, lobster_provider = subrule_lobster_trlc(ctx.files.requirements, ctx.file.config)
37+
return [DefaultInfo(files = depset([lobster_trlc_trace])), lobster_provider]
38+
39+
lobster_trlc = rule(
40+
implementation = _lobster_trlc_impl,
41+
attrs = {
42+
"requirements": attr.label_list(
43+
doc = "Targets that define requirements in the form of TRLC files.",
44+
providers = [TrlcProviderInfo],
45+
mandatory = True,
46+
),
47+
"config": attr.label(
48+
allow_single_file = True,
49+
mandatory = True,
50+
),
51+
},
52+
subrules = [subrule_lobster_trlc],
53+
)

bazel/providers.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LobsterProvider = provider(
2+
fields = {
3+
"lobster_input": "ABC",
4+
},
5+
)

0 commit comments

Comments
 (0)