Skip to content

Commit 245b589

Browse files
committed
Move build_aux to an inherent method on BuildManager
1 parent 4d13192 commit 245b589

File tree

2 files changed

+109
-105
lines changed

2 files changed

+109
-105
lines changed

src/dependencies.rs

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1+
use bstr::ByteSlice;
12
use cargo_metadata::{camino::Utf8PathBuf, DependencyKind};
23
use cargo_platform::Cfg;
34
use color_eyre::eyre::{bail, eyre, Result};
45
use std::{
56
collections::{hash_map::Entry, HashMap, HashSet},
67
ffi::OsString,
7-
path::PathBuf,
8+
path::{Path, PathBuf},
89
process::Command,
910
str::FromStr,
1011
sync::{Arc, OnceLock, RwLock},
1112
};
1213

1314
use crate::{
14-
build_aux, status_emitter::StatusEmitter, test_result::Errored, Config, Mode,
15-
OutputConflictHandling,
15+
crate_type, default_per_file_config,
16+
per_test_config::{Comments, TestConfig},
17+
rustc_stderr,
18+
status_emitter::StatusEmitter,
19+
test_result::Errored,
20+
Config, CrateType, Error, Mode, OutputConflictHandling,
1621
};
1722

1823
#[derive(Default, Debug)]
@@ -291,7 +296,7 @@ impl<'a> BuildManager<'a> {
291296
Err(())
292297
}
293298
},
294-
Build::Aux { aux_file } => match build_aux(aux_file, config, self) {
299+
Build::Aux { aux_file } => match self.build_aux(aux_file, config) {
295300
Ok(args) => Ok(args.iter().map(Into::into).collect()),
296301
Err(e) => {
297302
err = Some(e);
@@ -321,4 +326,104 @@ impl<'a> BuildManager<'a> {
321326
})
322327
})
323328
}
329+
330+
fn build_aux(
331+
&self,
332+
aux_file: &Path,
333+
config: &Config,
334+
) -> std::result::Result<Vec<OsString>, Errored> {
335+
let file_contents = std::fs::read(aux_file).map_err(|err| Errored {
336+
command: Command::new(format!("reading aux file `{}`", aux_file.display())),
337+
errors: vec![],
338+
stderr: err.to_string().into_bytes(),
339+
stdout: vec![],
340+
})?;
341+
let comments = Comments::parse(&file_contents, config.comment_defaults.clone(), aux_file)
342+
.map_err(|errors| Errored::new(errors, "parse aux comments"))?;
343+
assert_eq!(
344+
comments.revisions, None,
345+
"aux builds cannot specify revisions"
346+
);
347+
348+
let mut config = config.clone();
349+
350+
// Strip any `crate-type` flags from the args, as we need to set our own,
351+
// and they may conflict (e.g. `lib` vs `proc-macro`);
352+
let mut prev_was_crate_type = false;
353+
config.program.args.retain(|arg| {
354+
if prev_was_crate_type {
355+
prev_was_crate_type = false;
356+
return false;
357+
}
358+
if arg == "--test" {
359+
false
360+
} else if arg == "--crate-type" {
361+
prev_was_crate_type = true;
362+
false
363+
} else if let Some(arg) = arg.to_str() {
364+
!arg.starts_with("--crate-type=")
365+
} else {
366+
true
367+
}
368+
});
369+
370+
default_per_file_config(&mut config, aux_file, &file_contents);
371+
372+
match crate_type(&file_contents) {
373+
// Proc macros must be run on the host
374+
CrateType::ProcMacro => config.target = config.host.clone(),
375+
CrateType::Test | CrateType::Bin | CrateType::Lib => {}
376+
}
377+
378+
let mut config = TestConfig {
379+
config,
380+
revision: "",
381+
comments: &comments,
382+
path: aux_file,
383+
};
384+
385+
config.patch_out_dir();
386+
387+
let mut aux_cmd = config.build_command()?;
388+
389+
let mut extra_args = config.build_aux_files(aux_file.parent().unwrap(), self)?;
390+
// Make sure we see our dependencies
391+
aux_cmd.args(extra_args.iter());
392+
393+
aux_cmd.arg("--emit=link");
394+
let filename = aux_file.file_stem().unwrap().to_str().unwrap();
395+
let output = aux_cmd.output().unwrap();
396+
if !output.status.success() {
397+
let error = Error::Command {
398+
kind: "compilation of aux build failed".to_string(),
399+
status: output.status,
400+
};
401+
return Err(Errored {
402+
command: aux_cmd,
403+
errors: vec![error],
404+
stderr: rustc_stderr::process(aux_file, &output.stderr).rendered,
405+
stdout: output.stdout,
406+
});
407+
}
408+
409+
// Now run the command again to fetch the output filenames
410+
aux_cmd.arg("--print").arg("file-names");
411+
let output = aux_cmd.output().unwrap();
412+
assert!(output.status.success());
413+
414+
for file in output.stdout.lines() {
415+
let file = std::str::from_utf8(file).unwrap();
416+
let crate_name = filename.replace('-', "_");
417+
let path = config.config.out_dir.join(file);
418+
extra_args.push("--extern".into());
419+
let mut cname = OsString::from(&crate_name);
420+
cname.push("=");
421+
cname.push(path);
422+
extra_args.push(cname);
423+
// Help cargo find the crates added with `--extern`.
424+
extra_args.push("-L".into());
425+
extra_args.push(config.config.out_dir.as_os_str().to_os_string());
426+
}
427+
Ok(extra_args)
428+
}
324429
}

