-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild_spec.rs
579 lines (521 loc) · 20.8 KB
/
build_spec.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright (c) Contributors to the SPK project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/spkenv/spk
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::str::FromStr;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use spk_config::get_config;
use spk_schema_foundation::IsDefault;
use spk_schema_foundation::ident_build::BuildId;
use spk_schema_foundation::name::PkgName;
use spk_schema_foundation::option_map::{HOST_OPTIONS, OptionMap, Stringified};
use spk_schema_foundation::version::Compat;
use strum::Display;
use super::{Opt, ValidationSpec, v0};
use crate::name::{OptName, OptNameBuf};
use crate::option::{PkgOpt, VarOpt};
use crate::{Error, Result, Variant};
#[cfg(test)]
#[path = "./build_spec_test.rs"]
mod build_spec_test;
// Each AutoHostVars value adds a different set of host related options
// when used.
const DISTRO_ADDS: &[&OptName] = &[OptName::os(), OptName::arch(), OptName::distro()];
const ARCH_ADDS: &[&OptName] = &[OptName::os(), OptName::arch()];
const OS_ADDS: &[&OptName] = &[OptName::os()];
const NONE_ADDS: &[&OptName] = &[];
/// Describes what level of cross-platform compatibility the built package
/// should have.
#[derive(
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize, Display, Default,
)]
pub enum AutoHostVars {
/// Package can only be used on the same OS distribution. Adds
/// distro, arch, os, and <distroname> option vars.
#[default]
Distro,
/// Package can be used anywhere that has the same OS and cpu
/// type. Adds distro, and arch options vars.
Arch,
/// Package can be used on the same OS with any cpu or distro. Adds os option var.
Os,
/// Package can be used on any Os. Does not add any option vars.
None,
}
impl AutoHostVars {
fn names_added(&self) -> HashSet<&OptName> {
let names = match self {
AutoHostVars::Distro => DISTRO_ADDS,
AutoHostVars::Arch => ARCH_ADDS,
AutoHostVars::Os => OS_ADDS,
AutoHostVars::None => NONE_ADDS,
};
names.iter().copied().collect::<HashSet<&OptName>>()
}
/// Get host_options after filtering based on the cross Os
/// compatibility setting.
pub fn host_options(&self) -> Result<Vec<Opt>> {
let all_host_options = HOST_OPTIONS.get()?;
let mut settings = Vec::new();
let names_to_add = self.names_added();
for (name, _value) in all_host_options.iter() {
if !names_to_add.contains(&OptName::new(name)?) {
continue;
}
let opt = Opt::Var(VarOpt::new(name)?);
settings.push(opt)
}
let distro_name;
let fallback_name: OptNameBuf;
if AutoHostVars::Distro == *self {
match all_host_options.get(OptName::distro()) {
Some(distro) => {
distro_name = distro.clone();
match OptName::new(&distro_name) {
Ok(name) => {
let mut var_opt = VarOpt::new(name)?;
// Look for any configured compat rules for the
// distro
let config = get_config()?;
if let Some(rule) = config.host_options.distro_rules.get(&distro_name) {
if let Some(compat_rule) = &rule.compat_rule {
var_opt.compat = Some(
Compat::from_str(compat_rule).map_err(|err| {
Error::SpkConfigError(spk_config::Error::Config(config::ConfigError::Message(format!("Invalid compat rule found in config for distro {distro_name}: {err}"))))
})?,
);
}
}
settings.push(Opt::Var(var_opt));
}
Err(err) => {
fallback_name = OptNameBuf::new_lossy(&distro_name);
tracing::warn!(
"Reported distro id ({}) is not a valid var option name: {err}. A {} var will be used instead.",
distro_name.to_string(),
fallback_name
);
settings.push(Opt::Var(VarOpt::new(&fallback_name)?));
}
}
}
None => {
tracing::warn!(
"No distro name set by host. A {}= will be used instead.",
OptName::unknown_distro()
);
settings.push(Opt::Var(VarOpt::new(OptName::unknown_distro())?));
}
}
}
Ok(settings)
}
}
impl IsDefault for AutoHostVars {
fn is_default(&self) -> bool {
self == &Self::default()
}
}
/// A set of structured inputs used to build a package.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct BuildSpec {
pub script: Script,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub options: Vec<Opt>,
/// The raw variant specs as they were parsed from the recipe, so the
/// recipe can be serialized back out with the same variant spec.
#[serde(default, rename = "variants", skip_serializing_if = "Vec::is_empty")]
raw_variants: Vec<v0::VariantSpec>,
/// The parsed variants, which are used for building.
#[serde(skip)]
pub variants: Vec<v0::Variant>,
#[serde(default, skip_serializing_if = "ValidationSpec::is_default")]
pub validation: ValidationSpec,
#[serde(default, skip_serializing_if = "AutoHostVars::is_default")]
pub auto_host_vars: AutoHostVars,
}
impl Default for BuildSpec {
fn default() -> Self {
Self {
script: Script(vec!["sh ./build.sh".into()]),
options: Vec::new(),
raw_variants: Vec::new(),
variants: Vec::new(),
validation: ValidationSpec::default(),
auto_host_vars: AutoHostVars::default(),
}
}
}
impl BuildSpec {
/// Returns this build's options, plus any additional ones needed
/// for building the given variant
pub fn opts_for_variant<V>(&self, variant: &V) -> Result<Vec<Opt>>
where
V: Variant,
{
let mut opts = self.options.clone();
let mut known = opts
.iter()
.map(Opt::full_name)
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let mut known_pkg_options_with_index = opts
.iter()
.enumerate()
.filter_map(|(i, o)| match o {
Opt::Pkg(_) => Some((o.full_name().to_owned(), i)),
_ => None,
})
.collect::<HashMap<_, _>>();
// inject additional package options for items in the variant that
// were not present in the original package
let reqs = variant.additional_requirements().into_owned();
for req in reqs.into_iter() {
let mut opt = Opt::try_from(req)?;
if known.insert(opt.full_name().to_owned()) {
// Maintain pkg index when inserting a new PkgOpt.
if let Opt::Pkg(_) = &opt {
known_pkg_options_with_index.insert(opt.full_name().to_owned(), opts.len());
};
opts.push(opt);
continue;
}
if let Opt::Pkg(pkg) = &mut opt {
// This is an existing PkgOpt; merge the requests.
match known_pkg_options_with_index.get(pkg.pkg.as_opt_name()) {
Some(&idx) => {
match &mut opts[idx] {
Opt::Pkg(pkg_in_opts) => {
// Merge the components of the existing option with the
// additional one(s) from the variant.
let pkg_components = std::mem::take(&mut pkg.components);
pkg_in_opts.components.extend(pkg_components.into_inner());
// The default value is overridden by the
// variant.
pkg_in_opts.default = std::mem::take(&mut pkg.default);
}
Opt::Var(_) => {
debug_assert!(
false,
"known_pkg_options_with_index should only index PkgOpt options"
);
}
};
}
None => {
debug_assert!(
false,
"known_pkg_options_with_index should already contain all PkgOpt names"
);
}
};
}
}
// Add any host options that are not already present.
let host_opts = self.auto_host_vars.host_options()?;
for opt in host_opts.iter() {
if known.insert(opt.full_name().to_owned()) {
opts.push(opt.clone());
}
}
Ok(opts)
}
pub fn resolve_options_for_pkg_name<V>(
&self,
pkg_name: &PkgName,
variant: &V,
) -> Result<(OptionMap, Vec<Opt>)>
where
V: Variant,
{
let given = variant.options();
let opts = self.opts_for_variant(variant)?;
let mut resolved = OptionMap::default();
for opt in &opts {
let given_value = match opt.full_name().namespace() {
Some(_) => given
.get(opt.full_name())
.or_else(|| given.get(opt.full_name().without_namespace())),
None => given
.get(&opt.full_name().with_namespace(pkg_name))
.or_else(|| given.get(opt.full_name())),
};
let value = opt.get_value(given_value.map(String::as_ref));
let compat = opt.validate(Some(&value));
if !compat.is_ok() {
return Err(Error::String(compat.to_string()));
}
resolved.insert(opt.full_name().to_owned(), value);
}
Ok((resolved, opts))
}
/// Add or update an option in this build spec.
///
/// An option is replaced if it shares a name with the given option,
/// otherwise the option is appended to the build options
pub fn upsert_opt(&mut self, opt: Opt) {
for other in self.options.iter_mut() {
if other.full_name() == opt.full_name() {
let _ = std::mem::replace(other, opt);
return;
}
}
self.options.push(opt);
}
pub(crate) fn build_digest<V>(&self, pkg_name: &PkgName, variant: &V) -> Result<BuildId>
where
V: Variant,
{
let (options, opts) = self.resolve_options_for_pkg_name(pkg_name, variant)?;
let mut hasher = ring::digest::Context::new(&ring::digest::SHA1_FOR_LEGACY_USE_ONLY);
for (name, value) in options.iter() {
hasher.update(name.as_bytes());
hasher.update(b"=");
hasher.update(value.as_bytes());
hasher.update(&[0]);
}
for requirement in opts
.into_iter()
.filter_map(|opt| match opt {
Opt::Pkg(pkg) => Some(pkg),
Opt::Var(_) => None,
})
.sorted_unstable_by_key(|o| o.pkg.clone())
{
let PkgOpt {
pkg, components, ..
} = requirement;
if components.is_empty() {
continue;
}
hasher.update(pkg.as_bytes());
hasher.update(b"=");
for component in components.iter() {
// It is not possible to have a custom named component with
// the same name as a reserved name, so taking the stringified
// name is enough to ensure uniqueness.
hasher.update(component.as_str().as_bytes());
hasher.update(b",");
}
hasher.update(&[1]);
}
let digest = hasher.finish();
Ok(BuildId::new_from_bytes(digest.as_ref()))
}
}
impl IsDefault for BuildSpec {
fn is_default(&self) -> bool {
self == &Self::default()
}
}
impl TryFrom<UncheckedBuildSpec> for BuildSpec {
type Error = crate::Error;
fn try_from(bs: UncheckedBuildSpec) -> std::result::Result<Self, Self::Error> {
let bs = unsafe {
// Safety: this function bypasses checks, but we are
// going to perform those checks before returning the value
bs.into_inner()
};
// Calculating the build ids of the variants here would require having
// access to the package name, what option overrides are in effect,
// if host options are are disabled, or what the host options are.
//
// Instead of comparing via build id, we just compare the variant
// content to check that they are unique.
let mut unique_variants = HashMap::new();
for variant in bs.variants.iter() {
let variant_uniqueness_key = {
// OptionMaps are already sorted.
let options = variant.options();
// Sort the additional requirements so two variants with the
// same requirements but in a different order are still
// considered the same.
let requirements = variant
.additional_requirements()
.iter()
.cloned()
.sorted()
.collect::<Vec<_>>();
(options, requirements)
};
let variants_with_key = unique_variants
.entry(variant_uniqueness_key)
.or_insert_with(Vec::new);
variants_with_key.push(variant);
if variants_with_key.len() < 2 {
continue;
}
let details = variants_with_key
.iter()
.map(|o| format!(" - {o:#}"))
.collect::<Vec<_>>()
.join("\n");
return Err(crate::Error::String(format!(
"Multiple variants would produce the same build:\n{details}"
)));
}
Ok(bs)
}
}
impl<'de> Deserialize<'de> for BuildSpec {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
UncheckedBuildSpec::deserialize(deserializer)
.and_then(|bs| bs.try_into().map_err(serde::de::Error::custom))
}
}
/// A [`BuildSpec`] that can be deserialized more forgivingly.
///
/// This exists to help with backwards-compatibility where the data
/// being deserialized must be trusted (eg it's from a repository)
/// but may also not adhere to all of the (potentially new) validation
/// that is done on the normal build spec
pub(crate) struct UncheckedBuildSpec(BuildSpec);
impl UncheckedBuildSpec {
/// Unwrap this instance into a true validated [`BuildSpec`].
///
/// This function is unsafe, [`TryInto::try_into`] can
/// be used instead to perform the necessary validations.
///
/// # Safety:
/// This function bypassed additional
/// validation of the internal build spec data
/// which should usually be done
pub unsafe fn into_inner(self) -> BuildSpec {
self.0
}
}
impl<'de> Deserialize<'de> for UncheckedBuildSpec {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct UncheckedBuildSpecVisitor;
impl<'de> serde::de::Visitor<'de> for UncheckedBuildSpecVisitor {
type Value = UncheckedBuildSpec;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a build specification")
}
fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut variants = Vec::<v0::VariantSpec>::new();
let mut unchecked = BuildSpec::default();
while let Some(key) = map.next_key::<Stringified>()? {
match key.as_str() {
"script" => unchecked.script = map.next_value::<Script>()?,
"options" => {
unchecked.options = map.next_value::<Vec<Opt>>()?;
let mut unique_options = HashSet::new();
for opt in unchecked.options.iter() {
let full_name = opt.full_name();
if unique_options.contains(full_name) {
return Err(serde::de::Error::custom(format!(
"build option was specified more than once: {full_name}",
)));
}
unique_options.insert(full_name);
}
}
"variants" => {
unchecked.raw_variants = map.next_value()?;
}
"validation" => {
unchecked.validation = map.next_value::<ValidationSpec>()?
}
"auto_host_vars" => {
unchecked.auto_host_vars = map.next_value::<AutoHostVars>()?
}
_ => {
// for forwards compatibility we ignore any unrecognized
// field, but consume it just the same
// TODO: could we check for possible typos in here?
map.next_value::<serde::de::IgnoredAny>()?;
}
}
}
if variants.is_empty() {
variants.push(Default::default());
}
// we can only parse out the final variant forms after all the
// build options have been loaded
unchecked.variants = unchecked
.raw_variants
.iter()
.map(|o| v0::Variant::from_spec(o.clone(), &unchecked.options))
.collect::<Result<Vec<_>>>()
.map_err(serde::de::Error::custom)?;
Ok(UncheckedBuildSpec(unchecked))
}
}
deserializer.deserialize_map(UncheckedBuildSpecVisitor)
}
}
/// Some shell script to be executed
#[derive(Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Script(Vec<String>);
impl std::ops::Deref for Script {
type Target = Vec<String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Script {
/// Create a new instance that contains the given lines of script.
pub fn new<I, S>(script: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self(script.into_iter().map(Into::into).collect())
}
}
impl From<Vec<String>> for Script {
fn from(v: Vec<String>) -> Self {
Self(v)
}
}
impl<'de> Deserialize<'de> for Script {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct ScriptVisitor;
impl<'de> serde::de::Visitor<'de> for ScriptVisitor {
type Value = Vec<String>;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a string or list of strings")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut script = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Some(line) = seq.next_element::<Stringified>()? {
script.push(line.0)
}
Ok(script)
}
fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.split('\n').map(String::from).collect())
}
}
deserializer.deserialize_any(ScriptVisitor).map(Self)
}
}
impl serde::ser::Serialize for Script {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_seq(self.0.iter())
}
}