forc_pkg/manifest/
build_profile.rs

1use serde::{Deserialize, Serialize};
2use sway_core::{Backtrace, 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    #[serde(default)]
39    pub backtrace: Backtrace,
40}
41
42impl BuildProfile {
43    pub const DEBUG: &'static str = "debug";
44    pub const RELEASE: &'static str = "release";
45    pub const DEFAULT: &'static str = Self::DEBUG;
46
47    pub fn debug() -> Self {
48        Self {
49            name: Self::DEBUG.into(),
50            print_ast: false,
51            print_dca_graph: None,
52            print_dca_graph_url_format: None,
53            print_ir: PrintIr::default(),
54            print_asm: PrintAsm::default(),
55            print_bytecode: false,
56            print_bytecode_spans: false,
57            terse: false,
58            time_phases: false,
59            profile: false,
60            metrics_outfile: None,
61            include_tests: false,
62            error_on_warnings: false,
63            reverse_results: false,
64            optimization_level: OptLevel::Opt0,
65            backtrace: Backtrace::AllExceptNever,
66        }
67    }
68
69    pub fn release() -> Self {
70        Self {
71            name: Self::RELEASE.to_string(),
72            print_ast: false,
73            print_dca_graph: None,
74            print_dca_graph_url_format: None,
75            print_ir: PrintIr::default(),
76            print_asm: PrintAsm::default(),
77            print_bytecode: false,
78            print_bytecode_spans: false,
79            terse: false,
80            time_phases: false,
81            profile: false,
82            metrics_outfile: None,
83            include_tests: false,
84            error_on_warnings: false,
85            reverse_results: false,
86            optimization_level: OptLevel::Opt1,
87            backtrace: Backtrace::OnlyAlways,
88        }
89    }
90
91    pub fn is_release(&self) -> bool {
92        self.name == Self::RELEASE
93    }
94}
95
96impl Default for BuildProfile {
97    fn default() -> Self {
98        Self::debug()
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use crate::{BuildProfile, PackageManifest};
105    use sway_core::{Backtrace, OptLevel, PrintAsm, PrintIr};
106
107    #[test]
108    fn test_build_profiles() {
109        let manifest = PackageManifest::from_dir("./tests/sections").expect("manifest");
110        let build_profiles = manifest.build_profile.expect("build profile");
111        assert_eq!(build_profiles.len(), 5);
112
113        // Standard debug profile without adaptations.
114        let expected = BuildProfile::debug();
115        let profile = build_profiles.get("debug").expect("debug profile");
116        assert_eq!(*profile, expected);
117
118        // Profile based on debug profile with adjusted ASM printing options.
119        let expected = BuildProfile {
120            name: "".into(),
121            print_asm: PrintAsm::r#final(),
122            ..BuildProfile::debug()
123        };
124        let profile = build_profiles.get("custom_asm").expect("custom profile");
125        assert_eq!(*profile, expected);
126
127        // Profile based on debug profile with adjusted IR printing options.
128        let expected = BuildProfile {
129            name: "".into(),
130            print_ir: PrintIr {
131                initial: true,
132                r#final: false,
133                modified_only: true,
134                passes: vec!["dce".to_string(), "sroa".to_string()],
135            },
136            ..BuildProfile::debug()
137        };
138        let profile = build_profiles
139            .get("custom_ir")
140            .expect("custom profile for IR");
141        assert_eq!(*profile, expected);
142
143        // Profile based on debug profile with adjusted backtrace option.
144        let expected = BuildProfile {
145            name: "".into(),
146            backtrace: Backtrace::OnlyAlways,
147            ..BuildProfile::debug()
148        };
149        let profile = build_profiles
150            .get("custom_backtrace")
151            .expect("custom profile for backtrace");
152        assert_eq!(*profile, expected);
153
154        // Adapted release profile.
155        let expected = BuildProfile {
156            name: "".into(),
157            print_ast: true,
158            print_dca_graph: Some("dca_graph".into()),
159            print_dca_graph_url_format: Some("print_dca_graph_url_format".into()),
160            print_ir: PrintIr::r#final(),
161            print_asm: PrintAsm::all(),
162            print_bytecode: true,
163            print_bytecode_spans: false,
164            terse: true,
165            time_phases: true,
166            profile: false,
167            metrics_outfile: Some("metrics_outfile".into()),
168            include_tests: true,
169            error_on_warnings: true,
170            reverse_results: true,
171            optimization_level: OptLevel::Opt0,
172            backtrace: Backtrace::None,
173        };
174        let profile = build_profiles.get("release").expect("release profile");
175        assert_eq!(*profile, expected);
176    }
177}