Skip to content

Commit 9281179

Browse files
committed
Add debug variant of "bundled" (try #2)
This adds back the ability to compile the debug version of SDL2 when the cargo build is a debug build. Previous attempt (PR #1081) broke dynamic linking bundled builds on Windows, forgetting to copy the resulting DLL correctly and misspecifying the name of the library in that case.
1 parent bfd9840 commit 9281179

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another.
33

44
### Unreleased
55

6+
* [PR #1092](https://github.com/Rust-SDL2/rust-sdl2/pull/1092) Add debug builds to "bundled", second attempt.
7+
68
* Rollback PR #1081: Broke dynamic linking on Windows #1088
79

810
### v0.34.4

sdl2-sys/build.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ fn patch_sdl2(sdl2_source_path: &Path) {
293293
#[cfg(feature = "bundled")]
294294
fn compile_sdl2(sdl2_build_path: &Path, target_os: &str) -> PathBuf {
295295
let mut cfg = cmake::Config::new(sdl2_build_path);
296-
cfg.profile("release");
297296

298297
// Override __FLTUSED__ to keep the _fltused symbol from getting defined in the static build.
299298
// This conflicts and fails to link properly when building statically on Windows, likely due to
@@ -367,6 +366,20 @@ fn compute_include_paths() -> Vec<String> {
367366
include_paths
368367
}
369368

369+
/// There's no easy way to extract this suffix from `cmake::Config` so we have to emulate their
370+
/// behaviour here (see the source for `cmake::Config::build`).
371+
fn debug_postfix() -> &'static str {
372+
match (
373+
&env::var("OPT_LEVEL").unwrap_or_default()[..],
374+
&env::var("PROFILE").unwrap_or_default()[..],
375+
) {
376+
("1", _) | ("2", _) | ("3", _) | ("s", _) | ("z", _) => "",
377+
("0", _) => "d",
378+
(_, "debug") => "d",
379+
(_, _) => "",
380+
}
381+
}
382+
370383
fn link_sdl2(target_os: &str) {
371384
#[cfg(all(feature = "use-pkgconfig", not(feature = "bundled")))]
372385
{
@@ -403,6 +416,8 @@ fn link_sdl2(target_os: &str) {
403416
if cfg!(feature = "bundled") || cfg!(not(feature = "use-pkgconfig")) {
404417
if cfg!(feature = "use_mac_framework") && target_os == "darwin" {
405418
println!("cargo:rustc-flags=-l framework=SDL2");
419+
} else if target_os.contains("windows") {
420+
println!("cargo:rustc-flags=-l SDL2{}", debug_postfix());
406421
} else if target_os != "emscripten" {
407422
println!("cargo:rustc-flags=-l SDL2");
408423
}
@@ -414,8 +429,8 @@ fn link_sdl2(target_os: &str) {
414429
if cfg!(feature = "bundled")
415430
|| (cfg!(feature = "use-pkgconfig") == false && cfg!(feature = "use-vcpkg") == false)
416431
{
417-
println!("cargo:rustc-link-lib=static=SDL2main");
418-
println!("cargo:rustc-link-lib=static=SDL2");
432+
println!("cargo:rustc-link-lib=static=SDL2main{}", debug_postfix());
433+
println!("cargo:rustc-link-lib=static=SDL2{}", debug_postfix());
419434
}
420435

421436
// Also linked to any required libraries for each supported platform
@@ -552,9 +567,9 @@ fn copy_dynamic_libraries(sdl2_compiled_path: &PathBuf, target_os: &str) {
552567
// copy sdl2.dll out of its build tree and down to the top level cargo
553568
// binary output directory.
554569
if target_os.contains("windows") {
555-
let sdl2_dll_name = "SDL2.dll";
570+
let sdl2_dll_name = format!("SDL2{}.dll", debug_postfix());
556571
let sdl2_bin_path = sdl2_compiled_path.join("bin");
557-
let src_dll_path = sdl2_bin_path.join(sdl2_dll_name);
572+
let src_dll_path = sdl2_bin_path.join(&sdl2_dll_name);
558573

559574
// Copy the dll to:
560575
// * target dir: as a product ship product of the build,

0 commit comments

Comments
 (0)