cargo_wizard/
predefined.rs

1use crate::template::{dev_profile, release_profile, TemplateItemId};
2use crate::toml::TomlValue;
3use crate::utils::get_core_count;
4use crate::{Template, WizardOptions};
5
6/// Enumeration of predefined templates.
7#[derive(clap::ValueEnum, Clone, Copy, Debug)]
8pub enum PredefinedTemplateKind {
9    /// Profile designed for fast compilation times.
10    FastCompile,
11    /// Profile designed for fast runtime performance.
12    FastRuntime,
13    /// Profile designed for minimal binary size.
14    MinSize,
15}
16
17impl PredefinedTemplateKind {
18    pub fn build_template(&self, options: &WizardOptions) -> Template {
19        match self {
20            PredefinedTemplateKind::FastCompile => fast_compile_template(options),
21            PredefinedTemplateKind::FastRuntime => fast_runtime_template(),
22            PredefinedTemplateKind::MinSize => min_size_template(),
23        }
24    }
25}
26
27/// Template that focuses on quick compile time.
28pub fn fast_compile_template(options: &WizardOptions) -> Template {
29    let mut builder = dev_profile().item(TemplateItemId::DebugInfo, TomlValue::int(0));
30
31    #[cfg(unix)]
32    {
33        builder = builder.item(TemplateItemId::Linker, TomlValue::string("lld"));
34    }
35
36    if options.nightly_items_enabled() {
37        builder = builder
38            .item(
39                TemplateItemId::CodegenBackend,
40                TomlValue::string("cranelift"),
41            )
42            .item(
43                TemplateItemId::FrontendThreads,
44                TomlValue::Int(get_core_count()),
45            )
46    }
47    builder.build()
48}
49
50/// Template that focuses on maximum runtime performance.
51pub fn fast_runtime_template() -> Template {
52    release_profile()
53        .item(TemplateItemId::Lto, TomlValue::bool(true))
54        .item(TemplateItemId::CodegenUnits, TomlValue::int(1))
55        .item(TemplateItemId::Panic, TomlValue::string("abort"))
56        .item(
57            TemplateItemId::TargetCpuInstructionSet,
58            TomlValue::string("native"),
59        )
60        .build()
61}
62
63/// Template that template focuses on minimal binary size.
64pub fn min_size_template() -> Template {
65    release_profile()
66        .item(TemplateItemId::DebugInfo, TomlValue::bool(false))
67        .item(TemplateItemId::Strip, TomlValue::bool(true))
68        .item(TemplateItemId::Lto, TomlValue::bool(true))
69        .item(TemplateItemId::OptimizationLevel, TomlValue::string("z"))
70        .item(TemplateItemId::CodegenUnits, TomlValue::int(1))
71        .item(TemplateItemId::Panic, TomlValue::string("abort"))
72        .build()
73}
74
75/// Test that the predefined templates can be created without panicking.
76#[cfg(test)]
77mod tests {
78    use crate::{fast_compile_template, fast_runtime_template, min_size_template, WizardOptions};
79
80    #[test]
81    fn create_fast_compile_template() {
82        fast_compile_template(&WizardOptions::default());
83    }
84
85    #[test]
86    fn create_fast_runtime_template() {
87        fast_runtime_template();
88    }
89
90    #[test]
91    fn create_min_size_template() {
92        min_size_template();
93    }
94}