Skip to content

Commit

Permalink
Refactor build script: separate library and indings in separate funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
madwizard-thomas committed Nov 13, 2023
1 parent 71f3518 commit aa88305
Showing 1 changed file with 118 additions and 97 deletions.
215 changes: 118 additions & 97 deletions lvgl-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,52 @@ fn main() {
let project_dir = canonicalize(PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()));
let shims_dir = project_dir.join("shims");
let vendor = project_dir.join("vendor");
let lvgl_src = vendor.join("lvgl").join("src");
#[cfg(feature = "rust_timer")]
let timer_shim = vendor.join("include").join("timer");
println!("cargo:rerun-if-env-changed={}", CONFIG_NAME);
let lv_config_dir = get_conf_path(&vendor);
let font_extra_src: Option<PathBuf> = get_font_extra_dir();
if let Some(p) = &font_extra_src {
println!("cargo:rerun-if-changed={}", p.to_str().unwrap())
}

let conf = BuildConf {
lv_config_dir: lv_config_dir.as_path(),
vendor: vendor.as_path(),
shims_dir: &shims_dir,
font_extra_src: font_extra_src.as_ref().map(PathBuf::as_path),
};

let font_extra_src: Option<PathBuf>;
compile_library(&conf);
generate_bindings(&conf);
}

fn get_font_extra_dir() -> Option<PathBuf> {
if let Ok(v) = env::var("PWD") {
let current_dir = canonicalize(PathBuf::from(v));
font_extra_src = {
if let Ok(p) = env::var("LVGL_FONTS_DIR") {
Some(canonicalize(PathBuf::from(p)))
} else if current_dir.join("fonts").exists() {
Some(current_dir.join("fonts"))
} else {
None
}
};
if let Ok(p) = env::var("LVGL_FONTS_DIR") {
Some(canonicalize(PathBuf::from(p)))
} else if current_dir.join("fonts").exists() {
Some(current_dir.join("fonts"))
} else {
None
}
} else {
font_extra_src = None
None
}
}

struct BuildConf<'a> {
lv_config_dir: &'a Path,
vendor: &'a Path,
shims_dir: &'a Path,
font_extra_src: Option<&'a Path>,
}

