forc_pkg/manifest/
build_profile.rs

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