forc_pkg/manifest/
build_profile.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use serde::{Deserialize, Serialize};
use sway_core::{OptLevel, PrintAsm, PrintIr};

/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct BuildProfile {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub print_ast: bool,
    pub print_dca_graph: Option<String>,
    pub print_dca_graph_url_format: Option<String>,
    #[serde(default)]
    pub print_ir: PrintIr,
    #[serde(default)]
    pub print_asm: PrintAsm,
    #[serde(default)]
    pub print_bytecode: bool,
    #[serde(default)]
    pub print_bytecode_spans: bool,
    #[serde(default)]
    pub terse: bool,
    #[serde(default)]
    pub time_phases: bool,
    #[serde(default)]
    pub profile: bool,
    #[serde(default)]
    pub metrics_outfile: Option<String>,
    #[serde(default)]
    pub include_tests: bool,
    #[serde(default)]
    pub error_on_warnings: bool,
    #[serde(default)]
    pub reverse_results: bool,
    #[serde(default)]
    pub optimization_level: OptLevel,
}

impl BuildProfile {
    pub const DEBUG: &'static str = "debug";
    pub const RELEASE: &'static str = "release";
    pub const DEFAULT: &'static str = Self::DEBUG;

    pub fn debug() -> Self {
        Self {
            name: Self::DEBUG.into(),
            print_ast: false,
            print_dca_graph: None,
            print_dca_graph_url_format: None,
            print_ir: PrintIr::default(),
            print_asm: PrintAsm::default(),
            print_bytecode: false,
            print_bytecode_spans: false,
            terse: false,
            time_phases: false,
            profile: false,
            metrics_outfile: None,
            include_tests: false,
            error_on_warnings: false,
            reverse_results: false,
            optimization_level: OptLevel::Opt0,
        }
    }

    pub fn release() -> Self {
        Self {
            name: Self::RELEASE.to_string(),
            print_ast: false,
            print_dca_graph: None,
            print_dca_graph_url_format: None,
            print_ir: PrintIr::default(),
            print_asm: PrintAsm::default(),
            print_bytecode: false,
            print_bytecode_spans: false,
            terse: false,
            time_phases: false,
            profile: false,
            metrics_outfile: None,
            include_tests: false,
            error_on_warnings: false,
            reverse_results: false,
            optimization_level: OptLevel::Opt1,
        }
    }
}

impl Default for BuildProfile {
    fn default() -> Self {
        Self::debug()
    }
}

#[cfg(test)]
mod tests {
    use crate::{BuildProfile, PackageManifest};
    use sway_core::{OptLevel, PrintAsm, PrintIr};

    #[test]
    fn test_build_profiles() {
        let manifest = PackageManifest::from_dir("./tests/sections").expect("manifest");
        let build_profiles = manifest.build_profile.expect("build profile");
        assert_eq!(build_profiles.len(), 4);

        // Standard debug profile without adaptations.
        let expected = BuildProfile::debug();
        let profile = build_profiles.get("debug").expect("debug profile");
        assert_eq!(*profile, expected);

        // Profile based on debug profile with adjusted ASM printing options.
        let expected = BuildProfile {
            name: "".into(),
            print_asm: PrintAsm::r#final(),
            ..BuildProfile::debug()
        };
        let profile = build_profiles.get("custom_asm").expect("custom profile");
        assert_eq!(*profile, expected);

        // Profile based on debug profile with adjusted IR printing options.
        let expected = BuildProfile {
            name: "".into(),
            print_ir: PrintIr {
                initial: true,
                r#final: false,
                modified_only: true,
                passes: vec!["dce".to_string(), "sroa".to_string()],
            },
            ..BuildProfile::debug()
        };
        let profile = build_profiles
            .get("custom_ir")
            .expect("custom profile for IR");
        assert_eq!(*profile, expected);

        // Adapted release profile.
        let expected = BuildProfile {
            name: "".into(),
            print_ast: true,
            print_dca_graph: Some("dca_graph".into()),
            print_dca_graph_url_format: Some("print_dca_graph_url_format".into()),
            print_ir: PrintIr::r#final(),
            print_asm: PrintAsm::all(),
            print_bytecode: true,
            print_bytecode_spans: false,
            terse: true,
            time_phases: true,
            profile: false,
            metrics_outfile: Some("metrics_outfile".into()),
            include_tests: true,
            error_on_warnings: true,
            reverse_results: true,
            optimization_level: OptLevel::Opt0,
        };
        let profile = build_profiles.get("release").expect("release profile");
        assert_eq!(*profile, expected);
    }
}