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
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ jobs:
- os: ubuntu-24.04-arm
bazel: 8.x
mojo: 1.1.0
- os: macos-15
bazel: 8.x
mojo: 1.2.0
- os: ubuntu-latest
bazel: 8.x
mojo: 1.2.0
- os: ubuntu-24.04-arm
bazel: 8.x
mojo: 1.2.0
fail-fast: false
runs-on: ${{ matrix.os }}
env:
Expand Down
26 changes: 25 additions & 1 deletion mojo/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("//mojo:mojo_host_platform.bzl", "mojo_host_platform")
load("//mojo/private:mojo_gpu_toolchains_repository.bzl", "mojo_gpu_toolchains_repository")

_PLATFORMS = ["linux_aarch64", "linux_x86_64", "macos_arm64"]
_DEFAULT_VERSION = "1.2.0"
_DEFAULT_VERSION = "1.3.0.dev2026111601"

# Generated from official wheel metadata; update with tools/getshas.py.
_KNOWN_RELEASES = {
Expand Down Expand Up @@ -62,6 +62,25 @@ _KNOWN_RELEASES = {
sha256 = "3d631f7e798c11d0029c4a892ebae1f5742751fac0c526af74b45a8ae07c3acb",
),
},
# TODO: Remove when 1.3.0 releases
"1.3.0.dev2026111601": {
"linux_aarch64": struct(
url = "https://whl.modular.com/nightly/mojo-compiler/mojo_compiler-1.3.0.dev2026111601-py3-none-manylinux_2_34_aarch64.whl",
sha256 = "963141d97bed5d7afc01eea54dcfc12143b345f02ee8ed91a2f08254da24604c",
),
"linux_x86_64": struct(
url = "https://whl.modular.com/nightly/mojo-compiler/mojo_compiler-1.3.0.dev2026111601-py3-none-manylinux_2_34_x86_64.whl",
sha256 = "6a74919264b34f19b5674587e4ef9b8105241897800316f8aa40c545533fca0f",
),
"macos_arm64": struct(
url = "https://whl.modular.com/nightly/mojo-compiler/mojo_compiler-1.3.0.dev2026111601-py3-none-macosx_13_0_arm64.whl",
sha256 = "1a24cdd221274ab7c6aa397c329f36c958ab5dd86a575a343e7963908efbb19f",
),
"mojo_compiler_mojo_libs": struct(
url = "https://whl.modular.com/nightly/mojo-compiler-mojo-libs/mojo_compiler_mojo_libs-1.3.0.dev2026111601-py3-none-any.whl",
sha256 = "32b44ec09bbe092715bc7e0333bdcf089e24ed74196f1ac62ab489c7977049fc",
),
},
}
_PLATFORM_MAPPINGS = {
"linux_aarch64": "manylinux_2_34_aarch64",
Expand Down Expand Up @@ -145,11 +164,15 @@ toolchain(

rctx.file("BUILD.bazel", content = "\n".join(lines))

version = tuple([int(part) for part in rctx.attr.version.split(".")[:3]])
rctx.file("version.bzl", content = "MOJO_VERSION = {}\n".format(repr(version)))

_mojo_toolchain_hub = repository_rule(
implementation = _mojo_toolchain_hub_impl,
doc = "A convenience repository for registering all potential Mojo toolchains at once.",
attrs = {
"platforms": attr.string_list(doc = "The platforms to register Mojo toolchains for."),
"version": attr.string(doc = "The Mojo toolchain version, exposed as MOJO_VERSION in version.bzl.", mandatory = True),
},
)

Expand Down Expand Up @@ -186,6 +209,7 @@ def _setup_toolchains(root_module, rules_mojo_module):
_mojo_toolchain_hub(
name = "mojo_toolchains",
platforms = _PLATFORMS,
version = tags.version,
)

def _mojo_impl(mctx):
Expand Down
34 changes: 34 additions & 0 deletions mojo/mojo_library.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Compile Mojo files into a precompiled object (mojoc) that can be consumed by
other Mojo targets."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//mojo:providers.bzl", "MojoInfo")
Expand All @@ -12,6 +13,29 @@ def _format_include(arg):
def _format_root(arg):
return arg.dirname

def _format_root_import(ctx):
package, import_path = ctx
return ["-I", paths.normalize(paths.join(package, import_path))]

def _count_up_dirs(path):
# type: (string) -> int
# We make some assumptions about the import path for simplicity.
# The grammar is essentially ("." | ".." "/.."*) "/"?
segments = path.split("/")

if len(segments) == 1 and segments[0] == ".":
return 0

# We allow a trailing slash
if segments[-1] == "":
segments.pop()

for segment in segments:
if segment != "..":
fail("invalid import path: {}".format(path))

return len(segments)

def _mojo_library_implementation(ctx):
mojo_toolchain = ctx.toolchains["//:toolchain_type"].mojo_toolchain_info
build_env = getattr(ctx.toolchains["//:toolchain_type"], "build_env", {})
Expand Down Expand Up @@ -44,6 +68,14 @@ def _mojo_library_implementation(ctx):
if not file.dirname.startswith(root_directory):
args.add_all([file], map_each = _format_include)

up_dirs = _count_up_dirs(ctx.attr.import_path)
if up_dirs > 0:
args.add("-namespace")
package_parts = ctx.label.package.split("/")
namespace = ".".join(package_parts[len(package_parts) - up_dirs:])
args.add(namespace)
args.add_all([(ctx.label.package, ctx.attr.import_path)], map_each = _format_root_import)

output_group_kwargs = {}
precompile_outputs = [mojo_precmp_file]
if ctx.attr._export_fixits[BuildSettingInfo].value:
Expand Down Expand Up @@ -143,6 +175,8 @@ mojo_shared_library that transitively depends on it.
doc = """
Path relative to this package where it should be imported from.
Generally something like `.`, `..`, or `../..`.

Note: If anything other than ".", implies a `-namespace`.
""",
default = ".",
),
Expand Down
2 changes: 0 additions & 2 deletions tests/nested/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ load("//mojo:mojo_test.bzl", "mojo_test")
mojo_test(
name = "main",
srcs = ["main.mojo"],
# TODO: Mojo doesn't officially support this yet, re-enable when it does
tags = ["manual"],
visibility = ["//tests:__subpackages__"],
deps = ["//tests/nested/outer:inner"],
)
4 changes: 2 additions & 2 deletions tests/nested/outer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@mojo_toolchains//:version.bzl", "MOJO_VERSION")
load("//mojo:mojo_library.bzl", "mojo_library")

mojo_library(
Expand All @@ -6,7 +7,6 @@ mojo_library(
"__init__.mojo",
],
import_path = "..",
# TODO: Mojo doesn't officially support this yet, re-enable when it does
tags = ["manual"],
target_compatible_with = [] if MOJO_VERSION >= (1, 3, 0) else ["@platforms//:incompatible"], # Needs Mojo 1.3+
visibility = ["//tests:__subpackages__"],
)
Loading