Skip to content

Commit 5df6e1e

Browse files
committed
feat: allow Rust build when using the shared library
1 parent 3a4b6a4 commit 5df6e1e

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

bindings/rust/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[package]
22
name = "inputtino"
3-
version = "0.1.0"
3+
version = "2024.8.1"
44
edition = "2021"
55
license = "MIT"
66
rust-version = "1.72"
77
links = "libinputtino"
8+
homepage = "https://github.com/games-on-whales/inputtino"
9+
authors = ["ABeltramo"]
10+
description = "Rust bindings for inputtino"
811

912
[lib]
1013
name = "inputtino_rs"
@@ -13,6 +16,7 @@ path = "src/lib.rs"
1316
[build-dependencies]
1417
bindgen = "0.69.4"
1518
cmake = "0.1"
19+
pkg-config = "0.3.30"
1620

1721
[dev-dependencies]
1822
input = "0.9.0"

bindings/rust/build.rs

+43-39
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
11
extern crate bindgen;
2+
extern crate pkg_config;
23

34
use std::env;
45
use std::path::PathBuf;
56

67
use cmake::Config;
78

89
fn main() {
9-
let build_static = false;
10+
// Options
11+
let build_c_bindings = env::var("INPUTTINO_BUILD_C_BINDINGS").unwrap_or("FALSE".to_string()) == "TRUE";
12+
let build_static = env::var("INPUTTINO_BUILD_STATIC").unwrap_or("FALSE".to_string()) == "TRUE";
1013

11-
// This is the directory where the `c` library is located.
12-
let libdir_path = PathBuf::from("../../")
13-
// Canonicalize the path as `rustc-link-search` requires an absolute
14-
// path.
15-
.canonicalize()
16-
.expect("cannot canonicalize path");
14+
// The bindgen::Builder is the main entry point
15+
// to bindgen, and lets you build up options for
16+
// the resulting bindings.
17+
let mut bindings = bindgen::Builder::default()
18+
.use_core()
19+
.default_enum_style(bindgen::EnumVariation::Rust {
20+
non_exhaustive: false,
21+
})
22+
// Set the INPUTTINO_STATIC_DEFINE macro
23+
.clang_arg(if build_static { "-D INPUTTINO_STATIC_DEFINE=1" } else { "" })
24+
// The input header we would like to generate bindings for.
25+
.header("wrapper.hpp");
1726

18-
// Compile the library using CMake
19-
let dst = Config::new(libdir_path)
20-
.target("libinputtino")
21-
.define("BUILD_SHARED_LIBS", if build_static { "OFF" } else { "ON" })
22-
.define("LIBINPUTTINO_INSTALL", "ON")
23-
.define("BUILD_TESTING", "OFF")
24-
.define("BUILD_SERVER", "OFF")
25-
.define("BUILD_C_BINDINGS", "ON")
26-
.profile("Release")
27-
.define("CMAKE_CONFIGURATION_TYPES", "Release")
28-
.build();
27+
if build_c_bindings {
28+
let libdir_path = PathBuf::from("../../")
29+
// Canonicalize the path as `rustc-link-search` requires an absolute
30+
// path.
31+
.canonicalize()
32+
.expect("cannot canonicalize path");
33+
34+
// Compile the library using CMake
35+
let dst = Config::new(libdir_path)
36+
.target("libinputtino")
37+
.define("BUILD_SHARED_LIBS", if build_static { "OFF" } else { "ON" })
38+
.define("LIBINPUTTINO_INSTALL", "ON")
39+
.define("BUILD_TESTING", "OFF")
40+
.define("BUILD_SERVER", "OFF")
41+
.define("BUILD_C_BINDINGS", "ON")
42+
.profile("Release")
43+
.define("CMAKE_CONFIGURATION_TYPES", "Release")
44+
.build();
45+
46+
println!("cargo:rustc-link-search=native={}/lib", dst.display());
47+
bindings = bindings.clang_arg(format!("-I{}/include/", dst.display()))
48+
} else {
49+
let lib = pkg_config::probe_library("libinputtino").unwrap();
50+
bindings = bindings.clang_arg(format!("-I{}", lib.include_paths[0].display()));
51+
}
2952

3053
// Dependencies
3154
if !build_static {
3255
println!("cargo:rustc-link-lib=evdev");
3356
println!("cargo:rustc-link-lib=stdc++");
3457
}
3558

36-
//libinputtino
37-
println!("cargo:rustc-link-search=native={}/lib", dst.display());
3859
println!("cargo:rustc-link-lib={}libinputtino", if build_static { "static=" } else { "" });
3960

40-
// The bindgen::Builder is the main entry point
41-
// to bindgen, and lets you build up options for
42-
// the resulting bindings.
43-
let bindings = bindgen::Builder::default()
44-
.use_core()
45-
.default_enum_style(bindgen::EnumVariation::Rust {
46-
non_exhaustive: false,
47-
})
48-
// Add the include directory
49-
.clang_arg(format!("-I{}/include/", dst.display()))
50-
// Set the INPUTTINO_STATIC_DEFINE macro
51-
.clang_arg(if build_static {"-D INPUTTINO_STATIC_DEFINE=1"} else {""})
52-
// The input header we would like to generate bindings for.
53-
.header("wrapper.hpp")
54-
// Finish the builder and generate the bindings.
55-
.generate()
56-
// Unwrap the Result and panic on failure.
57-
.expect("Unable to generate bindings");
61+
let out = bindings.generate().expect("Unable to generate bindings");
5862

5963
// Write the bindings to the $OUT_DIR/bindings.rs file.
6064
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
61-
bindings
65+
out
6266
.write_to_file(out_path)
6367
.expect("Couldn't write bindings!");
6468
}

0 commit comments

Comments
 (0)