From d5d9ea1ab45e0725393846db06076701d6525759 Mon Sep 17 00:00:00 2001 From: Nathan Voglsam Date: Fri, 14 Jun 2024 18:50:14 +1000 Subject: [PATCH 1/2] Add new use_ios_framework for linking to SDL2.framework on iOS --- Cargo.toml | 1 + README.md | 4 ++++ sdl2-sys/Cargo.toml | 1 + sdl2-sys/build.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d431be61c5..f9870cc5f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ use-bindgen = ["sdl2-sys/use-bindgen"] use-pkgconfig = ["sdl2-sys/use-pkgconfig"] use-vcpkg = ["sdl2-sys/use-vcpkg"] use_mac_framework = ["sdl2-sys/use_mac_framework"] +use_ios_framework = ["sdl2-sys/use_ios_framework"] bundled = ["sdl2-sys/bundled"] static-link = ["sdl2-sys/static-link"] diff --git a/README.md b/README.md index 26fec10068..15e964f88f 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Rust-SDL2 uses the MIT license, but SDL2 itselfais in under the zlib license. * `use-pkgconfig` use pkg-config to detect where your library is located on your system. Mostly useful on unix systems for static linking. * `static-link` to link to SDL2 statically instead of dynamically. * `use_mac_framework` to use SDL2 from a Framework, on macOS only +* `use_ios_framework` to use SDL2 from a Framework, on iOS only * `bundled`, which pulls the SDL repository and compiles it from source. More information below. # Documentation @@ -141,6 +142,9 @@ default = [] use_sdl2_mac_framework = ["sdl2/use_mac_framework"] ``` +Similarly for iOS you can follow the same process using the `use_ios_framework` feature. However +official builds of the iOS framework are not available so you must compile your own SDL2.framework. + #### Static linking on macOS using vcpkg Instructions to generate a static binary on macOS and other operating systems using [vcpkg][vcpkg] are [here][cargo-vcpkg-usage]. diff --git a/sdl2-sys/Cargo.toml b/sdl2-sys/Cargo.toml index c86cead37e..2044d00e6f 100644 --- a/sdl2-sys/Cargo.toml +++ b/sdl2-sys/Cargo.toml @@ -51,6 +51,7 @@ use-vcpkg = ["vcpkg"] use-bindgen = ["bindgen"] static-link = [] use_mac_framework = [] +use_ios_framework = [] bundled = ["cmake"] mixer = [] image = [] diff --git a/sdl2-sys/build.rs b/sdl2-sys/build.rs index 067d89f05a..59b3852062 100644 --- a/sdl2-sys/build.rs +++ b/sdl2-sys/build.rs @@ -232,7 +232,9 @@ fn link_sdl2(target_os: &str) { // pkg-config automatically prints this output when probing, // however pkg_config isn't used with the feature "bundled" if cfg!(feature = "bundled") || cfg!(not(feature = "use-pkgconfig")) { - if cfg!(feature = "use_mac_framework") && target_os == "darwin" { + let use_mac_framework = cfg!(feature = "use_mac_framework") && target_os == "darwin"; + let use_ios_framework = cfg!(feature = "use_ios_framework") && target_os == "ios"; + if use_mac_framework || use_ios_framework { println!("cargo:rustc-flags=-l framework=SDL2"); } else if target_os != "emscripten" { println!("cargo:rustc-flags=-l SDL2"); @@ -328,7 +330,15 @@ fn link_sdl2(target_os: &str) { } else if target_os.contains("windows") { println!("cargo:rustc-flags=-l SDL2_mixer"); } else if target_os.contains("darwin") { - if cfg!(any(mac_framework, feature = "use_mac_framework")) { + let use_framework = cfg!(any(mac_framework, feature = "use_mac_framework")); + if use_framework { + println!("cargo:rustc-flags=-l framework=SDL2_mixer"); + } else { + println!("cargo:rustc-flags=-l SDL2_mixer"); + } + } else if target_os.contains("ios") { + let use_framework = cfg!(any(ios_framework, feature = "use_ios_framework")); + if use_framework { println!("cargo:rustc-flags=-l framework=SDL2_mixer"); } else { println!("cargo:rustc-flags=-l SDL2_mixer"); @@ -344,7 +354,15 @@ fn link_sdl2(target_os: &str) { } else if target_os.contains("windows") { println!("cargo:rustc-flags=-l SDL2_image"); } else if target_os.contains("darwin") { - if cfg!(any(mac_framework, feature = "use_mac_framework")) { + let use_framework = cfg!(any(mac_framework, feature = "use_mac_framework")); + if use_framework { + println!("cargo:rustc-flags=-l framework=SDL2_image"); + } else { + println!("cargo:rustc-flags=-l SDL2_image"); + } + } else if target_os.contains("ios") { + let use_framework = cfg!(any(ios_framework, feature = "use_ios_framework")); + if use_framework { println!("cargo:rustc-flags=-l framework=SDL2_image"); } else { println!("cargo:rustc-flags=-l SDL2_image"); @@ -360,7 +378,15 @@ fn link_sdl2(target_os: &str) { } else if target_os.contains("windows") { println!("cargo:rustc-flags=-l SDL2_ttf"); } else if target_os.contains("darwin") { - if cfg!(any(mac_framework, feature = "use_mac_framework")) { + let use_framework = cfg!(any(mac_framework, feature = "use_mac_framework")); + if use_framework { + println!("cargo:rustc-flags=-l framework=SDL2_ttf"); + } else { + println!("cargo:rustc-flags=-l SDL2_ttf"); + } + } else if target_os.contains("ios") { + let use_framework = cfg!(any(ios_framework, feature = "use_ios_framework")); + if use_framework { println!("cargo:rustc-flags=-l framework=SDL2_ttf"); } else { println!("cargo:rustc-flags=-l SDL2_ttf"); @@ -376,7 +402,15 @@ fn link_sdl2(target_os: &str) { } else if target_os.contains("windows") { println!("cargo:rustc-flags=-l SDL2_gfx"); } else if target_os.contains("darwin") { - if cfg!(any(mac_framework, feature = "use_mac_framework")) { + let use_framework = cfg!(any(mac_framework, feature = "use_mac_framework")); + if use_framework { + println!("cargo:rustc-flags=-l framework=SDL2_gfx"); + } else { + println!("cargo:rustc-flags=-l SDL2_gfx"); + } + } else if target_os.contains("ios") { + let use_framework = cfg!(any(ios_framework, feature = "use_ios_framework")); + if use_framework { println!("cargo:rustc-flags=-l framework=SDL2_gfx"); } else { println!("cargo:rustc-flags=-l SDL2_gfx"); From 2a9eb7d9bb71a0e92dc2c832b098a13616870b98 Mon Sep 17 00:00:00 2001 From: Nathan Voglsam Date: Sat, 15 Jun 2024 14:06:34 +1000 Subject: [PATCH 2/2] Update readme and changelog --- README.md | 9 +++++++++ changelog.md | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 15e964f88f..af8ff59605 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,15 @@ use_sdl2_mac_framework = ["sdl2/use_mac_framework"] Similarly for iOS you can follow the same process using the `use_ios_framework` feature. However official builds of the iOS framework are not available so you must compile your own SDL2.framework. +Using the iOS framework also requires adding the 'Frameworks' directory to your rpath so that the +dynamic linker can find SDL2.framework inside your app bundle. This is done by adding this to your +`build.rs`: + +```rust +#[cfg(target_os="ios")] +println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/Frameworks"); +``` + #### Static linking on macOS using vcpkg Instructions to generate a static binary on macOS and other operating systems using [vcpkg][vcpkg] are [here][cargo-vcpkg-usage]. diff --git a/changelog.md b/changelog.md index 2610725fd2..c19ab71cde 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,10 @@ In this file will be listed the changes, especially the breaking ones that one should be careful of when upgrading from a version of rust-sdl2 to another. +### Next + +[PR #1407](https://github.com/Rust-SDL2/rust-sdl2/pull/1407) Add new use_ios_framework for linking to SDL2.framework on iOS + ### v0.37.0 [PR #1406](https://github.com/Rust-SDL2/rust-sdl2/pull/1406) Update bindings to SDL 2.0.26, add Event.is\_touch() for mouse events, upgrade wgpu to 0.20 in examples