Skip to content

Rust language support

It would be nice if CMake had built-in support for compiling plain Rust files with rustc.

This includes support for:

  • Rust-built libraries, even if they may be built as an opaque rlib (which in turn naturally would only be linkable by other Rust files).
  • Cross-compilation (choosing the correct Rust target).
  • Translation of common CMake variables to the equivalent rustc flags. Examples include POSITION_INDEPENDENT_CODE -> -Crelocation-model=..., CMAKE_BUILD_TYPE -> -Copt-level=... -Cdebuginfo=... and CMAKE_SYSTEM_PROCESSOR -> -Ctarget-cpu=....
  • Some sort of capability to easily add cfgs.

Discussed previously on Discourse, I'm opening this issue to have somewhere to track this. Note that this is distinct from integration with Cargo, which is being worked on in the Corrosion project.


Example of a minimal project I'd expect to work, if this feature existed:

// foo.rs
pub fn foo() {
    println!("foo");
}
// bar.rs
fn main() {
    foo::foo();
}
# CMakeLists.txt
cmake_minimum_required(VERSION X.Y)
project(Foobar Rust)

# Roughly: `rustc --crate-type=rlib foo.rs -o $(CMAKE_LIBRARY_PATH)/libfoo.rlib`
add_library(foo foo.rs)

# Roughly: `rustc -L $(CMAKE_LIBRARY_PATH) bar.rs`
add_executable(bar bar.rs)

# Roughly: add `--extern=foo`, such that you do not need `extern crate foo;`
target_link_libraries(bar foo)

(There are a few more such examples in the linked Discourse post).

Edited by Mads Marquart
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information