Skip to content

Commit 8a3ed3c

Browse files
committed
Use first target in extra-targets if default-target is unset
This is almost entirely untested.
1 parent 9eae896 commit 8a3ed3c

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

src/docbuilder/metadata.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashSet;
21
use std::path::Path;
32
use toml::Value;
43
use error::Result;
@@ -130,27 +129,34 @@ impl Metadata {
130129

131130
metadata
132131
}
133-
pub(super) fn select_extra_targets<'a>(&'a self, default_target: &str) -> HashSet<&'a str> {
134-
use super::rustwide_builder::TARGETS;
135-
// this is a breaking change, don't enable it by default
136-
let build_specific = std::env::var("DOCS_RS_BUILD_ONLY_SPECIFIED_TARGETS")
137-
.map(|s| s == "true").unwrap_or(false);
138-
// If the env variable is set, _only_ build the specified targets
139-
// If no targets are specified, only build the default target.
140-
let mut extra_targets: HashSet<_> = if build_specific {
141-
self.extra_targets
142-
.as_ref()
143-
.map(|v| v.iter().map(|s| s.as_str()).collect())
144-
.unwrap_or_default()
145-
// Otherwise, let people opt-in to only having specific targets
146-
} else if let Some(explicit_targets) = &self.extra_targets {
147-
explicit_targets.iter().map(|s| s.as_str()).collect()
148-
// Otherwise, keep the existing behavior
132+
pub(super) fn select_extra_targets<'a: 'ret, 'b: 'ret, 'ret>(&'a self) -> (&'ret str, Vec<&'ret str>) {
133+
use super::rustwide_builder::{DEFAULT_TARGET, TARGETS};
134+
// Let people opt-in to only having specific targets
135+
// Ideally this would use Iterator instead of Vec so I could collect to a `HashSet`,
136+
// but I had trouble with `chain()` ¯\_(ツ)_/¯
137+
let mut all_targets: Vec<_> = self.default_target.as_deref().into_iter().collect();
138+
match &self.extra_targets {
139+
Some(targets) => all_targets.extend(targets.iter().map(|s| s.as_str())),
140+
None => all_targets.extend(TARGETS.iter().copied()),
141+
};
142+
143+
// default_target unset and extra_targets set to `[]`
144+
let landing_page = if all_targets.is_empty() {
145+
DEFAULT_TARGET
149146
} else {
150-
TARGETS.iter().copied().collect()
147+
// This `swap_remove` has to come before the `sort()` to keep the ordering
148+
// `swap_remove` is ok because ordering doesn't matter except for first element
149+
all_targets.swap_remove(0)
151150
};
152-
extra_targets.remove(default_target);
153-
extra_targets
151+
// Remove duplicates
152+
all_targets.sort();
153+
all_targets.dedup();
154+
// Remove landing_page so we don't build it twice.
155+
// It wasn't removed during dedup because we called `swap_remove()` first.
156+
if let Ok(index) = all_targets.binary_search(&landing_page) {
157+
all_targets.swap_remove(index);
158+
}
159+
(landing_page, all_targets)
154160
}
155161
}
156162

@@ -279,6 +285,5 @@ mod test {
279285

280286
// try it with a different default target just for sanity
281287
assert!(metadata.select_extra_targets("i686-pc-windows-msvc").is_empty());
282-
283288
}
284289
}

src/docbuilder/rustwide_builder.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::Metadata;
2121
const USER_AGENT: &str = "docs.rs builder (https://github.com/rust-lang/docs.rs)";
2222
const DEFAULT_RUSTWIDE_WORKSPACE: &str = ".rustwide";
2323