fn compile_library(conf: &BuildConf) {
let vendor = conf.vendor;

let lvgl_src = vendor.join("lvgl").join("src");
#[cfg(feature = "rust_timer")]
let timer_shim = vendor.join("include").join("timer");

// Some basic defaults; SDL2 is the only driver enabled in the provided
// driver config by default
Expand All @@ -57,71 +84,6 @@ fn main() {

#[cfg(feature = "drivers")]
let drivers = vendor.join("lv_drivers");
println!("cargo:rerun-if-env-changed={}", CONFIG_NAME);
let lv_config_dir = {
let conf_path = env::var(CONFIG_NAME)
.map(PathBuf::from)
.unwrap_or_else(|_| {
match std::env::var("DOCS_RS") {
Ok(_) => {
// We've detected that we are building for docs.rs
// so let's use the vendored `lv_conf.h` file.
vendor.join("include")
}
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")
}
}
});

if !conf_path.exists() {
panic!(
"Directory {} referenced by {} needs to exist",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
if !conf_path.is_dir() {
panic!("{} needs to be a directory", CONFIG_NAME);
}
if !conf_path.join("lv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
#[cfg(feature = "drivers")]
if !conf_path.join("lv_drv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_drv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}

if let Some(p) = &font_extra_src {
println!("cargo:rerun-if-changed={}", p.to_str().unwrap())
}

println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_conf.h").to_str().unwrap()
);
#[cfg(feature = "drivers")]
println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_drv_conf.h").to_str().unwrap()
);
conf_path
};

#[cfg(feature = "drivers")]
{
Expand All @@ -130,20 +92,20 @@ fn main() {
}

let mut cfg = Build::new();
if let Some(p) = &font_extra_src {
if let Some(p) = conf.font_extra_src {
add_c_files(&mut cfg, p)
}
add_c_files(&mut cfg, &lvgl_src);
add_c_files(&mut cfg, &shims_dir);
add_c_files(&mut cfg, conf.shims_dir);
#[cfg(feature = "drivers")]
add_c_files(&mut cfg, &drivers);

cfg.define("LV_CONF_INCLUDE_SIMPLE", Some("1"))
.include(&lvgl_src)
.include(&vendor)
.warnings(false)
.include(&lv_config_dir);
if let Some(p) = &font_extra_src {
.include(conf.lv_config_dir);
if let Some(p) = conf.font_extra_src {
cfg.includes(p);
}
#[cfg(feature = "rust_timer")]
Expand All @@ -155,12 +117,19 @@ fn main() {

cfg.compile("lvgl");

#[cfg(feature = "drivers")]
link_extra.split(',').for_each(|a| {
println!("cargo:rustc-link-lib={a}");
//println!("cargo:rustc-link-search=")
});
}
fn generate_bindings(conf: &BuildConf) {
let mut cc_args = vec![
"-DLV_CONF_INCLUDE_SIMPLE=1",
"-I",
lv_config_dir.to_str().unwrap(),
conf.lv_config_dir.to_str().unwrap(),
"-I",
vendor.to_str().unwrap(),
conf.vendor.to_str().unwrap(),
"-fvisibility=default",
];

Expand Down Expand Up @@ -213,11 +182,11 @@ fn main() {

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings =
bindgen::Builder::default().header(shims_dir.join("lvgl_sys.h").to_str().unwrap());
let bindings = add_font_headers(bindings, &font_extra_src);
bindgen::Builder::default().header(conf.shims_dir.join("lvgl_sys.h").to_str().unwrap());
let bindings = add_font_headers(bindings, conf.font_extra_src);
#[cfg(feature = "drivers")]
let bindings = bindings
.header(shims_dir.join("lvgl_drv.h").to_str().unwrap())
.header(conf.shims_dir.join("lvgl_drv.h").to_str().unwrap())
.parse_callbacks(Box::new(ignored_macros));
//#[cfg(feature = "rust_timer")]
//let bindings = bindings.header(shims_dir.join("rs_timer.h").to_str().unwrap());
Expand All @@ -235,21 +204,73 @@ fn main() {
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Can't write bindings!");
}

fn get_conf_path(vendor: &PathBuf) -> PathBuf {
let conf_path = env::var(CONFIG_NAME)
.map(PathBuf::from)
.unwrap_or_else(|_| {
match std::env::var("DOCS_RS") {
Ok(_) => {
// We've detected that we are building for docs.rs
// so let's use the vendored `lv_conf.h` file.
vendor.join("include")
}
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")
}
}
});

if !conf_path.exists() {
panic!(
"Directory {} referenced by {} needs to exist",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
if !conf_path.is_dir() {
panic!("{} needs to be a directory", CONFIG_NAME);
}
if !conf_path.join("lv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
#[cfg(feature = "drivers")]
link_extra.split(',').for_each(|a| {
println!("cargo:rustc-link-lib={a}");
//println!("cargo:rustc-link-search=")
})
if !conf_path.join("lv_drv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_drv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}

println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_conf.h").to_str().unwrap()
);
#[cfg(feature = "drivers")]
println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_drv_conf.h").to_str().unwrap()
);
conf_path
}

fn add_font_headers(
bindings: bindgen::Builder,
dir: &Option<impl AsRef<Path>>,
) -> bindgen::Builder {
fn add_font_headers(bindings: bindgen::Builder, dir: Option<&Path>) -> bindgen::Builder {
if let Some(p) = dir {
let mut temp = bindings;
for e in p.as_ref().read_dir().unwrap() {
for e in p.read_dir().unwrap() {
let e = e.unwrap();
let path = e.path();
if !e.file_type().unwrap().is_dir()
Expand Down

0 comments on commit aa88305

Please sign in to comment.