compiletest/directives/
auxiliary.rs

1//! Code for dealing with test directives that request an "auxiliary" crate to
2//! be built and made available to the test in some way.
3
4use std::iter;
5
6use camino::Utf8Path;
7
8use super::directives::{AUX_BIN, AUX_BUILD, AUX_CODEGEN_BACKEND, AUX_CRATE, PROC_MACRO};
9use crate::common::Config;
10use crate::directives::DirectiveLine;
11
12/// Properties parsed from `aux-*` test directives.
13#[derive(Clone, Debug, Default)]
14pub(crate) struct AuxProps {
15    /// Other crates that should be built and made available to this test.
16    /// These are filenames relative to `./auxiliary/` in the test's directory.
17    pub(crate) builds: Vec<String>,
18    /// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
19    pub(crate) bins: Vec<String>,
20    /// Similar to `builds`, but a list of NAME=somelib.rs of dependencies
21    /// to build and pass with the `--extern` flag.
22    pub(crate) crates: Vec<(String, String)>,
23    /// Same as `builds`, but for proc-macros.
24    pub(crate) proc_macros: Vec<String>,
25    /// Similar to `builds`, but also uses the resulting dylib as a
26    /// `-Zcodegen-backend` when compiling the test file.
27    pub(crate) codegen_backend: Option<String>,
28}
29
30impl AuxProps {
31    /// Yields all of the paths (relative to `./auxiliary/`) that have been
32    /// specified in `aux-*` directives for this test.
33    pub(crate) fn all_aux_path_strings(&self) -> impl Iterator<Item = &str> {
34        let Self { builds, bins, crates, proc_macros, codegen_backend } = self;
35
36        iter::empty()
37            .chain(builds.iter().map(String::as_str))
38            .chain(bins.iter().map(String::as_str))
39            .chain(crates.iter().map(|(_, path)| path.as_str()))
40            .chain(proc_macros.iter().map(String::as_str))
41            .chain(codegen_backend.iter().map(String::as_str))
42    }
43}
44
45/// If the given test directive line contains an `aux-*` directive, parse it
46/// and update [`AuxProps`] accordingly.
47pub(super) fn parse_and_update_aux(
48    config: &Config,
49    directive_line: &DirectiveLine<'_>,
50    testfile: &Utf8Path,
51    aux: &mut AuxProps,
52) {
53    if !(directive_line.name.starts_with("aux-") || directive_line.name == "proc-macro") {
54        return;
55    }
56
57    let ln = directive_line;
58
59    config.push_name_value_directive(ln, AUX_BUILD, testfile, &mut aux.builds, |r| {
60        r.trim().to_string()
61    });
62    config
63        .push_name_value_directive(ln, AUX_BIN, testfile, &mut aux.bins, |r| r.trim().to_string());
64    config.push_name_value_directive(ln, AUX_CRATE, testfile, &mut aux.crates, parse_aux_crate);
65    config.push_name_value_directive(ln, PROC_MACRO, testfile, &mut aux.proc_macros, |r| {
66        r.trim().to_string()
67    });
68    if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND, testfile) {
69        aux.codegen_backend = Some(r.trim().to_owned());
70    }
71}
72
73fn parse_aux_crate(r: String) -> (String, String) {
74    let mut parts = r.trim().splitn(2, '=');
75    (
76        parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string(),
77        parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string(),
78    )
79}