24-
const DEFAULT_TARGET: &str = "x86_64-unknown-linux-gnu";
24+
pub(super) const DEFAULT_TARGET: &str = "x86_64-unknown-linux-gnu";
2525
pub(super) const TARGETS: &[&str] = &[
2626
"i686-pc-windows-msvc",
2727
"i686-unknown-linux-gnu",
@@ -190,7 +190,8 @@ impl RustwideBuilder {
190190
.build(&self.toolchain, &krate, sandbox)
191191
.run(|build| {
192192
let metadata = Metadata::from_source_dir(&build.host_source_dir())?;
193-
let res = self.execute_build(None, build, &limits, &metadata)?;
193+
194+
let res = self.execute_build(DEFAULT_TARGET, true, build, &limits, &metadata)?;
194195
if !res.result.successful {
195196
bail!("failed to build dummy crate for {}", self.rustc_version);
196197
}
@@ -314,9 +315,10 @@ impl RustwideBuilder {
314315
let mut has_docs = false;
315316
let mut successful_targets = Vec::new();
316317
let metadata = Metadata::from_source_dir(&build.host_source_dir())?;
318+
let (default_target, other_targets) = metadata.select_extra_targets();
317319

318320
// Do an initial build and then copy the sources in the database
319-
let res = self.execute_build(None, &build, &limits, &metadata)?;
321+
let res = self.execute_build(default_target, true, &build, &limits, &metadata)?;
320322
if res.result.successful {
321323
debug!("adding sources into database");
322324
let prefix = format!("sources/{}/{}", name, version);
@@ -342,10 +344,9 @@ impl RustwideBuilder {
342344
)?;
343345

344346
successful_targets.push(res.target.clone());
345-
let targets = metadata.select_extra_targets(&res.target);
346347

347348
// Then build the documentation for all the targets
348-
for target in targets {
349+
for target in other_targets {
349350
debug!("building package {} {} for {}", name, version, target);
350351
self.build_target(
351352
target,
@@ -400,7 +401,7 @@ impl RustwideBuilder {
400401
successful_targets: &mut Vec<String>,
401402
metadata: &Metadata,
402403
) -> Result<()> {
403-
let target_res = self.execute_build(Some(target), build, limits, metadata)?;
404+
let target_res = self.execute_build(target, false, build, limits, metadata)?;
404405
if target_res.result.successful {
405406
// Cargo is not giving any error and not generating documentation of some crates
406407
// when we use a target compile options. Check documentation exists before
@@ -416,17 +417,15 @@ impl RustwideBuilder {
416417

417418
fn execute_build(
418419
&self,
419-
target: Option<&str>,
420+
target: &str,
421+
is_default_target: bool,
420422
build: &Build,
421423
limits: &Limits,
422424
metadata: &Metadata,
423425
) -> Result<FullBuildResult> {
424426
let cargo_metadata =
425427
CargoMetadata::load(&self.workspace, &self.toolchain, &build.host_source_dir())?;
426428

427-
let is_default_target = target.is_none();
428-
let target = target.or_else(|| metadata.default_target.as_ref().map(|s| s.as_str()));
429-
430429
let mut rustdoc_flags: Vec<String> = vec![
431430
"-Z".to_string(),
432431
"unstable-options".to_string(),
@@ -448,9 +447,9 @@ impl RustwideBuilder {
448447
rustdoc_flags.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
449448
}
450449
let mut cargo_args = vec!["doc".to_owned(), "--lib".to_owned(), "--no-deps".to_owned()];
451-
if let Some(explicit_target) = target {
450+
if target != DEFAULT_TARGET {
452451
cargo_args.push("--target".to_owned());
453-
cargo_args.push(explicit_target.to_owned());
452+
cargo_args.push(target.to_owned());
454453
};
455454
if let Some(features) = &metadata.features {
456455
cargo_args.push("--features".to_owned());
@@ -488,15 +487,13 @@ impl RustwideBuilder {
488487
// cargo will put the output in `target/<target>/doc`.
489488
// However, if this is the default build, we don't want it there,
490489
// we want it in `target/doc`.
491-
if let Some(explicit_target) = target {
492-
if is_default_target {
493-
// mv target/$explicit_target/doc target/doc
494-
let target_dir = build.host_target_dir();
495-
let old_dir = target_dir.join(explicit_target).join("doc");
496-
let new_dir = target_dir.join("doc");
497-
debug!("rename {} to {}", old_dir.display(), new_dir.display());
498-
std::fs::rename(old_dir, new_dir)?;
499-
}
490+
if target != DEFAULT_TARGET && is_default_target {
491+
// mv target/target/doc target/doc
492+
let target_dir = build.host_target_dir();
493+
let old_dir = target_dir.join(target).join("doc");
494+
let new_dir = target_dir.join("doc");
495+
debug!("rename {} to {}", old_dir.display(), new_dir.display());
496+
std::fs::rename(old_dir, new_dir)?;
500497
}
501498

502499
Ok(FullBuildResult {
@@ -507,7 +504,7 @@ impl RustwideBuilder {
507504
successful,
508505
},
509506
cargo_metadata,
510-
target: target.unwrap_or(DEFAULT_TARGET).to_string(),
507+
target: target.to_string(),
511508
})
512509
}
513510

0 commit comments

Comments
 (0)