forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_config.rs
162 lines (149 loc) · 4.43 KB
/
build_config.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
158
159
160
161
162
use std::{path::PathBuf, sync::Arc};
use serde::{Deserialize, Serialize};
use strum::EnumString;
#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
clap::ValueEnum,
EnumString,
)]
pub enum BuildTarget {
#[default]
#[serde(rename = "fuel")]
#[clap(name = "fuel")]
#[strum(serialize = "fuel")]
Fuel,
#[serde(rename = "evm")]
#[clap(name = "evm")]
#[strum(serialize = "evm")]
EVM,
#[serde(rename = "midenvm")]
#[clap(name = "midenvm")]
#[strum(serialize = "midenvm")]
MidenVM,
}
/// Configuration for the overall build and compilation process.
#[derive(Clone)]
pub struct BuildConfig {
// Build target for code generation.
pub(crate) build_target: BuildTarget,
// The canonical file path to the root module.
// E.g. `/home/user/project/src/main.sw`.
pub(crate) canonical_root_module: Arc<PathBuf>,
pub(crate) print_dca_graph: Option<String>,
pub(crate) print_dca_graph_url_format: Option<String>,
pub(crate) print_intermediate_asm: bool,
pub(crate) print_finalized_asm: bool,
pub(crate) print_ir: bool,
pub(crate) include_tests: bool,
pub time_phases: bool,
pub metrics_outfile: Option<String>,
}
impl BuildConfig {
/// Construct a `BuildConfig` from a relative path to the root module and the canonical path to
/// the manifest directory.
///
/// The `root_module` path must be either canonical, or relative to the directory containing
/// the manifest. E.g. `project/src/main.sw` or `project/src/lib.sw`.
///
/// The `canonical_manifest_dir` must be the canonical (aka absolute) path to the directory
/// containing the `Forc.toml` file for the project. E.g. `/home/user/project`.
pub fn root_from_file_name_and_manifest_path(
root_module: PathBuf,
canonical_manifest_dir: PathBuf,
build_target: BuildTarget,
) -> Self {
assert!(
canonical_manifest_dir.has_root(),
"manifest dir must be a canonical path",
);
let canonical_root_module = match root_module.has_root() {
true => root_module,
false => {
assert!(
root_module.starts_with(canonical_manifest_dir.file_stem().unwrap()),
"file_name must be either absolute or relative to manifest directory",
);
canonical_manifest_dir
.parent()
.expect("unable to retrieve manifest directory parent")
.join(&root_module)
}
};
Self {
build_target,
canonical_root_module: Arc::new(canonical_root_module),
print_dca_graph: None,
print_dca_graph_url_format: None,
print_intermediate_asm: false,
print_finalized_asm: false,
print_ir: false,
include_tests: false,
time_phases: false,
metrics_outfile: None,
}
}
pub fn print_dca_graph(self, a: Option<String>) -> Self {
Self {
print_dca_graph: a,
..self
}
}
pub fn print_dca_graph_url_format(self, a: Option<String>) -> Self {
Self {
print_dca_graph_url_format: a,
..self
}
}
pub fn print_intermediate_asm(self, a: bool) -> Self {
Self {
print_intermediate_asm: a,
..self
}
}
pub fn print_finalized_asm(self, a: bool) -> Self {
Self {
print_finalized_asm: a,
..self
}
}
pub fn print_ir(self, a: bool) -> Self {
Self {
print_ir: a,
..self
}
}
pub fn time_phases(self, a: bool) -> Self {
Self {
time_phases: a,
..self
}
}
pub fn metrics(self, a: Option<String>) -> Self {
Self {
metrics_outfile: a,
..self
}
}
/// Whether or not to include test functions in parsing, type-checking and codegen.
///
/// This should be set to `true` by invocations like `forc test` or `forc check --tests`.
///
/// Default: `false`
pub fn include_tests(self, include_tests: bool) -> Self {
Self {
include_tests,
..self
}
}
pub fn canonical_root_module(&self) -> Arc<PathBuf> {
self.canonical_root_module.clone()
}
}