From 4f3fdd8e49f8cd9d286fdc50994c1f6a4eac36be Mon Sep 17 00:00:00 2001 From: Thomas Bleeker Date: Sun, 12 Nov 2023 21:57:30 +0100 Subject: [PATCH] Add library and raw-bindings features to lvgl-sys. Correct CROSS_COMPILE target for bindgen --- lvgl-sys/Cargo.toml | 2 ++ lvgl-sys/build.rs | 10 +++++++++- lvgl-sys/src/lib.rs | 2 ++ lvgl/Cargo.toml | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lvgl-sys/Cargo.toml b/lvgl-sys/Cargo.toml index b42c74be..674f8693 100644 --- a/lvgl-sys/Cargo.toml +++ b/lvgl-sys/Cargo.toml @@ -32,6 +32,8 @@ cc = "1.0.79" bindgen = "0.65.1" [features] +library = [] +raw-bindings = [] use-vendored-config = [] drivers = [] rust_timer = [] diff --git a/lvgl-sys/build.rs b/lvgl-sys/build.rs index 09ffee21..268b5680 100644 --- a/lvgl-sys/build.rs +++ b/lvgl-sys/build.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "library")] use cc::Build; #[cfg(feature = "drivers")] use std::collections::HashSet; @@ -41,7 +42,9 @@ fn main() { font_extra_src: font_extra_src.as_ref().map(PathBuf::as_path), }; + #[cfg(feature = "library")] compile_library(&conf); + generate_bindings(&conf); } @@ -67,6 +70,7 @@ struct BuildConf<'a> { font_extra_src: Option<&'a Path>, } +#[cfg(feature = "library")] fn compile_library(conf: &BuildConf) { let vendor = conf.vendor; @@ -134,7 +138,10 @@ fn generate_bindings(conf: &BuildConf) { ]; // Set correct target triple for bindgen when cross-compiling - let target = env::var("TARGET").expect("Cargo build scripts always have TARGET"); + let target = env::var("CROSS_COMPILE").map_or_else( + |_| env::var("TARGET").expect("Cargo build scripts always have TARGET"), + |c| c.trim_end_matches('-').to_owned(), + ); let host = env::var("HOST").expect("Cargo build scripts always have HOST"); if target != host { cc_args.push("-target"); @@ -285,6 +292,7 @@ fn add_font_headers(bindings: bindgen::Builder, dir: Option<&Path>) -> bindgen:: } } +#[cfg(feature = "library")] fn add_c_files(build: &mut cc::Build, path: impl AsRef) { for e in path.as_ref().read_dir().unwrap() { let e = e.unwrap(); diff --git a/lvgl-sys/src/lib.rs b/lvgl-sys/src/lib.rs index fd35f222..cbce70b0 100644 --- a/lvgl-sys/src/lib.rs +++ b/lvgl-sys/src/lib.rs @@ -7,10 +7,12 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +#[cfg(feature = "raw-bindings")] pub fn _bindgen_raw_src() -> &'static str { include_str!(concat!(env!("OUT_DIR"), "/bindings.rs")) } +#[cfg(feature = "library")] mod string_impl; #[cfg(test)] diff --git a/lvgl/Cargo.toml b/lvgl/Cargo.toml index 15069d1c..58bed958 100644 --- a/lvgl/Cargo.toml +++ b/lvgl/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["littlevgl", "lvgl", "graphical_interfaces"] build = "build.rs" [dependencies] -lvgl-sys = { version = "0.6.2", path = "../lvgl-sys" } +lvgl-sys = { version = "0.6.2", path = "../lvgl-sys", features = ["library"]} cty = "0.2.2" embedded-graphics = { version = "0.8.0", optional = true } cstr_core = { version = "0.2.6", default-features = false, features = ["alloc"] } @@ -77,7 +77,7 @@ unsafe_no_autoinit = [] quote = "1.0.23" proc-macro2 = "1.0.51" lvgl-codegen = { version = "0.6.2", path = "../lvgl-codegen" } -lvgl-sys = { version = "0.6.2", path = "../lvgl-sys" } +lvgl-sys = { version = "0.6.2", path = "../lvgl-sys", features = ["raw-bindings"]} [dev-dependencies] embedded-graphics-simulator = "0.5.0"