Skip to content

//mojo:mojo.bzl

Build files should import these rules from @rules_mojo//mojo:defs.bzl.

mojo_binary

mojo_binary(name, deps, srcs, compile_flags, compile_string_flags, defines, includes, runtime_data)

Creates an executable.

Example:

mojo_binary(
name = "hello",
srcs = ["hello.mojo"],
)

Attributes

NameDescription
nameName, required.

A unique name for this target.
depsList of labels, optional, defaults to [].

The dependencies for this target.

Use ll_library targets here. Other targets won’t work.
srcsList of labels, optional, defaults to [].

Compilable source files for this target.

Allowed file extensions: [".mojo", ".🔥"].

Place headers in the hdrs attribute.
compile_flagsList of strings, optional, defaults to [].

Flags for the compiler.

Pass a list of strings here. For instance ["-O3", "-std=c++20"].

Split flag pairs -Xclang -somearg into separate flags ["-Xclang", "-somearg"].

Unavailable to downstream targets.
compile_string_flagsList of labels, optional, defaults to [].

Flags for the compiler in the form of string_flags.

Splits the values of each string_flag along colons like so:

load(“@bazel_skylib//rules:common_settings
.bzl”, “string_flag”)

string_flag(
name = “myflags”,
build_setting_default = “a:b:c
”,
)

