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: 2 additions & 3 deletions mojo/mojo_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def _count_up_dirs(path):

def _mojo_library_implementation(ctx):
mojo_toolchain = ctx.toolchains["//:toolchain_type"].mojo_toolchain_info
build_env = getattr(ctx.toolchains["//:toolchain_type"], "build_env", {})

mojo_precmp_file = ctx.actions.declare_file(ctx.label.name + ".mojoc")
args = ctx.actions.args()
Expand Down Expand Up @@ -89,7 +88,7 @@ def _mojo_library_implementation(ctx):
ctx.actions.run(
executable = mojo_toolchain.mojo,
inputs = depset(ctx.files.srcs + ctx.files.additional_compiler_inputs, transitive = [transitive_mojodeps]),
tools = mojo_toolchain.all_tools,
tools = mojo_toolchain.precompile_tools,
outputs = precompile_outputs,
arguments = [args, file_args],
mnemonic = "MojoPrecompile",
Expand All @@ -98,7 +97,7 @@ def _mojo_library_implementation(ctx):
"MODULAR_CRASH_REPORTING_ENABLED": "false",
"PATH": "/dev/null", # Avoid using the host's PATH
"TEST_TMPDIR": ".", # Make sure any cache files are written to somewhere bazel will cleanup
} | build_env,
} | mojo_toolchain.build_env,
use_default_shell_env = True,
toolchain = "//:toolchain_type",
execution_requirements = {
Expand Down
13 changes: 5 additions & 8 deletions mojo/private/mojo_binary_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ def _format_include(arg):
def _mojo_binary_test_implementation(ctx, *, shared_library = False):
cc_toolchain = find_cpp_toolchain(ctx)
mojo_toolchain = ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"].mojo_toolchain_info
build_env = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "build_env", {})
runtime_env_extra = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "runtime_env", {})
extra_runfiles = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "extra_runfiles", [])
py_toolchain = ctx.toolchains[_PYTHON_TOOLCHAIN_TYPE]

object_file = ctx.actions.declare_file(ctx.label.name + ".lo")
Expand Down Expand Up @@ -147,7 +144,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):

ctx.actions.run(
executable = mojo_toolchain.mojo,
tools = mojo_toolchain.all_tools,
tools = mojo_toolchain.build_tools,
inputs = depset(ctx.files.srcs + ctx.files.additional_compiler_inputs, transitive = [transitive_mojodeps]),
outputs = compile_outputs,
arguments = [args],
Expand All @@ -160,7 +157,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
"MODULAR_MOJO_MAX_LLD_PATH": "/dev/null", # Make sure this fails if accessed
"PATH": "/dev/null", # Avoid using the host's PATH
"TEST_TMPDIR": ".",
} | build_env,
} | mojo_toolchain.build_env,
use_default_shell_env = True,
exec_group = "mojo_compile",
toolchain = "//:toolchain_type",
Expand Down Expand Up @@ -209,8 +206,8 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
transitive_runfiles = [
ctx.runfiles(transitive_files = py_toolchain.py3_runtime.files),
]
if extra_runfiles:
transitive_runfiles.append(ctx.runfiles(files = extra_runfiles))
if mojo_toolchain.extra_runfiles:
transitive_runfiles.append(ctx.runfiles(files = mojo_toolchain.extra_runfiles))
for target in data:
transitive_runfiles.append(target[DefaultInfo].default_runfiles)

