Skip to content

Commit a538de7

Browse files
committed
Bindgen is now optional
1 parent bb6caa8 commit a538de7

File tree

5 files changed

+16022
-67
lines changed

5 files changed

+16022
-67
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ optional = true
3535

3636
[features]
3737

38-
default = ["use-pkgconfig"]
38+
default = []
3939
ttf = []
4040
image = []
4141
gfx = ["c_vec"]
4242
mixer = []
4343

44+
use-bindgen = ["sdl2-sys/use-bindgen"]
4445
use-pkgconfig = ["sdl2-sys/use-pkgconfig"]
4546
use_mac_framework = ["sdl2-sys/use_mac_framework"]

sdl2-sys/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ build = "build.rs"
1515
name = "sdl2_sys"
1616
path = "src/lib.rs"
1717

18-
[build-dependencies]
19-
bindgen = "0.29"
18+
[build-dependencies.bindgen]
19+
version = "0.30"
20+
optional = true
2021

2122
[build-dependencies.pkg-config]
2223
version = "0.3.9"
@@ -26,4 +27,5 @@ optional = true
2627

2728
default = []
2829
use-pkgconfig = ["pkg-config"]
30+
use-bindgen = ["bindgen"]
2931
use_mac_framework = []

sdl2-sys/build.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,57 @@
1+
#![allow(unused_imports, dead_code)]
2+
13
#[cfg(feature = "pkg-config")]
24
extern crate pkg_config;
5+
#[cfg(feature = "bindgen")]
36
extern crate bindgen;
47

58
use std::path::PathBuf;
69
use std::env;
10+
use std::fs;
711

812
const SDL2_BUNDLED_VERSION: &str = "2.0.5";
913

1014
fn main() {
1115
let target = env::var("TARGET").expect("Cargo build scripts always have TARGET");
1216
let host = env::var("HOST").expect("Cargo build scripts always have HOST");
13-
let target_os = target.split("-").nth(2).unwrap();
17+
18+
prepare_bindings(&target, &host);
19+
20+
if get_os_from_triple(&target).unwrap() == "ios" {
21+
println!("cargo:rustc-flags=-l framework=AVFoundation");
22+
println!("cargo:rustc-flags=-l framework=AudioToolbox");
23+
println!("cargo:rustc-flags=-l framework=CoreAudio");
24+
println!("cargo:rustc-flags=-l framework=CoreGraphics");
25+
println!("cargo:rustc-flags=-l framework=CoreMotion");
26+
println!("cargo:rustc-flags=-l framework=Foundation");
27+
println!("cargo:rustc-flags=-l framework=GameController");
28+
println!("cargo:rustc-flags=-l framework=OpenGLES");
29+
println!("cargo:rustc-flags=-l framework=QuartzCore");
30+
println!("cargo:rustc-flags=-l framework=UIKit");
31+
}
32+
}
33+
34+
#[cfg(not(feature = "pkg-config"))]
35+
fn build_pkgconfig() -> bool {
36+
false
37+
}
38+
39+
#[cfg(feature = "pkg-config")]
40+
fn build_pkgconfig() -> bool {
41+
pkg_config::probe_library("sdl2").is_ok()
42+
}
43+
44+
#[cfg(not(feature = "bindgen"))]
45+
fn prepare_bindings(target: &str, host: &str) {
46+
add_explicit_linker_flags(get_os_from_triple(target).unwrap());
47+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
48+
let crate_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
49+
fs::copy(crate_path.join("pregenerated_bindings.rs"), out_path.join("bindings.rs"))
50+
.expect("Couldn't find pregenerated bindings!");
51+
}
52+
53+
#[cfg(feature = "bindgen")]
54+
fn prepare_bindings(target: &str, host: &str) {
1455
let mut bindings = bindgen::Builder::default();
1556

1657
// Set correct target triple when cross-compiling
@@ -21,7 +62,7 @@ fn main() {
2162

2263
if let Ok(include_path) = env::var("SDL2_INCLUDE_PATH") {
2364
bindings = bindings.clang_arg(String::from("-I") + &include_path);
24-
add_explicit_linker_flags(target_os);
65+
add_explicit_linker_flags(get_os_from_triple(target).unwrap());
2566
} else if build_pkgconfig() {
2667
#[cfg(feature = "pkg-config")]
2768
for path in &pkg_config::find_library("sdl2").unwrap().include_paths {
@@ -34,20 +75,7 @@ fn main() {
3475
include_path.push("include");
3576
bindings = bindings.clang_arg(String::from("-I") +
3677
&include_path.into_os_string().into_string().unwrap());
37-
add_explicit_linker_flags(target_os);
38-
}
39-
40-
if target_os == "ios" {
41-
println!("cargo:rustc-flags=-l framework=AVFoundation");
42-
println!("cargo:rustc-flags=-l framework=AudioToolbox");
43-
println!("cargo:rustc-flags=-l framework=CoreAudio");
44-
println!("cargo:rustc-flags=-l framework=CoreGraphics");
45-
println!("cargo:rustc-flags=-l framework=CoreMotion");
46-
println!("cargo:rustc-flags=-l framework=Foundation");
47-
println!("cargo:rustc-flags=-l framework=GameController");
48-
println!("cargo:rustc-flags=-l framework=OpenGLES");
49-
println!("cargo:rustc-flags=-l framework=QuartzCore");
50-
println!("cargo:rustc-flags=-l framework=UIKit");
78+
add_explicit_linker_flags(get_os_from_triple(target).unwrap());
5179
}
5280

5381
let bindings = bindings
@@ -69,14 +97,9 @@ fn main() {
6997
.expect("Couldn't write bindings!");
7098
}
7199

72-
#[cfg(not(feature = "pkg-config"))]
73-
fn build_pkgconfig() -> bool {
74-
false
75-
}
76-
77-
#[cfg(feature = "pkg-config")]
78-
fn build_pkgconfig() -> bool {
79-
pkg_config::probe_library("sdl2").is_ok()
100+
fn get_os_from_triple(triple: &str) -> Option<&str>
101+
{
102+
triple.split("-").nth(2)
80103
}
81104

82105
fn add_explicit_linker_flags(target_os: &str) {

0 commit comments

Comments
 (0)