forked from a1ien/libusb1-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
186 lines (165 loc) · 6.64 KB
/
Copy pathbuild.rs
File metadata and controls
186 lines (165 loc) · 6.64 KB
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
use std::env;
use std::io::Read;
use std::path::{Path, PathBuf};
use tar::Archive;
static VERSION: &'static str = "1.0.22";
fn link(name: &str, bundled: bool) {
use std::env::var;
let target = var("TARGET").unwrap();
let target: Vec<_> = target.split('-').collect();
if target.get(2) == Some(&"windows") {
println!("cargo:rustc-link-lib=dylib={}", name);
if bundled && target.get(3) == Some(&"gnu") {
let dir = var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}/{}", dir, target[0]);
}
}
}
pub fn link_framework(name: &str) {
println!("cargo:rustc-link-lib=framework={}", name);
}
#[cfg(target_env = "msvc")]
fn find_libusb_pkg() -> bool {
match vcpkg::probe_package("libusb-1.0") {
Ok(lib) => {
for lib_dir in &lib.link_paths {
println!("cargo:lib={}", lib_dir.to_str().unwrap());
}
for include_dir in &lib.include_paths {
println!("cargo:include={}", include_dir.to_str().unwrap());
}
true
}
Err(e) => {
println!("Can't find libusb pkg: {:?}", e);
false
}
}
}
#[cfg(not(target_env = "msvc"))]
fn find_libusb_pkg() -> bool {
match pkg_config::probe_library("libusb-1.0") {
Ok(lib) => {
for lib_dir in &lib.link_paths {
println!("cargo:lib={}", lib_dir.to_str().unwrap());
}
for include_dir in &lib.include_paths {
println!("cargo:include={}", include_dir.to_str().unwrap());
}
true
}
Err(e) => {
println!("Can't find libusb pkg: {:?}", e);
false
}
}
}
fn unpack<R: Read>(data: R, dst: &Path) -> std::io::Result<()> {
let mut archive = Archive::new(data);
let skip: PathBuf = "README".into();
for entry in archive.entries()? {
let mut entry = entry?;
if entry.path()?.file_name().unwrap() == skip {
continue;
}
entry.unpack_in(dst)?;
}
Ok(())
}
fn extract_source() -> PathBuf {
use libflate::gzip::Decoder;
use std::{fs, io::Cursor};
let basename = format!("libusb-{}", VERSION);
let filename = format!("libusb/{}.tar.gz", basename);
let mut source_dir = PathBuf::from(env::var("OUT_DIR").unwrap()).join("source");
let data = Cursor::new(fs::read(&filename).unwrap());
let gz_decoder = Decoder::new(data).unwrap();
unpack(gz_decoder, &source_dir).unwrap();
source_dir.push(basename);
source_dir
}
fn make_source() {
let libusb_source = extract_source();
std::fs::File::create(format!("{}/{}", libusb_source.display(), "config.h")).unwrap();
let mut base_config = cc::Build::new();
base_config.include(&libusb_source);
base_config.include(libusb_source.join("libusb"));
// When building libusb from source, allow use of its logging facilities to aid debugging.
// FIXME: This does not link correctly under MinGW due to a rustc bug, so only do it on MSVC
// Ref: https://github.com/rust-lang/rust/issues/47048
if cfg!(target_env = "msvc") {
base_config.define("ENABLE_LOGGING", Some("1"));
}
if cfg!(target_os = "macos") {
base_config.define("OS_DARWIN", Some("1"));
base_config.file(libusb_source.join("libusb/os/darwin_usb.c"));
link_framework("CoreFoundation");
link_framework("IOKit");
link("objc", false);
}
if cfg!(target_os = "linux") {
base_config.define("OS_LINUX", Some("1"));
base_config.define("HAVE_ASM_TYPES_H", Some("1"));
base_config.define("HAVE_LINUX_NETLINK_H", Some("1"));
base_config.define("HAVE_SYS_SOCKET_H", Some("1"));
base_config.define("USBI_TIMERFD_AVAILABLE", Some("1"));
base_config.file(libusb_source.join("libusb/os/linux_netlink.c"));
base_config.file(libusb_source.join("libusb/os/linux_usbfs.c"));
base_config.define("POLL_NFDS_TYPE", Some("nfds_t"));
base_config.define("_GNU_SOURCE", Some("1"));
}
if cfg!(unix) {
base_config.define("HAVE_DLFCN_H", Some("1"));
base_config.define("HAVE_GETTIMEOFDAY", Some("1"));
base_config.define("HAVE_INTTYPES_H", Some("1"));
base_config.define("HAVE_MEMORY_H", Some("1"));
base_config.define("HAVE_POLL_H", Some("1"));
base_config.define("HAVE_STDINT_H", Some("1"));
base_config.define("HAVE_STDLIB_H", Some("1"));
base_config.define("HAVE_STRINGS_H", Some("1"));
base_config.define("HAVE_STRING_H", Some("1"));
base_config.define("HAVE_STRUCT_TIMESPEC", Some("1"));
base_config.define("HAVE_SYS_STAT_H", Some("1"));
base_config.define("HAVE_SYS_TIME_H", Some("1"));
base_config.define("HAVE_SYS_TYPES_H", Some("1"));
base_config.define("HAVE_UNISTD_H", Some("1"));
base_config.define("POLL_NFDS_TYPE", Some("nfds_t"));
base_config.define("STDC_HEADERS", Some("1"));
base_config.define("THREADS_POSIX", Some("1"));
base_config.define(
"DEFAULT_VISIBILITY",
Some("__attribute__((visibility(\"default\")))"),
);
base_config.file(libusb_source.join("libusb/os/poll_posix.c"));
base_config.file(libusb_source.join("libusb/os/threads_posix.c"));
}
if cfg!(windows) {
#[cfg(target_env = "msvc")]
base_config.define("_TIMESPEC_DEFINED", Some("1"));
base_config.warnings(false);
base_config.define("OS_WINDOWS", Some("1"));
base_config.file(libusb_source.join("libusb/os/poll_windows.c"));
base_config.file(libusb_source.join("libusb/os/threads_windows.c"));
base_config.file(libusb_source.join("libusb/os/windows_nt_common.c"));
base_config.file(libusb_source.join("libusb/os/windows_usbdk.c"));
base_config.file(libusb_source.join("libusb/os/windows_winusb.c"));
base_config.define("DEFAULT_VISIBILITY", Some(""));
base_config.define("POLL_NFDS_TYPE", Some("unsigned int"));
base_config.define("HAVE_SIGNAL_H", Some("1"));
base_config.define("HAVE_SYS_TYPES_H", Some("1"));
link("user32", false);
}
base_config.file(libusb_source.join("libusb/core.c"));
base_config.file(libusb_source.join("libusb/descriptor.c"));
base_config.file(libusb_source.join("libusb/hotplug.c"));
base_config.file(libusb_source.join("libusb/io.c"));
base_config.file(libusb_source.join("libusb/strerror.c"));
base_config.file(libusb_source.join("libusb/sync.c"));
base_config.compile("libusb.a");
}
fn main() {
if !find_libusb_pkg() {
extract_source();
make_source();
}
}