src/lib.rs

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use per_test_config::TestConfig;
1717
use rustc_stderr::Message;
1818
use status_emitter::{StatusEmitter, TestStatus};
1919
use std::collections::VecDeque;
20-
use std::ffi::OsString;
2120
use std::num::NonZeroUsize;
2221
use std::path::{Component, Path, Prefix};
2322
use std::process::{Command, Output};
@@ -429,106 +428,6 @@ fn parse_and_test_file(
429428
.collect())
430429
}
431430

432-
fn build_aux(
433-
aux_file: &Path,
434-
config: &Config,
435-
build_manager: &BuildManager<'_>,
436-
) -> std::result::Result<Vec<OsString>, Errored> {
437-
let file_contents = std::fs::read(aux_file).map_err(|err| Errored {
438-
command: Command::new(format!("reading aux file `{}`", aux_file.display())),
439-
errors: vec![],
440-
stderr: err.to_string().into_bytes(),
441-
stdout: vec![],
442-
})?;
443-
let comments = Comments::parse(&file_contents, config.comment_defaults.clone(), aux_file)
444-
.map_err(|errors| Errored::new(errors, "parse aux comments"))?;
445-
assert_eq!(
446-
comments.revisions, None,
447-
"aux builds cannot specify revisions"
448-
);
449-
450-
let mut config = config.clone();
451-
452-
// Strip any `crate-type` flags from the args, as we need to set our own,
453-
// and they may conflict (e.g. `lib` vs `proc-macro`);
454-
let mut prev_was_crate_type = false;
455-
config.program.args.retain(|arg| {
456-
if prev_was_crate_type {
457-
prev_was_crate_type = false;
458-
return false;
459-
}
460-
if arg == "--test" {
461-
false
462-
} else if arg == "--crate-type" {
463-
prev_was_crate_type = true;
464-
false
465-
} else if let Some(arg) = arg.to_str() {
466-
!arg.starts_with("--crate-type=")
467-
} else {
468-
true
469-
}
470-
});
471-
472-
default_per_file_config(&mut config, aux_file, &file_contents);
473-
474-
match crate_type(&file_contents) {
475-
// Proc macros must be run on the host
476-
CrateType::ProcMacro => config.target = config.host.clone(),
477-
CrateType::Test | CrateType::Bin | CrateType::Lib => {}
478-
}
479-
480-
let mut config = TestConfig {
481-
config,
482-
revision: "",
483-
comments: &comments,
484-
path: aux_file,
485-
};
486-
487-
config.patch_out_dir();
488-
489-
let mut aux_cmd = config.build_command()?;
490-
491-
let mut extra_args = config.build_aux_files(aux_file.parent().unwrap(), build_manager)?;
492-
// Make sure we see our dependencies
493-
aux_cmd.args(extra_args.iter());
494-
495-
aux_cmd.arg("--emit=link");
496-
let filename = aux_file.file_stem().unwrap().to_str().unwrap();
497-
let output = aux_cmd.output().unwrap();
498-
if !output.status.success() {
499-
let error = Error::Command {
500-
kind: "compilation of aux build failed".to_string(),
501-
status: output.status,
502-
};
503-
return Err(Errored {
504-
command: aux_cmd,
505-
errors: vec![error],
506-
stderr: rustc_stderr::process(aux_file, &output.stderr).rendered,
507-
stdout: output.stdout,
508-
});
509-
}
510-
511-
// Now run the command again to fetch the output filenames
512-
aux_cmd.arg("--print").arg("file-names");
513-
let output = aux_cmd.output().unwrap();
514-
assert!(output.status.success());
515-
516-
for file in output.stdout.lines() {
517-
let file = std::str::from_utf8(file).unwrap();
518-
let crate_name = filename.replace('-', "_");
519-
let path = config.config.out_dir.join(file);
520-
extra_args.push("--extern".into());
521-
let mut cname = OsString::from(&crate_name);
522-
cname.push("=");
523-
cname.push(path);
524-
extra_args.push(cname);
525-
// Help cargo find the crates added with `--extern`.
526-
extra_args.push("-L".into());
527-
extra_args.push(config.config.out_dir.as_os_str().to_os_string());
528-
}
529-
Ok(extra_args)
530-
}
531-
532431
fn run_command(mut cmd: Command) -> Result<(Command, Output), Errored> {
533432
match cmd.output() {
534433
Err(err) => Err(Errored {

0 commit comments

Comments
 (0)