Julia Bazel rules

Rules

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcsFiles to include at their path relative to the package, or to strip_prefix within it.List of labelsoptional[]
outThe tarball to write. Defaults to <name>.tar.gz.Label; nonconfigurableoptionalNone
artifact_nameThe artifact's name in Artifacts.toml. Defaults to the target name.Stringoptional""
filesFiles 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 -> Stringoptional{}
lazyMark the artifact lazy, so Julia only downloads it on first use.BooleanoptionalFalse
platform_tagsPlatform 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 stringsoptional[]
strip_prefixA directory below the package that srcs paths are made relative to.Stringoptional""
urlThe 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.Stringoptional""

julia_binary

load("@rules_julia//julia:defs.bzl", "julia_binary")

julia_binary(name, deps, srcs, data, env, julia_flags, main)

A Julia executable.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia source files (.jl files).List of labelsrequired
dataAdditional files needed at runtimeList of labelsoptional[]
envEnvironment variables to set when running the binary. Supports $(location) expansion.Dictionary: String -> Stringoptional{}
julia_flagsFlags 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 stringsoptional[]
mainThe main entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name.LabeloptionalNone

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
configThe config file (.JuliaFormatter.toml) containing JuliaFormatter settings.Labeloptional"@rules_julia//julia/settings:formatter_config"
targetThe target to run JuliaFormatter on.Labelrequired

julia_library

load("@rules_julia//julia:defs.bzl", "julia_library")

julia_library(name, deps, srcs, data, precompile)

A sharable Julia library or module.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia source files (.jl files)List of labelsrequired
dataAdditional files needed at runtimeList of labelsoptional[]
precompilePrecompile 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.BooleanoptionalTrue

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
binaryThe julia_binary (or julia_library) package to build into the system image.Labelrequired
cpu_targetThe --cpu-target to compile the system image for. Defaults to the toolchain's portable multi-versioned target for its architecture.Stringoptional""
precompile_statementsFiles of precompile statements (as produced by julia --trace-compile) whose methods are compiled into the system image.List of labelsoptional[]

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsThe packages to compile into the image.List of labelsrequired
cpu_targetThe --cpu-target to compile the system image for. Defaults to the toolchain's portable multi-versioned target for its architecture.Stringoptional""
precompile_statementsFiles of precompile statements (as produced by julia --trace-compile) whose methods are compiled into the system image.List of labelsoptional[]

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia test source files (.jl files).List of labelsrequired
dataAdditional files needed at runtime for the testList of labelsoptional[]
envEnvironment variables to set when running the test. Supports $(location) expansion.Dictionary: String -> Stringoptional{}
julia_flagsFlags 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 stringsoptional[]
mainThe main test entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name.LabeloptionalNone

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
check_boundsThe 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 -> Stringoptional{"dbg": "yes", "fastbuild": "auto", "opt": "auto"}
cpu_targetThe default --cpu-target for system images built with this toolchain. Should be portable across machines of the toolchain's architecture.Stringoptional"generic"
cxxstring_abiThe 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.Stringoptional"cxx11"
debug_infoThe 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 -> Stringoptional{"dbg": "2", "fastbuild": "1", "opt": "1"}
juliaThe path to the Julia binary (julia or julia.exe).Labelrequired
libgfortran_versionThe 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.Stringoptional"5"
link_filesThe libjulia libraries needed to link a system image.List of labelsoptional[]
linkerThe 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.Stringoptional"julia"
opt_levelThe 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 -> Stringoptional{"dbg": "0", "fastbuild": "2", "opt": "2"}
sysimageThe stock system image (sys.so, sys.dylib or sys.dll).LabeloptionalNone
versionThe Julia version string (e.g., '1.12.5').Stringrequired

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