2019-09-19 22:02:14 -04:00
|
|
|
use cmake::Config;
|
2019-09-19 23:17:31 -04:00
|
|
|
use dunce::canonicalize;
|
2019-09-19 22:02:14 -04:00
|
|
|
use std::env;
|
2019-09-19 23:06:37 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-09-19 22:02:14 -04:00
|
|
|
|
|
|
|
pub enum LinkType {
|
|
|
|
Dynamic,
|
2019-09-19 23:06:37 -04:00
|
|
|
Static,
|
2019-09-19 22:02:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LinkType {
|
|
|
|
fn detect() -> LinkType {
|
|
|
|
if cfg!(feature = "static") {
|
|
|
|
LinkType::Static
|
|
|
|
} else {
|
|
|
|
LinkType::Dynamic
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_lib(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
LinkType::Dynamic => "dylib=",
|
2019-09-19 23:06:37 -04:00
|
|
|
LinkType::Static => "static=",
|
2019-09-19 22:02:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn target_name(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
LinkType::Dynamic => "aeron_driver",
|
2019-09-19 23:06:37 -04:00
|
|
|
LinkType::Static => "aeron_driver_static",
|
2019-09-19 22:02:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
println!("cargo:rerun-if-changed=bindings.h");
|
|
|
|
|
|
|
|
let aeron_path = canonicalize(Path::new("./aeron")).unwrap();
|
|
|
|
let header_path = aeron_path.join("aeron-driver/src/main/c");
|
|
|
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
let link_type = LinkType::detect();
|
2019-09-19 23:06:37 -04:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-lib={}{}",
|
|
|
|
link_type.link_lib(),
|
|
|
|
link_type.target_name()
|
|
|
|
);
|
2019-09-21 18:06:48 -04:00
|
|
|
|
2020-03-07 19:38:28 -05:00
|
|
|
if let LinkType::Static = link_type {
|
|
|
|
// On Windows, there are some extra libraries needed for static link
|
|
|
|
// that aren't included by Aeron.
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
println!("cargo:rustc-link-lib=shell32");
|
|
|
|
println!("cargo:rustc-link-lib=iphlpapi");
|
2019-09-21 18:23:51 -04:00
|
|
|
}
|
2020-03-07 19:38:28 -05:00
|
|
|
}
|
2019-09-21 18:06:48 -04:00
|
|
|
|
2020-01-12 17:56:12 -05:00
|
|
|
let cmake_output = Config::new(&aeron_path)
|
|
|
|
.build_target(link_type.target_name())
|
|
|
|
.build();
|
2019-09-20 00:14:40 -04:00
|
|
|
|
|
|
|
// Trying to figure out the final path is a bit weird;
|
|
|
|
// For Linux/OSX, it's just build/lib
|
2019-09-21 14:02:11 -04:00
|
|
|
// For Windows, the .lib file is in build/lib/{profile}, but the DLL
|
|
|
|
// is shipped in build/binaries/{profile}
|
|
|
|
let base_lib_dir = cmake_output.join("build");
|
2019-09-21 14:03:07 -04:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
base_lib_dir.join("lib").display()
|
|
|
|
);
|
2019-09-21 11:58:26 -04:00
|
|
|
// Because the `cmake_output` path is different for debug/release, we're not worried
|
|
|
|
// about accidentally linking in the wrong library
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
2019-09-21 14:02:11 -04:00
|
|
|
base_lib_dir.join("lib/Debug").display()
|
2019-09-21 11:58:26 -04:00
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
2019-09-21 14:02:11 -04:00
|
|
|
base_lib_dir.join("binaries/Debug").display()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
base_lib_dir.join("lib/Release").display()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
base_lib_dir.join("binaries/Release").display()
|
2019-09-21 11:58:26 -04:00
|
|
|
);
|
2019-09-19 22:02:14 -04:00
|
|
|
|
|
|
|
println!("cargo:include={}", header_path.display());
|
|
|
|
let bindings = bindgen::Builder::default()
|
2020-01-12 20:26:08 -05:00
|
|
|
.clang_arg(format!("-I{}", header_path.display()))
|
2019-09-19 22:02:14 -04:00
|
|
|
.header("bindings.h")
|
2019-09-22 15:30:17 -04:00
|
|
|
.whitelist_function("aeron_.*")
|
|
|
|
.whitelist_type("aeron_.*")
|
2019-09-25 00:19:04 -04:00
|
|
|
.whitelist_var("AERON_.*")
|
2019-09-24 19:41:14 -04:00
|
|
|
.constified_enum_module("aeron_.*_enum")
|
2020-01-12 18:37:15 -05:00
|
|
|
// Some padding structures use arrays > 120 elements,
|
|
|
|
// so we can't derive Debug implementations
|
|
|
|
.derive_debug(false)
|
2019-09-19 22:02:14 -04:00
|
|
|
.generate()
|
|
|
|
.expect("Unable to generate aeron_driver bindings");
|
|
|
|
|
|
|
|
bindings
|
|
|
|
.write_to_file(out_path.join("bindings.rs"))
|
|
|
|
.expect("Couldn't write bindings!");
|
2019-09-19 23:06:37 -04:00
|
|
|
}
|