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
8 changes: 3 additions & 5 deletions mojo/mojo_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions mojo/private/mojo_binary_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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")

Expand Down
72 changes: 72 additions & 0 deletions mojo/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,75 @@ def is_exec_config(ctx):
return True

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.

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:]

label = _package_relative_label(ctx, 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)
return
fail("input not found in additional_compiler_inputs")
else:
args.add(copt)
108 changes: 108 additions & 0 deletions tests/expand_make_vars/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
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.

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_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"],
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",
],
)
Empty file.
2 changes: 2 additions & 0 deletions tests/expand_make_vars/main.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main():
pass
Loading