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