-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.rs
146 lines (126 loc) · 6.51 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
extern crate curl;
extern crate gl_generator;
use curl::easy::Easy;
use gl_generator::{Registry, Api, Profile, Fallbacks, GlobalGenerator};
use std::env;
use std::path::Path;
use std::process::Command;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::io;
fn main() {
let profile = env::var("PROFILE").unwrap_or("Debug".to_string());
let current_dir = std::env::current_dir().unwrap();
let target;
let target_os = env::var("TARGET").unwrap();
let android_sdk_root = env::var("ANDROID_SDK_ROOT").unwrap();
let android_ndk_root = env::var("ANDROID_NDK_ROOT").unwrap();
const SDL2_FILENAME: &'static str = "SDL2-2.0.9.zip";
const SDL2_URL: &'static str = "https://www.libsdl.org/release/SDL2-2.0.9.zip";
const SDL2_PATH: &'static str = "SDL2-2.0.9";
if profile == "Release" {
target = Path::new(¤t_dir).join("target/release");
} else {
target = Path::new(¤t_dir).join("target/debug");
}
let dest = env::var("OUT_DIR").unwrap();
let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap();
Registry::new(Api::Gles2, (2, 0), Profile::Core, Fallbacks::All, [])
.write_bindings(GlobalGenerator, &mut file)
.unwrap();
Command::new("rustc")
.arg("src/test_shared.rs")
.arg("--crate-name")
.arg("test_shared")
.arg("--crate-type")
.arg("dylib")
.arg("--out-dir")
.arg(target)
.output()
.unwrap_or_else(|e| panic!("failed to execute process: {}", e));
// Download SDL if needed
if !Path::new(SDL2_FILENAME).exists() {
download_from_url(SDL2_URL, SDL2_FILENAME);
}
if !Path::new(SDL2_PATH).exists() {
unzip_file(SDL2_FILENAME);
}
if target_os.contains("ios") {
if !Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphoneos").join("libSDL2.a").exists() {
Command::new("xcodebuild")
.args(&["-project", "SDL2-2.0.9/Xcode-iOS/SDL/SDL.xcodeproj", "-target", "libSDL-iOS", "-sdk", "iphoneos12.2"])
.status()
.expect("Error building iOS project");
}
if !Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphonesimulator").join("libSDL2.a").exists() {
Command::new("xcodebuild")
.args(&["-project", "SDL2-2.0.9/Xcode-iOS/SDL/SDL.xcodeproj", "-target", "libSDL-iOS", "-sdk", "iphonesimulator12.2"])
.status()
.expect("Error building iOS Simulator project");
}
//fs::copy(Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphoneos").join("libSDL2.a"), Path::new(¤t_dir).join("target").join(target_os).join("debug").join("libSDL2.a"));
println!("{:?}", Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphonesimulator").join("libSDL2.a"));
println!("{:?}", Path::new(¤t_dir).join("target").join("x86_64-apple-ios").join("debug").join("libSDL2.a"));
fs::copy(Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphoneos").join("libSDL2.a"), Path::new(¤t_dir).join("target").join("aarch64-apple-ios").join("debug").join("libSDL2.a")).expect("Cannot copy libSDL2 for iPhone OS");
fs::copy(Path::new(¤t_dir).join(SDL2_PATH).join("Xcode-iOS").join("SDL").join("build").join("Release-iphonesimulator").join("libSDL2.a"), Path::new(¤t_dir).join("target").join("x86_64-apple-ios").join("debug").join("libSDL2.a")).expect("Cannot copy libSDL2 for iPhone Simulator");
}
if target_os.contains("android") {
//println!("cargo:rustc-flags=-C linker=/Users/tanis/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc",);
let output = Command::new("../gradlew")
.args(&["assemble"])
.current_dir("android/Minigame/sdl")
.output()
.expect("Error building Android project");
println!("status: {}", output.status);
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
panic!("hmm");
/*
println!("cargo:rustc-flags=-L android/Minigame/sdl/build/intermediates/cmake/debug/obj/armeabi",);
println!("cargo:rustc-flags=-L android/Minigame/sdl/build/intermediates/cmake/debug/obj/armeabi-v7a",);
println!("cargo:rustc-flags=-L android/Minigame/sdl/build/intermediates/cmake/debug/obj/x86",);
println!("cargo:rustc-flags=-L android/Minigame/sdl/build/intermediates/cmake/debug/obj/arm64-v8a",);
println!("cargo:rustc-flags=-L android/Minigame/sdl/build/intermediates/cmake/debug/obj/x86_64",);
*/
// We should also add the following instead of defining our toolchain in .cargo/config
// -C link-arg=--sysroot=$NDK_ROOT/platforms/android-<api level you are targeting>/arch-arm
let abi = if target_os.contains("armv7") {
"armeabi-v7a"
} else if target_os.contains("aarch64") {
"arm64-v8a"
} else if target_os.contains("arm") {
"armeabi"
} else if target_os.contains("x86_64") {
"x86_64"
} else if target_os.contains("x86") {
"x86"
} else if target_os.contains("i686") {
"x86"
} else {
panic!("Invalid target architecture {}", target_os);
};
let src = Path::new(¤t_dir).join("target").join(target_os).join("debug").join("libminigame.so");
let dst = Path::new(¤t_dir).join("android/Minigame/app/src/main/jniLibs").join(abi).join("libminigame.so");
//panic!("{:?}", dst);
//std::fs::remove_file(Path::new(&dst)).unwrap();
// This won't work as it's being executed before the actual library has finished building :(
let res = fs::copy(src, dst);
}
}
fn download_from_url(url: &str, dst_file: &str) {
File::create(dst_file).and_then(|mut file| {
let mut curl = Easy::new();
curl.url(url).expect("Error setting url");
curl.write_function(move |data| Ok(file.write(data).expect("Error writing data")))
.expect("Error setting write function");
curl.perform().expect("Error downloading archive");
Ok(())
}).expect("Could not open output file");
}
fn unzip_file(filename: &str) {
Command::new("unzip")
.args(&[filename])
.status()
.expect("Error unzipping SDL2");
}