Taaitaaiger / jlrs

Julia bindings for Rust
MIT License
408 stars 21 forks source link
ffi julia rust

jlrs

Rust Docs License:MIT

jlrs is a crate that provides access to the Julia C API. It can be used to embed Julia in Rust applications and to write interop libraries to Rust crates that can be used by Julia.

Julia versions 1.6 up to and including 1.11 are supported, but only the LTS and stable versions are actively tested. Using the current stable version of Julia is highly recommended. The minimum supported Rust version is currently 1.77.

This readme only contains information about what features are supported by jlrs, what prerequisites must be met, and how to meet them. For information and examples about how to use jlrs, please read the docs. The documentation assumes you are already familiar with the Julia and Rust programming languages.

Overview

An incomplete list of features that are currently supported by jlrs:

Prerequisites

Julia must be installed before jlrs can be used, jlrs is compatible with Julia 1.6 up to and including Julia 1.11. If the JlrsCore package has not been installed, it will automatically be installed when jlrs is initialized by default. jlrs has not been tested with juliaup yet on Linux and macOS.

Linux

The recommended way to install Julia is to download the binaries from the official website, which is distributed as an archive containing a directory called julia-x.y.z. This directory contains several other directories, including a bin directory containing the julia executable.

During compilation, the paths to the header and library are normally detected automatically by executing the command which julia. The path to julia.h must be $(which julia)/../include/julia/julia.h and the path to the library $(which julia)/../lib/libjulia.so. If you want to override this default behaviour the JULIA_DIR environment variable must be set to the path to the appropriate julia.x-y-z directory, in this case $JULIA_DIR/include/julia/julia.h and $JULIA_DIR/lib/libjulia.so are used instead.

In order to be able to load libjulia.so this file must be on the library search path. If this is not the case you must add /path/to/julia-x.y.z/lib to the LD_LIBRARY_PATH environment variable.

macOS

Follow the instructions for Linux, but replace LD_LIBRARY_PATH with DYLD_LIBRARY_PATH.

Windows

Julia can be installed using juliaup, or with the installer or portable installation downloaded from the official website. In the first case, Julia has been likely installed in %USERPROFILE%\.julia\juliaup\julia-x.y.z+0~x64, while using the installer or extracting allows you to pick the destination. After installation or extraction a folder called Julia-x.y.z exists, which contains several folders including a bin folder containing julia.exe. The path to the bin folder must be added to the Path environment variable.

Julia is automatically detected by executing the command where julia. If this returns multiple locations the first one is used. The default can be overridden by setting the JULIA_DIR environment variable. This doesn't work correctly with juliaup, in this case the environment variable must be set.

Features

Most functionality of jlrs is only available if the proper features are enabled. These features generally belong to one of three categories: versions, runtimes and utilities.

Versions

The Julia C API is unstable and there are minor incompatibilities between different versions of Julia. To ensure the correct bindings are used for a particular version of Julia you must enable a version feature. The following version features currently exist:

Exactly one version feature must be enabled. Otherwise, jlrs will fail to compile.

If you want your crate to be compatible with multiple versions of Julia, you should "reexport" these version features as follows:

[features]
julia-1-6 = ["jlrs/julia-1-6"]
julia-1-7 = ["jlrs/julia-1-7"]
julia-1-8 = ["jlrs/julia-1-8"]
julia-1-9 = ["jlrs/julia-1-9"]
julia-1-10 = ["jlrs/julia-1-10"]
julia-1-11 = ["jlrs/julia-1-11"]

Runtimes

A runtime lets initialize Julia from Rust application, the following features enable a runtime:

WARNING: Runtime features must only be enabled by applications that embed Julia. Libraries must never enable a runtime feature.

WARNING: When a runtime feature is enabled on Linux, set RUSTFLAGS="-Clink-args=-rdynamic" if you want fast code.

Utilities

All other features are called utility features. The following are available:

You can enable all features except debug, i686, windows, no-link and yggdrasil by enabling the full feature. If you don't want to enable any runtimes either, you can use full-no-rt.