rules_julia
Overview
This repository implements Bazel rules for the Julia programming language.
Setup
To begin using the rules, add the following to your MODULE.bazel file.
bazel_dep(name = "rules_julia", version = "{version}")
julia = use_extension("@rules_julia//julia:extensions.bzl", "julia")
julia.toolchain(
name = "julia_toolchains",
version = "1.12.2",
)
use_repo(julia, "julia_toolchains")
register_toolchains(
"@julia_toolchains//:all",
)
Julia Bazel rules
Rules
Aspects
Module Extensions
julia_binary
load("@rules_julia//julia:defs.bzl", "julia_binary")
julia_binary(name, deps, srcs, data, env, 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 | {} |
| 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)
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 | [] |
julia_standalone_binary
load("@rules_julia//julia:defs.bzl", "julia_standalone_binary")
julia_standalone_binary(name, binary)
A rule for converting a julia_binary to a standalone application.
This rule uses PackageCompiler.jl to create a standalone executable that includes the Julia runtime and all dependencies. The resulting application can be distributed and run on machines without Julia installed.
Dependencies are provided hermetically through Bazel - no network access required! PackageCompiler.jl and all Julia package dependencies are managed through the Bazel build system.
Example:
julia_binary(
name = "my_app_bin",
srcs = ["my_app.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 target to convert into a standalone application | Label | required |
julia_test
load("@rules_julia//julia:defs.bzl", "julia_test")
julia_test(name, deps, srcs, data, env, 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 | {} |
| 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, julia, version)
A toolchain for building Julia targets.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| julia | The path to the Julia binary (julia or julia.exe). | Label | required | |
| 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
julia
julia = use_extension("@rules_julia//julia:defs.bzl", "julia")
Bzlmod extensions for Julia
Julia settings
Definitions for all @rules_julia//julia settings
formatter_config
--@rules_julia//julia/settings:formatter_config
The JuliaFormatter config file to use in formatting rules.
PARAMETERS
version
--@rules_julia//julia/settings:version
The version of julia to use
PARAMETERS
Julia Pkg rules
Rules
Module Extensions
julia_pkg_compiler
load("@rules_julia//julia/pkg:defs.bzl", "julia_pkg_compiler")
julia_pkg_compiler(name, manifest_bazel_json, manifest_toml, project_toml)
A rule for generating Manifest.toml and Manifest.bazel.json from Project.toml using Julia's Pkg manager.
This rule uses Julia's native package manager to resolve dependencies and generate
a Manifest.toml lockfile and a Manifest.bazel.json file with SHA256 hashes. The
Manifest.bazel.json can then be used with the pkg module extension to fetch and
build Julia packages hermetically with cryptographic verification.
The manifest_bazel_json attribute is optional. If not specified, the Manifest.bazel.json
file will be written next to the Manifest.toml file.
Note that when setting this target up for the first time, an empty Manifest.toml file
will need to be created. If you specify manifest_bazel_json, that file should also be
created as an empty JSON object {}.
Example:
julia_pkg_compiler(
name = "pkg_update",
project_toml = "Project.toml",
manifest_toml = "Manifest.toml",
# manifest_bazel_json is optional - defaults to Manifest.bazel.json next to Manifest.toml
)
Then run:
bazel run //:pkg_update
This will update Manifest.toml with resolved package versions and Manifest.bazel.json with SHA256 hashes for all packages and their dependency graph.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| manifest_bazel_json | The location of the Manifest.bazel.json lockfile to generate/update with SHA256 hashes. If not specified, defaults to Manifest.bazel.json next to the manifest_toml file. | Label | optional | None |
| manifest_toml | The location of the Manifest.toml lockfile to generate/update. | Label | required | |
| project_toml | The Project.toml file describing dependencies. | Label | required |
julia_pkg_test
load("@rules_julia//julia/pkg:defs.bzl", "julia_pkg_test")
julia_pkg_test(name, compiler)
A test rule that verifies Manifest.bazel.json is in sync with Manifest.toml.
This test ensures that the lockfile generated by pkg_compiler contains the correct package versions, UUIDs, and dependencies that match the Manifest.toml file.
The test will fail if:
- A package is missing from the lockfile
- Versions don't match between the two files
- UUIDs don't match
- The git-tree-sha1 is not in the package URL
- SHA256 hashes are missing
Example:
julia_pkg_compiler(
name = "pkg_update",
project_toml = "Project.toml",
manifest_toml = "Manifest.toml",
# Normally optional but required for the `julia_pkg_test` target.
manifest_bazel_json = "Manifest.bazel.json",
)
julia_pkg_test(
name = "pkg_sync_test",
compiler = ":pkg_update",
)
Then run:
bazel test //:pkg_sync_test
This will verify that both manifest files are synchronized. If they're out of sync, the test will fail with detailed error messages indicating what needs to be fixed.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| compiler | The julia_pkg_compiler target | Label | required |
pkg
pkg = use_extension("@rules_julia//julia/pkg:defs.bzl", "pkg")
pkg.install(name, lockfile, manifest)
pkg.pkg_annotation(dep, patch_args, patch_tool, patches)
A module for defining Julia package dependencies.
TAG CLASSES
install
Attributes
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | The name of the module to create | Name | required | |
| lockfile | The Manifest.bazel.json lockfile with SHA256 hashes for all packages. | Label | required | |
| manifest | The Manifest.toml lockfile associated with Project.toml (deprecated, use lockfile instead). | Label | optional | None |
pkg_annotation
Attributes
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| dep | The name of the package to annotate | String | required | |
| patch_args | Arguments to pass to the patch tool. See http_archive.patch_args | List of strings | optional | [] |
| patch_tool | The patch tool to use. See http_archive.patch_tool | String | optional | "" |
| patches | List of patch files to apply to the package. See http_archive.patches | List of labels | optional | [] |