@@ -3,21 +3,36 @@ use std::path::Path;
3
3
4
4
use crate::command::Command;
5
5
use crate::env::env_var;
6
+ use crate::target;
6
7
use crate::util::set_host_compiler_dylib_path;
7
8
8
- /// Construct a new `rustdoc` invocation. This will configure the host compiler runtime libs.
9
+ /// Construct a new `rustdoc` invocation with target automatically set to cross-compile target and
10
+ /// with host compiler runtime libs configured. Use [`bare_rustdoc`] to avoid automatically setting
11
+ /// cross-compile target.
9
12
#[track_caller]
10
13
pub fn rustdoc() -> Rustdoc {
11
14
Rustdoc::new()
12
15
}
13
16
17
+ /// Bare `rustdoc` invocation, no args set.
18
+ #[track_caller]
19
+ pub fn bare_rustdoc() -> Rustdoc {
20
+ Rustdoc::bare()
21
+ }
22
+
14
23
#[derive(Debug)]
15
24
#[must_use]
16
25
pub struct Rustdoc {
17
26
cmd: Command,
27
+ target: Option<String>,
18
28
}
19
29
20
- crate::macros::impl_common_helpers!(Rustdoc);
30
+ // Only fill in the target just before execution, so that it can be overridden.
31
+ crate::macros::impl_common_helpers!(Rustdoc, |rustdoc: &mut Rustdoc| {
32
+ if let Some(target) = &rustdoc.target {
33
+ rustdoc.cmd.arg(&format!("--target={target}"));
34
+ }
35
+ });
21
36
22
37
#[track_caller]
23
38
fn setup_common() -> Command {
@@ -28,11 +43,20 @@ fn setup_common() -> Command {
28
43
}
29
44
30
45
impl Rustdoc {
31
- /// Construct a bare `rustdoc` invocation. This will configure the host compiler runtime libs.
46
+ /// Construct a new `rustdoc` invocation with target automatically set to cross-compile target
47
+ /// and with host compiler runtime libs configured. Use [`bare_rustdoc`] to avoid automatically
48
+ /// setting cross-compile target.
32
49
#[track_caller]
33
50
pub fn new() -> Self {
34
51
let cmd = setup_common();
35
- Self { cmd }
52
+ Self { cmd, target: Some(target()) }
53
+ }
54
+
55
+ /// Bare `rustdoc` invocation, no args set.
56
+ #[track_caller]
57
+ pub fn bare() -> Self {
58
+ let cmd = setup_common();
59
+ Self { cmd, target: None }
36
60
}
37
61
38
62
/// Specify where an external library is located.
@@ -85,8 +109,9 @@ impl Rustdoc {
85
109
86
110
/// Specify the target triple, or a path to a custom target json spec file.
87
111
pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
88
- let target = target.as_ref();
89
- self.cmd.arg(format!("--target={target}"));
112
+ // We store the target as a separate field, so that it can be specified multiple times.
113
+ // This is in particular useful to override the default target set in `Rustdoc::new()`.
114
+ self.target = Some(target.as_ref().to_string());
90
115
self
91
116
}
92
117
0 commit comments