From 85e73cc6bec10c317ecc7b4af39957075d5f50b1 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Fri, 27 Nov 2026 02:22:43 -0500 Subject: [PATCH 1/2] feat: Improve environment variable support and split precompilation out more BREAKING CHANGE: Replaces `all_tools` in the mojo toolchain with a few more specific variables. Also changes the plumbing for `runtime_extra_env` (now `runtime_env`) and `extra_runfiles`. The goal here is twofold: 1. We plan to start using environment variables a bit more to configure Mojo within Bazel (outside of Bazel, we generally try to avoid them to make things simpler) 2. We improve the cache-hit rate of precompilation by removing some action inputs. Things like compilation backends should not be used by these actions, so we omit them. Adds several more fields to the toolchain info: - `build_env`: Was plumbed through on the side of the toolchain previously. Now only applies to compilation. - `build_tools`: Tools only used when _compiling_ mojo (on top of `precompile_tools`). - `precompile_env`: Environment to set when precompiling or compiling. - `precompile_tools`: Tools used when precompiling or compiling. - `runtime_env`: Renamed from `runtime_extra_env`, re-plumbed - `extra_runfiles`: Re-plumbed --- mojo/mojo_library.bzl | 5 ++-- mojo/private/mojo_binary_test.bzl | 13 ++++---- mojo/private/toolchain.BUILD | 3 +- mojo/providers.bzl | 7 ++++- mojo/toolchain.bzl | 50 ++++++++++++++++++++++++++----- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/mojo/mojo_library.bzl b/mojo/mojo_library.bzl index 276ce52..d19b56c 100644 --- a/mojo/mojo_library.bzl +++ b/mojo/mojo_library.bzl @@ -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() @@ -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", @@ -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 = { diff --git a/mojo/private/mojo_binary_test.bzl b/mojo/private/mojo_binary_test.bzl index b9ebfbb..06bba5c 100644 --- a/mojo/private/mojo_binary_test.bzl +++ b/mojo/private/mojo_binary_test.bzl @@ -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") @@ -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], @@ -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", @@ -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) @@ -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", diff --git a/mojo/private/toolchain.BUILD b/mojo/private/toolchain.BUILD index 63fd247..8224e8d 100644 --- a/mojo/private/toolchain.BUILD +++ b/mojo/private/toolchain.BUILD @@ -66,12 +66,13 @@ mojo_import( mojo_toolchain( name = "mojo_toolchain", - extra_tools = [lib[1] for lib in _COMPILER_LIBRARIES], + build_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"], ) diff --git a/mojo/providers.bzl b/mojo/providers.bzl index bf71813..d3cf5d9 100644 --- a/mojo/providers.bzl +++ b/mojo/providers.bzl @@ -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.", }, ) diff --git a/mojo/toolchain.bzl b/mojo/toolchain.bzl index 3b85405..cf6c944 100644 --- a/mojo/toolchain.bzl +++ b/mojo/toolchain.bzl @@ -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) @@ -29,12 +34,17 @@ def _mojo_toolchain_impl(ctx): return [ platform_common.ToolchainInfo( mojo_toolchain_info = MojoToolchainInfo( - all_tools = tool_files, copts = copts, + build_env = ctx.attr.build_env, + build_tools = 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, ), ), ] @@ -46,16 +56,31 @@ mojo_toolchain = rule( mandatory = False, doc = "Additional compiler options to pass to the Mojo compiler.", ), + "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, @@ -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"), ), From ce2c85aef8fed8eb4a9ba85ea0b8cbb3b7111474 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Mon, 30 Nov 2026 06:57:28 -0500 Subject: [PATCH 2/2] fix toolchain things --- mojo/private/toolchain.BUILD | 1 - mojo/toolchain.bzl | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mojo/private/toolchain.BUILD b/mojo/private/toolchain.BUILD index 8224e8d..ce4bf85 100644 --- a/mojo/private/toolchain.BUILD +++ b/mojo/private/toolchain.BUILD @@ -66,7 +66,6 @@ mojo_import( mojo_toolchain( name = "mojo_toolchain", - build_tools = [lib[1] for lib in _COMPILER_LIBRARIES], implicit_deps = [ name for name, _ in _RUNTIME_LIBRARIES diff --git a/mojo/toolchain.bzl b/mojo/toolchain.bzl index cf6c944..b67f18e 100644 --- a/mojo/toolchain.bzl +++ b/mojo/toolchain.bzl @@ -34,9 +34,9 @@ def _mojo_toolchain_impl(ctx): return [ platform_common.ToolchainInfo( mojo_toolchain_info = MojoToolchainInfo( - copts = copts, - build_env = ctx.attr.build_env, - build_tools = build_files, + 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, @@ -54,7 +54,7 @@ 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,