feat: refactor depcheck; depcheck for includes

This commit is contained in:
Gabriele Musco 2023-06-03 18:28:46 +02:00
commit 86ab0cdf31

View file

@ -1,13 +1,15 @@
use std::{env, path::Path}; use std::{env, path::Path};
pub enum DepType { pub enum DepType {
SharedObject(String), SharedObject,
Executable(String), Executable,
Include,
} }
pub struct Dependency { pub struct Dependency {
pub name: String, pub name: String,
pub dep_type: DepType, pub dep_type: DepType,
pub filename: String,
} }
fn shared_obj_paths() -> Vec<String> { fn shared_obj_paths() -> Vec<String> {
@ -23,37 +25,27 @@ fn shared_obj_paths() -> Vec<String> {
] ]
} }
macro_rules! executable_paths { fn include_paths() -> Vec<String> {
() => { vec!["/usr/include".to_string()]
env::var("PATH")
.expect("PATH environment variable not defined")
.split(':')
};
} }
pub fn check_dependency(dep: Dependency) -> bool { pub fn check_dependency(dep: Dependency) -> bool {
match dep.dep_type { for dir in match dep.dep_type {
DepType::SharedObject(filename) => { DepType::SharedObject => shared_obj_paths(),
for lib_path in shared_obj_paths() { DepType::Executable => env::var("PATH")
let path_s = &format!("{lp}/{fn}", lp=lib_path, fn=filename); .expect("PATH environment variable not defined")
let path = Path::new(&path_s); .split(':')
if path.is_file() { .map(str::to_string)
return true; .collect(),
} DepType::Include => include_paths(),
} } {
return false; let path_s = &format!("{dir}/{fname}", dir = dir, fname = dep.filename);
let path = Path::new(&path_s);
if path.is_file() {
return true;
} }
DepType::Executable(exec) => {
for path in executable_paths!() {
let path_s = &format!("{p}/{ex}", p=path, ex=exec);
let path = Path::new(path_s);
if path.is_file() {
return true;
}
}
return false;
},
} }
return false;
} }
#[cfg(test)] #[cfg(test)]
@ -64,7 +56,8 @@ mod tests {
fn can_find_libc() { fn can_find_libc() {
let libc_dep = Dependency { let libc_dep = Dependency {
name: "libc".into(), name: "libc".into(),
dep_type: DepType::SharedObject("libc.so".into()), dep_type: DepType::SharedObject,
filename: "libc.so".into(),
}; };
assert!(check_dependency(libc_dep)); assert!(check_dependency(libc_dep));
@ -74,7 +67,8 @@ mod tests {
fn can_find_ls() { fn can_find_ls() {
let libc_dep = Dependency { let libc_dep = Dependency {
name: "ls".into(), name: "ls".into(),
dep_type: DepType::Executable("ls".into()), dep_type: DepType::Executable,
filename: ("ls".into()),
}; };
assert!(check_dependency(libc_dep)); assert!(check_dependency(libc_dep));
@ -84,7 +78,8 @@ mod tests {
fn cannot_find_fake_lib() { fn cannot_find_fake_lib() {
let fake_dep = Dependency { let fake_dep = Dependency {
name: "fakedep".into(), name: "fakedep".into(),
dep_type: DepType::SharedObject("fakedep.so".into()), dep_type: DepType::SharedObject,
filename: "fakedep.so".into(),
}; };
assert!(!check_dependency(fake_dep)); assert!(!check_dependency(fake_dep));