Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document proc-macros despite bugs in rustdoc #491

Merged
merged 4 commits into from
Dec 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 61 additions & 55 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ static TARGETS: &[&str] = &[
"x86_64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
];
static DEFAULT_TARGET: &str = "x86_64-unknown-linux-gnu";

static ESSENTIAL_FILES_VERSIONED: &[&str] = &[
"brush.svg",
Expand Down Expand Up @@ -267,7 +266,7 @@ impl RustwideBuilder {
.build(&self.toolchain, &krate, sandbox)
.run(|build| {
let mut files_list = None;
let mut has_docs = false;
let (mut has_docs, mut in_target) = (false, false);
let mut successful_targets = Vec::new();

// Do an initial build and then copy the sources in the database
Expand All @@ -281,54 +280,47 @@ impl RustwideBuilder {
build.host_source_dir(),
)?);

has_docs = res
.cargo_metadata
.root()
.library_name()
.map(|name| {
build
.host_target_dir()
.join(&res.target)
.join("doc")
.join(name)
.is_dir()
})
.unwrap_or(false);
if let Some(name) = res.cargo_metadata.root().library_name() {
let host_target = build.host_target_dir();
if host_target
.join(&res.target)
.join("doc")
.join(&name)
.is_dir()
{
has_docs = true;
in_target = true;
// hack for proc-macro documentation:
// it really should be in target/$target/doc,
// but rustdoc has a bug and puts it in target/doc
} else if host_target.join("doc").join(name).is_dir() {
has_docs = true;
}
}
}

if has_docs {
debug!("adding documentation for the default target to the database");
self.copy_docs(
&build.host_target_dir(),
local_storage.path(),
&res.target,
if in_target { &res.target } else { "" },
true,
)?;

// Then build the documentation for all the targets
for target in TARGETS {
debug!("building package {} {} for {}", name, version, target);
let target_res = self.execute_build(Some(target), &build, &limits)?;
if target_res.successful {
// Cargo is not giving any error and not generating documentation of some crates
// when we use a target compile options. Check documentation exists before
// adding target to successfully_targets.
if build.host_target_dir().join(target).join("doc").is_dir() {
debug!(
"adding documentation for target {} to the database",
target
);
self.copy_docs(
&build.host_target_dir(),
local_storage.path(),
target,
false,
)?;
successful_targets.push(target.to_string());
}
if in_target {
// Then build the documentation for all the targets
for target in TARGETS {
debug!("building package {} {} for {}", name, version, target);
self.build_target(
target,
&build,
&limits,
&local_storage.path(),
&mut successful_targets,
)?;
}
}

self.upload_docs(&conn, name, version, local_storage.path())?;
}

Expand Down Expand Up @@ -362,6 +354,28 @@ impl RustwideBuilder {
Ok(res.successful)
}

fn build_target(
&self,
target: &str,
build: &Build,
limits: &Limits,
local_storage: &Path,
successful_targets: &mut Vec<String>,
) -> Result<()> {
let target_res = self.execute_build(Some(target), build, limits)?;
if target_res.successful {
// Cargo is not giving any error and not generating documentation of some crates
// when we use a target compile options. Check documentation exists before
// adding target to successfully_targets.
if build.host_target_dir().join(target).join("doc").is_dir() {
debug!("adding documentation for target {} to the database", target,);
self.copy_docs(&build.host_target_dir(), local_storage, target, false)?;
successful_targets.push(target.to_string());
}
}
Ok(())
}

fn execute_build(
&self,
target: Option<&str>,
Expand All @@ -372,14 +386,7 @@ impl RustwideBuilder {
let cargo_metadata =
CargoMetadata::load(&self.workspace, &self.toolchain, &build.host_source_dir())?;

let target = if let Some(target) = target {
target
} else if let Some(target) = metadata.default_target.as_ref().map(|s| s.as_str()) {
target
} else {
DEFAULT_TARGET
}
.to_string();
let target = target.or_else(|| metadata.default_target.as_ref().map(|s| s.as_str()));

let mut rustdoc_flags: Vec<String> = vec![
"-Z".to_string(),
Expand All @@ -402,13 +409,11 @@ impl RustwideBuilder {
if let Some(package_rustdoc_args) = &metadata.rustdoc_args {
rustdoc_flags.append(&mut package_rustdoc_args.iter().map(|s| s.to_owned()).collect());
}
let mut cargo_args = vec![
"doc".to_owned(),
"--lib".to_owned(),
"--no-deps".to_owned(),
"--target".to_owned(),
target.to_owned(),
];
let mut cargo_args = vec!["doc".to_owned(), "--lib".to_owned(), "--no-deps".to_owned()];
if let Some(explicit_target) = target {
cargo_args.push("--target".to_owned());
cargo_args.push(explicit_target.to_owned());
};
if let Some(features) = &metadata.features {
cargo_args.push("--features".to_owned());
cargo_args.push(features.join(" "));
Expand All @@ -432,8 +437,9 @@ impl RustwideBuilder {
"RUSTFLAGS",
metadata
.rustc_args
.as_ref()
.map(|args| args.join(" "))
.unwrap_or("".to_owned()),
.unwrap_or_default(),
)
.env("RUSTDOCFLAGS", rustdoc_flags.join(" "))
.args(&cargo_args)
Expand All @@ -447,7 +453,7 @@ impl RustwideBuilder {
docsrs_version: format!("docsrs {}", ::BUILD_VERSION),
successful,
cargo_metadata,
target: target.to_string(),
target: target.unwrap_or_default().to_string(),
})
}

Expand Down