forked from ruabmbua/hidapi-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
220 lines (196 loc) · 7.14 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// **************************************************************************
// Copyright (c) 2015 Roland Ruckerbauer All Rights Reserved.
//
// This file is part of hidapi_rust.
//
// hidapi_rust is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// hidapi_rust is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with hidapi_rust. If not, see <http://www.gnu.org/licenses/>.
// *************************************************************************
extern crate cc;
extern crate pkg_config;
use std::env;
fn main() {
let target = env::var("TARGET").unwrap();
println!("cargo::rustc-check-cfg=cfg(hidapi)");
println!("cargo::rustc-check-cfg=cfg(libusb)");
if target.contains("linux") {
compile_linux();
} else if target.contains("windows") {
compile_windows();
} else if target.contains("darwin") {
compile_macos();
} else if target.contains("freebsd") {
compile_freebsd();
} else if target.contains("openbsd") {
compile_openbsd();
} else if target.contains("illumos") {
compile_illumos();
} else {
panic!("Unsupported target os for hidapi-rs");
}
}
fn compile_linux() {
// First check the features enabled for the crate.
// Only one linux backend should be enabled at a time.
let avail_backends: [(&'static str, Box<dyn Fn()>); 5] = [
(
"LINUX_STATIC_HIDRAW",
Box::new(|| {
let mut config = cc::Build::new();
println!("cargo:rerun-if-changed=etc/hidapi/linux/hid.c");
config
.file("etc/hidapi/linux/hid.c")
.include("etc/hidapi/hidapi");
pkg_config::probe_library("libudev").expect("Unable to find libudev");
config.compile("libhidapi.a");
println!("cargo:rustc-cfg=hidapi");
}),
),
(
"LINUX_STATIC_LIBUSB",
Box::new(|| {
let mut config = cc::Build::new();
println!("cargo:rerun-if-changed=etc/hidapi/linux/hid.c");
config
.file("etc/hidapi/libusb/hid.c")
.include("etc/hidapi/hidapi");
let lib =
pkg_config::find_library("libusb-1.0").expect("Unable to find libusb-1.0");
for path in lib.include_paths {
config.include(
path.to_str()
.expect("Failed to convert include path to str"),
);
}
config.compile("libhidapi.a");
println!("cargo:rustc-cfg=libusb");
println!("cargo:rustc-cfg=hidapi");
}),
),
(
"LINUX_SHARED_HIDRAW",
Box::new(|| {
pkg_config::probe_library("hidapi-hidraw").expect("Unable to find hidapi-hidraw");
println!("cargo:rustc-cfg=hidapi");
}),
),
(
"LINUX_SHARED_LIBUSB",
Box::new(|| {
pkg_config::probe_library("libusb-1.0").expect("Unable to find libusb-1.0");
pkg_config::probe_library("hidapi-libusb").expect("Unable to find hidapi-libusb");
println!("cargo:rustc-cfg=libusb");
println!("cargo:rustc-cfg=hidapi");
}),
),
(
"LINUX_NATIVE",
Box::new(|| {
// The udev crate takes care of finding its library
}),
),
];
let mut backends = avail_backends
.iter()
.filter(|f| env::var(format!("CARGO_FEATURE_{}", f.0)).is_ok());
if backends.clone().count() != 1 {
panic!("Exactly one linux hidapi backend must be selected.");
}
// Build it!
(backends.next().unwrap().1)();
}
//#[cfg(all(feature = "shared-libusb", not(feature = "shared-hidraw")))]
//fn compile_linux() {
//
//}
//
//#[cfg(all(feature = "shared-hidraw"))]
//fn compile_linux() {
//
//}
fn compile_freebsd() {
pkg_config::probe_library("hidapi").expect("Unable to find hidapi");
println!("cargo:rustc-cfg=libusb");
println!("cargo:rustc-cfg=hidapi");
}
fn compile_openbsd() {
pkg_config::probe_library("hidapi-libusb").expect("Unable to find hidapi");
println!("cargo:rustc-cfg=libusb");
println!("cargo:rustc-cfg=hidapi");
}
fn compile_illumos() {
// First check the features enabled for the crate.
// Only one illumos backend should be enabled at a time.
let avail_backends: [(&'static str, Box<dyn Fn()>); 2] = [
(
"ILLUMOS_STATIC_LIBUSB",
Box::new(|| {
let mut config = cc::Build::new();
config
.file("etc/hidapi/libusb/hid.c")
.include("etc/hidapi/hidapi");
let lib =
pkg_config::find_library("libusb-1.0").expect("Unable to find libusb-1.0");
for path in lib.include_paths {
config.include(
path.to_str()
.expect("Failed to convert include path to str"),
);
}
config.compile("libhidapi.a");
}),
),
(
"ILLUMOS_SHARED_LIBUSB",
Box::new(|| {
pkg_config::probe_library("hidapi-libusb").expect("Unable to find hidapi-libusb");
}),
),
];
let mut backends = avail_backends
.iter()
.filter(|f| env::var(format!("CARGO_FEATURE_{}", f.0)).is_ok());
if backends.clone().count() != 1 {
panic!("Exactly one illumos hidapi backend must be selected.");
}
// Build it!
(backends.next().unwrap().1)();
println!("cargo:rustc-cfg=libusb");
println!("cargo:rustc-cfg=hidapi");
}
fn compile_windows() {
#[cfg(not(feature = "windows-native"))]
{
let linkage = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let mut cc = cc::Build::new();
cc.file("etc/hidapi/windows/hid.c")
.include("etc/hidapi/hidapi");
if linkage.contains("crt-static") {
// https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes
cc.static_crt(true);
}
cc.compile("libhidapi.a");
println!("cargo:rustc-link-lib=setupapi");
println!("cargo:rustc-cfg=hidapi");
}
}
fn compile_macos() {
cc::Build::new()
.file("etc/hidapi/mac/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");
println!("cargo:rustc-cfg=hidapi");
println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=AppKit")
}