Skip to content

Commit

Permalink
Add debug variant of "bundled" (try Rust-SDL2#2)
Browse files Browse the repository at this point in the history
This adds back the ability to compile the debug version of SDL2 when the
cargo build is a debug build.  Previous attempt (PR Rust-SDL2#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.
  • Loading branch information
waych committed Apr 23, 2021
1 parent bfd9840 commit 9281179
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another.

### Unreleased

* [PR #1092](https://github.com/Rust-SDL2/rust-sdl2/pull/1092) Add debug builds to "bundled", second attempt.

* Rollback PR #1081: Broke dynamic linking on Windows #1088

### v0.34.4
Expand Down
25 changes: 20 additions & 5 deletions sdl2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ fn patch_sdl2(sdl2_source_path: &Path) {
#[cfg(feature = "bundled")]
fn compile_sdl2(sdl2_build_path: &Path, target_os: &str) -> PathBuf {
let mut cfg = cmake::Config::new(sdl2_build_path);
cfg.profile("release");

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

/// There's no easy way to extract this suffix from `cmake::Config` so we have to emulate their
/// behaviour here (see the source for `cmake::Config::build`).
fn debug_postfix() -> &'static str {
match (
&env::var("OPT_LEVEL").unwrap_or_default()[..],
&env::var("PROFILE").unwrap_or_default()[..],
) {
("1", _) | ("2", _) | ("3", _) | ("s", _) | ("z", _) => "",
("0", _) => "d",
(_, "debug") => "d",
(_, _) => "",
}
}

fn link_sdl2(target_os: &str) {
#[cfg(all(feature = "use-pkgconfig", not(feature = "bundled")))]
{
Expand Down Expand Up @@ -403,6 +416,8 @@ fn link_sdl2(target_os: &str) {
if cfg!(feature = "bundled") || cfg!(not(feature = "use-pkgconfig")) {
if cfg!(feature = "use_mac_framework") && target_os == "darwin" {
println!("cargo:rustc-flags=-l framework=SDL2");
} else if target_os.contains("windows") {
println!("cargo:rustc-flags=-l SDL2{}", debug_postfix());
} else if target_os != "emscripten" {
println!("cargo:rustc-flags=-l SDL2");
}
Expand All @@ -414,8 +429,8 @@ fn link_sdl2(target_os: &str) {
if cfg!(feature = "bundled")
|| (cfg!(feature = "use-pkgconfig") == false && cfg!(feature = "use-vcpkg") == false)
{
println!("cargo:rustc-link-lib=static=SDL2main");
println!("cargo:rustc-link-lib=static=SDL2");
println!("cargo:rustc-link-lib=static=SDL2main{}", debug_postfix());
println!("cargo:rustc-link-lib=static=SDL2{}", debug_postfix());
}

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

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

0 comments on commit 9281179

Please sign in to comment.