Skip to content

Commit 8b8686a

Browse files
committed
feat: Add BPF triple constraint mapping
Add support for tier 3 targets bpfeb-unknown-none and bpfel-unknown-none (see https://github.com/rust-lang/rust/blob/f5e2df7/src/doc/rustc/src/platform-support.md?plain=1#L311-L312).
1 parent e2cc813 commit 8b8686a

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

rust/platform/BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package(default_visibility = ["//visibility:public"])
66
declare_config_settings()
77

88
# WASI Preview version constraint settings
9+
#
10+
# TODO(https://github.com/bazelbuild/platforms/pull/123): Replace with upstream.
911
constraint_setting(
1012
name = "wasi_version",
1113
default_constraint_value = ":wasi_preview_1",
@@ -21,6 +23,23 @@ constraint_value(
2123
constraint_setting = ":wasi_version",
2224
)
2325

26+
# BPF execution constraints
27+
#
28+
# TODO(https://github.com/bazelbuild/platforms/pull/131): Replace with upstream.
29+
constraint_setting(
30+
name = "bpf_arch",
31+
)
32+
33+
constraint_value(
34+
name = "cpu_bpfeb",
35+
constraint_setting = ":bpf_arch",
36+
)
37+
38+
constraint_value(
39+
name = "cpu_bpfel",
40+
constraint_setting = ":bpf_arch",
41+
)
42+
2443
package_group(
2544
name = "function_transition_allowlist",
2645
packages = [

rust/platform/platform.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ def declare_config_settings():
7272
deprecation = "Use `@rules_rust//rust/platform:armv7-unknown-linux-gnueabi` instead.",
7373
)
7474

75+
native.platform(
76+
name = "bpfeb",
77+
constraint_values = [
78+
str(Label("//rust/platform:cpu_bpfeb")),
79+
"@platforms//os:none",
80+
],
81+
)
82+
83+
native.platform(
84+
name = "bpfel",
85+
constraint_values = [
86+
str(Label("//rust/platform:cpu_bpfel")),
87+
"@platforms//os:none",
88+
],
89+
)
90+
7591
# Add alias for wasm to maintain backwards compatibility.
7692
native.alias(
7793
name = "wasm32-wasi",

rust/platform/triple_mappings.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ SUPPORTED_T2_PLATFORM_TRIPLES = {
7575

7676
_T3_PLATFORM_TRIPLES = {
7777
"aarch64-unknown-nto-qnx710": _support(std = True, host_tools = False),
78+
"bpfeb-unknown-none": _support(std = False, host_tools = False),
79+
"bpfel-unknown-none": _support(std = False, host_tools = False),
7880
"wasm64-unknown-unknown": _support(std = False, host_tools = False),
7981
}
8082

@@ -416,6 +418,16 @@ def triple_to_constraint_set(target_triple):
416418
Returns:
417419
list: A list of constraints (each represented by a list of strings)
418420
"""
421+
if target_triple == "bpfeb-unknown-none":
422+
return [
423+
str(Label("//rust/platform:cpu_bpfeb")),
424+
"@platforms//os:none",
425+
]
426+
if target_triple == "bpfel-unknown-none":
427+
return [
428+
str(Label("//rust/platform:cpu_bpfel")),
429+
"@platforms//os:none",
430+
]
419431
if target_triple == "wasm32-wasi":
420432
return [
421433
"@platforms//cpu:wasm32",

test/unit/platform_triple/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
load(":bpf_platform_test.bzl", "bpf_platform_test_suite")
12
load(":platform_triple_test.bzl", "platform_triple_test_suite")
23
load(":wasi_platform_test.bzl", "wasi_platform_test_suite")
34

5+
bpf_platform_test_suite(
6+
name = "bpf_platform_test_suite",
7+
)
8+
49
platform_triple_test_suite(
510
name = "platform_triple_test_suite",
611
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Tests for BPF platform constraint mappings"""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4+
load("//rust/platform:triple_mappings.bzl", "triple_to_constraint_set")
5+
6+
def _bpf_platform_constraints_test_impl(ctx):
7+
env = unittest.begin(ctx)
8+
9+
cpu_bpfeb = str(Label("//rust/platform:cpu_bpfeb"))
10+
cpu_bpfel = str(Label("//rust/platform:cpu_bpfel"))
11+
12+
bpfeb_constraints = triple_to_constraint_set("bpfeb-unknown-none")
13+
asserts.equals(
14+
env,
15+
[
16+
cpu_bpfeb,
17+
"@platforms//os:none",
18+
],
19+
bpfeb_constraints,
20+
"bpfeb-unknown-none should map to the BPF big-endian CPU and 'none' OS constraints",
21+
)
22+
23+
bpfel_constraints = triple_to_constraint_set("bpfel-unknown-none")
24+
asserts.equals(
25+
env,
26+
[
27+
cpu_bpfel,
28+
"@platforms//os:none",
29+
],
30+
bpfel_constraints,
31+
"bpfel-unknown-none should map to the BPF little-endian CPU and 'none' OS constraints",
32+
)
33+
34+
return unittest.end(env)
35+
36+
bpf_platform_constraints_test = unittest.make(_bpf_platform_constraints_test_impl)
37+
38+
def bpf_platform_test_suite(name, **kwargs):
39+
"""Define a test suite for BPF platform constraint mappings.
40+
41+
Args:
42+
name (str): The name of the test suite.
43+
**kwargs (dict): Additional keyword arguments for the test_suite.
44+
"""
45+
bpf_platform_constraints_test(
46+
name = "bpf_platform_constraints_test",
47+
)
48+
49+
native.test_suite(
50+
name = name,
51+
tests = [
52+
":bpf_platform_constraints_test",
53+
],
54+
**kwargs
55+
)

test/unit/platform_triple/platform_triple_test.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ def _construct_known_triples_test_impl(ctx):
125125
_assert_parts(env, triple("thumbv7em-none-eabi"), "thumbv7em", None, "none", "eabi")
126126
_assert_parts(env, triple("thumbv8m.main-none-eabi"), "thumbv8m.main", None, "none", "eabi")
127127

128+
# BPF targets
129+
_assert_parts(env, triple("bpfeb-unknown-none"), "bpfeb", "unknown", "none", None)
130+
_assert_parts(env, triple("bpfel-unknown-none"), "bpfel", "unknown", "none", None)
131+
128132
# Test all WASM32 targets
129133
_assert_parts(env, triple("wasm32-unknown-unknown"), "wasm32", "unknown", "unknown", None)
130134
_assert_parts(env, triple("wasm32-unknown-emscripten"), "wasm32", "unknown", "emscripten", None)

0 commit comments

Comments
 (0)