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