Skip to content

Commit

Permalink
Add a way to not build tier one targets by default
Browse files Browse the repository at this point in the history
This is still opt-in and off by default. However, it's useful for local
development and custom registries.
  • Loading branch information
jyn514 authored and Joshua Nelson committed Nov 22, 2020
1 parent 1c23b87 commit 31c864e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DOCSRS_LOG=docs_rs,rustwide=info
AWS_ACCESS_KEY_ID=cratesfyi
AWS_SECRET_ACCESS_KEY=secret_key
S3_ENDPOINT=http://localhost:9000
DOCSRS_INCLUDE_DEFAULT_TARGETS=false
46 changes: 32 additions & 14 deletions crates/metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! let metadata = Metadata::from_crate_root(&source_root)?;
//!
//! // Next, learn what arguments we need to pass to `cargo`.
//! let targets = metadata.targets();
//! let targets = metadata.targets(/* include_default_targets: */ true);
//! let mut cargo_args = metadata.cargo_args(&[], &[]);
//! cargo_args.push(targets.default_target.into());
//!
Expand Down Expand Up @@ -190,7 +190,10 @@ impl Metadata {
/// Return the targets that should be built.
///
/// The `default_target` will never be one of the `other_targets`.
pub fn targets(&self) -> BuildTargets<'_> {
/// If `include_default_targets` is `true` and `targets` is unset, this also includes
/// [`DEFAULT_TARGETS`]. Otherwise, if `include_default_targets` is `false` and `targets`
/// is unset, `other_targets` will be empty.
pub fn targets(&self, include_default_targets: bool) -> BuildTargets<'_> {
let default_target = self
.default_target
.as_deref()
Expand All @@ -202,12 +205,16 @@ impl Metadata {
})
.unwrap_or(HOST_TARGET);

// Let people opt-in to only having specific targets
let mut targets: HashSet<_> = self
let crate_targets = self
.targets
.as_ref()
.map(|targets| targets.iter().map(String::as_str).collect())
.unwrap_or_else(|| DEFAULT_TARGETS.iter().copied().collect());
.map(|targets| targets.iter().map(String::as_str).collect());
// Let people opt-in to only having specific targets
let mut targets: HashSet<_> = if include_default_targets {
crate_targets.unwrap_or_else(|| DEFAULT_TARGETS.iter().copied().collect())
} else {
crate_targets.unwrap_or_default()
};

targets.remove(&default_target);
BuildTargets {
Expand Down Expand Up @@ -412,7 +419,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: tier_one,
} = metadata.targets();
} = metadata.targets(true);
assert_eq!(default, HOST_TARGET);

// should be equal to TARGETS \ {HOST_TARGET}
Expand All @@ -434,7 +441,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, HOST_TARGET);
assert!(others.is_empty());
Expand All @@ -448,7 +455,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, "i686-pc-windows-msvc");
assert_eq!(others.len(), 1);
Expand All @@ -459,7 +466,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, HOST_TARGET);
assert!(others.is_empty());
Expand All @@ -473,7 +480,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, "i686-pc-windows-msvc");
assert!(others.is_empty());
Expand All @@ -483,7 +490,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, "i686-apple-darwin");
assert_eq!(others.len(), 1);
Expand All @@ -494,7 +501,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, "i686-apple-darwin");
assert!(others.is_empty());
Expand All @@ -504,7 +511,7 @@ mod test_targets {
let BuildTargets {
default_target: default,
other_targets: others,
} = metadata.targets();
} = metadata.targets(true);

assert_eq!(default, "i686-apple-darwin");
let tier_one_targets_no_default = DEFAULT_TARGETS
Expand All @@ -515,6 +522,17 @@ mod test_targets {

assert_eq!(others, tier_one_targets_no_default);
}

#[test]
fn no_default_targets() {
// if `targets` is unset, `other_targets` should be empty
let metadata = Metadata::default();
let BuildTargets {
other_targets: others,
..
} = metadata.targets(false);
assert!(others.is_empty(), "{:?}", others);
}
}

#[cfg(test)]
Expand Down
7 changes: 4 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use std::str::FromStr;

#[derive(Debug)]
pub struct Config {
// Build params
pub(crate) build_attempts: u16,

pub prefix: PathBuf,
pub registry_index_path: PathBuf,
pub registry_url: Option<String>,
Expand Down Expand Up @@ -41,11 +38,14 @@ pub struct Config {
// Time between 'git gc --auto' calls in seconds
pub(crate) registry_gc_interval: u64,

// Build params
pub(crate) build_attempts: u16,
pub(crate) rustwide_workspace: PathBuf,
pub(crate) inside_docker: bool,
pub(crate) local_docker_image: Option<String>,
pub(crate) toolchain: String,
pub(crate) build_cpu_limit: Option<u32>,
pub(crate) include_default_targets: bool,
}

impl Config {
Expand Down Expand Up @@ -89,6 +89,7 @@ impl Config {
local_docker_image: maybe_env("DOCS_RS_LOCAL_DOCKER_IMAGE")?,
toolchain: env("CRATESFYI_TOOLCHAIN", "nightly".to_string())?,
build_cpu_limit: maybe_env("DOCS_RS_BUILD_CPU_LIMIT")?,
include_default_targets: env("DOCSRS_INCLUDE_DEFAULT_TARGETS", true)?,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl RustwideBuilder {
let BuildTargets {
default_target,
other_targets,
} = metadata.targets();
} = metadata.targets(self.config.include_default_targets);

// Perform an initial build
let res = self.execute_build(default_target, true, &build, &limits, &metadata)?;
Expand Down

0 comments on commit 31c864e

Please sign in to comment.