Skip to content

Commit

Permalink
feat: allow Rust build when using the shared library
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Aug 1, 2024
1 parent 3a4b6a4 commit 5df6e1e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
6 changes: 5 additions & 1 deletion bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[package]
name = "inputtino"
version = "0.1.0"
version = "2024.8.1"
edition = "2021"
license = "MIT"
rust-version = "1.72"
links = "libinputtino"
homepage = "https://github.com/games-on-whales/inputtino"
authors = ["ABeltramo"]
description = "Rust bindings for inputtino"

[lib]
name = "inputtino_rs"
Expand All @@ -13,6 +16,7 @@ path = "src/lib.rs"
[build-dependencies]
bindgen = "0.69.4"
cmake = "0.1"
pkg-config = "0.3.30"

[dev-dependencies]
input = "0.9.0"
Expand Down
82 changes: 43 additions & 39 deletions bindings/rust/build.rs
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!");
}

0 comments on commit 5df6e1e

Please sign in to comment.