diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 75edc8ff78128..dc09d2d109330 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -18,7 +18,7 @@ use crate::core::builder::{ self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description, }; use crate::core::config::{Config, TargetSelection}; -use crate::helpers::{is_path_in_submodule, symlink_dir, t, up_to_date}; +use crate::helpers::{submodule_path_of, symlink_dir, t, up_to_date}; macro_rules! book { ($($name:ident, $path:expr, $book_name:expr, $lang:expr ;)+) => { @@ -44,8 +44,8 @@ macro_rules! book { } fn run(self, builder: &Builder<'_>) { - if is_path_in_submodule(&builder, $path) { - builder.require_submodule($path, None); + if let Some(submodule_path) = submodule_path_of(&builder, $path) { + builder.require_submodule(&submodule_path, None) } builder.ensure(RustbookSrc { @@ -933,9 +933,9 @@ macro_rules! tool_doc { fn run(self, builder: &Builder<'_>) { let mut source_type = SourceType::InTree; - if is_path_in_submodule(&builder, $path) { + if let Some(submodule_path) = submodule_path_of(&builder, $path) { source_type = SourceType::Submodule; - builder.require_submodule($path, None); + builder.require_submodule(&submodule_path, None); } let stage = builder.top_stage; diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 2ff99e7e49968..c4780cc56b2d3 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -60,10 +60,12 @@ pub fn is_dylib(path: &Path) -> bool { }) } -/// Returns `true` if the given path is part of a submodule. -pub fn is_path_in_submodule(builder: &Builder<'_>, path: &str) -> bool { +/// Return the path to the containing submodule if available. +pub fn submodule_path_of(builder: &Builder<'_>, path: &str) -> Option { let submodule_paths = build_helper::util::parse_gitmodules(&builder.src); - submodule_paths.iter().any(|submodule_path| path.starts_with(submodule_path)) + submodule_paths.iter().find_map(|submodule_path| { + if path.starts_with(submodule_path) { Some(submodule_path.to_string()) } else { None } + }) } fn is_aix_shared_archive(path: &Path) -> bool { diff --git a/src/bootstrap/src/utils/helpers/tests.rs b/src/bootstrap/src/utils/helpers/tests.rs index 7bd2a47c63c1c..613286cfaa89a 100644 --- a/src/bootstrap/src/utils/helpers/tests.rs +++ b/src/bootstrap/src/utils/helpers/tests.rs @@ -3,8 +3,8 @@ use std::io::Write; use std::path::PathBuf; use crate::utils::helpers::{ - check_cfg_arg, extract_beta_rev, hex_encode, is_path_in_submodule, make, program_out_of_date, - set_file_times, symlink_dir, + check_cfg_arg, extract_beta_rev, hex_encode, make, program_out_of_date, set_file_times, + submodule_path_of, symlink_dir, }; use crate::{Config, Flags}; @@ -117,16 +117,22 @@ fn test_set_file_times_sanity_check() { } #[test] -fn test_is_path_in_submodule() { +fn test_submodule_path_of() { let config = Config::parse_inner(Flags::parse(&["build".into(), "--dry-run".into()]), |&_| { Ok(Default::default()) }); let build = crate::Build::new(config.clone()); let builder = crate::core::builder::Builder::new(&build); - assert!(!is_path_in_submodule(&builder, "invalid/path")); - assert!(is_path_in_submodule(&builder, "src/tools/cargo")); - assert!(is_path_in_submodule(&builder, "src/llvm-project")); + assert_eq!(submodule_path_of(&builder, "invalid/path"), None); + assert_eq!(submodule_path_of(&builder, "src/tools/cargo"), Some("src/tools/cargo".to_string())); + assert_eq!( + submodule_path_of(&builder, "src/llvm-project"), + Some("src/llvm-project".to_string()) + ); // Make sure subdirs are handled properly - assert!(is_path_in_submodule(&builder, "src/tools/cargo/random-subdir")); + assert_eq!( + submodule_path_of(&builder, "src/tools/cargo/random-subdir"), + Some("src/tools/cargo".to_string()) + ); }