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..ce4bf85 100644 --- a/mojo/private/toolchain.BUILD +++ b/mojo/private/toolchain.BUILD @@ -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"], ) 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..b67f18e 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, + 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, ), ), ] @@ -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, @@ -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"), ),