Skip to content

//mojo:defs.bzl

Import these rules in your BUILD.bazel files.

To load for example the mojo_binary rule:

load("@rules_mojo//mojo:defs.bzl", "mojo_binary")

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.