Skip to content

Commit f26608b

Browse files
authored
Refactor repo rules into more cohesive files (#11)
1 parent 97022fa commit f26608b

File tree

4 files changed

+117
-109
lines changed

4 files changed

+117
-109
lines changed

MODULE.bazel.lock

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

impl/declare_toolchain.bzl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
2+
load("//impl:config.bzl", "get_config_from_env_vars")
23
# load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
34

45
def _declare_toolchain(name, visibility, cxx_std_lib, vendor, sysroot, all_tools, target_triple):
@@ -106,6 +107,9 @@ def _declare_toolchain(name, visibility, cxx_std_lib, vendor, sysroot, all_tools
106107

107108
declare_toolchain = macro(
108109
attrs = {
110+
# configurable = False is required because macros wrap configurable attrs in
111+
# a select(...) which cant be used when inlining the labels and strings in
112+
# the toolchain declaraltions.
109113
"cxx_std_lib": attr.string(mandatory = True, configurable = False),
110114
"vendor": attr.string(mandatory = True, configurable = False),
111115
"sysroot": attr.label(mandatory = True, configurable = False),
@@ -114,3 +118,60 @@ declare_toolchain = macro(
114118
},
115119
implementation = _declare_toolchain,
116120
)
121+
122+
def _eager_declare_toolchain_impl(rctx):
123+
"""Eagerly declare the toolchain(...) to determine which registered toolchain is valid for the current platform."""
124+
config = get_config_from_env_vars(rctx)
125+
126+
rctx.file(
127+
"BUILD",
128+
"""
129+
load("@toolchains_cc//impl:declare_toolchain.bzl", "declare_toolchain")
130+
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
131+
declare_toolchain(
132+
name = "{original_name}",
133+
cxx_std_lib = "{cxx_std_lib}",
134+
vendor = "{vendor}",
135+
target_triple = "{target_triple}",
136+
sysroot = "@@{bins_repo_name}//:{original_name}_bins.sysroot",
137+
all_tools = "@@{bins_repo_name}//:{original_name}_bins.all_tools",
138+
visibility = ["//visibility:public"],
139+
)
140+
141+
# TODO: currently cant declare this in the macro because this rule creates
142+
# a target that doesnt following the naming rules of macros.
143+
cc_toolchain(
144+
name = "{original_name}_cc_toolchain",
145+
args = [
146+
":{original_name}-no-canonical-prefixes",
147+
":{original_name}_target_triple",
148+
":{original_name}-sysroot-arg",
149+
":{original_name}_use_llvm_linker",
150+
":{original_name}_cxx_std_lib",
151+
],
152+
enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
153+
known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
154+
tool_map = "@@{bins_repo_name}//:{original_name}_bins.all_tools",
155+
visibility = ["//visibility:public"],
156+
)
157+
""".format(
158+
original_name = rctx.original_name,
159+
cxx_std_lib = config["cxx_std_lib"],
160+
vendor = config["vendor"],
161+
target_triple = config["triple"],
162+
bins_repo_name = rctx.name + "_bins",
163+
),
164+
)
165+
166+
eager_declare_toolchain = repository_rule(
167+
implementation = _eager_declare_toolchain_impl,
168+
attrs = {
169+
"toolchain_name": attr.string(
170+
mandatory = True,
171+
doc = "The name of the toolchain, used for registration.",
172+
),
173+
"_build_tpl": attr.label(
174+
default = "@toolchains_cc//:toolchain.BUILD.tpl",
175+
),
176+
},
177+
)

impl/declare_tools.bzl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ load("@bazel_skylib//rules/directory:directory.bzl", "directory")
22
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")
33
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
44
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
5+
load("//impl:alpine.bzl", "extract_alpine")
6+
load("//impl:config.bzl", "get_config_from_env_vars", "repro_dump")
7+
load("//impl:ubuntu.bzl", "extract_ubuntu")
58

69
def _declare_tools(name, visibility, all_files):
7-
print("Declaring tools for toolchain: {}".format(name))
810
native.filegroup(
911
name = "{}.all_files".format(name),
1012
srcs = all_files,
@@ -114,3 +116,47 @@ declare_tools = macro(
114116
},
115117
implementation = _declare_tools,
116118
)
119+
120+
def _lazy_download_bins_impl(rctx):
121+
"""Lazily downloads only the toolchain binaries for the configured platform."""
122+
config = get_config_from_env_vars(rctx)
123+
repro_dump(rctx, config)
124+
125+
# TODO: not a huge fan of vendor == "unknown" but it's how ubuntu distrubtions are packaged
126+
if config["vendor"] == "unknown":
127+
extract_ubuntu(rctx, config)
128+
elif config["vendor"] == "alpine":
129+
extract_alpine(rctx, config)
130+
else:
131+
fail("(toolchains_cc.bzl bug) Unknown vendor: %s" % config["vendor"])
132+
133+
rctx.download_and_extract(
134+
url = "https://github.com/reutermj/toolchains_cc.bzl/releases/download/binaries/llvm-19.1.7-linux-x86_64.tar.xz",
135+
)
136+
137+
rctx.file(
138+
"BUILD",
139+
"""
140+
load("@toolchains_cc//impl:declare_tools.bzl", "declare_tools")
141+
declare_tools(
142+
name = "{original_name}",
143+
all_files = glob(["**"]),
144+
visibility = ["//visibility:public"],
145+
)
146+
""".format(
147+
original_name = rctx.original_name,
148+
),
149+
)
150+
151+
lazy_download_bins = repository_rule(
152+
implementation = _lazy_download_bins_impl,
153+
attrs = {
154+
"toolchain_name": attr.string(
155+
mandatory = True,
156+
doc = "The name of the toolchain, used for registration.",
157+
),
158+
"_build_tpl": attr.label(
159+
default = "@toolchains_cc//:bins.BUILD.tpl",
160+
),
161+
},
162+
)

toolchains_cc.bzl

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,7 @@
11
"""Defines the repo rules and module extension for managing C++ toolchains across different platforms."""
22

3-
load("//impl:alpine.bzl", "extract_alpine")
4-
load("//impl:config.bzl", "get_config_from_env_vars", "repro_dump")
5-
load("//impl:ubuntu.bzl", "extract_ubuntu")
6-
7-
def _lazy_download_bins_impl(rctx):
8-
"""Lazily downloads only the toolchain binaries for the configured platform."""
9-
config = get_config_from_env_vars(rctx)
10-
repro_dump(rctx, config)
11-
12-
# TODO: not a huge fan of vendor == "unknown" but it's how ubuntu distrubtions are packaged
13-
if config["vendor"] == "unknown":
14-
extract_ubuntu(rctx, config)
15-
elif config["vendor"] == "alpine":
16-
extract_alpine(rctx, config)
17-
else:
18-
fail("(toolchains_cc.bzl bug) Unknown vendor: %s" % config["vendor"])
19-
20-
rctx.download_and_extract(
21-
url = "https://github.com/reutermj/toolchains_cc.bzl/releases/download/binaries/llvm-19.1.7-linux-x86_64.tar.xz",
22-
)
23-
24-
rctx.file(
25-
"BUILD",
26-
"""
27-
load("@toolchains_cc//impl:declare_tools.bzl", "declare_tools")
28-
declare_tools(
29-
name = "{original_name}",
30-
all_files = glob(["**"]),
31-
visibility = ["//visibility:public"],
32-
)
33-
""".format(
34-
original_name = rctx.original_name,
35-
),
36-
)
37-
38-
def _eager_declare_toolchain_impl(rctx):
39-
"""Eagerly declare the toolchain(...) to determine which registered toolchain is valid for the current platform."""
40-
config = get_config_from_env_vars(rctx)
41-
42-
rctx.file(
43-
"BUILD",
44-
"""
45-
load("@toolchains_cc//impl:declare_toolchain.bzl", "declare_toolchain")
46-
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
47-
declare_toolchain(
48-
name = "{original_name}",
49-
cxx_std_lib = "{cxx_std_lib}",
50-
vendor = "{vendor}",
51-
target_triple = "{target_triple}",
52-
sysroot = "@@{bins_repo_name}//:{original_name}_bins.sysroot",
53-
all_tools = "@@{bins_repo_name}//:{original_name}_bins.all_tools",
54-
visibility = ["//visibility:public"],
55-
)
56-
57-
# TODO: currently cant declare this in the macro because this rule creates
58-
# a target that doesnt following the naming rules of macros.
59-
cc_toolchain(
60-
name = "{original_name}_cc_toolchain",
61-
args = [
62-
":{original_name}-no-canonical-prefixes",
63-
":{original_name}_target_triple",
64-
":{original_name}-sysroot-arg",
65-
":{original_name}_use_llvm_linker",
66-
":{original_name}_cxx_std_lib",
67-
],
68-
enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
69-
known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
70-
tool_map = "@@{bins_repo_name}//:{original_name}_bins.all_tools",
71-
visibility = ["//visibility:public"],
72-
)
73-
""".format(
74-
original_name = rctx.original_name,
75-
cxx_std_lib = config["cxx_std_lib"],
76-
vendor = config["vendor"],
77-
target_triple = config["triple"],
78-
bins_repo_name = rctx.name + "_bins",
79-
),
80-
)
81-
82-
_lazy_download_bins = repository_rule(
83-
implementation = _lazy_download_bins_impl,
84-
attrs = {
85-
"toolchain_name": attr.string(
86-
mandatory = True,
87-
doc = "The name of the toolchain, used for registration.",
88-
),
89-
"_build_tpl": attr.label(
90-
default = "@toolchains_cc//:bins.BUILD.tpl",
91-
),
92-
},
93-
)
94-
95-
_eager_declare_toolchain = repository_rule(
96-
implementation = _eager_declare_toolchain_impl,
97-
attrs = {
98-
"toolchain_name": attr.string(
99-
mandatory = True,
100-
doc = "The name of the toolchain, used for registration.",
101-
),
102-
"_build_tpl": attr.label(
103-
default = "@toolchains_cc//:toolchain.BUILD.tpl",
104-
),
105-
},
106-
)
3+
load("//impl:declare_toolchain.bzl", "eager_declare_toolchain")
4+
load("//impl:declare_tools.bzl", "lazy_download_bins")
1075

1086
def _cxx_toolchains(module_ctx):
1097
for mod in module_ctx.modules:
@@ -145,11 +43,11 @@ def _cxx_toolchains(module_ctx):
14543
if declared_toolchain.name == "toolchains_cc_default_toolchain":
14644
toolchain_name = "toolchains_cc"
14745

148-
_eager_declare_toolchain(
46+
eager_declare_toolchain(
14947
name = declared_toolchain.name,
15048
toolchain_name = toolchain_name,
15149
)
152-
_lazy_download_bins(
50+
lazy_download_bins(
15351
name = declared_toolchain.name + "_bins",
15452
toolchain_name = toolchain_name,
15553
)

0 commit comments

Comments
 (0)