forked from ftdi-rs/libftd2xx-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
158 lines (147 loc) · 5.95 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
use std::env;
fn search_path<'a>() -> &'a str {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => {
#[cfg(not(feature = "static"))]
{
"vendor\\windows\\amd64"
}
#[cfg(feature = "static")]
{
"vendor\\windows\\Static\\amd64"
}
}
"x86" => {
#[cfg(not(feature = "static"))]
{
"vendor\\windows\\i386"
}
#[cfg(feature = "static")]
{
"vendor\\windows\\Static\\i386"
}
}
target_arch => panic!("Target architecture not supported: {}", target_arch),
},
"linux" => match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => "vendor/linux/x64/build",
"x86" => "vendor/linux/x86/build",
"arm" | "aarch64" => match env::var("TARGET").unwrap().as_str() {
"arm-unknown-linux-musleabihf" | "arm-unknown-linux-gnueabihf" => {
"vendor/linux/armv6-hf/build"
}
"armv7-unknown-linux-musleabihf" | "armv7-unknown-linux-gnueabihf" => {
"vendor/linux/armv7-hf/build"
}
"aarch64-unknown-linux-musl" | "aarch64-unknown-linux-gnu" => {
"vendor/linux/armv8-hf/build"
}
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" | "aarch64" => "vendor/macos/build",
target_arch => panic!("Target architecture not supported: {}", target_arch),
},
target_os => panic!("Target OS not supported: {}", target_os),
}
}
fn header_path<'a>() -> &'a str {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => "vendor/windows/ftd2xx.h",
"linux" => match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => "vendor/linux/x64/ftd2xx.h",
"x86" => "vendor/linux/x86/ftd2xx.h",
"arm" | "aarch64" => match env::var("TARGET").unwrap().as_str() {
"arm-unknown-linux-musleabihf" | "arm-unknown-linux-gnueabihf" => {
"vendor/linux/armv6-hf/ftd2xx.h"
}
"armv7-unknown-linux-musleabihf" | "armv7-unknown-linux-gnueabihf" => {
"vendor/linux/armv7-hf/ftd2xx.h"
}
"aarch64-unknown-linux-musl" | "aarch64-unknown-linux-gnu" => {
"vendor/linux/armv8-hf/ftd2xx.h"
}
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" | "aarch64" => "vendor/macos/ftd2xx.h",
target_arch => panic!("Target architecture not supported: {}", target_arch),
},
target_os => panic!("Target OS not supported: {}", target_os),
}
}
// 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=ftd2xx");
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() {
println!("cargo:rustc-link-lib=static=ftd2xx");
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => {}
"linux" => {}
"macos" => {
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 = env::current_dir().unwrap();
let mut header = cwd.clone();
header.push(header_path());
let mut search = cwd;
search.push(search_path());
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())
.allowlist_function("FT_.*")
.allowlist_type("FT_.*")
.allowlist_var("FT_.*")
.rustfmt_bindings(true)
.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!");
}
}