1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::builder::profile::{ResolvedCmake, ResolvedProfile};
/// Sanitizer kind for built-in --asan / --tsan flags.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SanitizerKind {
/// AddressSanitizer (--asan)
Address,
/// ThreadSanitizer (--tsan)
Thread,
}
impl SanitizerKind {
/// Short name used as the path segment, e.g. `ccgo_build/debug-asan/android/`.
pub fn name(&self) -> &'static str {
match self {
Self::Address => "asan",
Self::Thread => "tsan",
}
}
/// C/C++ compiler flags injected into cmake when no user-defined profile overrides.
fn compiler_flags(&self) -> &'static [&'static str] {
match self {
Self::Address => &["-fsanitize=address", "-fno-omit-frame-pointer"],
Self::Thread => &["-fsanitize=thread", "-fno-omit-frame-pointer"],
}
}
/// CMake arguments that carry sanitizer linker flags.
///
/// `-DCMAKE_EXE_LINKER_FLAGS` / `-DCMAKE_SHARED_LINKER_FLAGS` are used instead of a
/// dedicated linker-flags field because `CmakeUserConfig` does not have one, and passing
/// them as `-D` arguments is the idiomatic CMake way.
fn cmake_arguments(&self) -> Vec<String> {
let flag = match self {
Self::Address => "-fsanitize=address",
Self::Thread => "-fsanitize=thread",
};
vec![
format!("-DCMAKE_EXE_LINKER_FLAGS={flag}"),
format!("-DCMAKE_SHARED_LINKER_FLAGS={flag}"),
]
}
/// Build a synthetic `ResolvedProfile` with built-in sanitizer flags.
///
/// This is used when the user passes `--asan`/`--tsan` but has not defined a matching
/// `[profile.asan]` / `[profile.tsan]` in CCGO.toml.
pub fn to_resolved_profile(&self) -> ResolvedProfile {
let flags: Vec<String> = self.compiler_flags().iter().map(|s| s.to_string()).collect();
ResolvedProfile {
cmake: ResolvedCmake {
arguments: self.cmake_arguments(),
c_flags: flags.clone(),
cpp_flags: flags,
},
..ResolvedProfile::default()
}
}
}