Skip to content

Commit 2f7ec3a

Browse files
authored
Merge pull request #1102 from waych/copy_linux_so
Copy non-Windows dynamic libs and symlinks to output
2 parents 3374653 + 71faa02 commit 2f7ec3a

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

sdl2-sys/build.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,44 @@ fn find_cargo_target_dir() -> PathBuf {
544544
out_dir
545545
}
546546

547+
#[cfg(unix)]
548+
fn copy_library_symlink(src_path: &Path, target_path: &Path) {
549+
if let Ok(link_path) = fs::read_link(src_path) {
550+
// Copy symlinks to:
551+
// * target dir: as a product ship product of the build,
552+
// * deps directory: as comment example testing doesn't pick up the library search path
553+
// otherwise and fails.
554+
let deps_path = target_path.join("deps");
555+
for path in &[target_path, &deps_path] {
556+
let dst_path = path.join(src_path.file_name().expect("Path missing filename"));
557+
// Silently drop errors here, in case the symlink already exists.
558+
let _ = std::os::unix::fs::symlink(&link_path, &dst_path);
559+
}
560+
}
561+
}
562+
#[cfg(not(unix))]
563+
fn copy_library_symlink(src_path: &Path, target_path: &Path) {}
564+
565+
fn copy_library_file(src_path: &Path, target_path: &Path) {
566+
// Copy the shared libs to:
567+
// * target dir: as a product ship product of the build,
568+
// * deps directory: as comment example testing doesn't pick up the library search path
569+
// otherwise and fails.
570+
let deps_path = target_path.join("deps");
571+
for path in &[target_path, &deps_path] {
572+
let dst_path = path.join(src_path.file_name().expect("Path missing filename"));
573+
574+
fs::copy(&src_path, &dst_path).expect(&format!(
575+
"Failed to copy SDL2 dynamic library from {} to {}",
576+
src_path.to_string_lossy(),
577+
dst_path.to_string_lossy()
578+
));
579+
}
580+
}
581+
547582
fn copy_dynamic_libraries(sdl2_compiled_path: &PathBuf, target_os: &str) {
583+
let target_path = find_cargo_target_dir();
584+
548585
// Windows binaries do not embed library search paths, so successfully
549586
// linking the DLL isn't sufficient to find it at runtime -- it must be
550587
// either on PATH or in the current working directory when we run binaries
@@ -556,19 +593,19 @@ fn copy_dynamic_libraries(sdl2_compiled_path: &PathBuf, target_os: &str) {
556593
let sdl2_bin_path = sdl2_compiled_path.join("bin");
557594
let src_dll_path = sdl2_bin_path.join(sdl2_dll_name);
558595

559-
// Copy the dll to:
560-
// * target dir: as a product ship product of the build,
561-
// * deps directory: as comment example testing doesn't pick up the library search path
562-
// otherwise and fails.
563-
let target_path = find_cargo_target_dir();
564-
let deps_path = target_path.join("deps");
565-
for path in &[target_path, deps_path] {
566-
let dst_dll_path = path.join(&sdl2_dll_name);
567-
fs::copy(&src_dll_path, &dst_dll_path).expect(&format!(
568-
"Failed to copy SDL2 dynamic library from {} to {}",
569-
src_dll_path.to_string_lossy(),
570-
dst_dll_path.to_string_lossy()
571-
));
596+
copy_library_file(&src_dll_path, &target_path);
597+
} else if target_os != "emscripten" {
598+
// Find all libraries build and copy them, symlinks included.
599+
let lib_path = sdl2_compiled_path.join("lib");
600+
for entry in std::fs::read_dir(&lib_path).expect("Couldn't readdir lib") {
601+
let entry = entry.expect("Error looking at lib dir");
602+
if let Ok(file_type) = entry.file_type() {
603+
if file_type.is_symlink() {
604+
copy_library_symlink(&entry.path(), &target_path);
605+
} else if file_type.is_file() {
606+
copy_library_file(&entry.path(), &target_path)
607+
}
608+
}
572609
}
573610
}
574611
}

0 commit comments

Comments
 (0)