-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrecipe.rs
231 lines (192 loc) · 6.13 KB
/
recipe.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Contributors to the SPK project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/spkenv/spk
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::Path;
use spk_schema_ident::VersionIdent;
use crate::foundation::ident_build::BuildId;
use crate::foundation::option_map::OptionMap;
use crate::foundation::spec_ops::{Named, Versioned};
use crate::metadata::Meta;
use crate::{InputVariant, Package, RequirementsList, Result, TestStage, Variant};
/// Return the resolved packages from a solution.
pub trait BuildEnv {
type Package: super::Package;
fn build_env(&self) -> Vec<Self::Package>;
fn env_vars(&self) -> HashMap<String, String>;
}
/// An empty build environment implementation, primarily used in testing
impl BuildEnv for () {
type Package = crate::Spec;
fn build_env(&self) -> Vec<Self::Package> {
Vec::new()
}
fn env_vars(&self) -> HashMap<String, String> {
HashMap::default()
}
}
/// Can be used to build a package.
#[enum_dispatch::enum_dispatch]
pub trait Recipe:
Named
+ Versioned
+ super::Deprecate
+ crate::RuntimeEnvironment
+ Clone
+ Eq
+ std::hash::Hash
+ Sync
+ Send
{
type Output: super::Package;
type Variant: super::Variant + Clone;
type Test: super::Test;
/// Build an identifier to represent this recipe.
///
/// The returned identifier will never have an associated build.
fn ident(&self) -> &VersionIdent;
/// Calculate the build digest that would be produced by building this
/// recipe with the given options.
fn build_digest<V>(&self, variant: &V) -> Result<BuildId>
where
V: Variant;
/// Return the default variants defined in this recipe.
///
/// The prevailing option overrides are needed to return a correct default
/// variant, including host options if they are enabled.
fn default_variants(&self, options: &OptionMap) -> Cow<'_, Vec<Self::Variant>>;
/// Produce the full set of build options given the inputs.
///
/// The returned option map will include any values from the inputs
/// that are relevant to this recipe with the addition of any missing
/// default values. Any issues or invalid inputs results in an error.
fn resolve_options<V>(&self, variant: &V) -> Result<OptionMap>
where
V: Variant;
/// Identify the requirements for a build of this recipe.
///
/// This should also validate and include the items specified
/// by [`Variant::additional_requirements`].
fn get_build_requirements<V>(&self, variant: &V) -> Result<Cow<'_, RequirementsList>>
where
V: Variant;
/// Return the tests defined for this package at the given stage.
fn get_tests<V>(&self, stage: TestStage, variant: &V) -> Result<Vec<Self::Test>>
where
V: Variant;
/// Create a new source package from this recipe and the given parameters.
fn generate_source_build(&self, root: &Path) -> Result<Self::Output>;
/// Create a new binary package from this recipe and the given parameters.
fn generate_binary_build<V, E, P>(&self, variant: &V, build_env: &E) -> Result<Self::Output>
where
V: InputVariant,
E: BuildEnv<Package = P>,
P: Package;
/// Return the metadata for this package.
fn metadata(&self) -> &Meta;
}
impl<T> Recipe for std::sync::Arc<T>
where
T: Recipe,
{
type Output = T::Output;
type Variant = T::Variant;
type Test = T::Test;
fn ident(&self) -> &VersionIdent {
(**self).ident()
}
fn build_digest<V>(&self, variant: &V) -> Result<BuildId>
where
V: Variant,
{
(**self).build_digest(variant)
}
fn default_variants(&self, options: &OptionMap) -> Cow<'_, Vec<Self::Variant>> {
(**self).default_variants(options)
}
fn resolve_options<V>(&self, variant: &V) -> Result<OptionMap>
where
V: Variant,
{
(**self).resolve_options(variant)
}
fn get_build_requirements<V>(&self, variant: &V) -> Result<Cow<'_, RequirementsList>>
where
V: Variant,
{
(**self).get_build_requirements(variant)
}
fn get_tests<V>(&self, stage: TestStage, variant: &V) -> Result<Vec<Self::Test>>
where
V: Variant,
{
(**self).get_tests(stage, variant)
}
fn generate_source_build(&self, root: &Path) -> Result<Self::Output> {
(**self).generate_source_build(root)
}
fn generate_binary_build<V, E, P>(&self, variant: &V, build_env: &E) -> Result<Self::Output>
where
V: InputVariant,
E: BuildEnv<Package = P>,
P: Package,
{
(**self).generate_binary_build(variant, build_env)
}
fn metadata(&self) -> &Meta {
(**self).metadata()
}
}
impl<T> Recipe for &T
where
T: Recipe,
{
type Output = T::Output;
type Variant = T::Variant;
type Test = T::Test;
fn ident(&self) -> &VersionIdent {
(**self).ident()
}
fn build_digest<V>(&self, variant: &V) -> Result<BuildId>
where
V: Variant,
{
(**self).build_digest(variant)
}
fn default_variants(&self, options: &OptionMap) -> Cow<'_, Vec<Self::Variant>> {
(**self).default_variants(options)
}
fn resolve_options<V>(&self, variant: &V) -> Result<OptionMap>
where
V: Variant,
{
(**self).resolve_options(variant)
}
fn get_build_requirements<V>(&self, variant: &V) -> Result<Cow<'_, RequirementsList>>
where
V: Variant,
{
(**self).get_build_requirements(variant)
}
fn get_tests<V>(&self, stage: TestStage, variant: &V) -> Result<Vec<Self::Test>>
where
V: Variant,
{
(**self).get_tests(stage, variant)
}
fn generate_source_build(&self, root: &Path) -> Result<Self::Output> {
(**self).generate_source_build(root)
}
fn generate_binary_build<V, E, P>(&self, variant: &V, build_env: &E) -> Result<Self::Output>
where
V: InputVariant,
E: BuildEnv<Package = P>,
P: Package,
{
(**self).generate_binary_build(variant, build_env)
}
fn metadata(&self) -> &Meta {
(**self).metadata()
}
}