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