Rust flags for subcrate in Workspace

I have a workspace with one library crate lib and one binary crate app .

I now want to instrument the lib crate with LLVM's SanitizerCoverage and provide the Callback functions in the app crate, which should not be instrumented.

Unfortunately, I am struggling how to only pass the rustc flags to the lib crate:

rustflags = [
    "-Cpasses=sancov-module",
    "-Cllvm-args=-sanitizer-coverage-level=1",
    "-Cllvm-args=-sanitizer-coverage-trace-pc-guard",
]

If I put this in a [#build] section in .cargo/config.toml, the entire workspace is instrumented, even with the callback functions, thus causing a stack overflow. Putting the cargo config inside the lib crate does not have any effect (this is not supported by Cargo, as I've found out). Manually compiling the lib crate with cargo rustc and passing the compiler flag that way works, but if I then compile the app crate, it will recompile the lib crate again, this time without the instrumentation.

I've also tried to create a config.toml with a target cfg

[target.'cfg(instrumented)']
rustflags = [
    "-Cpasses=sancov-module",
    "-Cllvm-args=-sanitizer-coverage-level=1",
    "-Cllvm-args=-sanitizer-coverage-trace-pc-guard",
]

and set this config parameter in the buildscript of the lib crate, but this also does not have any effect.

Is there any way to do this? I don't mind dirty hacks and playing around manually with the linker :smile:

Thanks!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.