9

I would like to separate some performance intensive code into a .so (I am running Kubuntu Linux) while the main quantity of my code is compiled in debug mode. I want the faster compiles and run time support in my code, but it's unacceptable to run the small amount of intensive code with all the debug checks in it.

Is it possible to do this using Cargo? It seems that Cargo propagates the top level profile to the dependencies, so they are all compiled as release or debug, depending on what is requested of the main crate.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
stevenkucera
  • 3,855
  • 2
  • 18
  • 13
  • When faced with inflexible build systems, I have cheated and substituted the Debug builds with symbolic links to the Release builds... might get you going as we wait for a full answer :) – Matthieu M. Jul 07 '15 at 08:03
  • Can you tell me exactly what you did? Unoptimised crates seem to dislike talking to optimised crates. – stevenkucera Jul 07 '15 at 13:27
  • To be honest, I never used it in Rust only in C++ (part of why it was only a comment); it may be that the hash used in Rust libraries takes the optimization level into account, in which case I apologize for leading you astray. – Matthieu M. Jul 07 '15 at 13:30
  • OK fixed, I managed to achieve this using FFI linking to a fast dynamic library that cargo doesn't (need to) know about. – stevenkucera Jul 08 '15 at 05:21

1 Answers1

9

This is possible as of Rust 1.41 via overrides:

[package]
name = "speedy"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
image = "0.21.1"

# All dependencies (but not this crate itself or any workspace member)
# will be compiled with -Copt-level=2 . This includes build dependencies.
[profile.dev.package."*"]
opt-level = 2

The output with some details elided:

$ cargo build --verbose

   Compiling image v0.23.0
     Running `rustc [...] --crate-name image [...] -C opt-level=2 -C debuginfo=2 -C debug-assertions=on [...]`
   Compiling speedy v0.1.0 (/private/tmp/speedy)
     Running `rustc [...] --crate-name speedy [...] -C debuginfo=2 [...]`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I think, you should add link to docs in your answer: https://doc.rust-lang.org/cargo/reference/profiles.html#overrides Also, it is possible to separately override build scripts settings in the current version so it is worth mentioning as well. – Angelicos Phosphoros May 02 '23 at 13:40