Skip to content

Commit 7aaca21

Browse files
authored
Support build_env, runtime_env, and extra_runfiles in mojo toolchain (#81)
Read optional build_env, runtime_env, and extra_runfiles from the toolchain provider. Merge build_env into compile action environments for mojo_library, mojo_binary, and mojo_test. Merge runtime_env into the binary/test runtime environment, and add extra_runfiles to runfiles.
1 parent 759634e commit 7aaca21

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

mojo/mojo_library.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def _format_include(arg):
99

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

1314
mojo_package = ctx.actions.declare_file(ctx.label.name + ".mojopkg")
1415
args = ctx.actions.args()
@@ -54,7 +55,7 @@ def _mojo_library_implementation(ctx):
5455
"MODULAR_CRASH_REPORTING_ENABLED": "false",
5556
"PATH": "/dev/null", # Avoid using the host's PATH
5657
"TEST_TMPDIR": ".", # Make sure any cache files are written to somewhere bazel will cleanup
57-
},
58+
} | build_env,
5859
use_default_shell_env = True,
5960
toolchain = "//:toolchain_type",
6061
execution_requirements = {

mojo/private/mojo_binary_test.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def _find_main(name, srcs, main):
9393
def _mojo_binary_test_implementation(ctx, *, shared_library = False):
9494
cc_toolchain = find_cpp_toolchain(ctx)
9595
mojo_toolchain = ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"].mojo_toolchain_info
96+
build_env = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "build_env", {})
97+
runtime_env_extra = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "runtime_env", {})
98+
extra_runfiles = getattr(ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"], "extra_runfiles", [])
9699
py_toolchain = ctx.toolchains[_PYTHON_TOOLCHAIN_TYPE]
97100

98101
object_file = ctx.actions.declare_file(ctx.label.name + ".lo")
@@ -154,7 +157,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
154157
"MODULAR_MOJO_MAX_LLD_PATH": mojo_toolchain.lld.path,
155158
"PATH": "/dev/null", # Avoid using the host's PATH
156159
"TEST_TMPDIR": ".",
157-
},
160+
} | build_env,
158161
use_default_shell_env = True,
159162
exec_group = "mojo_compile",
160163
toolchain = "//:toolchain_type",
@@ -200,6 +203,8 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
200203
transitive_runfiles = [
201204
ctx.runfiles(transitive_files = py_toolchain.py3_runtime.files),
202205
]
206+
if extra_runfiles:
207+
transitive_runfiles.append(ctx.runfiles(files = extra_runfiles))
203208
for target in data:
204209
transitive_runfiles.append(target[DefaultInfo].default_runfiles)
205210

@@ -248,6 +253,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
248253
"PYTHONPATH": python_path,
249254
"PYTHONSAFEPATH": "affirmative",
250255
}
256+
runtime_env.update(runtime_env_extra)
251257
for key, value in runtime_env.items():
252258
runtime_env[key] = ctx.expand_make_variables(
253259
"env",

0 commit comments

Comments
 (0)