lvgl-rs/lvgl-sys/build.rs

153 lines
5.1 KiB
Rust
Raw Permalink 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() {
2022-05-17 01:32:56 +00:00
let project_dir = canonicalize(PathBuf::from(env::var("CARGO_MANIFEST_DIR").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-07 17:28:19 +00:00
let conf_path = env::var(CONFIG_NAME)
2022-05-17 01:32:56 +00:00
.map(PathBuf::from)
2021-03-07 17:28:19 +00:00
.unwrap_or_else(|_| {
2021-03-06 19:25:09 +00:00
match std::env::var("DOCS_RS") {
Ok(_) => {
// We've detected that we are building for docs.rs
2021-03-06 23:34:19 +00:00
// so let's use the vendored `lv_conf.h` file.
vendor.join("include")
2021-03-06 19:25:09 +00:00
}
2022-05-18 01:35:05 +00:00
Err(_) => {
#[cfg(not(feature = "use-vendored-config"))]
panic!(
"The environment variable {} is required to be defined",
CONFIG_NAME
);
#[cfg(feature = "use-vendored-config")]
vendor.join("include")
}
2021-03-07 17:28:19 +00:00
}
});
2020-04-12 18:37:26 +00:00
if !conf_path.exists() {
panic!(
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
);
2020-04-12 18:37:26 +00:00
}
if !conf_path.is_dir() {
panic!("{} needs to be a directory", CONFIG_NAME);
2020-04-12 18:37:26 +00:00
}
if !conf_path.join("lv_conf.h").exists() {
panic!(
2021-03-06 23:34:19 +00:00
"Directory {} referenced by {} needs to contain a file called lv_conf.h",
conf_path.to_string_lossy(),
2020-04-12 18:37:26 +00:00
CONFIG_NAME
);
2020-04-12 18:37:26 +00:00
}
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());
2021-03-07 17:28:19 +00:00
additional_args.push(format!(
"{}/upstream/emscripten/system/include/libc",
em_path
));
2020-09-21 18:01:31 +00:00
additional_args.push("-I".to_string());
2021-03-07 17:28:19 +00:00
additional_args.push(format!(
"{}/upstream/emscripten/system/lib/libc/musl/arch/emscripten",
em_path
));
2020-09-21 18:01:31 +00:00
additional_args.push("-I".to_string());
2021-03-07 17:28:19 +00:00
additional_args.push(format!(
"{}/upstream/emscripten/system/include/SDL",
em_path
));
2020-09-21 18:01:31 +00:00
}
}
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())
.generate_comments(false)
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");
2021-03-07 17:28:19 +00:00
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);
}
}
}
2022-05-17 01:32:56 +00:00
fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
let canonicalized = path.as_ref().canonicalize().unwrap();
let canonicalized = &*canonicalized.to_string_lossy();
PathBuf::from(canonicalized.strip_prefix(r"\\?\").unwrap_or(canonicalized))
}