Skip to content

Commit c33715b

Browse files
pyrrhoCobrand
authored andcommitted
"Install" Windows dynamic libraries
Windows binaries don't embed library search paths, so tests were failing for being unable to find sdl2.dll. This commit adds some logic to copy that DLL into a known-good location.
1 parent a0a1b4b commit c33715b

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

sdl2-sys/build.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,59 @@ fn link_sdl2(target_os: &str) {
281281
}
282282
}
283283

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+
284328
fn main() {
285329
let target = env::var("TARGET").expect("Cargo build scripts always have TARGET");
286330
let host = env::var("HOST").expect("Cargo build scripts always have HOST");
287331
let target_os = get_os_from_triple(target.as_str()).unwrap();
288332

333+
let sdl2_compiled_path;
289334
#[cfg(feature = "bundled")] {
290335
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);
292337

293338
let sdl2_downloaded_include_path = sdl2_source_path.join("include");
294339
let sdl2_compiled_lib_path = sdl2_compiled_path.join("lib");
@@ -311,6 +356,10 @@ fn main() {
311356
}
312357

313358
link_sdl2(target_os);
359+
360+
#[cfg(all(feature = "bundled", not(feature = "static-link")))] {
361+
copy_dynamic_libraries(&sdl2_compiled_path, target_os);
362+
}
314363
}
315364

316365
#[cfg(not(feature = "bindgen"))]

0 commit comments

Comments
 (0)