11"""The Mojo compiler toolchain."""
22
33load ("@rules_cc//cc/common:cc_info.bzl" , "CcInfo" )
4- load ("//mojo:providers.bzl" , "MojoInfo" , "MojoToolchainInfo" )
4+ load ("//mojo:providers.bzl" , "MojoCoptsToolchainInfo" , " MojoInfo" , "MojoToolchainInfo" )
55
66def _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