mojo_library(
# …
# Equivalent to compile_flags = ["a", "b", "c"]
compile_string_flags = [":myflags
"],
)


Useful for externally configurable build attributes, such as generated flags from Nix environments.
definesList of strings, optional, defaults to [].

Defines for this target.

Pass a list of strings here. For instance ["MYDEFINE_1", "MYDEFINE_2"].

Unavailable to downstream targets.
includesList of strings, optional, defaults to [].

Include paths, relative to the target workspace.

Uses -iquote.

Useful if you need custom include prefix stripping for dynamic paths, for instance the ones generated by bzlmod. Instead of compile_flags = ["-iquoteexternal/mydep.someversion/include"], use includes = ["include"] to add the path to the workspace automatically.

Expands paths starting with $(GENERATED) to the workspace location in the GENDIR path.

Unavailable to downstream targets.
runtime_dataList of labels, optional, defaults to [].

Extra files made available during test time.

mojo_library

mojo_library(name, deps, srcs, compile_flags, compile_string_flags, defines, includes)

Creates a Mojo package.

Example:

mojo_library(
name = "mypackage",
srcs = [
"__init__.mojo",
"my_package.mojo",
],
)

Attributes

NameDescription
nameName, required.

A unique name for this target.
depsList of labels, optional, defaults to [].

The dependencies for this target.

Use ll_library targets here. Other targets won’t work.
srcsList of labels, optional, defaults to [].

Compilable source files for this target.

Allowed file extensions: [".mojo", ".🔥"].

Place headers in the hdrs attribute.
compile_flagsList of strings, optional, defaults to [].

Flags for the compiler.

Pass a list of strings here. For instance ["-O3", "-std=c++20"].

Split flag pairs -Xclang -somearg into separate flags ["-Xclang", "-somearg"].

Unavailable to downstream targets.
compile_string_flagsList of labels, optional, defaults to [].

Flags for the compiler in the form of string_flags.

Splits the values of each string_flag along colons like so:

load(“@bazel_skylib//rules:common_settings
.bzl”, “string_flag”)

string_flag(
name = “myflags”,
build_setting_default = “a:b:c
”,
)

mojo_library(
# …
# Equivalent to compile_flags = ["a", "b", "c"]
compile_string_flags = [":myflags
"],
)


Useful for externally configurable build attributes, such as generated flags from Nix environments.
definesList of strings, optional, defaults to [].

Defines for this target.

Pass a list of strings here. For instance ["MYDEFINE_1", "MYDEFINE_2"].

Unavailable to downstream targets.
includesList of strings, optional, defaults to [].

Include paths, relative to the target workspace.

Uses -iquote.

Useful if you need custom include prefix stripping for dynamic paths, for instance the ones generated by bzlmod. Instead of compile_flags = ["-iquoteexternal/mydep.someversion/include"], use includes = ["include"] to add the path to the workspace automatically.

Expands paths starting with $(GENERATED) to the workspace location in the GENDIR path.

Unavailable to downstream targets.

mojo_test

mojo_test(name, deps, srcs, compile_flags, compile_string_flags, defines, includes, runtime_data)

Testable wrapper around mojo_binary.

Consider using this rule over Skylib’s native_test targets to propagate shared libraries to the test invocations.

Example:

mojo_test(
name = "hello_test",
srcs = ["my_test.mojo"],
)

Attributes

NameDescription
nameName, required.

A unique name for this target.
depsList of labels, optional, defaults to [].

The dependencies for this target.

Use ll_library targets here. Other targets won’t work.
srcsList of labels, optional, defaults to [].

Compilable source files for this target.

Allowed file extensions: [".mojo", ".🔥"].

Place headers in the hdrs attribute.
compile_flagsList of strings, optional, defaults to [].

Flags for the compiler.

Pass a list of strings here. For instance ["-O3", "-std=c++20"].

Split flag pairs -Xclang -somearg into separate flags ["-Xclang", "-somearg"].

Unavailable to downstream targets.
compile_string_flagsList of labels, optional, defaults to [].

Flags for the compiler in the form of string_flags.

Splits the values of each string_flag along colons like so:

load(“@bazel_skylib//rules:common_settings
.bzl”, “string_flag”)

string_flag(
name = “myflags”,
build_setting_default = “a:b:c
”,
)

mojo_library(
# …
# Equivalent to compile_flags = ["a", "b", "c"]
compile_string_flags = [":myflags
"],
)


Useful for externally configurable build attributes, such as generated flags from Nix environments.
definesList of strings, optional, defaults to [].

Defines for this target.

Pass a list of strings here. For instance ["MYDEFINE_1", "MYDEFINE_2"].

Unavailable to downstream targets.
includesList of strings, optional, defaults to [].

Include paths, relative to the target workspace.

Uses -iquote.

Useful if you need custom include prefix stripping for dynamic paths, for instance the ones generated by bzlmod. Instead of compile_flags = ["-iquoteexternal/mydep.someversion/include"], use includes = ["include"] to add the path to the workspace automatically.

Expands paths starting with $(GENERATED) to the workspace location in the GENDIR path.

Unavailable to downstream targets.
runtime_dataList of labels, optional, defaults to [].

Extra files made available during test time.

expand_includes

expand_includes(ctx, include_string)

Prefix include_string with the path to the workspace root.

If include_string starts with $(GENERATED), prefixes with theGENDIR path as well.

Args:

NameDescription
ctx
include_string

find_package_dir

find_package_dir(ctx)

Find the directory to turn into a package.

Each directory with an __init__.mojo file corresponds to a package.

This function constructs a package from the first __init__.mojo file it finds, starting from the top level directory. If you have files like this:

mypackage/
__init__.mojo
subpackage/
__init__.mojo

you’ll get an output package mypackage.mojopkg.

Args:

NameDescription
ctxThe rule context.

Returns:

The package directory.

mojo_artifact

mojo_artifact(ctx, filename)

Return a string of the form "{ctx.label.name}/filename".

Encapsulate intermediary build artifacts to avoid name clashes for files of the same name built by targets in the same build invocation.

Args:

NameDescription
ctxThe build context.
filenamedefaults to None.

An optional string representing a filename. If omitted, creates a path of the form "{ctx.label.name}".

resolve_rule_inputs

resolve_rule_inputs(ctx)

Gather the inputs for downstream actions.

Args:

NameDescription
ctxThe rule context.

Returns:

A tuple (defines, includes). See //mojo:actions .bzl for usage.