Skip to content

Commit

Permalink
Compute 'rustdoc --crate-type' support when Compilation is created
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Sep 16, 2019
1 parent 60afaa7 commit b68e854
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str;

use log::debug;
Expand Down Expand Up @@ -34,7 +33,7 @@ pub struct BuildContext<'a, 'cfg> {
pub packages: &'a PackageSet<'cfg>,

/// Information about the compiler.
pub rustc: Rc<Rustc>,
pub rustc: Rustc,
/// Build information for the host arch.
pub host_config: TargetConfig,
/// Build information for the target.
Expand All @@ -54,7 +53,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
units: &'a UnitInterner<'a>,
extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
) -> CargoResult<BuildContext<'a, 'cfg>> {
let rustc = Rc::new(config.load_global_rustc(Some(ws))?);
let rustc = config.load_global_rustc(Some(ws))?;

let host_config = TargetConfig::new(config, &rustc.host)?;
let target_config = match build_config.requested_target.as_ref() {
Expand Down
29 changes: 14 additions & 15 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::{BTreeSet, HashMap, HashSet};
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::rc::Rc;

use semver::Version;

Expand Down Expand Up @@ -76,7 +75,7 @@ pub struct Compilation<'cfg> {
primary_unit_rustc_process: Option<ProcessBuilder>,

target_runner: Option<(PathBuf, Vec<String>)>,
rustc: Rc<Rustc>,
supports_rustdoc_crate_type: bool,
}

impl<'cfg> Compilation<'cfg> {
Expand Down Expand Up @@ -113,7 +112,7 @@ impl<'cfg> Compilation<'cfg> {
host: bcx.host_triple().to_string(),
target: bcx.target_triple().to_string(),
target_runner: target_runner(bcx)?,
rustc: bcx.rustc.clone(),
supports_rustdoc_crate_type: supports_rustdoc_crate_type(bcx.config, &bcx.rustc)?,
})
}

Expand All @@ -139,25 +138,14 @@ impl<'cfg> Compilation<'cfg> {
Ok(p)
}

fn supports_rustdoc_crate_type(&self) -> CargoResult<bool> {
// NOTE: Unconditionally return 'true' once support for
// rustdoc '--crate-type' rides to stable
let mut crate_type_test = process(self.config.rustdoc()?);
// If '--crate-type' is not supported by rustcoc, this command
// will exit with an error. Otherwise, it will print a help message,
// and exit successfully
crate_type_test.args(&["--crate-type", "proc-macro", "--help"]);
Ok(self.rustc.cached_output(&crate_type_test).is_ok())
}

/// See `process`.
pub fn rustdoc_process(&self, pkg: &Package, target: &Target) -> CargoResult<ProcessBuilder> {
let mut p = self.fill_env(process(&*self.config.rustdoc()?), pkg, false)?;
if target.edition() != Edition::Edition2015 {
p.arg(format!("--edition={}", target.edition()));
}

if self.supports_rustdoc_crate_type()? {
if self.supports_rustdoc_crate_type {
for crate_type in target.rustc_crate_types() {
p.arg("--crate-type").arg(crate_type);
}
Expand Down Expand Up @@ -331,3 +319,14 @@ fn target_runner(bcx: &BuildContext<'_, '_>) -> CargoResult<Option<(PathBuf, Vec

Ok(None)
}

fn supports_rustdoc_crate_type(config: &Config, rustc: &Rustc) -> CargoResult<bool> {
// NOTE: Unconditionally return 'true' once support for
// rustdoc '--crate-type' rides to stable
let mut crate_type_test = process(config.rustdoc()?);
// If '--crate-type' is not supported by rustcoc, this command
// will exit with an error. Otherwise, it will print a help message,
// and exit successfully
crate_type_test.args(&["--crate-type", "proc-macro", "--help"]);
Ok(rustc.cached_output(&crate_type_test).is_ok())
}

0 comments on commit b68e854

Please sign in to comment.