Skip to content

Commit 759c0d2

Browse files
authored
Add copts_toolchain_type (#76)
This allows you to override copts / package_copts without overriding the entire mojo_toolchain (so you can still get the default compiler etc). This feels very heavy handed for this simple configuration but I haven't seen a better looking option
1 parent 909fe03 commit 759c0d2

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ toolchain_type(
1818
visibility = ["//visibility:public"],
1919
)
2020

21+
toolchain_type(
22+
name = "copts_toolchain_type",
23+
visibility = ["//visibility:public"],
24+
)
25+
2126
toolchain_type(
2227
name = "gpu_toolchain_type",
2328
visibility = ["//visibility:public"],

mojo/providers.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ MojoToolchainInfo = provider(
2020
},
2121
)
2222

23+
MojoCoptsToolchainInfo = provider(
24+
doc = "Provider holding additional compiler options for the Mojo compiler.",
25+
fields = {
26+
"copts": "Additional compiler options to pass to the Mojo compiler.",
27+
"package_copts": "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
28+
},
29+
)
30+
2331
MojoGPUToolchainInfo = provider(
2432
doc = "Provider holding information about the GPU being targeted by Mojo.",
2533
fields = {

mojo/toolchain.bzl

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""The Mojo compiler toolchain."""
22

33
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
4-
load("//mojo:providers.bzl", "MojoInfo", "MojoToolchainInfo")
4+
load("//mojo:providers.bzl", "MojoCoptsToolchainInfo", "MojoInfo", "MojoToolchainInfo")
55

66
def _mojo_toolchain_impl(ctx):
77
tool_files = []
@@ -10,6 +10,7 @@ def _mojo_toolchain_impl(ctx):
1010
tool_files.append(dep[DefaultInfo].files)
1111

1212
copts = list(ctx.attr.copts)
13+
package_copts = list(ctx.attr.package_copts)
1314
gpu_toolchain = ctx.toolchains["//:gpu_toolchain_type"]
1415
if gpu_toolchain:
1516
copts.append("--target-accelerator=" + gpu_toolchain.mojo_gpu_toolchain_info.target_accelerator)
@@ -22,12 +23,17 @@ def _mojo_toolchain_impl(ctx):
2223
if min_os:
2324
copts.append("--target-triple=arm64-apple-macosx{}".format(min_os))
2425

26+
copts_toolchain = ctx.toolchains["//:copts_toolchain_type"]
27+
if copts_toolchain:
28+
copts.extend(copts_toolchain.copts_toolchain_info.copts)
29+
package_copts = copts_toolchain.copts_toolchain_info.package_copts
30+
2531
return [
2632
platform_common.ToolchainInfo(
2733
mojo_toolchain_info = MojoToolchainInfo(
2834
all_tools = tool_files,
2935
copts = copts,
30-
package_copts = ctx.attr.package_copts,
36+
package_copts = package_copts,
3137
lld = ctx.executable.lld,
3238
mojo = ctx.executable.mojo,
3339
implicit_deps = ctx.attr.implicit_deps,
@@ -82,6 +88,34 @@ Defines the Mojo compiler toolchain.
8288
""",
8389
toolchains = [
8490
config_common.toolchain_type("//:gpu_toolchain_type", mandatory = False),
91+
config_common.toolchain_type("//:copts_toolchain_type", mandatory = False),
8592
],
8693
fragments = ["cpp", "apple"],
8794
)
95+
96+
def _mojo_copts_toolchain_impl(ctx):
97+
return [
98+
platform_common.ToolchainInfo(
99+
copts_toolchain_info = MojoCoptsToolchainInfo(
100+
copts = ctx.attr.copts,
101+
package_copts = ctx.attr.package_copts,
102+
),
103+
),
104+
]
105+
106+
mojo_copts_toolchain = rule(
107+
implementation = _mojo_copts_toolchain_impl,
108+
attrs = {
109+
"copts": attr.string_list(
110+
mandatory = True,
111+
doc = "Additional compiler options to pass to the Mojo compiler.",
112+
),
113+
"package_copts": attr.string_list(
114+
mandatory = True,
115+
doc = "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
116+
),
117+
},
118+
doc = """\
119+
Defines additional compiler options for the Mojo compiler.
120+
""",
121+
)

0 commit comments

Comments
 (0)