From 8255de7c2bf77265cf1fd8b23842c39b4efa2de2 Mon Sep 17 00:00:00 2001 From: Mike Waychison Date: Sun, 18 Apr 2021 07:58:44 -0700 Subject: [PATCH] Add missing debug_postfix when copying Windows DLL While here, also add CI testing for Windows bundled builds (both dynamic and static-link). Closes #1088. --- .github/workflows/CI.yml | 22 ++++++ sdl2-sys/build.rs | 154 ++++++++++++++------------------------- 2 files changed, 76 insertions(+), 100 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 9a711847277..6c1251ae487 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -31,6 +31,28 @@ jobs: cargo build --examples --features "${CI_BUILD_FEATURES}" cargo test --features "${CI_BUILD_FEATURES}" + build-windows: + name: build windows bundled + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + feature: ["", "static-link"] + steps: + - uses: actions/checkout@v2 + - name: Build SDL2 + shell: bash + env: + CI_BUILD_FEATURES: "gfx image ttf mixer bundled" + RUST_TEST_THREADS: 1 + run: | + set -xeuo pipefail + rustc --version + cargo --version + cargo build --features "${CI_BUILD_FEATURES},${{matrix.feature}}" + cargo build --examples --features "${CI_BUILD_FEATURES},${{matrix.feature}}" + cargo test --features "${CI_BUILD_FEATURES},${{matrix.feature}}" + build-linux: name: build linux runs-on: ubuntu-20.04 diff --git a/sdl2-sys/build.rs b/sdl2-sys/build.rs index a3dfcaeb697..740d6d25bb5 100644 --- a/sdl2-sys/build.rs +++ b/sdl2-sys/build.rs @@ -28,19 +28,16 @@ const LASTEST_SDL2_VERSION: &str = "2.0.14"; #[cfg(feature = "bindgen")] macro_rules! add_msvc_includes_to_bindings { ($bindings:expr) => { - $bindings = $bindings.clang_arg(format!( - "-IC:/Program Files (x86)/Windows Kits/8.1/Include/shared" - )); + $bindings = $bindings + .clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/8.1/Include/shared")); $bindings = $bindings.clang_arg(format!("-IC:/Program Files/LLVM/lib/clang/5.0.0/include")); $bindings = $bindings.clang_arg(format!( "-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/ucrt" )); - $bindings = $bindings.clang_arg(format!( - "-IC:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include" - )); - $bindings = $bindings.clang_arg(format!( - "-IC:/Program Files (x86)/Windows Kits/8.1/Include/um" - )); + $bindings = $bindings + .clang_arg(format!("-IC:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include")); + $bindings = + $bindings.clang_arg(format!("-IC:/Program Files (x86)/Windows Kits/8.1/Include/um")); }; } @@ -94,19 +91,12 @@ fn download_to(url: &str, dest: &str) { #[cfg(feature = "use-pkgconfig")] fn pkg_config_print(statik: bool, lib_name: &str) { - pkg_config::Config::new() - .statik(statik) - .probe(lib_name) - .unwrap(); + pkg_config::Config::new().statik(statik).probe(lib_name).unwrap(); } #[cfg(feature = "use-pkgconfig")] fn get_pkg_config() { - let statik: bool = if cfg!(feature = "static-link") { - true - } else { - false - }; + let statik: bool = if cfg!(feature = "static-link") { true } else { false }; pkg_config_print(statik, "sdl2"); if cfg!(feature = "image") { @@ -275,10 +265,8 @@ fn patch_sdl2(sdl2_source_path: &Path) { // ever have more than one hunk. assert!(added_file.len() == 1); let file_path = sdl2_source_path.join(added_file.path()); - let dst_file = fs::File::create(&file_path).expect(&format!( - "Failed to create file {}", - file_path.to_string_lossy() - )); + let dst_file = fs::File::create(&file_path) + .expect(&format!("Failed to create file {}", file_path.to_string_lossy())); let mut dst_buf = io::BufWriter::new(&dst_file); for line in added_file.into_iter().nth(0).unwrap().target_lines() { @@ -303,10 +291,7 @@ fn compile_sdl2(sdl2_build_path: &Path, target_os: &str) -> PathBuf { #[cfg(target_os = "linux")] { use version_compare::Version; - if let Ok(version) = std::process::Command::new("cc") - .arg("-dumpversion") - .output() - { + if let Ok(version) = std::process::Command::new("cc").arg("-dumpversion").output() { let local_ver = Version::from(std::str::from_utf8(&version.stdout).unwrap()).unwrap(); let affected_ver = Version::from("10").unwrap(); @@ -342,10 +327,8 @@ fn compute_include_paths() -> Vec { #[cfg(feature = "pkg-config")] { // don't print the "cargo:xxx" directives, we're just trying to get the include paths here - let pkg_config_library = pkg_config::Config::new() - .print_system_libs(false) - .probe("sdl2") - .unwrap(); + let pkg_config_library = + pkg_config::Config::new().print_system_libs(false).probe("sdl2").unwrap(); for path in pkg_config_library.include_paths { include_paths.push(format!("{}", path.display())); } @@ -354,10 +337,7 @@ fn compute_include_paths() -> Vec { #[cfg(feature = "vcpkg")] { // don't print the "cargo:xxx" directives, we're just trying to get the include paths here - let vcpkg_library = vcpkg::Config::new() - .cargo_metadata(false) - .probe("sdl2") - .unwrap(); + let vcpkg_library = vcpkg::Config::new().cargo_metadata(false).probe("sdl2").unwrap(); for path in vcpkg_library.include_paths { include_paths.push(format!("{}", path.display())); } @@ -366,6 +346,20 @@ fn compute_include_paths() -> Vec { 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")))] { @@ -410,19 +404,7 @@ fn link_sdl2(target_os: &str) { #[cfg(feature = "static-link")] { - // There's no way to extract this from `cmake::Config` so we have to emulate their - // behaviour here (see the source for `cmake::Config::build`). - let debug_postfix = match ( - &env::var("OPT_LEVEL").unwrap_or_default()[..], - &env::var("PROFILE").unwrap_or_default()[..], - ) { - ("1", _) | ("2", _) | ("3", _) | ("s", _) | ("z", _) => "", - ("0", _) => "d", - (_, "debug") => "d", - // ("0", _) => "", - // (_, "debug") => "", - (_, _) => "", - }; + let debug_postfix = debug_postfix(); if cfg!(feature = "bundled") || (cfg!(feature = "use-pkgconfig") == false && cfg!(feature = "use-vcpkg") == false) { @@ -564,12 +546,13 @@ 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 debug_postfix = debug_postfix(); + let sdl2_dll_name = format!("SDL2{}.dll", debug_postfix); let sdl2_bin_path = sdl2_compiled_path.join("bin"); let target_path = find_cargo_target_dir(); - let src_dll_path = sdl2_bin_path.join(sdl2_dll_name); - let dst_dll_path = target_path.join(sdl2_dll_name); + let src_dll_path = sdl2_bin_path.join(&sdl2_dll_name); + let dst_dll_path = target_path.join(&sdl2_dll_name); fs::copy(&src_dll_path, &dst_dll_path).expect(&format!( "Failed to copy SDL2 dynamic library from {} to {}", @@ -594,10 +577,7 @@ fn main() { let sdl2_downloaded_include_path = sdl2_source_path.join("include"); let sdl2_compiled_lib_path = sdl2_compiled_path.join("lib"); - println!( - "cargo:rustc-link-search={}", - sdl2_compiled_lib_path.display() - ); + println!("cargo:rustc-link-search={}", sdl2_compiled_lib_path.display()); #[cfg(feature = "bindgen")] { @@ -635,32 +615,20 @@ fn main() { fn copy_pregenerated_bindings() { let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - fs::copy( - crate_path.join("sdl_bindings.rs"), - out_path.join("sdl_bindings.rs"), - ) - .expect("Couldn't find pregenerated bindings!"); + fs::copy(crate_path.join("sdl_bindings.rs"), out_path.join("sdl_bindings.rs")) + .expect("Couldn't find pregenerated bindings!"); if cfg!(feature = "image") { - fs::copy( - crate_path.join("sdl_image_bindings.rs"), - out_path.join("sdl_image_bindings.rs"), - ) - .expect("Couldn't find pregenerated SDL_image bindings!"); + fs::copy(crate_path.join("sdl_image_bindings.rs"), out_path.join("sdl_image_bindings.rs")) + .expect("Couldn't find pregenerated SDL_image bindings!"); } if cfg!(feature = "ttf") { - fs::copy( - crate_path.join("sdl_ttf_bindings.rs"), - out_path.join("sdl_ttf_bindings.rs"), - ) - .expect("Couldn't find pregenerated SDL_ttf bindings!"); + fs::copy(crate_path.join("sdl_ttf_bindings.rs"), out_path.join("sdl_ttf_bindings.rs")) + .expect("Couldn't find pregenerated SDL_ttf bindings!"); } if cfg!(feature = "mixer") { - fs::copy( - crate_path.join("sdl_mixer_bindings.rs"), - out_path.join("sdl_mixer_bindings.rs"), - ) - .expect("Couldn't find pregenerated SDL_mixer bindings!"); + fs::copy(crate_path.join("sdl_mixer_bindings.rs"), out_path.join("sdl_mixer_bindings.rs")) + .expect("Couldn't find pregenerated SDL_mixer bindings!"); } if cfg!(feature = "gfx") { @@ -698,36 +666,24 @@ fn generate_bindings(target: &str, host: &str, headers_paths: &[String]) { let mut bindings = bindgen::Builder::default() // enable no_std-friendly output by only using core definitions .use_core() - .default_enum_style(bindgen::EnumVariation::Rust { - non_exhaustive: false, - }) + .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: false }) .ctypes_prefix("libc"); - let mut image_bindings = bindgen::Builder::default() - .use_core() - .raw_line("use crate::*;") - .ctypes_prefix("libc"); + let mut image_bindings = + bindgen::Builder::default().use_core().raw_line("use crate::*;").ctypes_prefix("libc"); - let mut ttf_bindings = bindgen::Builder::default() - .use_core() - .raw_line("use crate::*;") - .ctypes_prefix("libc"); + let mut ttf_bindings = + bindgen::Builder::default().use_core().raw_line("use crate::*;").ctypes_prefix("libc"); - let mut mixer_bindings = bindgen::Builder::default() - .use_core() - .raw_line("use crate::*;") - .ctypes_prefix("libc"); + let mut mixer_bindings = + bindgen::Builder::default().use_core().raw_line("use crate::*;").ctypes_prefix("libc"); let mut gfx_framerate_bindings = bindgen::Builder::default().use_core().ctypes_prefix("libc"); - let mut gfx_primitives_bindings = bindgen::Builder::default() - .use_core() - .raw_line("use crate::*;") - .ctypes_prefix("libc"); + let mut gfx_primitives_bindings = + bindgen::Builder::default().use_core().raw_line("use crate::*;").ctypes_prefix("libc"); let mut gfx_imagefilter_bindings = bindgen::Builder::default().use_core().ctypes_prefix("libc"); - let mut gfx_rotozoom_bindings = bindgen::Builder::default() - .use_core() - .raw_line("use crate::*;") - .ctypes_prefix("libc"); + let mut gfx_rotozoom_bindings = + bindgen::Builder::default().use_core().raw_line("use crate::*;").ctypes_prefix("libc"); // Set correct target triple for bindgen when cross-compiling if target != host { @@ -878,9 +834,7 @@ fn generate_bindings(target: &str, host: &str, headers_paths: &[String]) { let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("sdl_bindings.rs")) - .expect("Couldn't write bindings!"); + bindings.write_to_file(out_path.join("sdl_bindings.rs")).expect("Couldn't write bindings!"); if cfg!(feature = "image") { let image_bindings = image_bindings