-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
185 lines (164 loc) · 6.3 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
// SPDX-License-Identifier: MIT
use std::env;
use std::path::PathBuf;
cfg_if::cfg_if! {
if #[cfg(feature = "bindgen")] {
use bindgen;
}
}
cfg_if::cfg_if! {
if #[cfg(all(windows, target_env="msvc"))] {
fn find_library() -> Result<vcpkg::Library, vcpkg::Error> {
vcpkg::find_package("libftdi1")
}
} else {
fn find_library() -> Result<pkg_config::Library, pkg_config::Error> {
pkg_config::Config::new().atleast_version("1.4").probe("libftdi1")
}
}
}
fn main() {
if cfg!(feature = "vendored") {
build_source();
} else {
if cfg!(feature = "libusb1-sys") {
match env::var("DEP_USB_1.0_STATIC") {
Ok(ref val) if val == "1" => {
panic!("libftdi1-sys can use static libusb1-sys with `vendored` feature only");
}
_ => {}
}
}
if let Err(err) = find_library() {
println!(
"cargo:warning={}: {}",
"Config for libftdi1 not found, falling back to default search path", err,
);
println!("cargo:rustc-link-lib=dylib=ftdi1");
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "bindgen")] {
#[derive(Debug)]
struct Callbacks;
use bindgen::callbacks::{IntKind, MacroParsingBehavior, ParseCallbacks};
impl ParseCallbacks for Callbacks {
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
static OVERRIDE_IGNORE: &[&str] = &[
"SIO_RESET_PURGE_RX",
"SIO_RESET_PURGE_TX",
];
if OVERRIDE_IGNORE.contains(&name) {
MacroParsingBehavior::Ignore
} else {
MacroParsingBehavior::Default
}
}
fn int_macro(&self, name: &str, _value: i64)-> Option<IntKind> {
static OVERRIDE_SIGNED: &[&str] = &[
"SIO_DISABLE_FLOW_CTRL",
"SIO_RTS_CTS_HS",
"SIO_DTR_DSR_HS",
"SIO_XON_XOFF_HS",
];
if OVERRIDE_SIGNED.contains(&name) {
Some(IntKind::Int)
} else {
None
}
}
}
fn bindings_builder() -> bindgen::Builder {
bindgen::Builder::default()
.header(header_path())
.default_enum_style(bindgen::EnumVariation::NewType {
is_bitfield: false,
is_global: false,
})
.parse_callbacks(Box::new(Callbacks))
.allowlist_function("ftdi_.*")
.allowlist_type("ftdi_.*")
.generate_comments(false)
.allowlist_var("SIO_.*")
.no_copy("ftdi_eeprom")
.no_copy("ftdi_device_list")
.no_copy("ftdi_context")
.no_copy("ftdi_transfer_control")
.blocklist_type("timeval")
.blocklist_type("libusb_.*")
.blocklist_type("__.*")
.raw_line("pub type timeval = libc::timeval;")
// The following use too much macros to parse correctly in libftdi 1.5
// Inject manually, they're ignored in callbacks
.raw_line("#[deprecated] pub const SIO_RESET_PURGE_RX: u32 = 1;")
.raw_line("#[deprecated] pub const SIO_RESET_PURGE_TX: u32 = 2;")
}
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings_builder()
.generate()
.expect("Unable to generate bindings")
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
println!("cargo:rerun-if-env-changed=LIBFTDI1_SYS_DEVEL");
if env::var("LIBFTDI1_SYS_DEVEL").is_ok() {
bindings_builder()
.layout_tests(false)
.generate()
.expect("Unable to generated bindings without tests")
.write_to_file("src/pregenerated.rs.updated")
.expect("Couldn't update pregenerated bindings!");
}
} else {
// Anything not depending on bindgen feature goes here.
}
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "vendored")] {
fn build_source() {
let source = PathBuf::from(env::var("DEP_FTDI1_SOURCE_SOURCE_DIR")
.expect("no source found"));
let include_paths = link_and_get_include_paths();
let mut libftdi = cc::Build::new();
for path in include_paths {
libftdi.include(path);
}
libftdi
.files(&[source.join("ftdi.c"), source.join("ftdi_stream.c")])
.include(source)
.compile("ftdi");
}
fn header_path() -> String {
let source = PathBuf::from(env::var("DEP_FTDI1_SOURCE_SOURCE_DIR")
.expect("no source found"));
source.join("ftdi.h").to_str().unwrap().to_owned()
}
} else {
fn build_source() {}
fn header_path() -> String {
"wrapper.h".into()
}
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "libusb1-sys")] {
fn link_and_get_include_paths() -> Vec<PathBuf> {
vec![
PathBuf::from(env::var("DEP_USB_1.0_INCLUDE")
.expect("libusb is required for libftdi"))
]
}
} else if #[cfg(all(windows, target_env="msvc"))] {
fn link_and_get_include_paths() -> Vec<PathBuf> {
let libusb = vcpkg::find_package("libusb-1.0")
.expect("libusb is required for libftdi");
libusb.include_paths
}
} else {
fn link_and_get_include_paths() -> Vec<PathBuf> {
let libusb = pkg_config::probe_library("libusb-1.0")
.expect("libusb is required for libftdi");
libusb.include_paths
}
}
}