Expand Down Expand Up @@ -258,7 +255,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
"PYTHONPATH": python_path,
"PYTHONSAFEPATH": "affirmative",
}
runtime_env.update(runtime_env_extra)
runtime_env.update(mojo_toolchain.runtime_env)
for key, value in runtime_env.items():
runtime_env[key] = ctx.expand_make_variables(
"env",
Expand Down
2 changes: 1 addition & 1 deletion mojo/private/toolchain.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ mojo_import(

mojo_toolchain(
name = "mojo_toolchain",
extra_tools = [lib[1] for lib in _COMPILER_LIBRARIES],
implicit_deps = [
name
for name, _ in _RUNTIME_LIBRARIES
] + ([":std"] if "{INCLUDE_MOJOPKGS}" else []),
lld = "bin/lld",
mojo = "bin/mojo",
precompile_tools = [lib[1] for lib in _COMPILER_LIBRARIES],
visibility = ["//visibility:public"],
)
7 changes: 6 additions & 1 deletion mojo/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ MojoInfo = provider(
MojoToolchainInfo = provider(
doc = "Provider holding the tools for building Mojo targets.",
fields = {
"all_tools": "All the files that must be available in actions in order for the toolchain to work.",
"copts": "Additional compiler options to pass to the Mojo compiler.",
"build_env": "Dict of environment variables to set during compilation. Additive on precompile_env.",
"build_tools": "Extra files to make available to the compiler during compilation. Additive on precompile_tools.",
"precompile_copts": "Additional compiler options to pass to the Mojo compiler when running 'mojo precompile'.",
"precompile_env": "Dict of environment variables to set during precompilation.",
"precompile_tools": "Extra files to make available to the compiler during precompilation.",
"lld": "The lld compiler executable to link with",
"mojo": "The mojo compiler executable to build with",
"implicit_deps": "Implicit dependencies that every target should depend on, providing either CcInfo, or MojoInfo",
"runtime_env": "Dict of environment variables to set during runtime.",
"extra_runfiles": "Extra files to make available during runtime.",
},
)

Expand Down
54 changes: 45 additions & 9 deletions mojo/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//mojo:providers.bzl", "MojoCoptsToolchainInfo", "MojoInfo", "MojoToolchainInfo")

def _mojo_toolchain_impl(ctx):
tool_files = []
for dep in ctx.attr.implicit_deps + ctx.attr.extra_tools + [ctx.attr.lld, ctx.attr.mojo]:
tool_files.append(dep[DefaultInfo].default_runfiles.files)
tool_files.append(dep[DefaultInfo].files)
precompile_files = []
for dep in ctx.attr.precompile_tools + [ctx.attr.mojo]:
precompile_files.append(dep[DefaultInfo].default_runfiles.files)
precompile_files.append(dep[DefaultInfo].files)

build_files = []
for dep in ctx.attr.build_tools + [ctx.attr.lld]:
build_files.append(dep[DefaultInfo].default_runfiles.files)
build_files.append(dep[DefaultInfo].files)

copts = list(ctx.attr.copts)
precompile_copts = list(ctx.attr.precompile_copts)
Expand All @@ -29,12 +34,17 @@ def _mojo_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
mojo_toolchain_info = MojoToolchainInfo(
all_tools = tool_files,
copts = copts,
copts = precompile_copts + copts,
build_env = ctx.attr.precompile_env | ctx.attr.build_env,
build_tools = precompile_files + build_files,
precompile_copts = precompile_copts,
precompile_env = ctx.attr.precompile_env,
precompile_tools = precompile_files,
lld = ctx.executable.lld,
mojo = ctx.executable.mojo,
implicit_deps = ctx.attr.implicit_deps,
runtime_env = ctx.attr.runtime_env,
extra_runfiles = ctx.attr.extra_runfiles,
),
),
]
Expand All @@ -44,18 +54,33 @@ mojo_toolchain = rule(
attrs = {
"copts": attr.string_list(
mandatory = False,
doc = "Additional compiler options to pass to the Mojo compiler.",
doc = "Additional compiler options to pass to the Mojo compiler. Additive on precompile_copts.",
),
"build_env": attr.string_dict(
mandatory = False,
doc = "Dict of environment variables to set during compilation. Additive on precompile_env.",
),
"build_tools": attr.label_list(
providers = [DefaultInfo],
allow_files = True,
mandatory = False,
cfg = "exec",
doc = "Additional tools to make available to Mojo compile actions. Additive on precompile_tools.",
),
"precompile_copts": attr.string_list(
mandatory = False,
doc = "Additional compiler options to pass to the Mojo compiler when running 'mojo precompile'.",
),
"extra_tools": attr.label_list(
"precompile_env": attr.string_dict(
mandatory = False,
doc = "Dict of environment variables to set during precompilation.",
),
"precompile_tools": attr.label_list(
providers = [DefaultInfo],
allow_files = True,
mandatory = False,
cfg = "exec",
doc = "Additional tools to make available to every Mojo action.",
doc = "Additional tools to make available to Mojo precompile actions.",
),
"lld": attr.label(
allow_files = True,
Expand All @@ -77,6 +102,17 @@ mojo_toolchain = rule(
cfg = "target",
doc = "Implicit dependencies that every target should depend on, providing either CcInfo, or MojoInfo.",
),
"runtime_env": attr.string_dict(
mandatory = False,
doc = "Dict of environment variables to set on compiled Mojo programs at runtime.",
),
"extra_runfiles": attr.label_list(
providers = [DefaultInfo],
allow_files = True,
mandatory = False,
cfg = "target",
doc = "Additional files to make available to compiled Mojo programs at runtime.",
),
"_macos_constraint": attr.label(
default = Label("@platforms//os:macos"),
),
Expand Down
Loading