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
7 changes: 5 additions & 2 deletions mojo/mojo_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ def _mojo_import_impl(ctx):
return [
DefaultInfo(files = depset(mojo_deps, transitive = [transitive_mojodeps])),
MojoInfo(
import_paths = depset([pkg.dirname for pkg in mojo_deps], transitive = [import_paths]),
mojodeps = depset([pkg for pkg in mojo_deps], transitive = [transitive_mojodeps]),
import_paths = depset(
[struct(package = pkg, import_path = ".") for pkg in mojo_deps],
transitive = [import_paths],
),
mojodeps = depset(mojo_deps, transitive = [transitive_mojodeps]),
),
]

Expand Down
12 changes: 7 additions & 5 deletions mojo/mojo_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ other Mojo targets."""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//mojo:providers.bzl", "MojoInfo")
load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "is_exec_config")
load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config")

def _format_include(arg):
return ["-I", arg.dirname]
Expand Down Expand Up @@ -42,7 +42,7 @@ def _mojo_library_implementation(ctx):
output_group_kwargs["mojo_fixits"] = depset([fixits_file])
args.add("--experimental-export-fixit", fixits_file)

file_args.add_all(transitive_mojodeps, map_each = _format_include)
file_args.add_all(import_paths, map_each = format_import)
file_args.add(root_directory)
ctx.actions.run(
executable = mojo_toolchain.mojo,
Expand All @@ -68,15 +68,17 @@ def _mojo_library_implementation(ctx):
for target in ctx.attr.data:
transitive_runfiles.append(target[DefaultInfo].default_runfiles)

import_path = mojo_precmp_file.dirname + "/" + ctx.attr.import_path

return [
DefaultInfo(
files = depset([mojo_precmp_file]),
runfiles = ctx.runfiles(ctx.files.data).merge_all(transitive_runfiles),
),
MojoInfo(
import_paths = depset([import_path], transitive = [import_paths]),
# Passed through as a File + string combo so that it can be path mapped later.
import_paths = depset(
[struct(package = mojo_precmp_file, import_path = ctx.attr.import_path)],
transitive = [import_paths],
),
mojodeps = depset([mojo_precmp_file], transitive = [transitive_mojodeps]),
),
OutputGroupInfo(**output_group_kwargs),
Expand Down
7 changes: 2 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", "is_exec_config")
load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config")

_PYTHON_TOOLCHAIN_TYPE = "@rules_python//python:toolchain_type"
_ATTRS = {
Expand Down Expand Up @@ -93,9 +93,6 @@ def _find_main(name, srcs, main):
def _format_include(arg):
return ["-I", arg.dirname]

def _add_include(arg):
return ["-I", 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
Expand All @@ -121,7 +118,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):

all_deps = ctx.attr.deps + mojo_toolchain.implicit_deps + ([ctx.attr._link_extra_lib] if ctx.attr._link_extra_lib else [])
transitive_includes, transitive_mojodeps = collect_mojoinfo(all_deps)
args.add_all(transitive_includes, map_each = _add_include)
args.add_all(transitive_includes, map_each = format_import)

# NOTE: Argument order:
# 1. Basic functional arguments
Expand Down
8 changes: 7 additions & 1 deletion mojo/private/utils.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Helpers internal to rules_mojo."""

load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//mojo:providers.bzl", "MojoInfo")

MOJO_EXTENSIONS = ("mojo",)
Expand All @@ -12,7 +13,9 @@ def collect_mojoinfo(deps):
deps: A list of dependencies to collect MojoInfo from.

Returns:
A single MojoInfo object with the combined data.
A tuple (imports, mojodeps) of two depsets combining every MojoInfo in
deps: `imports` holds struct(package, import_path) entries for building
-I flags, `mojodeps` holds the precompiled mojo Files for action inputs.
"""
import_paths = []
mojodeps = []
Expand All @@ -24,6 +27,9 @@ def collect_mojoinfo(deps):

return depset(transitive = import_paths), depset(transitive = mojodeps)

def format_import(dep):
return ["-I", paths.normalize(paths.join(dep.package.dirname, dep.import_path))]

def is_exec_config(ctx):
"""Determines whether the current configuration is an exec configuration.

Expand Down
2 changes: 1 addition & 1 deletion mojo/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
MojoInfo = provider(
doc = "Information about how to build a Mojo target.",
fields = {
"import_paths": "Directories that should be passed with -I to mojo",
"import_paths": "Depset of struct(package = File, import_path = str). Each element is a mojoc file paired with its relative import path.",
"mojodeps": "The precompiled mojo files required by the target",
},
)
Expand Down
Loading