From e0950071b62fbee381df609002885d9d0db9af1c Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Sun, 15 Nov 2026 19:52:56 -0500 Subject: [PATCH 1/3] feat: Support path mapping for files passed through copts Previously this would not work as we used ctx.expand_location, which returned a string, and thus could not path map. Now we do a bit of magic to extract the label from the opt, map it to an input, and then pass that as a file to `args` so it can be path mapped. --- mojo/mojo_library.bzl | 8 +-- mojo/private/mojo_binary_test.bzl | 9 ++- mojo/private/utils.bzl | 59 +++++++++++++++++ tests/expand_make_vars/BUILD.bazel | 98 ++++++++++++++++++++++++++++ tests/expand_make_vars/__init__.mojo | 0 tests/expand_make_vars/main.mojo | 2 + 6 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 tests/expand_make_vars/BUILD.bazel create mode 100644 tests/expand_make_vars/__init__.mojo create mode 100644 tests/expand_make_vars/main.mojo diff --git a/mojo/mojo_library.bzl b/mojo/mojo_library.bzl index 3a50ad1..6accb5e 100644 --- a/mojo/mojo_library.bzl +++ b/mojo/mojo_library.bzl @@ -4,7 +4,7 @@ other Mojo targets.""" load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("//mojo:providers.bzl", "MojoInfo") -load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config") +load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config") def _format_include(arg): return ["-I", arg.dirname] @@ -25,10 +25,8 @@ def _mojo_library_implementation(ctx): args.add_all(mojo_toolchain.precompile_copts) if not is_exec_config(ctx): args.add_all(ctx.attr._mojo_precompile_copts[BuildSettingInfo].value) - args.add_all([ - ctx.expand_location(copt, targets = ctx.attr.additional_compiler_inputs) - for copt in ctx.attr.copts - ]) + for copt in ctx.attr.copts: + handle_copt(ctx, copt, args) # NOTE: cc deps are not passed to 'mojo precompile', they are only # propagated to the compile and link actions of the depending binary. diff --git a/mojo/private/mojo_binary_test.bzl b/mojo/private/mojo_binary_test.bzl index af29da0..b9ebfbb 100644 --- a/mojo/private/mojo_binary_test.bzl +++ b/mojo/private/mojo_binary_test.bzl @@ -9,7 +9,7 @@ load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("@rules_python//python:py_info.bzl", "PyInfo") load("//mojo:providers.bzl", "MojoInfo") load(":transitions.bzl", "python_version_transition") -load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config") +load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "handle_copt", "is_exec_config") _PYTHON_TOOLCHAIN_TYPE = "@rules_python//python:toolchain_type" _ATTRS = { @@ -131,10 +131,9 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False): # Ignore default mojo flags for exec built binaries if not is_exec_config(ctx): args.add_all(ctx.attr._mojo_copts[BuildSettingInfo].value) - args.add_all([ - ctx.expand_location(copt, targets = ctx.attr.additional_compiler_inputs) - for copt in ctx.attr.copts - ]) + for copt in ctx.attr.copts: + handle_copt(ctx, copt, args) + if ctx.attr.enable_assertions: args.add("-D", "ASSERT=all") diff --git a/mojo/private/utils.bzl b/mojo/private/utils.bzl index 816ed79..0c57025 100644 --- a/mojo/private/utils.bzl +++ b/mojo/private/utils.bzl @@ -62,3 +62,62 @@ def is_exec_config(ctx): return True return False + +def _extract_label(ctx, copt): + # type: (ctx, string) -> tuple[string, Label] + """Split a copt into a format string and a label. + + Take something like `--foo=$(location :bar)` and get `:bar` as a label + This is a minimum working version, there are likely cases that don't work, + but there are currently no uses for those extra cases. + """ + + start = -1 + labelstart = -1 + labelend = -1 + end = -1 + + # Not sure if we should even support all of these. + for var in ["execpath", "rootpath", "rlocationpath", "location"]: + start = copt.find("$(" + var) + if start != -1: + # Add the "$(" and a space at the end + labelstart = start + 2 + len(var) + 1 + break + if start == -1: + fail("expected one of $(execpath), $(rootpath), $(rlocationpath), or $(location)") + end = start + copt[start:].find(")") + if end == -1: + fail("expected closing )") + labelend = end + label_string = copt[labelstart:labelend] + format_string = copt[:start] + "%s" + copt[end + 1:] + + # TODO: 8.x support + label = ctx.package_relative_label(label_string) + + return format_string, label + +def handle_copt(ctx, copt, args): + # type: (ctx, string, Args) -> None + """Handles make variable expansion in copts + + This is primarily necessary to support path mapping + when labels are passed via a copt. + + Args: + ctx: the context + copt: the copt + args: Args object to add to + """ + if "$(" in copt: + format_string, label = _extract_label(ctx, copt) + for input in ctx.attr.additional_compiler_inputs: + if label != input.label: + continue + files = input.files.to_list() + if len(files) != 1: + fail("expected exactly one file") + args.add(files[0], format = format_string) + else: + args.add(copt) diff --git a/tests/expand_make_vars/BUILD.bazel b/tests/expand_make_vars/BUILD.bazel new file mode 100644 index 0000000..81b28f8 --- /dev/null +++ b/tests/expand_make_vars/BUILD.bazel @@ -0,0 +1,98 @@ +load("//mojo:mojo_library.bzl", "mojo_library") +load("//mojo:mojo_test.bzl", "mojo_test") + +# These tests ensure that we can pass `$(execpath :foo)` and similar to +# `copts` and have them correctly expand when path mapping. + +# Note that for these tests, we use `--bitcode-libs` with arbitrary files. +# This is the primary use case for this feature, but also thankfully +# `--bitcode-libs` seems to not care about the contents of the file, +# only that it can open it. + +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +genrule( + name = "gen_bc", + outs = ["input.bc"], + cmd = "touch $(OUTS)", +) + +# Also randomly sprinkle in various builtins (location, etc.) to show they work. + +mojo_test( + name = "single_arg_relative_path", + srcs = ["main.mojo"], + additional_compiler_inputs = [":input.bc"], + copts = ["--bitcode-libs=$(location :input.bc)"], +) + +mojo_test( + name = "two_args_relative_path", + srcs = ["main.mojo"], + additional_compiler_inputs = [":input.bc"], + copts = [ + "--bitcode-libs", + "$(execpath :input.bc)", + ], +) + +mojo_test( + name = "single_arg_absolute_path", + srcs = ["main.mojo"], + additional_compiler_inputs = ["//tests/expand_make_vars:input.bc"], + copts = ["--bitcode-libs=$(rlocationpath //tests/expand_make_vars:input.bc)"], +) + +mojo_test( + name = "two_args_absolute_path", + srcs = ["main.mojo"], + additional_compiler_inputs = ["//tests/expand_make_vars:input.bc"], + copts = [ + "--bitcode-libs", + "$(rootpath //tests/expand_make_vars:input.bc)", + ], +) + +mojo_library( + name = "single_arg_relative_path_lib", + srcs = ["__init__.mojo"], + additional_compiler_inputs = [":input.bc"], + copts = ["--bitcode-libs=$(rootpath :input.bc)"], +) + +mojo_library( + name = "two_args_relative_path_lib", + srcs = ["__init__.mojo"], + additional_compiler_inputs = [":input.bc"], + copts = [ + "--bitcode-libs", + "$(rlocationpath :input.bc)", + ], +) + +mojo_library( + name = "single_arg_absolute_path_lib", + srcs = ["__init__.mojo"], + additional_compiler_inputs = ["//tests/expand_make_vars:input.bc"], + copts = ["--bitcode-libs=$(execpath //tests/expand_make_vars:input.bc)"], +) + +mojo_library( + name = "two_args_absolute_path_lib", + srcs = ["__init__.mojo"], + additional_compiler_inputs = ["//tests/expand_make_vars:input.bc"], + copts = [ + "--bitcode-libs", + "$(location //tests/expand_make_vars:input.bc)", + ], +) + +build_test( + name = "lib_build_tests", + targets = [ + "single_arg_relative_path_lib", + "two_args_relative_path_lib", + "single_arg_absolute_path_lib", + "two_args_absolute_path_lib", + ], +) diff --git a/tests/expand_make_vars/__init__.mojo b/tests/expand_make_vars/__init__.mojo new file mode 100644 index 0000000..e69de29 diff --git a/tests/expand_make_vars/main.mojo b/tests/expand_make_vars/main.mojo new file mode 100644 index 0000000..336e825 --- /dev/null +++ b/tests/expand_make_vars/main.mojo @@ -0,0 +1,2 @@ +def main(): + pass From 8ab2fa159e2400bc138aa30b6cb9fd785f19c862 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Sun, 15 Nov 2026 19:53:49 -0500 Subject: [PATCH 2/3] move load() --- tests/expand_make_vars/BUILD.bazel | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/expand_make_vars/BUILD.bazel b/tests/expand_make_vars/BUILD.bazel index 81b28f8..974bfe2 100644 --- a/tests/expand_make_vars/BUILD.bazel +++ b/tests/expand_make_vars/BUILD.bazel @@ -1,3 +1,4 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//mojo:mojo_library.bzl", "mojo_library") load("//mojo:mojo_test.bzl", "mojo_test") @@ -9,8 +10,6 @@ load("//mojo:mojo_test.bzl", "mojo_test") # `--bitcode-libs` seems to not care about the contents of the file, # only that it can open it. -load("@bazel_skylib//rules:build_test.bzl", "build_test") - genrule( name = "gen_bc", outs = ["input.bc"], From c7dc072480862b05bbd1251ad7718b65a1eabc06 Mon Sep 17 00:00:00 2001 From: Alex Trotta Date: Sun, 15 Nov 2026 20:22:07 -0500 Subject: [PATCH 3/3] fix 8.x support --- mojo/private/utils.bzl | 17 +++++++++++++++-- tests/expand_make_vars/BUILD.bazel | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/mojo/private/utils.bzl b/mojo/private/utils.bzl index 0c57025..cfc6879 100644 --- a/mojo/private/utils.bzl +++ b/mojo/private/utils.bzl @@ -63,6 +63,18 @@ def is_exec_config(ctx): return False +def _package_relative_label(ctx, label): + # type: (ctx, string) -> Label + """Emulate ctx.package_relative_label in Bazel 8 + + Can drop this and replace with ctx.package_relative_label once we drop 8.x support. + """ + + if label.startswith(":"): + # Relative path + label = "//" + ctx.label.package + label + return Label(label) + def _extract_label(ctx, copt): # type: (ctx, string) -> tuple[string, Label] """Split a copt into a format string and a label. @@ -93,8 +105,7 @@ def _extract_label(ctx, copt): label_string = copt[labelstart:labelend] format_string = copt[:start] + "%s" + copt[end + 1:] - # TODO: 8.x support - label = ctx.package_relative_label(label_string) + label = _package_relative_label(ctx, label_string) return format_string, label @@ -119,5 +130,7 @@ def handle_copt(ctx, copt, args): if len(files) != 1: fail("expected exactly one file") args.add(files[0], format = format_string) + return + fail("input not found in additional_compiler_inputs") else: args.add(copt) diff --git a/tests/expand_make_vars/BUILD.bazel b/tests/expand_make_vars/BUILD.bazel index 974bfe2..1c198e8 100644 --- a/tests/expand_make_vars/BUILD.bazel +++ b/tests/expand_make_vars/BUILD.bazel @@ -52,6 +52,17 @@ mojo_test( ], ) +mojo_test( + name = "verbose_repo_path", + srcs = ["main.mojo"], + additional_compiler_inputs = ["@rules_mojo//tests/expand_make_vars:input.bc"], + copts = [ + "--bitcode-libs", + # Some arbitrary repo file + "$(rootpath @rules_mojo//tests/expand_make_vars:input.bc)", + ], +) + mojo_library( name = "single_arg_relative_path_lib", srcs = ["__init__.mojo"],