creator_tools/commands/common/
rust_compile.rs

1use crate::types::*;
2use std::path::Path;
3use std::process::Command;
4
5/// Initialises `cargo rustc` [`Command`] with given args and return it.
6///
7/// [`Command`]: std::process::Command
8pub fn cargo_rustc_command(
9    target: &Target,
10    project_path: &Path,
11    profile: &Profile,
12    features: &[String],
13    all_features: bool,
14    no_default_features: bool,
15    build_target: &BuildTarget,
16    crate_types: &[CrateType],
17) -> Command {
18    let mut cargo = Command::new("cargo");
19    cargo.arg("rustc");
20    match &target {
21        Target::Bin(name) => cargo.args(&["--bin", name]),
22        Target::Example(name) => cargo.args(&["--example", name]),
23        Target::Lib => cargo.arg("--lib"),
24    };
25    cargo.current_dir(project_path);
26    if profile == &Profile::Release {
27        cargo.arg("--release");
28    };
29    for feature in features.iter() {
30        cargo.args(&["--feature", feature]);
31    }
32    if all_features {
33        cargo.arg("--all-features");
34    };
35    if no_default_features {
36        cargo.arg("--no-default-features");
37    };
38    let triple = build_target.rust_triple();
39    cargo.args(&["--target", triple]);
40    if !crate_types.is_empty() {
41        // Creates a comma-separated string
42        let crate_types: String =
43            itertools::Itertools::intersperse(crate_types.iter().map(|v| v.as_ref()), ",")
44                .collect();
45        cargo.args(&["--", "--crate-type", &crate_types]);
46    };
47    cargo
48}