Skip to content

Commit fe6e042

Browse files
committed
img_tool: define explicit cross compiled img tool binaries for different exec platforms
1 parent 42a49c9 commit fe6e042

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

img_tool/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bazel_dep(name = "bazel_lib", version = "3.0.0-beta.1")
1212
bazel_dep(name = "bazel_skylib", version = "1.7.1")
1313
bazel_dep(name = "gazelle", version = "0.45.0")
1414
bazel_dep(name = "googleapis", version = "0.0.0-20251003-2193a2bf")
15+
bazel_dep(name = "platforms", version = "0.0.11")
1516
bazel_dep(name = "protobuf", version = "29.5")
1617
bazel_dep(name = "rules_go", version = "0.57.0")
1718
bazel_dep(name = "rules_proto", version = "7.1.0")

img_tool/cmd/img/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ go_binary(
3030
visibility = ["//visibility:public"],
3131
)
3232

33+
platform_tuples = [
34+
("linux", "amd64"),
35+
("linux", "arm64"),
36+
("darwin", "amd64"),
37+
("darwin", "arm64"),
38+
("windows", "amd64"),
39+
("windows", "arm64"),
40+
]
41+
42+
[
43+
go_binary(
44+
name = "img_{}_{}".format(os, arch),
45+
embed = [":img_lib"],
46+
goarch = arch,
47+
goos = os,
48+
pure = "on",
49+
visibility = ["//toolchain:__subpackages__"],
50+
)
51+
for os, arch in platform_tuples
52+
]
53+
3354
build_test(
3455
name = "test",
3556
targets = [":img"],

img_tool/toolchain/BUILD.bazel

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,29 @@ You need to register them explicitly if you want to build from source.
55
"""
66

77
load("@rules_img//img:image_toolchain.bzl", "TOOLCHAIN_TYPE", "image_toolchain")
8+
load(":def.bzl", "goarch_to_constraint", "goos_to_constraint", "platform_tuples")
89

910
package(
1011
default_visibility = ["//visibility:public"],
1112
)
1213

13-
image_toolchain(
14-
name = "image_toolchain",
15-
tool_exe = "//cmd/img",
16-
)
14+
[
15+
image_toolchain(
16+
name = "image_toolchain_{}_{}".format(os, arch),
17+
tool_exe = "//cmd/img:img_{}_{}".format(os, arch),
18+
)
19+
for os, arch in platform_tuples
20+
]
1721

18-
toolchain(
19-
name = "toolchain",
20-
toolchain = ":image_toolchain",
21-
toolchain_type = TOOLCHAIN_TYPE,
22-
)
22+
[
23+
toolchain(
24+
name = "toolchain_{}_{}".format(os, arch),
25+
exec_compatible_with = [
26+
goos_to_constraint(os),
27+
goarch_to_constraint(arch),
28+
],
29+
toolchain = ":image_toolchain_{}_{}".format(os, arch),
30+
toolchain_type = TOOLCHAIN_TYPE,
31+
)
32+
for os, arch in platform_tuples
33+
]

img_tool/toolchain/def.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Toolchain platform definitions and constraint mapping utilities.
2+
3+
This module provides utilities for mapping between Go platform identifiers
4+
(GOOS/GOARCH) and Bazel platform constraints.
5+
"""
6+
7+
# List of supported (OS, architecture) platform tuples
8+
platform_tuples = [
9+
("linux", "amd64"),
10+
("linux", "arm64"),
11+
("darwin", "amd64"),
12+
("darwin", "arm64"),
13+
("windows", "amd64"),
14+
("windows", "arm64"),
15+
]
16+
17+
def goos_to_constraint(goos):
18+
"""Converts a Go OS identifier to a Bazel platform constraint.
19+
20+
Args:
21+
goos: A string representing the Go OS identifier (e.g., "linux", "darwin", "windows")
22+
23+
Returns:
24+
A string representing the Bazel platform constraint label for the OS
25+
"""
26+
if goos == "linux":
27+
return "@platforms//os:linux"
28+
elif goos == "darwin":
29+
return "@platforms//os:macos"
30+
elif goos == "windows":
31+
return "@platforms//os:windows"
32+
else:
33+
fail("Unknown goos: {}".format(goos))
34+
35+
def goarch_to_constraint(goarch):
36+
"""Converts a Go architecture identifier to a Bazel platform constraint.
37+
38+
Args:
39+
goarch: A string representing the Go architecture identifier (e.g., "amd64", "arm64")
40+
41+
Returns:
42+
A string representing the Bazel platform constraint label for the CPU architecture
43+
"""
44+
if goarch == "amd64":
45+
return "@platforms//cpu:x86_64"
46+
elif goarch == "arm64":
47+
return "@platforms//cpu:aarch64"
48+
else:
49+
fail("Unknown goarch: {}".format(goarch))

0 commit comments

Comments
 (0)