forc_pkg/manifest/
build_profile.rs

1use serde::{Deserialize, Serialize};
2use sway_core::{OptLevel, PrintAsm, PrintIr};
3
4/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
5#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
6#[serde(rename_all = "kebab-case")]
7pub struct BuildProfile {
8    #[serde(default)]
9    pub name: String,
10    #[serde(default)]
11    pub print_ast: bool,
12    pub print_dca_graph: Option<String>,
13    pub print_dca_graph_url_format: Option<String>,
14    #[serde(default)]
15    pub print_ir: PrintIr,
16    #[serde(default)]
17    pub print_asm: PrintAsm,
18    #[serde(default)]
19    pub print_bytecode: bool,
20    #[serde(default)]
21    pub print_bytecode_spans: bool,
22    #[serde(default)]
23    pub terse: bool,
24    #[serde(default)]
25    pub time_phases: bool,
26    #[serde(default)]
27    pub profile: bool,
28    #[serde(default)]
29    pub metrics_outfile: Option<String>,
30    #[serde(default)]
31    pub include_tests: bool,
32    #[serde(default)]
33    pub error_on_warnings: bool,
34    #[serde(default)]
35    pub reverse_results: bool,
36    #[serde(default)]
37    pub optimization_level: OptLevel,
38}
39
40impl BuildProfile {
41    pub const DEBUG: &'static str = "debug";
42    pub const RELEASE: &'static str = "release";
43    pub const DEFAULT: &'static str = Self::DEBUG;
44
45    pub fn debug() -> Self {
46        Self {
47            name: Self::DEBUG.into(),
48            print_ast: false,
49            print_dca_graph: None,
50            print_dca_graph_url_format: None,
51            print_ir: PrintIr::default(),
52            print_asm: PrintAsm::default(),
53            print_bytecode: false,
54            print_bytecode_spans: false,
55            terse: false,
56            time_phases: false,
57            profile: false,
58            metrics_outfile: None,
59            include_tests: false,
60            error_on_warnings: false,
61            reverse_results: false,
62            optimization_level: OptLevel::Opt0,
63        }
64    }
65
66    pub fn release() -> Self {
67        Self {
68            name: Self::RELEASE.to_string(),
69            print_ast: false,
70            print_dca_graph: None,
71            print_dca_graph_url_format: None,
72            print_ir: PrintIr::default(),
73            print_asm: PrintAsm::default(),
74            print_bytecode: false,
75            print_bytecode_spans: false,
76            terse: false,
77            time_phases: false,
78            profile: false,
79            metrics_outfile: None,
80            include_tests: false,
81            error_on_warnings: false,
82            reverse_results: false,
83            optimization_level: OptLevel::Opt1,
84        }
85    }
86
87    pub fn is_release(&self) -> bool {
88        self.name == Self::RELEASE
89    }
90}
91
92impl Default for BuildProfile {
93    fn default() -> Self {
94        Self::debug()
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::{BuildProfile, PackageManifest};
101    use sway_core::{OptLevel, PrintAsm, PrintIr};
102
103    #[test]
104    fn test_build_profiles() {
105        let manifest = PackageManifest::from_dir("./tests/sections").expect("manifest");
106        let build_profiles = manifest.build_profile.expect("build profile");
107        assert_eq!(build_profiles.len(), 4);
108
109        // Standard debug profile without adaptations.
110        let expected = BuildProfile::debug();
111        let profile = build_profiles.get("debug").expect("debug profile");
112        assert_eq!(*profile, expected);
113
114        // Profile based on debug profile with adjusted ASM printing options.
115        let expected = BuildProfile {
116            name: "".into(),
117            print_asm: PrintAsm::r#final(),
118            ..BuildProfile::debug()
119        };
120        let profile = build_profiles.get("custom_asm").expect("custom profile");
121        assert_eq!(*profile, expected);
122
123        // Profile based on debug profile with adjusted IR printing options.
124        let expected = BuildProfile {
125            name: "".into(),
126            print_ir: PrintIr {
127                initial: true,
128                r#final: false,
129                modified_only: true,
130                passes: vec!["dce".to_string(), "sroa".to_string()],
131            },
132            ..BuildProfile::debug()
133        };
134        let profile = build_profiles
135            .get("custom_ir")
136            .expect("custom profile for IR");
137        assert_eq!(*profile, expected);
138
139        // Adapted release profile.
140        let expected = BuildProfile {
141            name: "".into(),
142            print_ast: true,
143            print_dca_graph: Some("dca_graph".into()),
144            print_dca_graph_url_format: Some("print_dca_graph_url_format".into()),
145            print_ir: PrintIr::r#final(),
146            print_asm: PrintAsm::all(),
147            print_bytecode: true,
148            print_bytecode_spans: false,
149            terse: true,
150            time_phases: true,
151            profile: false,
152            metrics_outfile: Some("metrics_outfile".into()),
153            include_tests: true,
154            error_on_warnings: true,
155            reverse_results: true,
156            optimization_level: OptLevel::Opt0,
157        };
158        let profile = build_profiles.get("release").expect("release profile");
159        assert_eq!(*profile, expected);
160    }
161}