@@ -122,7 +122,8 @@ pub struct Metadata {
122
122
targets : Option < Vec < String > > ,
123
123
124
124
/// List of command line arguments for `rustc`.
125
- rustc_args : Option < Vec < String > > ,
125
+ #[ serde( default ) ]
126
+ rustc_args : Vec < String > ,
126
127
127
128
/// List of command line arguments for `rustdoc`.
128
129
#[ serde( default ) ]
@@ -217,15 +218,22 @@ impl Metadata {
217
218
218
219
/// Return the arguments that should be passed to `cargo`.
219
220
///
220
- // TODO: maybe it shouldn't?
221
- /// This will always include `doc --lib --no-deps`.
221
+ /// This will always include `rustdoc --lib --`.
222
222
/// This will never include `--target`.
223
223
///
224
+ /// You can pass `additional_args` to cargo, as well as `rustdoc_args` to `rustdoc`.
225
+ /// Do not depend on modifying the `Vec` after it's returned; additional arguments
226
+ /// appended may be passed to rustdoc instead.
227
+ ///
224
228
/// Note that this does not necessarily reproduce the HTML _output_ of docs.rs exactly.
225
229
/// For example, the links may point somewhere different than they would on docs.rs.
226
230
/// However, rustdoc will see exactly the same code as it would on docs.rs, even counting `cfg`s.
227
- pub fn cargo_args ( & self ) -> Vec < String > {
228
- let mut cargo_args: Vec < String > = vec ! [ "doc" . into( ) , "--lib" . into( ) , "--no-deps" . into( ) ] ;
231
+ pub fn cargo_args (
232
+ & self ,
233
+ additional_args : & [ impl AsRef < str > ] ,
234
+ rustdoc_args : & [ impl AsRef < str > ] ,
235
+ ) -> Vec < String > {
236
+ let mut cargo_args: Vec < String > = vec ! [ "rustdoc" . into( ) , "--lib" . into( ) ] ;
229
237
230
238
if let Some ( features) = & self . features {
231
239
cargo_args. push ( "--features" . into ( ) ) ;
@@ -240,22 +248,31 @@ impl Metadata {
240
248
cargo_args. push ( "--no-default-features" . into ( ) ) ;
241
249
}
242
250
251
+ // Pass `RUSTFLAGS` using `cargo --config`, which handles whitespace correctly.
252
+ if !self . rustc_args . is_empty ( ) {
253
+ cargo_args. push ( "-Z" . into ( ) ) ;
254
+ cargo_args. push ( "unstable-options" . into ( ) ) ;
255
+ cargo_args. push ( "--config" . into ( ) ) ;
256
+ let rustflags =
257
+ toml:: to_string ( & self . rustc_args ) . expect ( "serializing a string should never fail" ) ;
258
+ cargo_args. push ( format ! ( "build.rustflags={}" , rustflags) ) ;
259
+ }
260
+
261
+ cargo_args. extend ( additional_args. iter ( ) . map ( |s| s. as_ref ( ) . to_owned ( ) ) ) ;
262
+ cargo_args. push ( "--" . into ( ) ) ;
263
+ cargo_args. extend_from_slice ( & self . rustdoc_args ) ;
264
+ cargo_args. extend ( rustdoc_args. iter ( ) . map ( |s| s. as_ref ( ) . to_owned ( ) ) ) ;
243
265
cargo_args
244
266
}
245
267
246
268
/// Return the environment variables that should be set when building this crate.
247
269
///
248
270
/// This will always contain at least `RUSTDOCFLAGS="-Z unstable-options"`.
249
271
pub fn environment_variables ( & self ) -> HashMap < & ' static str , String > {
250
- let joined = |v : & Option < Vec < _ > > | v. as_ref ( ) . map ( |args| args. join ( " " ) ) . unwrap_or_default ( ) ;
251
-
252
272
let mut map = HashMap :: new ( ) ;
253
- map. insert ( "RUSTFLAGS" , joined ( & self . rustc_args ) ) ;
254
- map. insert ( "RUSTDOCFLAGS" , self . rustdoc_args . join ( " " ) ) ;
255
273
// For docs.rs detection from build scripts:
256
274
// https://github.com/rust-lang/docs.rs/issues/147
257
275
map. insert ( "DOCS_RS" , "1" . into ( ) ) ;
258
-
259
276
map
260
277
}
261
278
}
0 commit comments