@@ -281,14 +281,59 @@ fn link_sdl2(target_os: &str) {
281
281
}
282
282
}
283
283
284
+ fn find_cargo_target_dir ( ) -> PathBuf {
285
+ // Infer the top level cargo target dir from the OUT_DIR by searching
286
+ // upwards until we get to $CARGO_TARGET_DIR/build/ (which is always one
287
+ // level up from the deepest directory containing our package name)
288
+ let pkg_name = env:: var ( "CARGO_PKG_NAME" ) . unwrap ( ) ;
289
+ let mut out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
290
+ loop {
291
+ {
292
+ let final_path_segment = out_dir. file_name ( ) . unwrap ( ) ;
293
+ if final_path_segment. to_string_lossy ( ) . contains ( & pkg_name) {
294
+ break ;
295
+ }
296
+ }
297
+ if !out_dir. pop ( ) {
298
+ panic ! ( "Malformed build path: {}" , out_dir. to_string_lossy( ) ) ;
299
+ }
300
+ }
301
+ out_dir. pop ( ) ;
302
+ out_dir. pop ( ) ;
303
+ out_dir
304
+ }
305
+
306
+ fn copy_dynamic_libraries ( sdl2_compiled_path : & PathBuf , target_os : & str ) {
307
+ // Windows binaries do not embed library search paths, so successfully
308
+ // linking the DLL isn't sufficient to find it at runtime -- it must be
309
+ // either on PATH or in the current working directory when we run binaries
310
+ // linked against it. In other words, to run the test suite we need to
311
+ // copy sdl2.dll out of its build tree and down to the top level cargo
312
+ // binary output directory.
313
+ if target_os. contains ( "windows" ) {
314
+ let sdl2_dll_name = "sdl2.dll" ;
315
+ let sdl2_bin_path = sdl2_compiled_path. join ( "bin" ) ;
316
+ let target_path = find_cargo_target_dir ( ) ;
317
+
318
+ let src_dll_path = sdl2_bin_path. join ( sdl2_dll_name) ;
319
+ let dst_dll_path = target_path. join ( sdl2_dll_name) ;
320
+
321
+ fs:: copy ( & src_dll_path, & dst_dll_path)
322
+ . expect ( & format ! ( "Failed to copy SDL2 dynamic library from {} to {}" ,
323
+ src_dll_path. to_string_lossy( ) ,
324
+ dst_dll_path. to_string_lossy( ) ) ) ;
325
+ }
326
+ }
327
+
284
328
fn main ( ) {
285
329
let target = env:: var ( "TARGET" ) . expect ( "Cargo build scripts always have TARGET" ) ;
286
330
let host = env:: var ( "HOST" ) . expect ( "Cargo build scripts always have HOST" ) ;
287
331
let target_os = get_os_from_triple ( target. as_str ( ) ) . unwrap ( ) ;
288
332
333
+ let sdl2_compiled_path;
289
334
#[ cfg( feature = "bundled" ) ] {
290
335
let sdl2_source_path = download_sdl2 ( ) ;
291
- let sdl2_compiled_path = compile_sdl2 ( sdl2_source_path. as_path ( ) , target_os) ;
336
+ sdl2_compiled_path = compile_sdl2 ( sdl2_source_path. as_path ( ) , target_os) ;
292
337
293
338
let sdl2_downloaded_include_path = sdl2_source_path. join ( "include" ) ;
294
339
let sdl2_compiled_lib_path = sdl2_compiled_path. join ( "lib" ) ;
@@ -311,6 +356,10 @@ fn main() {
311
356
}
312
357
313
358
link_sdl2 ( target_os) ;
359
+
360
+ #[ cfg( all( feature = "bundled" , not( feature = "static-link" ) ) ) ] {
361
+ copy_dynamic_libraries ( & sdl2_compiled_path, target_os) ;
362
+ }
314
363
}
315
364
316
365
#[ cfg( not( feature = "bindgen" ) ) ]
0 commit comments