bender 0.31.0

A dependency management tool for hardware projects.
Documentation
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
// Copyright (c) 2017-2018 ETH Zurich
// Fabian Schuiki <fschuiki@iis.ee.ethz.ch>

//! A source code manifest.
//!
//! This module implements a source code manifest.

#![deny(missing_docs)]

use std::fmt;
use std::iter::FromIterator;
use std::path::Path;

use indexmap::{IndexMap, IndexSet};
use serde::{Serialize, Serializer};

use crate::config::{Validate, ValidationContext};
use crate::diagnostic::{Diagnostics, Warnings};
use crate::error::Error;
use crate::target::{TargetSet, TargetSpec};
use semver;

/// A source file group.
#[derive(Serialize, Clone, Debug, Default)]
pub struct SourceGroup<'ctx> {
    /// The package which this source group represents.
    pub package: Option<&'ctx str>,
    /// Whether the source files in this group can be treated in parallel.
    pub independent: bool,
    /// The targets for which the sources should be considered.
    pub target: TargetSpec,
    /// The directories to search for include files, with their target specs.
    pub include_dirs: Vec<(TargetSpec, &'ctx Path)>,
    /// The directories exported by dependent package for include files, with their target specs.
    pub export_incdirs: IndexMap<String, Vec<(TargetSpec, &'ctx Path)>>,
    /// The preprocessor definitions.
    pub defines: IndexMap<String, Option<&'ctx str>>,
    /// The files in this group.
    pub files: Vec<SourceFile<'ctx>>,
    /// Package dependencies of this source group
    pub dependencies: IndexSet<String>,
    /// Version information of the package
    pub version: Option<semver::Version>,
    /// This group will override files in previous packages
    pub override_files: bool,
}

impl<'ctx> Validate for SourceGroup<'ctx> {
    type Output = SourceGroup<'ctx>;
    type Error = Error;
    fn validate(self, vctx: &ValidationContext) -> crate::error::Result<SourceGroup<'ctx>> {
        Ok(SourceGroup {
            files: self
                .files
                .into_iter()
                .map(|f| f.validate(vctx))
                .collect::<Result<Vec<_>, Error>>()?,
            include_dirs: self
                .include_dirs
                .into_iter()
                .map(|(trgt, p)| {
                    if !p.exists() || !p.is_dir() {
                        Warnings::IncludeDirMissing(p.to_path_buf()).emit();
                    }
                    Ok((trgt, p))
                })
                .collect::<Result<Vec<_>, Error>>()?,
            export_incdirs: self
                .export_incdirs
                .into_iter()
                .map(|(pkg, dirs)| {
                    let checked = dirs
                        .into_iter()
                        .map(|(trgt, p)| {
                            if !p.exists() || !p.is_dir() {
                                Warnings::IncludeDirMissing(p.to_path_buf()).emit();
                            }
                            Ok((trgt, p))
                        })
                        .collect::<Result<Vec<_>, Error>>()?;
                    Ok((pkg, checked))
                })
                .collect::<Result<IndexMap<_, _>, Error>>()?,
            ..self
        })
    }
}

impl<'ctx> SourceGroup<'ctx> {
    /// Simplify the source group. Removes empty subgroups and inlines subgroups
    /// with the same configuration.
    pub fn simplify(self) -> Self {
        let files = self
            .files
            .into_iter()
            .filter_map(|s| match s {
                SourceFile::Group(group) => {
                    let group = group.simplify();

                    // Discard empty groups.
                    if group.files.is_empty() {
                        return None;
                    }

                    // Drop groups with only one file.
                    if group.files.len() == 1
                        && group.include_dirs.is_empty()
                        && group.defines.is_empty()
                        && group.target.is_wildcard()
                        && group.package.is_none()
                    {
                        return Some(group.files.into_iter().next().unwrap());
                    }

                    // Preserve the rest.
                    Some(SourceFile::Group(Box::new(group)))
                }
                other => Some(other),
            })
            .collect();
        SourceGroup { files, ..self }
    }

    /// Filter the sources, keeping only the ones that apply to a target.
    pub fn filter_targets(&self, targets: &TargetSet) -> Option<SourceGroup<'ctx>> {
        let all_targets = match self.package {
            Some(pkg) => targets.reduce_for_dependency(pkg),
            None => targets.clone(),
        };

