Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ toolchain_type(
visibility = ["//visibility:public"],
)

toolchain_type(
name = "copts_toolchain_type",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "gpu_toolchain_type",
visibility = ["//visibility:public"],
Expand Down
8 changes: 8 additions & 0 deletions mojo/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ MojoToolchainInfo = provider(
},
)

MojoCoptsToolchainInfo = provider(
doc = "Provider holding additional compiler options for the Mojo compiler.",
fields = {
"copts": "Additional compiler options to pass to the Mojo compiler.",
"package_copts": "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
},
)

MojoGPUToolchainInfo = provider(
doc = "Provider holding information about the GPU being targeted by Mojo.",
fields = {
Expand Down
38 changes: 36 additions & 2 deletions mojo/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The Mojo compiler toolchain."""

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

def _mojo_toolchain_impl(ctx):
tool_files = []
Expand All @@ -10,6 +10,7 @@ def _mojo_toolchain_impl(ctx):
tool_files.append(dep[DefaultInfo].files)

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

copts_toolchain = ctx.toolchains["//:copts_toolchain_type"]
if copts_toolchain:
copts.extend(copts_toolchain.copts_toolchain_info.copts)
package_copts = copts_toolchain.copts_toolchain_info.package_copts

return [
platform_common.ToolchainInfo(
mojo_toolchain_info = MojoToolchainInfo(
all_tools = tool_files,
copts = copts,
package_copts = ctx.attr.package_copts,
package_copts = package_copts,
lld = ctx.executable.lld,
mojo = ctx.executable.mojo,
implicit_deps = ctx.attr.implicit_deps,
Expand Down Expand Up @@ -82,6 +88,34 @@ Defines the Mojo compiler toolchain.
""",
toolchains = [
config_common.toolchain_type("//:gpu_toolchain_type", mandatory = False),
config_common.toolchain_type("//:copts_toolchain_type", mandatory = False),
],
fragments = ["cpp", "apple"],
)

def _mojo_copts_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
copts_toolchain_info = MojoCoptsToolchainInfo(
copts = ctx.attr.copts,
package_copts = ctx.attr.package_copts,
),
),
]

mojo_copts_toolchain = rule(
implementation = _mojo_copts_toolchain_impl,
attrs = {
"copts": attr.string_list(
mandatory = True,
doc = "Additional compiler options to pass to the Mojo compiler.",
),
"package_copts": attr.string_list(
mandatory = True,
doc = "Additional compiler options to pass to the Mojo compiler when running 'mojo package'.",
),
},
doc = """\
Defines additional compiler options for the Mojo compiler.
""",
)