Precompilation Support

RustCall.jl supports Julia's precompilation system, enabling faster startup times for packages that use Rust bindings.

Overview

When you use @rust_crate in a Julia package, the bindings can be precompiled along with your package. This avoids runtime compilation and significantly reduces startup time.

Benefits

  • Faster startup: Precompiled bindings load instantly
  • Deployment-ready: No compilation needed at runtime
  • Reproducible builds: Same bindings every time
  • PackageCompiler.jl compatible: Works with standalone applications

How It Works

Runtime vs Precompile Time

Two ways to put a crate's bindings in a package, both precompiled:

@rust_crate at the package's top level (@rust_crate path submodule="Bindings" followed by using .Bindings: ...; see "Using @rust_crate inside a package" in Crate Bindings):

  1. During precompilation: @rust_crate scans the crate, builds it (into RustCall's cache) and generates the module, which is compiled into the package's cache; nothing is written into the package
  2. At runtime: the module's __init__ opens the cached library
  3. After RustCall.clear_cache() or a rebuild of the crate: the package's precompile cache is stale and the next using re-precompiles it

write_bindings_to_file (a deps/build.jl writes the bindings and copies the library into the package):

  1. During development: Generate bindings file once
  2. During precompilation: Julia compiles the bindings module
  3. At runtime: Precompiled bindings load instantly, from the library the package carries — no Rust toolchain needed on the machine that loads it

The Generation Process

Rust Crate               Julia Bindings File
    │                           │
    ├── Cargo.toml              │
    ├── src/                    │
    │   └── lib.rs ──────────►  MyBindings.jl
    │                           │
    └── target/                 │
        └── release/            │
            └── lib*.so ◄───────┘

Usage Guide

Step 1: Project Structure

Set up your package with this structure:

MyPackage/
├── Project.toml
├── src/
│   ├── MyPackage.jl          # Main module
│   └── generated/
│       └── RustBindings.jl   # Generated bindings (gitignore this)
├── deps/
│   ├── my_rust_crate/        # Your Rust crate
│   │   ├── Cargo.toml
│   │   └── src/lib.rs
│   └── lib/                  # Compiled library location
│       └── libmy_rust_crate.{so,dylib,dll}
└── build.jl                  # Build script

Step 2: Build Script

Create a build.jl to generate bindings:

# deps/build.jl
using RustCall

# Path to Rust crate
crate_path = joinpath(@__DIR__, "my_rust_crate")

# Output path for generated bindings
bindings_path = joinpath(dirname(@__DIR__), "src", "generated", "RustBindings.jl")

# Generate bindings
RustCall.write_bindings_to_file(
    crate_path,
    bindings_path,
    output_module_name = "RustBindings",
    build_release = true,
    relative_lib_path = "../../deps/lib"
)

Step 3: Package Module

Include the generated bindings in your package:

# src/MyPackage.jl
module MyPackage

# Include generated bindings
include("generated/RustBindings.jl")
using .RustBindings

# Re-export functions
export add, multiply, MyStruct

# Your Julia code here...

end

Step 4: Build During Development

Run the build script during development:

using Pkg
Pkg.build("MyPackage")

Or manually:

include("deps/build.jl")

PackageCompiler.jl Integration

RustCall.jl works with PackageCompiler.jl for creating standalone applications.

Creating a System Image

using PackageCompiler

create_sysimage(
    :MyPackage,
    sysimage_path = "my_sysimage.so",
    precompile_execution_file = "precompile_script.jl"
)

Creating an App

using PackageCompiler

create_app(
    ".",                    # Package directory
    "build",               # Output directory
    executables = ["main" => "main.jl"]
)

Important Considerations

  1. Library Path: Use relative_lib_path to ensure the compiled library is found relative to the bindings file
  2. Copy Library: Ensure the compiled .so/.dylib/.dll is included in the app bundle
  3. Precompile Script: Include calls to your Rust-backed functions in the precompile script

Example precompile script:

# precompile_script.jl
using MyPackage

# Warm up the functions
MyPackage.add(Int32(1), Int32(2))
MyPackage.multiply(1.0, 2.0)

Cache Behavior

During Development

  • Compiled libraries are cached in a Scratch.jl space, <depot>/scratchspaces/<RustCall UUID>/cache-v2/ (see the Performance guide)
  • Cache is keyed by source code hash
  • Rebuilding only happens when source changes

During Precompilation

  • Generated bindings reference a fixed library path
  • Library must exist at the specified path
  • No runtime compilation occurs

Cache Functions

# Clear the cache
RustCall.clear_cache()

# Check cache size
RustCall.get_cache_size()

# List cached libraries
RustCall.list_cached_libraries()

# Clean up old cache entries
RustCall.cleanup_old_cache(30)

Troubleshooting

Library Not Found

Error: could not load library "..." at runtime

Solutions:

  1. Verify relative_lib_path points to the correct location
  2. Check that the library was copied during build
  3. Ensure the library has correct permissions
# Debug: Check the library path
bindings_dir = dirname(pathof(MyPackage))
lib_path = joinpath(bindings_dir, "..", "deps", "lib", "libmy_crate.so")
@info "Library exists?" isfile(lib_path)

Precompilation Fails

Error: LoadError during precompilation

Solutions:

  1. Ensure __init__() function loads the library correctly
  2. Check that library path uses @__DIR__ for portability
  3. Verify the generated code syntax

Version Mismatch

Symptom: Functions behave unexpectedly after updating Rust code

Solution: Regenerate bindings after modifying Rust code:

Pkg.build("MyPackage")

Platform-Specific Issues

macOS:

  • Library extension is .dylib
  • May need to handle code signing

Linux:

  • Library extension is .so
  • Check LD_LIBRARY_PATH if needed

Windows:

  • Library extension is .dll
  • Place DLLs in the same directory as the Julia process or in PATH

Best Practices

  1. Gitignore generated files: Add src/generated/ to .gitignore
  2. Version your Rust code: Keep Rust crate under version control
  3. CI/CD integration: Run Pkg.build() in CI to generate fresh bindings
  4. Test precompilation: Include precompilation in your test suite
  5. Document dependencies: Note Rust toolchain requirements in README

Example: Complete Package

Here's a complete example of a precompilable package:

# Project.toml
name = "MyRustPackage"
uuid = "..."
version = "0.1.0"

[deps]
RustCall = "..."

[extras]
Test = "..."

[targets]
test = ["Test"]
# deps/build.jl
using RustCall

RustCall.write_bindings_to_file(
    joinpath(@__DIR__, "rust_crate"),
    joinpath(dirname(@__DIR__), "src", "generated", "Bindings.jl"),
    output_module_name = "Bindings",
    relative_lib_path = "../../deps/lib"
)
# src/MyRustPackage.jl
module MyRustPackage

include("generated/Bindings.jl")
using .Bindings

export rust_add, rust_multiply

rust_add(a, b) = Bindings.add(Int32(a), Int32(b))
rust_multiply(a, b) = Bindings.multiply(Float64(a), Float64(b))

end

The Python host path

@rust_crate ... pyo3_host=true and RustCall.pyo3_host_import build nothing while a package precompiles: the artifact is keyed by the Python interpreter PythonCall provides, which is not known until PythonCall's __init__, and an interpreter may not be started during precompilation. The generated bindings are compiled into the package image; the build and the import happen on the first call.

That first call compiles nothing of RustCall's (#449): the crate scan, the cache key and the cache lookup are in RustCall's own image, and the import is in RustCallPyO3HostExt's. What remains is the work itself — the extractor, the toolchain probes, cargo tree, one interpreter start, and the Cargo build when the artifact is not cached. To pay the build ahead of the first call, run the interpreter-free half (RustCall.build_pyo3_extension) earlier — with the interpreter PythonCall will run, which the import checks against. In __init__ the returned artifact can be kept, and the first call is then the import alone (RustCall.pyo3_host_import(artifact)); a deps/build.jl runs in another process and only warms the cache, so the runtime's pyo3_host_import(crate) still scans and probes but no longer builds. See "The first call, and paying it earlier" in PyO3 Crates.

See Also