Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true

[*.{yml,sh}]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "etc/hidapi"]
path = etc/hidapi
url = https://github.com/paritytech/hidapi.git
[submodule "etc/eudev"]
path = etc/eudev
url = https://github.com/gentoo/eudev.git
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ documentation = "https://beta.docs.rs/hidapi"
libc = "0.2.15"

[build-dependencies]
autotools = "0.2"
cc = "1.0"
147 changes: 124 additions & 23 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,133 @@
// along with hidapi_rust. If not, see <http://www.gnu.org/licenses/>.
// *************************************************************************

extern crate autotools;
Copy link
Author

@niklasad1 niklasad1 Feb 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suppose to be cross-platform and will use mingw-make on Windows!

extern crate cc;

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

#[cfg(windows)]
const GIT: &str = "git.exe";
#[cfg(not(windows))]
const GIT: &str = "git";

fn main() {
let target = env::var("TARGET").unwrap();

if target.contains("windows") {
cc::Build::new()
.file("etc/hidapi/windows/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");
println!("cargo:rustc-link-lib=setupapi");

} else if target.contains("darwin") {
cc::Build::new()
.file("etc/hidapi/mac/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");
println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");

} else if target.contains("linux") {
let mut config = cc::Build::new();
config.file("etc/hidapi/linux/hid.c").include("etc/hidapi/hidapi");
config.compile("libhidapi.a");
println!("cargo:rustc-link-lib=udev");
}
let target = env::var("TARGET").unwrap();

if target.contains("windows") {
cc::Build::new()
.file("etc/hidapi/windows/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");
println!("cargo:rustc-link-lib=setupapi");

} else if target.contains("darwin") {
cc::Build::new()
.file("etc/hidapi/mac/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");

println!("cargo:rustc-link-lib=framework=IOKit");
println!("cargo:rustc-link-lib=framework=CoreFoundation");

} else if target.contains("android") {
enable_android_hack();
env::set_var("CXX", "arm-linux-androideabi-clang++");
env::set_var("CC", "arm-linux-androideabi-gcc");
let libudev = autotools::Config::new("etc/eudev")
// -s: make symlinks
// -m: build if it applicable
// -i: install
// -v: verbose
// -f: consider all files obsolete
.reconf("-smivf")
.insource(true)
.host("arm-linux-androideabi")
.disable_shared()
.disable("introspection", None)
.disable("programs", None)
.disable("hwdb", None)
.cflag("-D LINE_MAX=2048")
.cflag("-D RLIMIT_NLIMITS=15")
.cflag("-D IPTOS_LOWCOST=2")
.cflag("-std=gnu99")
.build();

disable_android_hack();

let mut config = cc::Build::new();
config.file("etc/hidapi/linux/hid.c").include("etc/hidapi/hidapi");
config.compile("libhidapi.a");

println!("cargo:rustc-link-search=native={}/src/libudev/.libs", libudev.display());
println!("cargo:rustc-link-lib=static=udev");
} else if target.contains("linux") {

let libudev = autotools::Config::new("etc/eudev")
// -s: make symlinks
// -m: build if it applicable
// -i: install
// -v: verbose
// -f: consider all files obsolete
.reconf("-smivf")
.insource(true)
.disable_shared()
.build();

cc::Build::new()
.file("etc/hidapi/linux/hid.c")
.include("etc/hidapi/hidapi")
.compile("libhidapi.a");

println!("cargo:rustc-link-search=native={}/src/libudev/.libs", libudev.display());
println!("cargo:rustc-link-lib=static=udev");
}
}

fn enable_android_hack() {
let (start_dir, dst_dir) = get_dirs();
env::set_current_dir(dst_dir).expect("set current dir to \"etc/eudev\" failed");

let cmd = Command::new(GIT)
.args(&["checkout", "83d918449f22720d84a341a05e24b6d109e6d3ae"])
.status()
.expect("git checkout failed");
assert!(cmd.success(), format!("{}", cmd));

let cmd = Command::new(GIT)
.args(&["apply", "../libudev.patch"])
.status()
.expect("git apply etc/libudev.patch failed");
assert!(cmd.success(), format!("{}", cmd));

env::set_current_dir(start_dir).expect("set current dir to \"../..\" failed");
}

fn disable_android_hack() {
let (start_dir, dst_dir) = get_dirs();
env::set_current_dir(dst_dir).expect("set current dir to \"etc/eudev\" failed");

let cmd = Command::new(GIT)
.args(&["apply", "-R", "../libudev.patch"])
.status()
.expect("git revert patch failed");

assert!(cmd.success(), format!("{}", cmd));

let cmd = Command::new(GIT)
.args(&["checkout", "master"])
.status()
.expect("git checkout master failed");

assert!(cmd.success(), format!("{}", cmd));

env::set_current_dir(start_dir).expect("set current dir to \"../..\" failed");
}

fn get_dirs() -> (PathBuf, PathBuf) {
let start_dir = env::current_dir().expect("Current dir failed");
let dst_dir = start_dir.join("etc/eudev");
(start_dir.to_owned(), dst_dir.to_owned())
}
1 change: 1 addition & 0 deletions etc/eudev
Submodule eudev added at 34b203
Loading