-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
206 lines (192 loc) · 7.49 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
199
200
201
202
203
204
205
206
use std::{env, path::PathBuf};
fn search_path() -> PathBuf {
// PathBuf is used so that the path segment separators match the host system
// This is important for cross-compiling for Windows from Linux
let mut path: PathBuf = PathBuf::from("vendor");
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => {
path.push("windows");
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => {
path.push("x64");
#[cfg(feature = "static")]
{
path.push("Static");
}
}
"x86" => {
path.push("Win32");
#[cfg(feature = "static")]
{
path.push("Static");
}
}
target_arch => panic!("Target architecture not supported: {target_arch}"),
}
}
"linux" => {
path.push("linux");
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => path.push("x64"),
"x86" => path.push("x86"),
"arm" | "aarch64" => match env::var("TARGET").unwrap().as_str() {
"arm-unknown-linux-musleabihf" | "arm-unknown-linux-gnueabihf" => {
path.push("armv6-hf");
}
"armv7-unknown-linux-musleabihf" | "armv7-unknown-linux-gnueabihf" => {
path.push("armv7-hf");
}
"aarch64-unknown-linux-musl" | "aarch64-unknown-linux-gnu" => {
path.push("armv8-hf");
}
target => panic!("Target not supported: {target}"),
},
target_arch => panic!("Target architecture not supported: {target_arch}"),
}
//path.push("build");
}
"macos" => match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => {
path.push("macos");
path.push("x86_64");
}
"aarch64" => {
path.push("macos");
path.push("aarch64");
}
target_arch => panic!("Target architecture not supported: {target_arch}"),
},
target_os => panic!("Target OS not supported: {target_os}"),
}
path
}
fn header_path() -> PathBuf {
// PathBuf is used so that the path segment separators match the host system
// This is important for cross-compiling for Windows from Linux
let mut path: PathBuf = PathBuf::from("vendor");
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => path.push("windows"),
"linux" => {
path.push("linux");
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => path.push("x64"),
"x86" => path.push("x86"),
"arm" | "aarch64" => match env::var("TARGET").unwrap().as_str() {
"arm-unknown-linux-musleabihf" | "arm-unknown-linux-gnueabihf" => {
path.push("armv6-hf")
}
"armv7-unknown-linux-musleabihf" | "armv7-unknown-linux-gnueabihf" => {
path.push("armv7-hf")
}
"aarch64-unknown-linux-musl" | "aarch64-unknown-linux-gnu" => {
path.push("armv8-hf")
}
target => panic!("Target not supported: {target}"),
},
target_arch => panic!("Target architecture not supported: {target_arch}"),
}
}
"macos" => match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => {
path.push("macos");
path.push("x86_64");
}
"aarch64" => {
path.push("macos");
path.push("aarch64");
}
target_arch => panic!("Target architecture not supported: {target_arch}"),
},
target_os => panic!("Target OS not supported: {target_os}"),
}
// Windows source is caps, which only matters for cross compiling...
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => {
path.push("FTD3XX.h");
}
_ => {
path.push("ftd3xx.h");
}
}
path
}
// This adds sysroot to bindgen if cross-compiling.
// This is not great, please open an issue or pull-request if you know of a
// better way to handle this problem.
#[cfg(feature = "bindgen")]
fn clang_args() -> &'static [&'static str] {
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
#[cfg(all(target_os = "linux", not(target_arch = "arm")))]
"arm" => &["--sysroot=/usr/arm-linux-gnueabihf"],
#[cfg(all(target_os = "linux", not(target_arch = "aarch64")))]
"aarch64" => &["--sysroot=/usr/aarch64-linux-gnu"],
_ => &[],
}
}
#[cfg(not(feature = "static"))]
fn linker_options() {
println!("cargo:rustc-link-lib=dylib=ftd3xx");
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"macos" => {
println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
}
"linux" | "windows" => {}
target_os => panic!("Target OS not supported: {target_os}"),
}
}
#[cfg(feature = "static")]
fn linker_options() {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => {
println!("cargo:rustc-link-lib=static=ftd3xx")
}
"linux" => {
println!("cargo:rustc-link-lib=static=ftd3xx-static")
}
"macos" => {
println!("cargo:rustc-link-lib=static=ftd3xx-static");
println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
}
target_os => panic!("Target OS not supported: {target_os}"),
}
}
fn main() {
let cwd: PathBuf = env::current_dir().unwrap();
let mut header: PathBuf = cwd.clone();
header.push(header_path());
let mut search: PathBuf = cwd;
search.push(search_path());
// Debug output to be able to see search path
//println!("cargo:warning=search path: {}", search.display());
println!(
"cargo:rustc-link-search=native={}",
search.to_str().unwrap()
);
linker_options();
println!("cargo:rerun-if-changed={}", header.to_str().unwrap());
println!("cargo:rerun-if-env-changed=LIBMSVC_PATH");
#[cfg(feature = "bindgen")]
{
let bindings = bindgen::Builder::default()
.header(header.to_str().unwrap())
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: false,
})
.allowlist_function("FT_.*")
.allowlist_type("FT_.*")
.allowlist_type("_FT_.*")
.allowlist_var("FT_.*")
.formatter(bindgen::Formatter::Rustfmt)
.derive_default(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_args(clang_args())
.generate()
.expect("Unable to generate bindings");
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
}