lvgl-rs/lvgl-sys/build.rs

128 lines
4.5 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 = {
2021-03-06 19:25:09 +00:00
let conf_path = env::var(CONFIG_NAME).map(|raw_path| PathBuf::from(raw_path)).unwrap_or_else(|_| {
match std::env::var("DOCS_RS") {
Ok(_) => {
// We've detected that we are building for docs.rs
// so let's set the examples `lv_conf.h` file.
project_dir.join("..").join("examples").join("include")
}
Err(_) => panic!(
"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
if !conf_path.exists() {
panic!(format!(
2021-03-06 19:25:09 +00:00
"Directory {} referenced by {} needs to exist",
conf_path.to_string_lossy(),
2020-04-12 18:37:26 +00:00
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-09-21 18:01:31 +00:00
let mut cc_args = vec![
2020-04-11 17:39:20 +00:00
"-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-09-21 18:01:31 +00:00
"-fvisibility=default",
2020-04-11 17:39:20 +00:00
];
2020-05-30 14:45:58 +00:00
2020-09-21 18:01:31 +00:00
// Set correct target triple for bindgen when cross-compiling
let target = env::var("TARGET").expect("Cargo build scripts always have TARGET");
let host = env::var("HOST").expect("Cargo build scripts always have HOST");
if target != host {
cc_args.push("-target");
cc_args.push(target.as_str());
}
let mut additional_args = Vec::new();
if target.ends_with("emscripten") {
if let Ok(em_path) = env::var("EMSDK") {
additional_args.push("-I".to_string());
additional_args.push(format!("{}/upstream/emscripten/system/include/libc", em_path));
additional_args.push("-I".to_string());
additional_args.push(format!("{}/upstream/emscripten/system/lib/libc/musl/arch/emscripten", em_path));
additional_args.push("-I".to_string());
additional_args.push(format!("{}/upstream/emscripten/system/include/SDL", em_path));
}
}
2020-05-30 14:45:58 +00:00
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
2020-09-21 18:01:31 +00:00
let bindings = 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)
2020-09-21 18:01:31 +00:00
.clang_args(&additional_args)
.generate()
2020-09-21 18:01:31 +00:00
.expect("Unable to generate bindings");
bindings.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);
}
}
}