creator_tools/commands/android/
rust_compile.rs

1use crate::commands::common::cargo_rustc_command;
2use crate::error::*;
3use crate::tools::*;
4use crate::types::*;
5use std::path::Path;
6
7/// Compiles rust code for android.
8pub fn compile_rust_for_android(
9    ndk: &AndroidNdk,
10    target: Target,
11    build_target: AndroidTarget,
12    project_path: &Path,
13    profile: Profile,
14    features: Vec<String>,
15    all_features: bool,
16    no_default_features: bool,
17    target_sdk_version: u32,
18) -> Result<()> {
19    let crate_types = vec![CrateType::Cdylib];
20    let mut cargo = cargo_rustc_command(
21        &target,
22        project_path,
23        &profile,
24        &features,
25        all_features,
26        no_default_features,
27        &build_target.into(),
28        &crate_types,
29    );
30    let triple = build_target.rust_triple();
31    // Takes clang and clang_pp paths
32    let (clang, clang_pp) = ndk.clang(build_target, target_sdk_version)?;
33    cargo.env(format!("CC_{}", triple), &clang);
34    cargo.env(format!("CXX_{}", triple), &clang_pp);
35    cargo.env(cargo_env_target_cfg("LINKER", triple), &clang);
36    let ar = ndk.toolchain_bin("ar", build_target)?;
37    cargo.env(format!("AR_{}", triple), &ar);
38    cargo.env(cargo_env_target_cfg("AR", triple), &ar);
39    cargo.output_err(true)?;
40    Ok(())
41}
42
43fn cargo_env_target_cfg(tool: &str, target: &str) -> String {
44    let utarget = target.replace("-", "_");
45    let env = format!("CARGO_TARGET_{}_{}", &utarget, tool);
46    env.to_uppercase()
47}