//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:
Attributes
Name | Description |
---|---|
name | Name , required.A unique name for this target. |
deps | List of labels , optional, defaults to [] .The dependencies for this target. Use ll_library targets here. Other targets won’t work. |
srcs | List of labels , optional, defaults to [] .Compilable source files for this target. Allowed file extensions: [".mojo", ".🔥"] .Place headers in the hdrs attribute. |
compile_flags | List 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_flags | List of labels , optional, defaults to [] .Flags for the compiler in the form of string_flag s.Splits the values of each string_flag along colons like so:
Useful for externally configurable build attributes, such as generated flags from Nix environments. |
defines | List 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. |
includes | List 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_data | List 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:
Attributes
Name | Description |
---|---|
name | Name , required.A unique name for this target. |
deps | List of labels , optional, defaults to [] .The dependencies for this target. Use ll_library targets here. Other targets won’t work. |
srcs | List of labels , optional, defaults to [] .Compilable source files for this target. Allowed file extensions: [".mojo", ".🔥"] .Place headers in the hdrs attribute. |
compile_flags | List 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_flags | List of labels , optional, defaults to [] .Flags for the compiler in the form of string_flag s.Splits the values of each string_flag along colons like so:
Useful for externally configurable build attributes, such as generated flags from Nix environments. |
defines | List 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. |
includes | List 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:
Attributes
Name | Description |
---|---|
name | Name , required.A unique name for this target. |
deps | List of labels , optional, defaults to [] .The dependencies for this target. Use ll_library targets here. Other targets won’t work. |
srcs | List of labels , optional, defaults to [] .Compilable source files for this target. Allowed file extensions: [".mojo", ".🔥"] .Place headers in the hdrs attribute. |
compile_flags | List 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_flags | List of labels , optional, defaults to [] .Flags for the compiler in the form of string_flag s.Splits the values of each string_flag along colons like so:
Useful for externally configurable build attributes, such as generated flags from Nix environments. |
defines | List 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. |
includes | List 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_data | List 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:
Name | Description |
---|---|
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:
you’ll get an output package mypackage.mojopkg
.
Args:
Name | Description |
---|---|
ctx | The 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:
Name | Description |
---|---|
ctx | The build context. |
filename | defaults 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:
Name | Description |
---|---|
ctx | The rule context. |
Returns:
A tuple (defines, includes)
. See
//mojo:actions
.bzl for usage.