Julia Bazel rules
Rules
- julia_artifact
- julia_binary
- julia_format_test
- julia_library
- julia_standalone_binary
- julia_sysimage
- julia_test
- julia_toolchain
Aspects
julia_artifact
load("@rules_julia//julia:defs.bzl", "julia_artifact")
julia_artifact(name, srcs, out, artifact_name, files, lazy, platform_tags, strip_prefix, url)
Build a Julia artifact from Bazel targets.
Julia distributes binaries and data as artifacts: gzipped tarballs identified by
the git tree hash of their contents and listed in a package's Artifacts.toml
with a download URL and SHA-256. This rule produces both halves: the tarball,
packed with the same Tar and 7z Julia itself uses, and an Artifacts.toml
holding the entry for it, with both hashes computed by the code Pkg uses to
verify downloads. Upload the tarball to the URL and merge the entry into the
package's Artifacts.toml; Pkg and artifact"name" then install it like any
other artifact.
Files from srcs keep their path relative to the package (or strip_prefix
below it); files places outputs from anywhere at an explicit path. Directories
(tree artifacts) are copied whole.
Platform specific artifacts set platform_tags. The values are taken from the
target platform (os, arch) and the @rules_julia//julia/settings flags
(libgfortran_version and cxxstring_abi from the toolchain; libc is assumed glibc on Linux), so building the same target
under several --platforms yields one entry per platform, ready to be
concatenated. Platform independent artifacts (data, models, ...) leave it empty.
Example:
julia_artifact(
name = "libfoo",
srcs = ["include/foo.h"],
files = {":libfoo_shared": "lib/libfoo.so"},
platform_tags = ["os", "arch", "libc"],
url = "https://github.com/me/Foo.jl/releases/download/v1.0.0/{file}",
)
produces libfoo.tar.gz and libfoo.Artifacts.toml:
[[libfoo]]
arch = "x86_64"
git-tree-sha1 = "..."
libc = "glibc"
os = "linux"
[[libfoo.download]]
sha256 = "..."
url = "https://github.com/me/Foo.jl/releases/download/v1.0.0/libfoo.tar.gz"
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| srcs | Files to include at their path relative to the package, or to strip_prefix within it. | List of labels | optional | [] |
| out | The tarball to write. Defaults to <name>.tar.gz. | Label; nonconfigurable | optional | None |
| artifact_name | The artifact's name in Artifacts.toml. Defaults to the target name. | String | optional | "" |
| files | Files or directories to place at an explicit path inside the artifact, mapping each label to its destination. . places a directory's contents at the root. | Dictionary: Label -> String | optional | {} |
| lazy | Mark the artifact lazy, so Julia only downloads it on first use. | Boolean | optional | False |
| platform_tags | Platform tags to record, any of os, arch, libc, libgfortran_version and cxxstring_abi. Empty for platform independent artifacts. libc is only recorded on Linux. | List of strings | optional | [] |
| strip_prefix | A directory below the package that srcs paths are made relative to. | String | optional | "" |
| url | The URL the tarball will be downloadable from, recorded in the entry. {file} expands to the tarball's file name. Without it the entry has no download table. | String | optional | "" |
julia_binary
load("@rules_julia//julia:defs.bzl", "julia_binary")
julia_binary(name, deps, srcs, data, env, julia_flags, main)
A Julia executable.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Other Julia libraries this target depends on | List of labels | optional | [] |
| srcs | Julia source files (.jl files). | List of labels | required | |
| data | Additional files needed at runtime | List of labels | optional | [] |
| env | Environment variables to set when running the binary. Supports $(location) expansion. | Dictionary: String -> String | optional | {} |
| julia_flags | Flags for the julia interpreter itself, such as --color=yes or --threads=auto; program arguments go in args. Flags that affect code generation (-O, --check-bounds, -g) are rejected here because build-time compile caches must be built with the same values: they follow --compilation_mode through the toolchain's opt_level, check_bounds and debug_info attributes instead. Flags the rules manage (--compiled-modules, --sysimage, --project) are rejected too. | List of strings | optional | [] |
| main | The main entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name. | Label | optional | None |
julia_format_test
load("@rules_julia//julia:defs.bzl", "julia_format_test")
julia_format_test(name, config, target)
A rule for running JuliaFormatter on a Julia target.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| config | The config file (.JuliaFormatter.toml) containing JuliaFormatter settings. | Label | optional | "@rules_julia//julia/settings:formatter_config" |
| target | The target to run JuliaFormatter on. | Label | required |
julia_library
load("@rules_julia//julia:defs.bzl", "julia_library")
julia_library(name, deps, srcs, data, precompile)
A sharable Julia library or module.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Other Julia libraries this target depends on | List of labels | optional | [] |
| srcs | Julia source files (.jl files) | List of labels | required | |
| data | Additional files needed at runtime | List of labels | optional | [] |
| precompile | Precompile the library at build time. The resulting cache is placed on DEPOT_PATH for every binary and test that depends on this library. Requires the module entry point <name>.jl (typically src/<name>.jl) and Julia 1.11+; otherwise no cache is produced. | Boolean | optional | True |
julia_standalone_binary
load("@rules_julia//julia:defs.bzl", "julia_standalone_binary")
julia_standalone_binary(name, binary, cpu_target, precompile_statements)
Build a Julia package and its dependencies into a custom system image and produce an executable that starts Julia with it.
The system image is produced in two actions: julia --output-o emits the
object archive of the image, which is then linked into a shared library.
By default the link uses the lld bundled with Julia, the same way Julia
links its own package images; --@rules_julia//julia/settings:linker=cc
selects the C++ toolchain instead. The executable is the regular Julia
wrapper started with --sysimage, so all code is loaded precompiled and
startup is immediate. No network access or host tools outside the Bazel
toolchains are involved.
The target in binary must be a Julia package: its entry point is
src/<name>.jl defining module <name> with a julia_main()::Cint function,
which is called with the command line arguments in ARGS.
Example:
julia_binary(
name = "my_app_bin",
srcs = ["src/my_app_bin.jl"],
deps = ["//my/lib"],
)
julia_standalone_binary(
name = "my_app",
binary = ":my_app_bin",
)
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| binary | The julia_binary (or julia_library) package to build into the system image. | Label | required | |
| cpu_target | The --cpu-target to compile the system image for. Defaults to the toolchain's portable multi-versioned target for its architecture. | String | optional | "" |
| precompile_statements | Files of precompile statements (as produced by julia --trace-compile) whose methods are compiled into the system image. | List of labels | optional | [] |
julia_sysimage
load("@rules_julia//julia:defs.bzl", "julia_sysimage")
julia_sysimage(name, deps, cpu_target, precompile_statements)
Compile Julia packages and their dependencies into a system image.
A system image is Julia's own ahead-of-time format: the shared library that
julia --sysimage loads in place of the stock one, with every included module
already compiled. The image is produced in two actions: julia --output-o
emits the object archive, which is then linked into a shared library. By
default the link uses the lld bundled with Julia, the same way Julia links
its own package images; --@rules_julia//julia/settings:linker=cc selects the
C++ toolchain instead. No network access or host tools outside the Bazel
toolchains are involved, and the image is rebuilt only when its inputs change.
Every target in deps must be a Julia package: its entry point is
src/<name>.jl defining module <name>. The output is the shared library;
the object archive is available in the archive output group. For a
self-contained executable around a package with a julia_main function, see
julia_standalone_binary, which builds on the same actions.
Example:
julia_sysimage(
name = "image",
deps = ["@deps//:JSON", "//my/lib"],
)
julia --sysimage=$(bazel cquery --output=files //:image) -e 'using JSON'
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | The packages to compile into the image. | List of labels | required | |
| cpu_target | The --cpu-target to compile the system image for. Defaults to the toolchain's portable multi-versioned target for its architecture. | String | optional | "" |
| precompile_statements | Files of precompile statements (as produced by julia --trace-compile) whose methods are compiled into the system image. | List of labels | optional | [] |
julia_test
load("@rules_julia//julia:defs.bzl", "julia_test")
julia_test(name, deps, srcs, data, env, julia_flags, main)
A Julia test executable.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Other Julia libraries this target depends on | List of labels | optional | [] |
| srcs | Julia test source files (.jl files). | List of labels | required | |
| data | Additional files needed at runtime for the test | List of labels | optional | [] |
| env | Environment variables to set when running the test. Supports $(location) expansion. | Dictionary: String -> String | optional | {} |
| julia_flags | Flags for the julia interpreter itself, such as --color=yes or --threads=auto; program arguments go in args. Flags that affect code generation (-O, --check-bounds, -g) are rejected here because build-time compile caches must be built with the same values: they follow --compilation_mode through the toolchain's opt_level, check_bounds and debug_info attributes instead. Flags the rules manage (--compiled-modules, --sysimage, --project) are rejected too. | List of strings | optional | [] |
| main | The main test entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name. | Label | optional | None |
julia_toolchain
load("@rules_julia//julia:defs.bzl", "julia_toolchain")
julia_toolchain(name, check_bounds, cpu_target, cxxstring_abi, debug_info, julia,
libgfortran_version, link_files, linker, opt_level, sysimage, version)
A toolchain for building Julia targets.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| check_bounds | The bounds checking mode (julia --check-bounds=<mode>) per --compilation_mode. auto honours @inbounds, yes always checks, no never does. Julia rejects compile caches built with a different mode, so the same value is used for build-time precompilation, system images and the binaries that load them. Values other than Julia's default (auto) make Julia compile its bundled standard library at run time. | Dictionary: String -> String | optional | {"dbg": "yes", "fastbuild": "auto", "opt": "auto"} |
| cpu_target | The default --cpu-target for system images built with this toolchain. Should be portable across machines of the toolchain's architecture. | String | optional | "generic" |
| cxxstring_abi | The C++ std::string ABI (cxx03 or cxx11) of the C++ runtime bundled with this Julia. Prebuilt artifacts with C++ code are published for both and the variant matching the toolchain is selected. Official Julia binaries use cxx11. | String | optional | "cxx11" |
| debug_info | The debug info level (julia -g<n>) per --compilation_mode. Part of Julia's compile cache key, so applied uniformly like opt_level. Julia's default is 1. | Dictionary: String -> String | optional | {"dbg": "2", "fastbuild": "1", "opt": "1"} |
| julia | The path to the Julia binary (julia or julia.exe). | Label | required | |
| libgfortran_version | The major version (3, 4 or 5) of the libgfortran bundled with this Julia. Prebuilt artifacts with Fortran code are published per libgfortran ABI and the variant matching the toolchain is selected. Official Julia binaries bundle 5. | String | optional | "5" |
| link_files | The libjulia libraries needed to link a system image. | List of labels | optional | [] |
| linker | The linker used for system images built with this toolchain unless overridden by --@rules_julia//julia/settings:linker. julia uses the lld bundled with Julia; cc uses the C++ toolchain. | String | optional | "julia" |
| opt_level | The optimization level (julia -O<n>) per --compilation_mode. Julia rejects compile caches built with a different level, so the same value is used for build-time precompilation, system images and the binaries that load them. Julia's default, 2, is also its performance setting, and the standard library caches bundled with Julia are built at it; other values make Julia compile the standard library at run time. opt therefore keeps 2 by default and only dbg deviates. | Dictionary: String -> String | optional | {"dbg": "0", "fastbuild": "2", "opt": "2"} |
| sysimage | The stock system image (sys.so, sys.dylib or sys.dll). | Label | optional | None |
| version | The Julia version string (e.g., '1.12.5'). | String | required |
julia_format_aspect
load("@rules_julia//julia:defs.bzl", "julia_format_aspect")
julia_format_aspect()
An aspect for running JuliaFormatter on targets with Julia sources.
ASPECT ATTRIBUTES
ATTRIBUTES