Skip to content

Commit

Permalink
Make aux-builds a custom build thing instead of a builtin thing
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 7, 2024
1 parent a2d2f87 commit 2a7734c
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 136 deletions.
77 changes: 70 additions & 7 deletions src/aux_builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,92 @@
// lol we can't name this file `aux.rs` on windows

use bstr::ByteSlice;
use spanned::Spanned;
use std::{ffi::OsString, path::PathBuf, process::Command};

use crate::{
build_manager::{Build, BuildManager},
custom_flags::Flag,
default_per_file_config,
per_test_config::{Comments, TestConfig},
rustc_stderr, CrateType, Error, Errored,
};

impl Flag for AuxBuilder {
fn clone_inner(&self) -> Box<dyn Flag> {
Box::new(self.clone())
}
fn apply(
&self,
cmd: &mut Command,
config: &TestConfig<'_>,
build_manager: &BuildManager<'_>,
) -> Result<(), Errored> {
let mut extra_args = vec![];
let aux = &self.aux_file;
let aux_dir = config.aux_dir;
let extra_args: &mut Vec<OsString> = &mut extra_args;
let line = aux.line();
let aux_file = if aux.starts_with("..") {
aux_dir.parent().unwrap().join(&aux.content)
} else {
aux_dir.join(&aux.content)
};
extra_args.extend(
build_manager
.build(AuxBuilder {
aux_file: Spanned::new(
crate::core::strip_path_prefix(
&aux_file.canonicalize().map_err(|err| Errored {
command: Command::new(format!(
"canonicalizing path `{}`",
aux_file.display()
)),
errors: vec![],
stderr: err.to_string().into_bytes(),
stdout: vec![],
})?,
&std::env::current_dir().unwrap(),
)
.collect(),
aux.span(),
),
})
.map_err(
|Errored {
command,
errors,
stderr,
stdout,
}| Errored {
command,
errors: vec![Error::Aux {
path: aux_file.to_path_buf(),
errors,
line,
}],
stderr,
stdout,
},
)?,
);
cmd.args(extra_args);
Ok(())
}
}

/// Build an aux-build.
/// Custom `//@aux-build` flag handler.
#[derive(Clone, Debug)]
pub struct AuxBuilder {
/// Full path to the file (including `auxiliary` folder prefix)
pub aux_file: PathBuf,
pub aux_file: Spanned<PathBuf>,
}

