Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit d629f66

Browse files
committed
fix(cleanup + use autotools-rs)
1 parent 1df89dd commit d629f66

File tree

2 files changed

+72
-47
lines changed

2 files changed

+72
-47
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ documentation = "https://beta.docs.rs/hidapi"
1717
libc = "0.2.15"
1818

1919
[build-dependencies]
20+
autotools = "0.2"
2021
cc = "1.0"

build.rs

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,12 @@
1717
// along with hidapi_rust. If not, see <http://www.gnu.org/licenses/>.
1818
// *************************************************************************
1919

20+
extern crate autotools;
2021
extern crate cc;
2122

2223
use std::process::Command;
2324
use std::env;
24-
25-
// Copy-pasted from `https://github.com/paritytech/parity-ethereum/blob/cb03f380ab2bb37ff18771e6886c42098ad8b15a/docker/android/Dockerfile#L44-L62`
26-
const ANDROID_CFG: [&str; 5] = [
27-
"git checkout 83d918449f22720d84a341a05e24b6d109e6d3ae",
28-
"git apply ../libudev.patch",
29-
"./autogen.sh",
30-
"./configure --disable-introspection --disable-programs --disable-hwdb --host=arm-linux-androideabi --prefix=/opt/ndk-standalone/sysroot/usr/ --enable-shared=false",
31-
"make"
32-
];
33-
34-
const LINUX_CFG: [&str; 3] = [
35-
"./autogen.sh",
36-
"./configure",
37-
"make"
38-
];
25+
use std::path::Path;
3926

4027
fn main() {
4128
let target = env::var("TARGET").unwrap();
@@ -57,53 +44,90 @@ fn main() {
5744
println!("cargo:rustc-link-lib=framework=CoreFoundation");
5845

5946
} else if target.contains("android") {
60-
set_android_flags();
61-
execute_shell_cmd(&ANDROID_CFG);
47+
enable_android_hack();
48+
env::set_var("CXX", "arm-linux-androideabi-clang++");
49+
env::set_var("CC", "arm-linux-androideabi-gcc");
50+
let libudev = autotools::Config::new("etc/eudev")
51+
// -s: make symlinks
52+
// -m: build if it applicable
53+
// -i: install
54+
// -v: verbose
55+
// -f: consider all files obsolete
56+
.reconf("-smivf")
57+
.insource(true)
58+
.host("arm-linux-androideabi")
59+
.disable_shared()
60+
.disable("-introspection", None)
61+
.disable("-programs", None)
62+
.disable("-hwdb", None)
63+
.cflag("-D LINE_MAX=2048")
64+
.cflag("-D RLIMIT_NLIMITS=15")
65+
.cflag("-D IPTOS_LOWCOST=2")
66+
.cflag("-std=gnu99")
67+
.build();
68+
69+
disable_android_hack();
70+
6271
let mut config = cc::Build::new();
6372
config.file("etc/hidapi/linux/hid.c").include("etc/hidapi/hidapi");
6473
config.compile("libhidapi.a");
6574

66-
println!("cargo:rustc-link-search=native=./etc/eudev/src/libudev/.libs");
75+
println!("cargo:rustc-link-search=native={}/src/libudev/.libs", libudev.display());
6776
println!("cargo:rustc-link-lib=static=udev");
6877
} else if target.contains("linux") {
69-
execute_shell_cmd(&LINUX_CFG);
70-
let mut config = cc::Build::new();
71-
config.file("etc/hidapi/linux/hid.c").include("etc/hidapi/hidapi");
72-
config.compile("libhidapi.a");
7378

74-
println!("cargo:rustc-link-search=native=./etc/eudev/src/libudev/.libs");
79+
let libudev = autotools::Config::new("etc/eudev")
80+
// -s: make symlinks
81+
// -m: build if it applicable
82+
// -i: install
83+
// -v: verbose
84+
// -f: consider all files obsolete
85+
.reconf("-smivf")
86+
.insource(true)
87+
.disable_shared()
88+
.build();
89+
90+
cc::Build::new()
91+
.file("etc/hidapi/linux/hid.c")
92+
.include("etc/hidapi/hidapi")
93+
.compile("libhidapi.a");
94+
95+
println!("cargo:rustc-link-search=native={}/src/libudev/.libs", libudev.display());
7596
println!("cargo:rustc-link-lib=static=udev");
7697
}
7798
}
7899

79-
fn set_android_flags() {
80-
env::set_var("CFLAGS", "-D LINE_MAX=2048 -D RLIMIT_NLIMITS=15 -D IPTOS_LOWCOST=2 -std=gnu99");
81-
env::set_var("CXX", "arm-linux-androideabi-clang++");
82-
env::set_var("CC", "arm-linux-androideabi-gcc");
83-
}
100+
fn enable_android_hack() {
101+
env::set_current_dir(Path::new("etc/eudev"))
102+
.expect("Could not find the directory: \"etc\\eudev\"");
84103

85-
fn execute_shell_cmd(commands: &[&str]) {
86-
let start = std::env::current_dir().expect("Couldn't fetch current directory");
87-
let target = std::path::Path::new(&start).join("etc/eudev");
88-
env::set_current_dir(target).expect("Could not find the directory: \"etc\\eudev\"");
104+
Command::new("git")
105+
.args(&["checkout", "83d918449f22720d84a341a05e24b6d109e6d3ae"])
106+
.status()
107+
.expect("git checkout failed");
89108

90-
for full_cmd in commands.iter() {
91-
let ignore_error = full_cmd.starts_with("git") && full_cmd.contains("apply");
109+
Command::new("git")
110+
.args(&["apply", "../libudev.patch"])
111+
.status()
112+
.expect("git apply etc/libudev.patch failed");
92113

93-
let mut it = full_cmd.split_whitespace();
94-
let cmd = it.next().expect("A command should have at least one element; qed");
114+
env::set_current_dir(Path::new("../.."))
115+
.expect("Could not find the directory: \"etc\\eudev\"");
116+
}
95117

96-
let this_cmd = Command::new(cmd)
97-
.args(it)
98-
.status()
99-
.expect(&format!("Command {} failed", cmd));
118+
fn disable_android_hack() {
119+
env::set_current_dir(Path::new("etc/eudev"))
120+
.expect("Could not find the directory: \"etc\\eudev\"");
100121

101-
if !this_cmd.success() && !ignore_error {
102-
panic!("{}", this_cmd);
103-
} else if ignore_error {
104-
println!("IGNORED \"git apply error\" {}", this_cmd);
105-
}
106-
}
122+
Command::new("git")
123+
.args(&["apply", "-R", "../libudev.patch"])
124+
.status()
125+
.expect("git revert patch failed");
126+
Command::new("git")
127+
.args(&["checkout", "master"])
128+
.status()
129+
.expect("git checkout master failed");
107130

108-
env::set_current_dir(start).expect("Couldn't go back to start directory");
131+
env::set_current_dir(Path::new("../.."))
132+
.expect("Could not find the directory: \"etc\\eudev\"");
109133
}

0 commit comments

Comments
 (0)