1
+ #![ allow( unused_imports, dead_code) ]
2
+
1
3
#[ cfg( feature = "pkg-config" ) ]
2
4
extern crate pkg_config;
5
+ #[ cfg( feature = "bindgen" ) ]
3
6
extern crate bindgen;
4
7
5
8
use std:: path:: PathBuf ;
6
9
use std:: env;
10
+ use std:: fs;
7
11
8
12
const SDL2_BUNDLED_VERSION : & str = "2.0.5" ;
9
13
10
14
fn main ( ) {
11
15
let target = env:: var ( "TARGET" ) . expect ( "Cargo build scripts always have TARGET" ) ;
12
16
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 ) {
14
55
let mut bindings = bindgen:: Builder :: default ( ) ;
15
56
16
57
// Set correct target triple when cross-compiling
@@ -21,7 +62,7 @@ fn main() {
21
62
22
63
if let Ok ( include_path) = env:: var ( "SDL2_INCLUDE_PATH" ) {
23
64
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 ( ) ) ;
25
66
} else if build_pkgconfig ( ) {
26
67
#[ cfg( feature = "pkg-config" ) ]
27
68
for path in & pkg_config:: find_library ( "sdl2" ) . unwrap ( ) . include_paths {
@@ -34,20 +75,7 @@ fn main() {
34
75
include_path. push ( "include" ) ;
35
76
bindings = bindings. clang_arg ( String :: from ( "-I" ) +
36
77
& 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 ( ) ) ;
51
79
}
52
80
53
81
let bindings = bindings
@@ -69,14 +97,9 @@ fn main() {
69
97
. expect ( "Couldn't write bindings!" ) ;
70
98
}
71
99
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 )
80
103
}
81
104
82
105
fn add_explicit_linker_flags ( target_os : & str ) {
0 commit comments