impl Build for AuxBuilder {
fn build(&self, build_manager: &BuildManager<'_>) -> Result<Vec<OsString>, Errored> {
let mut config = build_manager.config().clone();
let file_contents = std::fs::read(&self.aux_file).map_err(|err| Errored {
let file_contents = std::fs::read(&self.aux_file.content).map_err(|err| Errored {
command: Command::new(format!("reading aux file `{}`", self.aux_file.display())),
errors: vec![],
stderr: err.to_string().into_bytes(),
Expand Down Expand Up @@ -53,11 +120,6 @@ impl Build for AuxBuilder {

let mut aux_cmd = config.build_command(build_manager)?;

let mut extra_args =
config.build_aux_files(self.aux_file.parent().unwrap(), build_manager)?;
// Make sure we see our dependencies
aux_cmd.args(extra_args.iter());

aux_cmd.arg("--emit=link");
let filename = self.aux_file.file_stem().unwrap().to_str().unwrap();
let output = aux_cmd.output().unwrap();
Expand All @@ -79,6 +141,7 @@ impl Build for AuxBuilder {
let output = aux_cmd.output().unwrap();
assert!(output.status.success());

let mut extra_args = vec![];
for file in output.stdout.lines() {
let file = std::str::from_utf8(file).unwrap();
let crate_name = filename.replace('-', "_");
Expand Down
55 changes: 34 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use regex::bytes::Regex;
use spanned::Spanned;

use crate::{
custom_flags::{run::Run, rustfix::RustfixMode, Flag},
dependencies::build_dependencies,
filter::Match,
parser::CommandParserFunc,
per_test_config::{Comments, Condition, TestConfig},
CommandBuilder, Mode,
aux_builds::AuxBuilder, build_manager::BuildManager, custom_flags::{run::Run, rustfix::RustfixMode, Flag}, dependencies::build_dependencies, filter::Match, parser::CommandParserFunc, per_test_config::{Comments, Condition, TestConfig}, CommandBuilder, Errored, Mode
};
pub use color_eyre;
use color_eyre::eyre::Result;
Expand Down Expand Up @@ -76,8 +71,9 @@ impl Config {
Box::new(Edition(self.0.clone()))
}

fn apply(&self, cmd: &mut std::process::Command, _config: &TestConfig<'_>) {
fn apply(&self, cmd: &mut std::process::Command, _config: &TestConfig<'_>, _build_manager: &BuildManager<'_>) -> Result<(), Errored> {
cmd.arg("--edition").arg(&self.0);
Ok(())
}
}

Expand All @@ -100,13 +96,13 @@ impl Config {
}
}

let _ = comment_defaults
.base()
.custom
.insert("edition", Spanned::dummy(Box::new(Edition("2021".into()))));
let _ = comment_defaults.base().custom.insert(
"edition",
Spanned::dummy(vec![Box::new(Edition("2021".into()))]),
);
let _ = comment_defaults.base().custom.insert(
"rustfix",
Spanned::dummy(Box::new(RustfixMode::MachineApplicable)),
Spanned::dummy(vec![Box::new(RustfixMode::MachineApplicable)]),
);
let filters = vec![
(Match::PathBackslash, b"/".to_vec()),
Expand Down Expand Up @@ -149,7 +145,7 @@ impl Config {
// args are ignored (can be used as comment)
let prev = parser
.custom
.insert("no-rustfix", Spanned::new(Box::new(()), span.clone()));
.insert("no-rustfix", Spanned::new(vec![Box::new(())], span.clone()));
parser.check(span, prev.is_none(), "cannot specify `no-rustfix` twice");
});

Expand All @@ -158,7 +154,7 @@ impl Config {
.insert("edition", |parser, args, span| {
let prev = parser.custom.insert(
"edition",
Spanned::new(Box::new(Edition((*args).into())), args.span()),
Spanned::new(vec![Box::new(Edition((*args).into()))], args.span()),
);
parser.check(span, prev.is_none(), "cannot specify `edition` twice");
});
Expand All @@ -168,7 +164,7 @@ impl Config {
.insert("needs-asm-support", |parser, args, span| {
let prev = parser.custom.insert(
"needs-asm-support",
Spanned::new(Box::new(NeedsAsmSupport), args.span()),
Spanned::new(vec![Box::new(NeedsAsmSupport)], args.span()),
);
parser.check(
span,
Expand All @@ -186,13 +182,13 @@ impl Config {
let set = |exit_code| {
parser.custom.insert(
"run",
Spanned::new(Box::new(Run { exit_code }), args.span()),
Spanned::new(vec![Box::new(Run { exit_code })], args.span()),
);
parser.mode = Spanned::new(Mode::Pass, args.span()).into();

let prev = parser
.custom
.insert("no-rustfix", Spanned::new(Box::new(()), span.clone()));
.insert("no-rustfix", Spanned::new(vec![Box::new(())], span.clone()));
parser.check(span, prev.is_none(), "`run` implies `no-rustfix`");
};
if args.is_empty() {
Expand All @@ -206,6 +202,22 @@ impl Config {
}
}
});
config.custom_comments.insert("aux-build", |parser, args, span| {
let name = match args.split_once(":") {
Some((name, rest)) => {
parser.error(rest.span(), "proc macros are now auto-detected, you can remove the `:proc-macro` after the file name");
name
},
None => args,
};

parser
.custom
.entry("aux-build")
.or_insert_with(|| Spanned::new(vec![], span))
.content
.push(Box::new(AuxBuilder { aux_file: name.map(|n| n.into())}));
});
config
}

Expand Down Expand Up @@ -408,10 +420,11 @@ impl Config {
{
return self.run_only_ignored;
}
if comments
.for_revision(revision)
.any(|r| r.custom.values().any(|flag| flag.test_condition(self)))
{
if comments.for_revision(revision).any(|r| {
r.custom
.values()
.any(|flags| flags.content.iter().any(|flag| flag.test_condition(self)))
}) {
return self.run_only_ignored;
}
comments
Expand Down
9 changes: 8 additions & 1 deletion src/custom_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ pub trait Flag: Send + Sync + UnwindSafe + RefUnwindSafe + std::fmt::Debug {
fn clone_inner(&self) -> Box<dyn Flag>;

/// Modify a command to what the flag specifies
fn apply(&self, _cmd: &mut Command, _config: &TestConfig<'_>) {}
fn apply(
&self,
_cmd: &mut Command,
_config: &TestConfig<'_>,
_build_manager: &BuildManager<'_>,
) -> Result<(), Errored> {
Ok(())
}

/// Whether this flag causes a test to be filtered out
fn test_condition(&self, _config: &Config) -> bool {
Expand Down
1 change: 0 additions & 1 deletion src/custom_flags/rustfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl Flag for RustfixMode {
error_in_other_files: vec![],
error_matches: vec![],
require_annotations_for_level: Default::default(),
aux_builds: config.collect(|r| r.aux_builds.iter().cloned()),
mode: OptWithLine::new(Mode::Pass, Span::default()),
diagnostic_code_prefix: OptWithLine::new(String::new(), Span::default()),
custom: config
Expand Down
23 changes: 2 additions & 21 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
collections::HashMap,
num::NonZeroUsize,
path::{Path, PathBuf},
process::Command,
};
use std::{collections::HashMap, num::NonZeroUsize, path::Path, process::Command};

use bstr::{ByteSlice, Utf8Error};
use regex::bytes::Regex;
Expand Down Expand Up @@ -151,8 +146,6 @@ pub struct Revisioned {
/// Ignore diagnostics below this level.
/// `None` means pick the lowest level from the `error_pattern`s.
pub require_annotations_for_level: OptWithLine<Level>,
/// Files that get built and exposed as dependencies to the current test.
pub aux_builds: Vec<Spanned<PathBuf>>,
/// The mode this test is being run in.
pub mode: OptWithLine<Mode>,
/// Prefix added to all diagnostic code matchers. Note this will make it impossible
Expand All @@ -162,7 +155,7 @@ pub struct Revisioned {
/// The keys are just labels for overwriting or retrieving the value later.
/// They are mostly used by `Config::custom_comments` handlers,
/// `ui_test` itself only ever looks at the values, not the keys.
pub custom: HashMap<&'static str, Spanned<Box<dyn Flag>>>,
pub custom: HashMap<&'static str, Spanned<Vec<Box<dyn Flag>>>>,
}

/// Main entry point to parsing comments and handling parsing errors.
Expand Down Expand Up @@ -411,7 +404,6 @@ impl CommentParser<Comments> {
error_in_other_files,
error_matches,
require_annotations_for_level,
aux_builds,
mode,
diagnostic_code_prefix,
custom,
Expand All @@ -428,7 +420,6 @@ impl CommentParser<Comments> {
normalize_stdout.extend(defaults.normalize_stdout);
error_in_other_files.extend(defaults.error_in_other_files);
error_matches.extend(defaults.error_matches);
aux_builds.extend(defaults.aux_builds);
if require_annotations_for_level.is_none() {
*require_annotations_for_level = defaults.require_annotations_for_level;
}
Expand Down Expand Up @@ -698,16 +689,6 @@ impl CommentParser<Comments> {
"run-rustfix" => (this, _args, span){
this.error(span, "rustfix is now ran by default when applicable suggestions are found");
}
"aux-build" => (this, args, _span){
let name = match args.split_once(":") {
Some((name, rest)) => {
this.error(rest.span(), "proc macros are now auto-detected, you can remove the `:proc-macro` after the file name");
name
},
None => args,
};
this.aux_builds.push(name.map(Into::into));
}
"check-pass" => (this, _args, span){
let prev = this.mode.set(Mode::Pass, span.clone());
// args are ignored (can be used as comment)
Expand Down
Loading

0 comments on commit 2a7734c

Please sign in to comment.