Skip to content

Commit

Permalink
Add missing debug_postfix when copying Windows DLL
Browse files Browse the repository at this point in the history
While here, also add CI testing for Windows bundled builds (both dynamic
and static-link).

Closes Rust-SDL2#1088.
  • Loading branch information
waych committed Apr 18, 2021
1 parent 6d18305 commit 8255de7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 100 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
154 changes: 54 additions & 100 deletions sdl2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
};
}

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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() {
Expand All @@ -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();

Expand Down Expand Up @@ -342,10 +327,8 @@ fn compute_include_paths() -> Vec<String> {
#[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()));
}
Expand All @@ -354,10 +337,7 @@ fn compute_include_paths() -> Vec<String> {
#[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()));
}
Expand All @@ -366,6 +346,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 @@ -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)
{
Expand Down Expand Up @@ -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 {}",
Expand All @@ -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")]
{
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8255de7

Please sign in to comment.