forked from jkvargas/russimp-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
198 lines (161 loc) · 5.35 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use flate2::read::GzDecoder;
use std::{env, fs, io, path::PathBuf};
struct Library(&'static str, &'static str);
const fn static_lib() -> &'static str {
if cfg!(feature = "static-link") {
"static"
} else {
"dylib"
}
}
const fn build_zlib() -> bool {
cfg!(not(feature = "nozlib"))
}
// Compiler specific compiler flags for CMake
fn compiler_flags() -> Vec<&'static str> {
let mut flags = Vec::new();
if cfg!(target_env = "msvc") {
flags.push("/EHsc");
// Find Ninja
if which::which("ninja").is_ok() {
env::set_var("CMAKE_GENERATOR", "Ninja");
}
}
flags
}
fn lib_names() -> Vec<Library> {
let mut names = Vec::new();
if cfg!(target_os = "windows") && cfg!(target_env = "gnu") {
panic!("Windows GNU is not supported, assimp fails to build for some reason.\nSee https://github.com/assimp/assimp/issues/4868");
} else {
names.push(Library("assimp", static_lib()));
}
if build_zlib() {
names.push(Library("zlibstatic", "static"));
} else {
names.push(Library("z", "dylib"));
}
if cfg!(target_os = "linux") {
names.push(Library("stdc++", "dylib"));
}
if cfg!(target_os = "macos") {
names.push(Library("c++", "dylib"));
}
names
}
fn build_from_source() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Build Zlib from source?
let build_zlib = if build_zlib() { "ON" } else { "OFF" };
// Build static libs?
let build_shared = if static_lib() == "static" {
"OFF"
} else {
"ON"
};
// CMake
let mut cmake = cmake::Config::new("assimp");
cmake
.profile("Release")
.static_crt(true)
.out_dir(out_dir.join(static_lib()))
.define("BUILD_SHARED_LIBS", build_shared)
.define("ASSIMP_BUILD_ASSIMP_TOOLS", "OFF")
.define("ASSIMP_BUILD_TESTS", "OFF")
.define("ASSIMP_BUILD_ZLIB", build_zlib)
.define("LIBRARY_SUFFIX", "");
// Add compiler flags
for flag in compiler_flags().iter() {
cmake.cflag(flag);
cmake.cxxflag(flag);
}
let cmake_dir = cmake.build();
println!(
"cargo:rustc-link-search=native={}",
cmake_dir.join("lib").display()
);
println!(
"cargo:rustc-link-search=native={}",
cmake_dir.join("bin").display()
);
}
fn link_from_package() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let target = env::var("TARGET").unwrap();
let crate_version = env::var("CARGO_PKG_VERSION").unwrap();
let archive_name = format!(
"russimp-{}-{}-{}.tar.gz",
crate_version,
target,
static_lib()
);
let ar_src_dir;
if option_env!("RUSSIMP_PACKAGE_DIR").is_some() {
ar_src_dir = PathBuf::from(env::var("RUSSIMP_PACKAGE_DIR").unwrap());
} else {
ar_src_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let dl_link = format!(
"https://github.com/jkvargas/russimp-sys/releases/download/v{}/{}",
crate_version, archive_name
);
match fs::File::open(ar_src_dir.join(&archive_name)) {
Ok(_) => {}
Err(_) => {
let resp = reqwest::blocking::get(dl_link).unwrap();
let mut bytes = io::Cursor::new(resp.bytes().unwrap());
let mut file = fs::File::create(ar_src_dir.join(&archive_name)).unwrap();
io::copy(&mut bytes, &mut file).unwrap();
}
}
}
dbg!(ar_src_dir.join(&archive_name));
let file = fs::File::open(ar_src_dir.join(&archive_name)).unwrap();
let mut archive = tar::Archive::new(GzDecoder::new(file));
let ar_dest_dir = out_dir.join(static_lib());
archive.unpack(&ar_dest_dir).unwrap();
println!(
"cargo:rustc-link-search=native={}",
ar_dest_dir.join("lib").display()
);
println!(
"cargo:rustc-link-search=native={}",
ar_dest_dir.join("bin").display()
);
}
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
if cfg!(feature = "build-assimp") {
build_from_source();
} else if cfg!(feature = "prebuilt") {
link_from_package();
}
bindgen::builder()
.header("wrapper.h")
.clang_arg(format!("-I{}", out_dir.join(static_lib()).join("include").display()))
.clang_arg(format!("-I{}", "assimp/include"))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.allowlist_type("ai.*")
.allowlist_function("ai.*")
.allowlist_var("ai.*")
.allowlist_var("AI_.*")
.derive_partialeq(true)
.derive_eq(true)
.derive_hash(true)
.derive_debug(true)
.generate()
.unwrap()
.write_to_file(out_dir.join("bindings.rs"))
.expect("Could not generate russimp bindings, for details see https://github.com/jkvargas/russimp-sys");
let mut built_opts = built::Options::default();
built_opts
.set_dependencies(false)
.set_compiler(false)
.set_ci(false)
.set_cfg(false);
built::write_built_file_with_opts(&built_opts, &manifest_dir, &out_dir.join("built.rs"))
.unwrap();
for n in lib_names().iter() {
println!("cargo:rustc-link-lib={}={}", n.1, n.0);
}
}