        if !self.target.matches(&all_targets) {
            return None;
        }
        let files = self
            .files
            .iter()
            .filter_map(|file| match *file {
                SourceFile::Group(ref group) => group
                    .filter_targets(targets)
                    .map(|g| SourceFile::Group(Box::new(g))),
                ref other => Some(other.clone()),
            })
            .collect();
        let include_dirs = self
            .include_dirs
            .iter()
            .filter(|(trgt, _)| trgt.matches(&all_targets))
            .cloned()
            .collect();

        let mut target_defs = all_targets
            .into_iter()
            .map(|t| "TARGET_".to_owned() + &t.to_uppercase())
            .collect::<IndexSet<_>>();
        target_defs.sort();
        let mut defines: IndexMap<String, Option<&str>> = self.defines.clone();
        if self.package.is_some() {
            defines.extend(target_defs.into_iter().map(|t| (t, None)));
        }

        let export_incdirs = self
            .export_incdirs
            .iter()
            .map(|(pkg, dirs)| {
                let pkg_targets = targets.reduce_for_dependency(pkg.as_str());
                let filtered = dirs
                    .iter()
                    .filter(|(trgt, _)| trgt.matches(&pkg_targets))
                    .cloned()
                    .collect();
                (pkg.clone(), filtered)
            })
            .collect();

        Some(
            SourceGroup {
                include_dirs,
                defines: defines.clone(),
                export_incdirs,
                files,
                ..self.clone()
            }
            .simplify(),
        )
    }

    /// Assigns target to SourceGroup without target
    pub fn assign_target(&self, target: String) -> SourceGroup<'ctx> {
        let files = self
            .files
            .iter()
            .filter_map(|file| match *file {
                SourceFile::Group(ref group) => Some(group.assign_target(target.clone()))
                    .map(|g| SourceFile::Group(Box::new(g))),
                ref other => Some(other.clone()),
            })
            .collect();

        SourceGroup {
            target: if self.target.is_wildcard() {
                TargetSpec::Name(target)
            } else {
                self.target.clone()
            },
            files,
            ..self.clone()
        }
    }

    /// Recursively get dependency names.
    fn get_deps(
        &self,
        packages: &IndexSet<String>,
        excludes: &IndexSet<String>,
    ) -> IndexSet<String> {
        let mut result = packages.clone();

        if let Some(x) = self.package {
            if result.contains(x) {
                result.extend(IndexSet::<String>::from_iter(self.dependencies.clone()));
                result = &result - excludes;
            }
        }

        for file in &self.files {
            if let SourceFile::Group(group) = file {
                result.extend(group.get_deps(&result, excludes));
            }
        }

        result
    }

    /// Get list of packages based on constraints.
    pub fn get_package_list(
        &self,
        top_package: String,
        packages: &IndexSet<String>,
        excludes: &IndexSet<String>,
        no_deps: bool,
    ) -> IndexSet<String> {
        let mut result = IndexSet::new();

        if !packages.is_empty() {
            result.extend(packages.clone());
        } else {
            result.insert(top_package);
        }

        result = &result - excludes;

        if !no_deps {
            let mut curr_length = 0;
            while curr_length < result.len() {
                curr_length = result.len();
                result.extend(self.get_deps(&result, excludes));
            }
        }

        result
    }

    /// Filter the sources, keeping only the ones that apply to the selected packages.
    pub fn filter_packages(&self, packages: &IndexSet<String>) -> Option<SourceGroup<'ctx>> {
        let mut files = Vec::new();

        if self.package.is_none() || packages.contains(self.package.unwrap()) {
            files = self
                .files
                .iter()
                .filter_map(|file| match *file {
                    SourceFile::Group(ref group) => group
                        .filter_packages(packages)
                        .map(|g| SourceFile::Group(Box::new(g))),
                    ref other => Some(other.clone()),
                })
                .collect();
        }

        let export_incdirs = self.export_incdirs.clone();
        Some(
            SourceGroup {
                export_incdirs,
                files,
                ..self.clone()
            }
            .simplify(),
        )
    }

    /// Return list of unique include directories for the current src
    pub fn get_incdirs(self) -> Vec<&'ctx Path> {
        let incdirs = self
            .include_dirs
            .into_iter()
            .map(|(_, path)| path)
            .chain(
                self.export_incdirs
                    .into_iter()
                    .flat_map(|(_, v)| v.into_iter().map(|(_, path)| path)),
            )
            .fold(IndexSet::new(), |mut acc, inc_dir| {
                acc.insert(inc_dir);
                acc
            });
        incdirs.into_iter().collect()
    }

    /// Flatten nested source groups.
    ///
    /// Removes all levels of hierarchy and produces a canonical list of source
    /// groups.
    pub fn flatten(self) -> Vec<SourceGroup<'ctx>> {
        let mut v = vec![];
        self.flatten_into(&mut v);
        v
    }

    fn flatten_into(mut self, into: &mut Vec<SourceGroup<'ctx>>) {
        let mut files = vec![];
        let subfiles = std::mem::take(&mut self.files);
        let flush_files = |files: &mut Vec<SourceFile<'ctx>>, into: &mut Vec<SourceGroup<'ctx>>| {
            if files.is_empty() {
                return;
            }
            let files = std::mem::take(files);
            into.push(SourceGroup {
                files,
                ..self.clone()
            });
        };
        for file in subfiles {
            match file {
                SourceFile::File(_, _) => {
                    files.push(file);
                }
                SourceFile::Group(grp) => {
                    let mut grp = *grp;
                    if !self.independent {
                        flush_files(&mut files, into);
                    }
                    grp.package = grp.package.or(self.package);
                    grp.independent &= self.independent;
                    grp.target = TargetSpec::All(
                        [&self.target, &grp.target]
                            .iter()
                            .map(|&i| i.clone())
                            .collect(),
                    );
                    grp.include_dirs = self
                        .include_dirs
                        .iter()
                        .cloned()
                        .chain(grp.include_dirs.into_iter())
                        .collect();
                    grp.defines = self
                        .defines
                        .iter()
                        .map(|(k, v)| (k.clone(), *v))
                        .chain(grp.defines.into_iter())
                        .collect();
                    grp.flatten_into(into);
                }
            }
        }
        flush_files(&mut files, into);
    }

    /// Get available targets in sourcegroup.
    pub fn get_avail_targets(&self) -> IndexSet<String> {
        let mut targets = self.target.get_avail();
        for file in &self.files {
            match file {
                SourceFile::File(..) => {}
                SourceFile::Group(group) => {
                    targets.extend(group.get_avail_targets());
                }
            }
        }
        targets
    }
}

