|
1 | 1 | extern crate bindgen;
|
| 2 | +extern crate pkg_config; |
2 | 3 |
|
3 | 4 | use std::env;
|
4 | 5 | use std::path::PathBuf;
|
5 | 6 |
|
6 | 7 | use cmake::Config;
|
7 | 8 |
|
8 | 9 | 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"; |
10 | 13 |
|
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"); |
17 | 26 |
|
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 | + } |
29 | 52 |
|
30 | 53 | // Dependencies
|
31 | 54 | if !build_static {
|
32 | 55 | println!("cargo:rustc-link-lib=evdev");
|
33 | 56 | println!("cargo:rustc-link-lib=stdc++");
|
34 | 57 | }
|
35 | 58 |
|
36 |
| - //libinputtino |
37 |
| - println!("cargo:rustc-link-search=native={}/lib", dst.display()); |
38 | 59 | println!("cargo:rustc-link-lib={}libinputtino", if build_static { "static=" } else { "" });
|
39 | 60 |
|
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"); |
58 | 62 |
|
59 | 63 | // Write the bindings to the $OUT_DIR/bindings.rs file.
|
60 | 64 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
|
61 |
| - bindings |
| 65 | + out |
62 | 66 | .write_to_file(out_path)
|
63 | 67 | .expect("Couldn't write bindings!");
|
64 | 68 | }
|
0 commit comments