Skip to content

Commit 803321b

Browse files
committed
Add preliminary support for running doctests.
This partially addresses #225. This only works on nightly, due to the use of the unstable feature [doctest-xcompile](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#doctest-xcompile) The relevant tracking issues are: - rust-lang/cargo#7040 - rust-lang/rust#64245 If the subcommand is `test` and the compiler is nightly, we provide the `-Zdoctest-xcompile` flag if `CROSS_UNSTABLE_ENABLE_DOCTESTS=true`.
1 parent 2f24bd4 commit 803321b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ passthrough = [
207207
]
208208
```
209209

210+
### Unstable Features
211+
212+
Certain unstable features can enable additional functionality useful to
213+
cross-compiling. Note that these are unstable, and may be removed at any
214+
time (particularly if the feature is stabilized or removed), and will
215+
only be used on a nightly channel.
216+
217+
- `CROSS_UNSTABLE_ENABLE_DOCTESTS=true`: also run doctests.
218+
210219
### Mounting volumes into the build environment
211220

212221
In addition to passing environment variables, you can also specify environment

src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Args {
1414
pub target: Option<Target>,
1515
pub target_dir: Option<PathBuf>,
1616
pub docker_in_docker: bool,
17+
pub enable_doctests: bool,
1718
}
1819

1920
// Fix for issue #581. target_dir must be absolute.
@@ -75,6 +76,9 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
7576
let docker_in_docker = env::var("CROSS_DOCKER_IN_DOCKER")
7677
.map(|s| bool::from_str(&s).unwrap_or_default())
7778
.unwrap_or_default();
79+
let enable_doctests = env::var("CROSS_UNSTABLE_ENABLE_DOCTESTS")
80+
.map(|s| bool::from_str(&s).unwrap_or_default())
81+
.unwrap_or_default();
7882

7983
Ok(Args {
8084
all,
@@ -83,5 +87,6 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
8387
target,
8488
target_dir,
8589
docker_in_docker,
90+
enable_doctests,
8691
})
8792
}

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ fn run() -> Result<ExitStatus> {
279279

280280
let host_version_meta =
281281
rustc_version::version_meta().wrap_err("couldn't fetch the `rustc` version")?;
282+
let is_nightly = rustc::is_nightly(&args.channel, &host_version_meta);
282283
if let Some(root) = cargo::root()? {
283284
let host = host_version_meta.host();
284285
let toml = toml(&root)?;
@@ -358,7 +359,7 @@ fn run() -> Result<ExitStatus> {
358359
.map(|sc| sc.needs_interpreter())
359360
.unwrap_or(false);
360361

361-
let filtered_args = if args
362+
let mut filtered_args = if args
362363
.subcommand
363364
.map_or(false, |s| !s.needs_target_in_command())
364365
{
@@ -384,6 +385,11 @@ fn run() -> Result<ExitStatus> {
384385
args.all.clone()
385386
};
386387

388+
let is_test = args.subcommand.map(|sc| sc == Subcommand::Test).unwrap_or(false);
389+
if is_test && args.enable_doctests && is_nightly {
390+
filtered_args.push("-Zdoctest-xcompile".to_string());
391+
}
392+
387393
if target.needs_docker() && args.subcommand.map(|sc| sc.needs_docker()).unwrap_or(false)
388394
{
389395
if host_version_meta.needs_interpreter()

src/rustc.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22
use std::process::Command;
33

4-
use rustc_version::{Version, VersionMeta};
4+
use rustc_version::{Channel, Version, VersionMeta};
55

66
use crate::errors::*;
77
use crate::extensions::CommandExt;
@@ -33,6 +33,14 @@ impl VersionMetaExt for VersionMeta {
3333
}
3434
}
3535

36+
pub fn is_nightly(channel: &Option<String>, host_version_meta: &VersionMeta) -> bool {
37+
if let Some(channel) = channel {
38+
channel.contains("nightly")
39+
} else {
40+
host_version_meta.channel == Channel::Nightly
41+
}
42+
}
43+
3644
pub fn target_list(verbose: bool) -> Result<TargetList> {
3745
Command::new("rustc")
3846
.args(&["--print", "target-list"])

0 commit comments

Comments
 (0)