-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow Rust build when using the shared library
- Loading branch information
Showing
2 changed files
with
48 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
extern crate bindgen; | ||
extern crate pkg_config; | ||
|
||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
use cmake::Config; | ||
|
||
fn main() { | ||
let build_static = false; | ||
// Options | ||
let build_c_bindings = env::var("INPUTTINO_BUILD_C_BINDINGS").unwrap_or("FALSE".to_string()) == "TRUE"; | ||
let build_static = env::var("INPUTTINO_BUILD_STATIC").unwrap_or("FALSE".to_string()) == "TRUE"; | ||
|
||
// This is the directory where the `c` library is located. | ||
let libdir_path = PathBuf::from("../../") | ||
// Canonicalize the path as `rustc-link-search` requires an absolute | ||
// path. | ||
.canonicalize() | ||
.expect("cannot canonicalize path"); | ||
// The bindgen::Builder is the main entry point | ||
// to bindgen, and lets you build up options for | ||
// the resulting bindings. | ||
let mut bindings = bindgen::Builder::default() | ||
.use_core() | ||
.default_enum_style(bindgen::EnumVariation::Rust { | ||
non_exhaustive: false, | ||
}) | ||
// Set the INPUTTINO_STATIC_DEFINE macro | ||
.clang_arg(if build_static { "-D INPUTTINO_STATIC_DEFINE=1" } else { "" }) | ||
// The input header we would like to generate bindings for. | ||
.header("wrapper.hpp"); | ||
|
||
// Compile the library using CMake | ||
let dst = Config::new(libdir_path) | ||
.target("libinputtino") | ||
.define("BUILD_SHARED_LIBS", if build_static { "OFF" } else { "ON" }) | ||
.define("LIBINPUTTINO_INSTALL", "ON") | ||
.define("BUILD_TESTING", "OFF") | ||
.define("BUILD_SERVER", "OFF") | ||
.define("BUILD_C_BINDINGS", "ON") | ||
.profile("Release") | ||
.define("CMAKE_CONFIGURATION_TYPES", "Release") | ||
.build(); | ||
if build_c_bindings { | ||
let libdir_path = PathBuf::from("../../") | ||
// Canonicalize the path as `rustc-link-search` requires an absolute | ||
// path. | ||
.canonicalize() | ||
.expect("cannot canonicalize path"); | ||
|
||
// Compile the library using CMake | ||
let dst = Config::new(libdir_path) | ||
.target("libinputtino") | ||
.define("BUILD_SHARED_LIBS", if build_static { "OFF" } else { "ON" }) | ||
.define("LIBINPUTTINO_INSTALL", "ON") | ||
.define("BUILD_TESTING", "OFF") | ||
.define("BUILD_SERVER", "OFF") | ||
.define("BUILD_C_BINDINGS", "ON") | ||
.profile("Release") | ||
.define("CMAKE_CONFIGURATION_TYPES", "Release") | ||
.build(); | ||
|
||
println!("cargo:rustc-link-search=native={}/lib", dst.display()); | ||
bindings = bindings.clang_arg(format!("-I{}/include/", dst.display())) | ||
} else { | ||
let lib = pkg_config::probe_library("libinputtino").unwrap(); | ||
bindings = bindings.clang_arg(format!("-I{}", lib.include_paths[0].display())); | ||
} | ||
|
||
// Dependencies | ||
if !build_static { | ||
println!("cargo:rustc-link-lib=evdev"); | ||
println!("cargo:rustc-link-lib=stdc++"); | ||
} | ||
|
||
//libinputtino | ||
println!("cargo:rustc-link-search=native={}/lib", dst.display()); | ||
println!("cargo:rustc-link-lib={}libinputtino", if build_static { "static=" } else { "" }); | ||
|
||
// The bindgen::Builder is the main entry point | ||
// to bindgen, and lets you build up options for | ||
// the resulting bindings. | ||
let bindings = bindgen::Builder::default() | ||
.use_core() | ||
.default_enum_style(bindgen::EnumVariation::Rust { | ||
non_exhaustive: false, | ||
}) | ||
// Add the include directory | ||
.clang_arg(format!("-I{}/include/", dst.display())) | ||
// Set the INPUTTINO_STATIC_DEFINE macro | ||
.clang_arg(if build_static {"-D INPUTTINO_STATIC_DEFINE=1"} else {""}) | ||
// The input header we would like to generate bindings for. | ||
.header("wrapper.hpp") | ||
// Finish the builder and generate the bindings. | ||
.generate() | ||
// Unwrap the Result and panic on failure. | ||
.expect("Unable to generate bindings"); | ||
let out = bindings.generate().expect("Unable to generate bindings"); | ||
|
||
// Write the bindings to the $OUT_DIR/bindings.rs file. | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs"); | ||
bindings | ||
out | ||
.write_to_file(out_path) | ||
.expect("Couldn't write bindings!"); | ||
} |