5

If I specify panic like this, it works for all targets:

[profile.release]
panic = "abort"

I want to specify panic = "abort" only for target=arm-linux-androideabi.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
user1244932
  • 6,781
  • 3
  • 37
  • 88

2 Answers2

6

You will need to add a .cargo/config to your project and use it to specify the panic settings instead of Cargo.toml:

[target.arm-linux-androideabi]
rustflags = ["-C", "panic=abort"]

The two main configuration headings you will want to look at are [target.$triple] and [target.'cfg(...)'].

Brian Cain
  • 13,996
  • 3
  • 46
  • 85
Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
0

There is another way you could try to accomplish this: add a custom panic handler, gather a stack-trace, parse the stack-trace to determine what library the panic occurred in, and then only abort if you detect it as having occurred within that library.

Is this hacky/brittle? Yes.

But the option is there if for some reason you want it!

Starting point:

panic::set_hook(Box::new(|info| {
    //let stacktrace = Backtrace::capture();
    let stacktrace = Backtrace::force_capture();
    println!("Got panic. @info:{}\n@stackTrace:{}", info, stacktrace);
    if guess_library_from_stacktrace(stacktrace) == "library-X" {
        std::process::abort();
    }
}));

Here is a GitHub comment with more details: https://github.com/tokio-rs/tokio/issues/2002#issuecomment-1020443386

Venryx
  • 12,262
  • 10
  • 60
  • 82