summaryrefslogtreecommitdiff
path: root/hidapi-sys/build.rs
blob: 6ad919775fd14248c35926ccf11d0c0733210b15 (plain)
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
extern crate gcc;
extern crate pkg_config;

use std::env;
use std::io;
use std::path::PathBuf;
use std::process::Command;

fn main() {
	if env::var("CARGO_FEATURE_BUILD").is_err() {
		return;
	}

	fetch().unwrap();
	build().unwrap();

	println!("cargo:rustc-link-search=native={}", output().to_string_lossy());
}

fn output() -> PathBuf {
	PathBuf::from(env::var("OUT_DIR").unwrap())
}

fn source() -> PathBuf {
	output().join("hidapi")
}

fn fetch() -> io::Result<()> {
	Command::new("git")
		.current_dir(&output())
		.arg("clone")
		.arg("https://github.com/signal11/hidapi.git")
		.arg("hidapi")
		.status()?;

	Ok(())
}

#[cfg(target_os = "linux")]
fn build() -> io::Result<()> {
	let mut config = gcc::Config::new();

	config.file(source().join("libusb/hid.c"));
	config.include(source().join("hidapi"));

	for path in pkg_config::find_library("libusb-1.0").unwrap().include_paths {
		config.include(path.to_str().unwrap());
	}

	config.compile("libhidapi-libusb.a");

	Ok(())
}

#[cfg(target_os = "macos")]
fn build() -> io::Result<()> {
	let mut config = gcc::Config::new();

	config.file(source().join("libusb/hid.c"));
	config.include(source().join("hidapi"));

	for path in pkg_config::find_library("libusb-1.0").unwrap().include_paths {
		config.include(path.to_str().unwrap());
	}

	config.compile("libhidapi.a");

	Ok(())
}

#[cfg(target_os = "windows")]
fn build() -> io::Result<()> {
	let mut config = gcc::Config::new();

	config.file(source().join("windows/hid.c"));
	config.include(source().join("hidapi"));

	config.compile("libhidapi.a");

	Ok(())
}