@@ -3,21 +3,36 @@ use std::path::Path;
33
44use crate :: command:: Command ;
55use crate :: env:: env_var;
6+ use crate :: target;
67use crate :: util:: set_host_compiler_dylib_path;
78
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.
912#[ track_caller]
1013pub fn rustdoc ( ) -> Rustdoc {
1114 Rustdoc :: new ( )
1215}
1316
17+ /// Bare `rustdoc` invocation, no args set.
18+ #[ track_caller]
19+ pub fn bare_rustdoc ( ) -> Rustdoc {
20+ Rustdoc :: bare ( )
21+ }
22+
1423#[ derive( Debug ) ]
1524#[ must_use]
1625pub struct Rustdoc {
1726 cmd : Command ,
27+ target : Option < String > ,
1828}
1929
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+ } ) ;
2136
2237#[ track_caller]
2338fn setup_common ( ) -> Command {
@@ -28,11 +43,20 @@ fn setup_common() -> Command {
2843}
2944
3045impl 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.
3249 #[ track_caller]
3350 pub fn new ( ) -> Self {
3451 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 }
3660 }
3761
3862 /// Specify where an external library is located.
@@ -85,8 +109,9 @@ impl Rustdoc {
85109
86110 /// Specify the target triple, or a path to a custom target json spec file.
87111 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 ( ) ) ;
90115 self
91116 }
92117
0 commit comments