From 43f2a2d4c404bd4253baa8e9dc21008cfc6b8f4b Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 12 Jun 2020 13:23:16 +0200 Subject: [PATCH] Use lvgl-codegen --- .gitignore | 1 + lvgl-codegen/Cargo.toml | 5 ---- lvgl-codegen/src/main.rs | 16 ++++++++++- lvgl/Cargo.toml | 2 +- lvgl/build.rs | 58 +++++++++++++--------------------------- lvgl/src/widgets/mod.rs | 3 ++- 6 files changed, 37 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index e629e17..03e3fc8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ examples/demo/target/ lvgl-sys/target/ lvgl/target/ lvgl-sys/src/bindings.rs +lvgl/src/widgets/generated.rs diff --git a/lvgl-codegen/Cargo.toml b/lvgl-codegen/Cargo.toml index 2dc7cad..20d4121 100644 --- a/lvgl-codegen/Cargo.toml +++ b/lvgl-codegen/Cargo.toml @@ -8,11 +8,6 @@ license = "MIT" publish = false build = "build.rs" -[lib] -name = "lvgl_codegen" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] regex = "1.3.9" quote = "1.0.7" diff --git a/lvgl-codegen/src/main.rs b/lvgl-codegen/src/main.rs index 55754bc..156d069 100644 --- a/lvgl-codegen/src/main.rs +++ b/lvgl-codegen/src/main.rs @@ -1,8 +1,16 @@ use lvgl_codegen::{CodeGen, Rusty}; use proc_macro2::TokenStream; use quote::quote; +use std::fs::File; +use std::io::prelude::*; +use std::path::Path; fn main() { + let rs = Path::new(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../lvgl/src/widgets/generated.rs" + )); + let codegen = CodeGen::new().unwrap(); let widgets_impl: Vec = codegen @@ -15,5 +23,11 @@ fn main() { #(#widgets_impl)* }; - println!("{}", code.to_string()); + let mut file = File::create(rs).unwrap(); + writeln!( + file, + "/* automatically generated by lvgl-codegen */\n{}", + code + ) + .unwrap(); } diff --git a/lvgl/Cargo.toml b/lvgl/Cargo.toml index 27acea9..1302afe 100644 --- a/lvgl/Cargo.toml +++ b/lvgl/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT" readme = "../README.md" categories = ["api-bindings", "embedded", "gui", "no-std"] keywords = ["littlevgl", "lvgl", "graphical_interfaces"] +include = ["Cargo.toml", "src/**/*", "src/widgets/generated.rs"] [dependencies] lvgl-sys = { path = "../lvgl-sys", version = "0.2.0" } @@ -18,6 +19,5 @@ cstr_core = { version = "0.2.0", default-features = false, features = ["alloc"] bitflags = "1.2.1" [build-dependencies] -lvgl-codegen = { path = "../lvgl-codegen" } quote = "1.0.7" proc-macro2 = "1.0.18" diff --git a/lvgl/build.rs b/lvgl/build.rs index 01ac3d8..cb054ae 100644 --- a/lvgl/build.rs +++ b/lvgl/build.rs @@ -1,48 +1,26 @@ use std::ffi::OsStr; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::Command; use std::{env, fs, path}; fn main() { - let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) - .canonicalize() - .unwrap(); + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let widgets_rs_path = manifest_dir.join("src/widgets/generated.rs"); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let gen_code = out_path.join("bindings.rs"); - - let code = invoke_command("../target/debug/lvgl-codegen", &[""], &project_dir); - fs::write(&gen_code, code).unwrap(); - - // // Format generated code - // let _ = invoke_command( - // "cargo", - // &["fmt", "-p", gen_code.to_str().unwrap()], - // &project_dir, - // ); -} - -fn invoke_command(command: C, args: I, cur_dir: D) -> String -where - C: AsRef, - I: IntoIterator, - S: AsRef, - D: AsRef, -{ - Command::new(command) - .current_dir(cur_dir) - .args(args) - .output() - .ok() - .and_then(|output| { - if output.status.success() { - Some(String::from_utf8_lossy(&output.stdout).trim().to_string()) - } else { + if !widgets_rs_path.exists() { + println!("Generating `src/widgets/generated.rs`"); + let status = Command::new(manifest_dir.join("../target/debug/lvgl-codegen")) + .spawn() + .unwrap_or_else(|_| { panic!( - "{}", - String::from_utf8_lossy(&output.stderr).trim().to_string() - ); - } - }) - .unwrap() + "Code generation failed because no codegen executable was found. \ + Please run `cargo build --package lvgl-codegen` and then try again.", + ) + }) + .wait() + .unwrap(); + if !status.success() { + panic!("Code generation failed"); + } + } } diff --git a/lvgl/src/widgets/mod.rs b/lvgl/src/widgets/mod.rs index f28c06e..9abd935 100644 --- a/lvgl/src/widgets/mod.rs +++ b/lvgl/src/widgets/mod.rs @@ -3,8 +3,9 @@ mod bar; mod gauge; mod label; -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +include!("generated.rs"); +use crate::Widget; pub use arc::*; pub use bar::*; pub use gauge::*;