@@ -3,21 +3,35 @@ 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.
9+ /// Construct a new `rustdoc` invocation with target automatically set to cross-compile target. Use
10+ /// [`bare_rustdoc`] to avoid this.
911#[ track_caller]
1012pub fn rustdoc ( ) -> Rustdoc {
1113 Rustdoc :: new ( )
1214}
1315
16+ /// Bare `rustdoc` invocation, no args set.
17+ #[ track_caller]
18+ pub fn bare_rustdoc ( ) -> Rustdoc {
19+ Rustdoc :: bare ( )
20+ }
21+
1422#[ derive( Debug ) ]
1523#[ must_use]
1624pub struct Rustdoc {
1725 cmd : Command ,
26+ target : Option < String > ,
1827}
1928
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+ } ) ;
2135
2236#[ track_caller]
2337fn setup_common ( ) -> Command {
@@ -28,11 +42,19 @@ fn setup_common() -> Command {
2842}
2943
3044impl 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.
3247 #[ track_caller]
3348 pub fn new ( ) -> Self {
3449 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 }
3658 }
3759
3860 /// Specify where an external library is located.
@@ -85,8 +107,9 @@ impl Rustdoc {
85107
86108 /// Specify the target triple, or a path to a custom target json spec file.
87109 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 ( ) ) ;
90113 self
91114 }
92115
0 commit comments