ndk_build/
target.rs

1use crate::error::NdkError;
2use serde::Deserialize;
3
4#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
5#[repr(u8)]
6pub enum Target {
7    #[serde(rename = "armv7-linux-androideabi")]
8    ArmV7a = 1,
9    #[serde(rename = "aarch64-linux-android")]
10    Arm64V8a = 2,
11    #[serde(rename = "i686-linux-android")]
12    X86 = 3,
13    #[serde(rename = "x86_64-linux-android")]
14    X86_64 = 4,
15}
16
17impl Target {
18    /// Identifier used in the NDK to refer to the ABI
19    pub fn android_abi(self) -> &'static str {
20        match self {
21            Self::Arm64V8a => "arm64-v8a",
22            Self::ArmV7a => "armeabi-v7a",
23            Self::X86 => "x86",
24            Self::X86_64 => "x86_64",
25        }
26    }
27
28    /// Returns `Target` for abi.
29    pub fn from_android_abi(abi: &str) -> Result<Self, NdkError> {
30        match abi {
31            "arm64-v8a" => Ok(Self::Arm64V8a),
32            "armeabi-v7a" => Ok(Self::ArmV7a),
33            "x86" => Ok(Self::X86),
34            "x86_64" => Ok(Self::X86_64),
35            _ => Err(NdkError::UnsupportedTarget),
36        }
37    }
38
39    /// Returns the triple used by the rust build tools
40    pub fn rust_triple(self) -> &'static str {
41        match self {
42            Self::Arm64V8a => "aarch64-linux-android",
43            Self::ArmV7a => "armv7-linux-androideabi",
44            Self::X86 => "i686-linux-android",
45            Self::X86_64 => "x86_64-linux-android",
46        }
47    }
48
49    /// Returns `Target` for rust triple.
50    pub fn from_rust_triple(triple: &str) -> Result<Self, NdkError> {
51        match triple {
52            "aarch64-linux-android" => Ok(Self::Arm64V8a),
53            "armv7-linux-androideabi" => Ok(Self::ArmV7a),
54            "i686-linux-android" => Ok(Self::X86),
55            "x86_64-linux-android" => Ok(Self::X86_64),
56            _ => Err(NdkError::UnsupportedTarget),
57        }
58    }
59
60    // Returns the triple NDK provided LLVM
61    pub fn ndk_llvm_triple(self) -> &'static str {
62        match self {
63            Self::Arm64V8a => "aarch64-linux-android",
64            Self::ArmV7a => "armv7a-linux-androideabi",
65            Self::X86 => "i686-linux-android",
66            Self::X86_64 => "x86_64-linux-android",
67        }
68    }
69
70    /// Returns the triple used by the non-LLVM parts of the NDK
71    pub fn ndk_triple(self) -> &'static str {
72        match self {
73            Self::Arm64V8a => "aarch64-linux-android",
74            Self::ArmV7a => "arm-linux-androideabi",
75            Self::X86 => "i686-linux-android",
76            Self::X86_64 => "x86_64-linux-android",
77        }
78    }
79}