Skip to content

Commit 7a073c4

Browse files
committed
Add a way to not build tier one targets by default
This is still opt-in and off by default. However, it's useful for local development and custom registries.
1 parent c0718f5 commit 7a073c4

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ DOCSRS_LOG=docs_rs,rustwide=info
44
AWS_ACCESS_KEY_ID=cratesfyi
55
AWS_SECRET_ACCESS_KEY=secret_key
66
S3_ENDPOINT=http://localhost:9000
7+
DOCSRS_INCLUDE_DEFAULT_TARGETS=false

crates/metadata/lib.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! let metadata = Metadata::from_crate_root(&source_root)?;
2121
//!
2222
//! // Next, learn what arguments we need to pass to `cargo`.
23-
//! let targets = metadata.targets();
23+
//! let targets = metadata.targets(/* include_default_targets: */ true);
2424
//! let mut cargo_args = metadata.cargo_args(&[], &[]);
2525
//! cargo_args.push(targets.default_target.into());
2626
//!
@@ -190,7 +190,10 @@ impl Metadata {
190190
/// Return the targets that should be built.
191191
///
192192
/// The `default_target` will never be one of the `other_targets`.
193-
pub fn targets(&self) -> BuildTargets<'_> {
193+
/// If `include_default_targets` is `true` and `targets` is unset, this also includes
194+
/// [`DEFAULT_TARGETS`]. Otherwise, if `include_default_targets` is `false` and `targets`
195+
/// is unset, `other_targets` will be empty.
196+
pub fn targets(&self, include_default_targets: bool) -> BuildTargets<'_> {
194197
let default_target = self
195198
.default_target
196199
.as_deref()
@@ -202,12 +205,16 @@ impl Metadata {
202205
})
203206
.unwrap_or(HOST_TARGET);
204207

205-
// Let people opt-in to only having specific targets
206-
let mut targets: HashSet<_> = self
208+
let crate_targets = self
207209
.targets
208210
.as_ref()
209-
.map(|targets| targets.iter().map(String::as_str).collect())
210-
.unwrap_or_else(|| DEFAULT_TARGETS.iter().copied().collect());
211+
.map(|targets| targets.iter().map(String::as_str).collect());
212+
// Let people opt-in to only having specific targets
213+
let mut targets: HashSet<_> = if include_default_targets {
214+
crate_targets.unwrap_or_else(|| DEFAULT_TARGETS.iter().copied().collect())
215+
} else {
216+
crate_targets.unwrap_or_default()
217+
};
211218

212219
targets.remove(&default_target);
213220
BuildTargets {
@@ -411,7 +418,7 @@ mod test_targets {
411418
let BuildTargets {
412419
default_target: default,
413420
other_targets: tier_one,
414-
} = metadata.targets();
421+
} = metadata.targets(true);
415422
assert_eq!(default, HOST_TARGET);
416423

417424
// should be equal to TARGETS \ {HOST_TARGET}
@@ -433,7 +440,7 @@ mod test_targets {
433440
let BuildTargets {
434441
default_target: default,
435442
other_targets: others,
436-
} = metadata.targets();
443+
} = metadata.targets(true);
437444

438445
assert_eq!(default, HOST_TARGET);
439446
assert!(others.is_empty());
@@ -447,7 +454,7 @@ mod test_targets {
447454
let BuildTargets {
448455
default_target: default,
449456
other_targets: others,
450-
} = metadata.targets();
457+
} = metadata.targets(true);
451458

452459
assert_eq!(default, "i686-pc-windows-msvc");
453460
assert_eq!(others.len(), 1);
@@ -458,7 +465,7 @@ mod test_targets {
458465
let BuildTargets {
459466
default_target: default,
460467
other_targets: others,
461-
} = metadata.targets();
468+
} = metadata.targets(true);
462469

463470
assert_eq!(default, HOST_TARGET);
464471
assert!(others.is_empty());
@@ -472,7 +479,7 @@ mod test_targets {
472479
let BuildTargets {
473480
default_target: default,
474481
other_targets: others,
475-
} = metadata.targets();
482+
} = metadata.targets(true);
476483

477484
assert_eq!(default, "i686-pc-windows-msvc");
478485
assert!(others.is_empty());
@@ -482,7 +489,7 @@ mod test_targets {
482489
let BuildTargets {
483490
default_target: default,
484491
other_targets: others,
485-
} = metadata.targets();
492+
} = metadata.targets(true);
486493

487494
assert_eq!(default, "i686-apple-darwin");
488495
assert_eq!(others.len(), 1);
@@ -493,7 +500,7 @@ mod test_targets {
493500
let BuildTargets {
494501
default_target: default,
495502
other_targets: others,
496-
} = metadata.targets();
503+
} = metadata.targets(true);
497504

498505
assert_eq!(default, "i686-apple-darwin");
499506
assert!(others.is_empty());
@@ -503,7 +510,7 @@ mod test_targets {
503510
let BuildTargets {
504511
default_target: default,
505512
other_targets: others,
506-
} = metadata.targets();
513+
} = metadata.targets(true);
507514

508515
assert_eq!(default, "i686-apple-darwin");
509516
let tier_one_targets_no_default = DEFAULT_TARGETS
@@ -514,6 +521,17 @@ mod test_targets {
514521

515522
assert_eq!(others, tier_one_targets_no_default);
516523
}
524+
525+
#[test]
526+
fn no_default_targets() {
527+
// if `targets` is unset, `other_targets` should be empty
528+
let metadata = Metadata::default();
529+
let BuildTargets {
530+
other_targets: others,
531+
..
532+
} = metadata.targets(false);
533+
assert!(others.is_empty(), "{:?}", others);
534+
}
517535
}
518536

519537
#[cfg(test)]

src/config.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ use std::str::FromStr;
77

88
#[derive(Debug)]
99
pub struct Config {
10-
// Build params
11-
pub(crate) build_attempts: u16,
12-
1310
pub prefix: PathBuf,
1411
pub registry_index_path: PathBuf,
1512
pub registry_url: Option<String>,
@@ -41,11 +38,14 @@ pub struct Config {
4138
// Time between 'git gc --auto' calls in seconds
4239
pub(crate) registry_gc_interval: u64,
4340

41+
// Build params
42+
pub(crate) build_attempts: u16,
4443
pub(crate) rustwide_workspace: PathBuf,
4544
pub(crate) inside_docker: bool,
4645
pub(crate) local_docker_image: Option<String>,
4746
pub(crate) toolchain: String,
4847
pub(crate) build_cpu_limit: Option<u32>,
48+
pub(crate) include_default_targets: bool,
4949
}
5050

5151
impl Config {
@@ -89,6 +89,7 @@ impl Config {
8989
local_docker_image: maybe_env("DOCS_RS_LOCAL_DOCKER_IMAGE")?,
9090
toolchain: env("CRATESFYI_TOOLCHAIN", "nightly".to_string())?,
9191
build_cpu_limit: maybe_env("DOCS_RS_BUILD_CPU_LIMIT")?,
92+
include_default_targets: env("DOCSRS_INCLUDE_DEFAULT_TARGETS", true)?,
9293
})
9394
}
9495

src/docbuilder/rustwide_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl RustwideBuilder {
333333
let BuildTargets {
334334
default_target,
335335
other_targets,
336-
} = metadata.targets();
336+
} = metadata.targets(self.config.include_default_targets);
337337

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

0 commit comments

Comments
 (0)