/// File types for a source file.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SourceType {
    /// A Verilog file.
    Verilog,
    // /// A SystemVerilog file.
    // SystemVerilog,
    /// A VHDL file.
    Vhdl,
    /// Unknown file type
    Unknown,
}

/// A source file.
///
/// This can either be an individual file, or a subgroup of files.
#[derive(Clone)]
pub enum SourceFile<'ctx> {
    /// A file.
    File(&'ctx Path, &'ctx Option<SourceType>),
    /// A group of files.
    Group(Box<SourceGroup<'ctx>>),
}

impl<'ctx> fmt::Debug for SourceFile<'ctx> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SourceFile::File(path, ty) => {
                fmt::Debug::fmt(path, f)?;
                if let Some(t) = ty {
                    write!(f, " as {:?}", t)
                } else {
                    write!(f, " as <unknown>")
                }
            }
            SourceFile::Group(ref srcs) => fmt::Debug::fmt(srcs, f),
        }
    }
}

impl<'ctx> From<SourceGroup<'ctx>> for SourceFile<'ctx> {
    fn from(group: SourceGroup<'ctx>) -> SourceFile<'ctx> {
        SourceFile::Group(Box::new(group))
    }
}

impl<'ctx> From<&'ctx Path> for SourceFile<'ctx> {
    fn from(path: &'ctx Path) -> SourceFile<'ctx> {
        SourceFile::File(path, &None)
    }
}

impl<'ctx> Validate for SourceFile<'ctx> {
    type Output = SourceFile<'ctx>;
    type Error = Error;
    fn validate(self, vctx: &ValidationContext) -> Result<SourceFile<'ctx>, Error> {
        match self {
            SourceFile::File(path, ty) => {
                let env_path_buf =
                    crate::config::env_path_from_string(path.to_string_lossy().to_string())?;
                let exists = env_path_buf.exists() && env_path_buf.is_file();
                if exists || Diagnostics::is_suppressed("E31") {
                    if !exists {
                        Warnings::FileMissing {
                            path: env_path_buf.clone(),
                        }
                        .emit();
                    }
                    Ok(SourceFile::File(path, ty))
                } else {
                    Err(Error::new(format!(
                        "[E31] File {} doesn't exist",
                        env_path_buf.to_string_lossy()
                    )))
                }
            }
            SourceFile::Group(srcs) => Ok(SourceFile::Group(Box::new(srcs.validate(vctx)?))),
        }
    }
}

// Custom serialization for source files.
impl<'ctx> Serialize for SourceFile<'ctx> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            SourceFile::File(path, _) => path.serialize(serializer),
            SourceFile::Group(ref group) => group.serialize(serializer),
        }
    }
}