Skip to content

Commit afdb627

Browse files
authored
Rollup merge of #134967 - onur-ozkan:auto-submodule-handler, r=jieyouxu
handle submodules automatically on `doc` steps Helps to make `doc` macros less complicated.
2 parents 372442f + 6eb9ebf commit afdb627

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

src/bootstrap/src/core/build_steps/doc.rs

+17-30
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,10 @@ use crate::core::builder::{
1818
self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description,
1919
};
2020
use crate::core::config::{Config, TargetSelection};
21-
use crate::utils::helpers::{symlink_dir, t, up_to_date};
22-
23-
macro_rules! submodule_helper {
24-
($path:expr, submodule) => {
25-
$path
26-
};
27-
($path:expr, submodule = $submodule:literal) => {
28-
$submodule
29-
};
30-
}
21+
use crate::helpers::{is_path_in_submodule, symlink_dir, t, up_to_date};
3122

3223
macro_rules! book {
33-
($($name:ident, $path:expr, $book_name:expr, $lang:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
24+
($($name:ident, $path:expr, $book_name:expr, $lang:expr ;)+) => {
3425
$(
3526
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
3627
pub struct $name {
@@ -53,10 +44,10 @@ macro_rules! book {
5344
}
5445

5546
fn run(self, builder: &Builder<'_>) {
56-
$(
57-
let path = submodule_helper!( $path, submodule $( = $submodule )? );
58-
builder.require_submodule(path, None);
59-
)?
47+
if is_path_in_submodule(&builder, $path) {
48+
builder.require_submodule($path, None);
49+
}
50+
6051
builder.ensure(RustbookSrc {
6152
target: self.target,
6253
name: $book_name.to_owned(),
@@ -77,12 +68,12 @@ macro_rules! book {
7768
// FIXME: Make checking for a submodule automatic somehow (maybe by having a list of all submodules
7869
// and checking against it?).
7970
book!(
80-
CargoBook, "src/tools/cargo/src/doc", "cargo", &[], submodule = "src/tools/cargo";
71+
CargoBook, "src/tools/cargo/src/doc", "cargo", &[];
8172
ClippyBook, "src/tools/clippy/book", "clippy", &[];
82-
EditionGuide, "src/doc/edition-guide", "edition-guide", &[], submodule;
83-
EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[], submodule;
84-
Nomicon, "src/doc/nomicon", "nomicon", &[], submodule;
85-
RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja", "zh"], submodule;
73+
EditionGuide, "src/doc/edition-guide", "edition-guide", &[];
74+
EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[];
75+
Nomicon, "src/doc/nomicon", "nomicon", &[];
76+
RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja", "zh"];
8677
RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
8778
StyleGuide, "src/doc/style-guide", "style-guide", &[];
8879
);
@@ -910,7 +901,6 @@ macro_rules! tool_doc {
910901
$(rustc_tool = $rustc_tool:literal, )?
911902
$(is_library = $is_library:expr,)?
912903
$(crates = $crates:expr)?
913-
$(, submodule $(= $submodule:literal)? )?
914904
) => {
915905
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
916906
pub struct $tool {
@@ -938,14 +928,12 @@ macro_rules! tool_doc {
938928
/// we do not merge it with the other documentation from std, test and
939929
/// proc_macros. This is largely just a wrapper around `cargo doc`.
940930
fn run(self, builder: &Builder<'_>) {
941-
let source_type = SourceType::InTree;
942-
$(
943-
let _ = source_type; // silence the "unused variable" warning
944-
let source_type = SourceType::Submodule;
931+
let mut source_type = SourceType::InTree;
945932

946-
let path = submodule_helper!( $path, submodule $( = $submodule )? );
947-
builder.require_submodule(path, None);
948-
)?
933+
if is_path_in_submodule(&builder, $path) {
934+
source_type = SourceType::Submodule;
935+
builder.require_submodule($path, None);
936+
}
949937

950938
let stage = builder.top_stage;
951939
let target = self.target;
@@ -1054,8 +1042,7 @@ tool_doc!(
10541042
"crates-io",
10551043
"mdman",
10561044
"rustfix",
1057-
],
1058-
submodule = "src/tools/cargo"
1045+
]
10591046
);
10601047
tool_doc!(Tidy, "src/tools/tidy", rustc_tool = false, crates = ["tidy"]);
10611048
tool_doc!(

src/bootstrap/src/utils/helpers.rs

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ pub fn is_dylib(path: &Path) -> bool {
6060
})
6161
}
6262

63+
/// Returns `true` if the given path is part of a submodule.
64+
pub fn is_path_in_submodule(builder: &Builder<'_>, path: &str) -> bool {
65+
let submodule_paths = build_helper::util::parse_gitmodules(&builder.src);
66+
submodule_paths.iter().any(|submodule_path| path.starts_with(submodule_path))
67+
}
68+
6369
fn is_aix_shared_archive(path: &Path) -> bool {
6470
let file = match fs::File::open(path) {
6571
Ok(file) => file,

src/bootstrap/src/utils/helpers/tests.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::io::Write;
33
use std::path::PathBuf;
44

55
use crate::utils::helpers::{
6-
check_cfg_arg, extract_beta_rev, hex_encode, make, program_out_of_date, set_file_times,
7-
symlink_dir,
6+
check_cfg_arg, extract_beta_rev, hex_encode, is_path_in_submodule, make, program_out_of_date,
7+
set_file_times, symlink_dir,
88
};
99
use crate::{Config, Flags};
1010

@@ -115,3 +115,18 @@ fn test_set_file_times_sanity_check() {
115115
assert_eq!(found_metadata.accessed().unwrap(), unix_epoch);
116116
assert_eq!(found_metadata.modified().unwrap(), unix_epoch)
117117
}
118+
119+
#[test]
120+
fn test_is_path_in_submodule() {
121+
let config = Config::parse_inner(Flags::parse(&["build".into(), "--dry-run".into()]), |&_| {
122+
Ok(Default::default())
123+
});
124+
125+
let build = crate::Build::new(config.clone());
126+
let builder = crate::core::builder::Builder::new(&build);
127+
assert!(!is_path_in_submodule(&builder, "invalid/path"));
128+
assert!(is_path_in_submodule(&builder, "src/tools/cargo"));
129+
assert!(is_path_in_submodule(&builder, "src/llvm-project"));
130+
// Make sure subdirs are handled properly
131+
assert!(is_path_in_submodule(&builder, "src/tools/cargo/random-subdir"));
132+
}

0 commit comments

Comments
 (0)