3
3
//! `./x.py test` (aka [`Kind::Test`]) is currently allowed to reach build steps in other modules.
4
4
//! However, this contains ~all test parts we expect people to be able to build and run locally.
5
5
6
+ use std:: collections:: HashSet ;
6
7
use std:: ffi:: { OsStr , OsString } ;
7
8
use std:: path:: { Path , PathBuf } ;
8
9
use std:: { env, fs, iter} ;
9
10
10
11
use clap_complete:: shells;
11
12
13
+ use crate :: core:: build_steps:: compile:: run_cargo;
12
14
use crate :: core:: build_steps:: doc:: DocumentationFormat ;
13
15
use crate :: core:: build_steps:: synthetic_targets:: MirOptPanicAbortSyntheticTarget ;
14
16
use crate :: core:: build_steps:: tool:: { self , SourceType , Tool } ;
@@ -2185,6 +2187,7 @@ struct BookTest {
2185
2187
path : PathBuf ,
2186
2188
name : & ' static str ,
2187
2189
is_ext_doc : bool ,
2190
+ dependencies : Vec < & ' static str > ,
2188
2191
}
2189
2192
2190
2193
impl Step for BookTest {
@@ -2237,6 +2240,57 @@ impl BookTest {
2237
2240
// Books often have feature-gated example text.
2238
2241
rustbook_cmd. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
2239
2242
rustbook_cmd. env ( "PATH" , new_path) . arg ( "test" ) . arg ( path) ;
2243
+
2244
+ // Books may also need to build dependencies. For example, `TheBook` has
2245
+ // code samples which use the `trpl` crate. For the `rustdoc` invocation
2246
+ // to find them them successfully, they need to be built first and their
2247
+ // paths used to generate the
2248
+ let libs = if !self . dependencies . is_empty ( ) {
2249
+ let mut lib_paths = vec ! [ ] ;
2250
+ for dep in self . dependencies {
2251
+ let mode = Mode :: ToolRustc ;
2252
+ let target = builder. config . build ;
2253
+ let cargo = tool:: prepare_tool_cargo (
2254
+ builder,
2255
+ compiler,
2256
+ mode,
2257
+ target,
2258
+ Kind :: Build ,
2259
+ dep,
2260
+ SourceType :: Submodule ,
2261
+ & [ ] ,
2262
+ ) ;
2263
+
2264
+ let stamp = builder
2265
+ . cargo_out ( compiler, mode, target)
2266
+ . join ( PathBuf :: from ( dep) . file_name ( ) . unwrap ( ) )
2267
+ . with_extension ( "stamp" ) ;
2268
+
2269
+ let output_paths = run_cargo ( builder, cargo, vec ! [ ] , & stamp, vec ! [ ] , false , false ) ;
2270
+ let directories = output_paths
2271
+ . into_iter ( )
2272
+ . filter_map ( |p| p. parent ( ) . map ( ToOwned :: to_owned) )
2273
+ . fold ( HashSet :: new ( ) , |mut set, dir| {
2274
+ set. insert ( dir) ;
2275
+ set
2276
+ } ) ;
2277
+
2278
+ lib_paths. extend ( directories) ;
2279
+ }
2280
+ lib_paths
2281
+ } else {
2282
+ vec ! [ ]
2283
+ } ;
2284
+
2285
+ if !libs. is_empty ( ) {
2286
+ let paths = libs
2287
+ . into_iter ( )
2288
+ . map ( |path| path. into_os_string ( ) )
2289
+ . collect :: < Vec < OsString > > ( )
2290
+ . join ( OsStr :: new ( "," ) ) ;
2291
+ rustbook_cmd. args ( [ OsString :: from ( "--library-path" ) , paths] ) ;
2292
+ }
2293
+
2240
2294
builder. add_rust_test_threads ( & mut rustbook_cmd) ;
2241
2295
let _guard = builder. msg (
2242
2296
Kind :: Test ,
@@ -2295,6 +2349,7 @@ macro_rules! test_book {
2295
2349
$name: ident, $path: expr, $book_name: expr,
2296
2350
default =$default: expr
2297
2351
$( , submodules = $submodules: expr) ?
2352
+ $( , dependencies=$dependencies: expr) ?
2298
2353
;
2299
2354
) +) => {
2300
2355
$(
@@ -2324,11 +2379,21 @@ macro_rules! test_book {
2324
2379
builder. require_submodule( submodule, None ) ;
2325
2380
}
2326
2381
) *
2382
+
2383
+ let dependencies = vec![ ] ;
2384
+ $(
2385
+ let mut dependencies = dependencies;
2386
+ for dep in $dependencies {
2387
+ dependencies. push( dep) ;
2388
+ }
2389
+ ) ?
2390
+
2327
2391
builder. ensure( BookTest {
2328
2392
compiler: self . compiler,
2329
2393
path: PathBuf :: from( $path) ,
2330
2394
name: $book_name,
2331
2395
is_ext_doc: !$default,
2396
+ dependencies,
2332
2397
} ) ;
2333
2398
}
2334
2399
}
@@ -2343,7 +2408,7 @@ test_book!(
2343
2408
RustcBook , "src/doc/rustc" , "rustc" , default =true ;
2344
2409
RustByExample , "src/doc/rust-by-example" , "rust-by-example" , default =false , submodules=[ "src/doc/rust-by-example" ] ;
2345
2410
EmbeddedBook , "src/doc/embedded-book" , "embedded-book" , default =false , submodules=[ "src/doc/embedded-book" ] ;
2346
- TheBook , "src/doc/book" , "book" , default =false , submodules=[ "src/doc/book" ] ;
2411
+ TheBook , "src/doc/book" , "book" , default =false , submodules=[ "src/doc/book" ] , dependencies= [ "src/doc/book/packages/trpl" ] ;
2347
2412
UnstableBook , "src/doc/unstable-book" , "unstable-book" , default =true ;
2348
2413
EditionGuide , "src/doc/edition-guide" , "edition-guide" , default =false , submodules=[ "src/doc/edition-guide" ] ;
2349
2414
) ;
0 commit comments