-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
84 lines (70 loc) · 2.41 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
use std::{env, io};
use std::path::PathBuf;
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_path = PathBuf::from(env::var("OUT_DIR")?);
let rust_path = env::current_dir()?;
let native_path = rust_path.join("native");
env::set_current_dir(&native_path)?;
// Make sure the platform is supported.
if !cfg!(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
)) {
panic!("unsupported platform");
}
// Link any platform-specific dependencies.
if cfg!(target_os = "linux") {
pkg_config::Config::new()
.atleast_version("3.14")
.probe("gtk+-3.0")?;
pkg_config::Config::new()
.atleast_version("2.22")
.probe("webkit2gtk-4.0")?;
} else if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=dylib=ole32");
println!("cargo:rustc-link-lib=dylib=user32");
println!("cargo:rustc-link-lib=dylib=windowsapp");
} else if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=framework=Cocoa");
println!("cargo:rustc-link-lib=framework=WebKit");
}
// Build and link to the library.
if cfg!(target_os = "linux") {
let mut cc = cc::Build::new();
for flag in sh("pkg-config --cflags gtk+-3.0 webkit2gtk-4.0")?.split_whitespace() {
cc.flag(flag);
}
cc.file("gtk.c").compile("tether");
} else if cfg!(target_os = "windows") {
cc::Build::new()
.flag("/EHsc")
.flag("/std:c++17")
.file("winapi.cpp")
.compile("tether");
} else if cfg!(target_os = "macos") {
cc::Build::new()
.flag("-ObjC")
.flag("-fobjc-arc")
.file("cocoa.m")
.compile("tether");
}
// Generate the bindings to the library.
bindgen::Builder::default()
.header("tether.h")
.generate()
.map_err(|()| io::Error::new(io::ErrorKind::Other, "bindgen failed"))?
.write_to_file(out_path.join("bindings.rs"))?;
Ok(())
}
fn sh(script: &str) -> io::Result<String> {
Command::new("sh")
.args(&["-c", script])
.output()
.and_then(|o| if o.status.success() {
Ok(String::from_utf8_lossy(&o.stdout).into())
} else {
Err(io::Error::new(io::ErrorKind::Other, "script failed"))
})
}