lvgl-rs/lvgl-sys/build.rs

98 lines
3 KiB
Rust
Raw Normal View History

2020-04-11 17:39:20 +00:00
use cc::Build;
use std::{env, path::Path, path::PathBuf};
2020-04-10 17:12:10 +00:00
2020-04-12 18:37:26 +00:00
static CONFIG_NAME: &str = "DEP_LV_CONFIG_PATH";
2020-04-10 17:12:10 +00:00
fn main() {
let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.canonicalize()
.unwrap();
2020-04-21 08:52:21 +00:00
let shims_dir = project_dir.join("shims");
2020-04-12 11:41:36 +00:00
let vendor = project_dir.join("vendor");
let vendor_src = vendor.join("lvgl").join("src");
2020-04-10 17:12:10 +00:00
2020-04-12 18:37:26 +00:00
let lv_config_dir = {
2020-06-02 17:59:41 +00:00
let raw_path = env::var(CONFIG_NAME).unwrap_or_else(|_| {
panic!(
2020-04-12 18:37:26 +00:00
"The environment variable {} is required to be defined",
CONFIG_NAME
2020-06-02 17:59:41 +00:00
);
});
2020-04-12 18:37:26 +00:00
let conf_path = PathBuf::from(raw_path);
2020-04-12 18:37:26 +00:00
if !conf_path.exists() {
panic!(format!(
"Directory referenced by {} needs to exist",
CONFIG_NAME
));
}
if !conf_path.is_dir() {
panic!(format!("{} needs to be a directory", CONFIG_NAME));
}
if !conf_path.join("lv_conf.h").exists() {
panic!(format!(
"Directory referenced by {} needs to contain a file called lv_conf.h",
CONFIG_NAME
));
}
println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_conf.h").to_str().unwrap()
);
conf_path
};
2020-04-10 17:12:10 +00:00
2020-04-12 18:37:26 +00:00
let mut cfg = Build::new();
2020-04-12 11:41:36 +00:00
add_c_files(&mut cfg, vendor_src.join("lv_core"));
add_c_files(&mut cfg, vendor_src.join("lv_draw"));
add_c_files(&mut cfg, vendor_src.join("lv_font"));
2020-06-04 00:00:31 +00:00
add_c_files(&mut cfg, vendor_src.join("lv_gpu"));
2020-04-12 11:41:36 +00:00
add_c_files(&mut cfg, vendor_src.join("lv_hal"));
add_c_files(&mut cfg, vendor_src.join("lv_misc"));
2020-04-18 17:20:38 +00:00
add_c_files(&mut cfg, vendor_src.join("lv_themes"));
2020-06-04 00:00:31 +00:00
add_c_files(&mut cfg, vendor_src.join("lv_widgets"));
2020-04-13 18:52:06 +00:00
add_c_files(&mut cfg, &lv_config_dir);
2020-04-21 08:52:21 +00:00
add_c_files(&mut cfg, &shims_dir);
2020-04-10 18:34:07 +00:00
cfg.define("LV_CONF_INCLUDE_SIMPLE", Some("1"))
2020-04-12 11:41:36 +00:00
.include(&vendor_src)
.include(&vendor)
.warnings(false)
2020-04-12 18:37:26 +00:00
.include(&lv_config_dir)
.compile("lvgl");
2020-04-11 17:39:20 +00:00
let cc_args = [
"-DLV_CONF_INCLUDE_SIMPLE=1",
"-I",
2020-04-12 18:37:26 +00:00
lv_config_dir.to_str().unwrap(),
2020-04-12 11:41:36 +00:00
"-I",
2020-04-12 18:37:26 +00:00
vendor.to_str().unwrap(),
2020-04-11 17:39:20 +00:00
];
2020-05-30 14:45:58 +00:00
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindgen::Builder::default()
2020-04-21 08:52:21 +00:00
.header(shims_dir.join("lvgl_sys.h").to_str().unwrap())
2020-04-11 18:35:33 +00:00
.layout_tests(false)
.use_core()
2020-05-30 05:40:13 +00:00
.rustfmt_bindings(true)
2020-04-11 18:35:33 +00:00
.ctypes_prefix("cty")
.clang_args(&cc_args)
.generate()
.expect("Unable to generate bindings")
2020-05-30 14:45:58 +00:00
.write_to_file(out_path.join("bindings.rs"))
.expect("Can't write bindings!");
2020-04-10 17:12:10 +00:00
}
2020-04-10 18:34:07 +00:00
fn add_c_files(build: &mut cc::Build, path: impl AsRef<Path>) {
for e in path.as_ref().read_dir().unwrap() {
let e = e.unwrap();
let path = e.path();
if e.file_type().unwrap().is_dir() {
// skip dirs for now
} else if path.extension().and_then(|s| s.to_str()) == Some("c") {
build.file(&path);
}
}
}