Skip to content

Commit

Permalink
add filter_local_include_dirs for mesa & llvm-project (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjodinchr authored Dec 20, 2024
1 parent a00adb2 commit 21e67e5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/project/clspv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Project for Clspv {
self,
)?;

self.gen_deps = Vec::from_iter(package.get_gen_deps());
self.gen_deps = package.get_gen_deps();

Ok(package)
}
Expand Down
63 changes: 8 additions & 55 deletions src/project/llvm_project.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright 2024 ninja-to-soong authors
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::fs::File;

use crate::project::*;

const CMAKE_GENERATED: &str = "cmake_generated";
Expand Down Expand Up @@ -90,64 +87,20 @@ impl Project for LlvmProject {
];
gen_deps.extend(missing_gen_deps.iter().map(|dep| PathBuf::from(dep)));

let mut gen_deps_folders = HashSet::new();
for gen_dep in &gen_deps {
let folder = gen_dep.parent().unwrap();
if let Some((include_folder, _)) = split_path(folder, "include") {
gen_deps_folders.insert(include_folder);
} else {
gen_deps_folders.insert(PathBuf::from(folder));
}
}
for module in package.get_modules() {
module.update_prop("local_include_dirs", |prop| match prop {
SoongProp::VecStr(dirs) => SoongProp::VecStr(
dirs.into_iter()
.filter(|dir| {
if let Ok(strip) = Path::new(&dir).strip_prefix(CMAKE_GENERATED) {
if !gen_deps_folders.contains(strip) {
return false;
}
}
return true;
})
.collect(),
),
_ => prop,
});
}

let mut gen_deps_sorted = Vec::from_iter(gen_deps);
gen_deps_sorted.sort();
package.filter_local_include_dirs(CMAKE_GENERATED, &gen_deps);
gen_deps.sort();
write_file(
&ctx.test_path
.join(self.get_id().str())
.join("generated_deps.txt"),
&format!("{0:#?}", &gen_deps_sorted),
&format!("{0:#?}", &gen_deps),
)?;

if ctx.copy_to_aosp {
let cmake_generated_path = self.get_id().android_path(ctx).join(CMAKE_GENERATED);
if File::open(&cmake_generated_path).is_ok() {
if let Err(err) = std::fs::remove_dir_all(&cmake_generated_path) {
return error!("remove_dir_all failed: {err}");
}

print_verbose!("{cmake_generated_path:#?} removed");
}
for file in gen_deps_sorted {
let from = self.build_path.join(&file);
let to = cmake_generated_path.join(file);
let to_path = to.parent().unwrap();
if let Err(err) = std::fs::create_dir_all(to_path) {
return error!("create_dir_all({to_path:#?}) failed: {err}");
}
copy_file(&from, &to)?;
}
print_verbose!(
"Files copied from {0:#?} to {cmake_generated_path:#?}",
&self.build_path
);
copy_files(
&self.build_path,
&self.get_id().android_path(ctx).join(CMAKE_GENERATED),
gen_deps,
)?;
}

let cmake_generated_path = Path::new(CMAKE_GENERATED);
Expand Down
66 changes: 9 additions & 57 deletions src/project/mesa.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright 2024 ninja-to-soong authors
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::fs::File;

use super::*;

const MESON_GENERATED: &str = "meson_generated";
Expand Down Expand Up @@ -56,7 +53,6 @@ impl Project for Mesa {
} else {
mesa_test_path.clone()
};

if !ctx.skip_gen_ninja {
execute_cmd!(
"bash",
Expand All @@ -70,7 +66,6 @@ impl Project for Mesa {
]
)?;
}

if !ctx.skip_build {
execute_cmd!(
"meson",
Expand All @@ -79,7 +74,6 @@ impl Project for Mesa {
}

let targets = parse_build_ninja::<ninja_target::meson::MesonNinjaTarget>(&self.build_path)?;

let mut package = SoongPackage::new(
&self.src_path,
&self.ndk_path,
Expand All @@ -92,64 +86,22 @@ impl Project for Mesa {
let targets_to_generate = TARGETS.iter().map(|target| PathBuf::from(target)).collect();
package.generate(targets_to_generate, targets, self)?;

let gen_deps = package.get_gen_deps();
let mut gen_deps_folders = HashSet::new();
for gen_dep in &gen_deps {
let mut path = gen_dep.clone();
while let Some(parent) = path.parent() {
path = PathBuf::from(parent);
gen_deps_folders.insert(path.clone());
}
}
for module in package.get_modules() {
module.update_prop("local_include_dirs", |prop| match prop {
SoongProp::VecStr(dirs) => SoongProp::VecStr(
dirs.into_iter()
.filter(|dir| {
if let Ok(strip) = Path::new(&dir).strip_prefix(MESON_GENERATED) {
if !gen_deps_folders.contains(strip) {
return false;
}
}
return true;
})
.collect(),
),
_ => prop,
});
}
let mut gen_deps = package.get_gen_deps();

let mut gen_deps_sorted = Vec::from_iter(gen_deps);
gen_deps_sorted.sort();
package.filter_local_include_dirs(MESON_GENERATED, &gen_deps);
gen_deps.sort();
write_file(
&ctx.test_path
.join(self.get_id().str())
.join("generated_deps.txt"),
&format!("{0:#?}", &gen_deps_sorted),
&format!("{0:#?}", &gen_deps),
)?;

if ctx.copy_to_aosp {
let meson_generated_path = self.get_id().android_path(ctx).join(MESON_GENERATED);
if File::open(&meson_generated_path).is_ok() {
if let Err(err) = std::fs::remove_dir_all(&meson_generated_path) {
return error!("remove_dir_all failed: {err}");
}

print_verbose!("{meson_generated_path:#?} removed");
}
for dep in gen_deps_sorted {
let from = self.build_path.join(&dep);
let to = meson_generated_path.join(&dep);
let to_path = to.parent().unwrap();
if let Err(err) = std::fs::create_dir_all(to_path) {
return error!("create_dir_all({to_path:#?}) failed: {err}");
}
copy_file(&from, &to)?;
}
print_verbose!(
"Files copied from {0:#?} to {meson_generated_path:#?}",
&self.build_path
);
copy_files(
&self.build_path,
&self.get_id().android_path(ctx).join(MESON_GENERATED),
gen_deps,
)?;
}

Ok(package)
Expand Down
2 changes: 1 addition & 1 deletion src/project/spirv_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Project for SpirvTools {
vec![String::from("include")],
));

self.gen_deps = Vec::from_iter(package.get_gen_deps());
self.gen_deps = package.get_gen_deps();

Ok(package)
}
Expand Down
40 changes: 32 additions & 8 deletions src/soong_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,38 @@ impl<'a> SoongPackage<'a> {
package
}

pub fn get_modules(&mut self) -> &mut Vec<SoongModule> {
&mut self.modules
}

pub fn add_module(&mut self, module: SoongModule) {
self.modules.push(module);
}

pub fn filter_local_include_dirs(&mut self, prefix: &str, files: &Vec<PathBuf>) {
let mut set = HashSet::new();
for file in files {
let mut path = file.clone();
while let Some(parent) = path.parent() {
path = PathBuf::from(parent);
set.insert(path.clone());
}
}
for module in &mut self.modules {
module.update_prop("local_include_dirs", |prop| match prop {
SoongProp::VecStr(dirs) => SoongProp::VecStr(
dirs.into_iter()
.filter(|dir| {
if let Ok(strip) = Path::new(&dir).strip_prefix(prefix) {
if !set.contains(strip) {
return false;
}
}
return true;
})
.collect(),
),
_ => prop,
});
}
}

pub fn print(self) -> String {
let mut package = String::from(
"//
Expand All @@ -96,12 +120,12 @@ impl<'a> SoongPackage<'a> {
package
}

pub fn get_gen_deps(&self) -> HashSet<PathBuf> {
self.gen_deps.to_owned()
pub fn get_gen_deps(&self) -> Vec<PathBuf> {
Vec::from_iter(self.gen_deps.to_owned())
}

pub fn get_generated_libraries(&self) -> HashSet<PathBuf> {
self.generated_libraries.to_owned()
pub fn get_generated_libraries(&self) -> Vec<PathBuf> {
Vec::from_iter(self.generated_libraries.to_owned())
}

fn get_defines(&self, defines: Vec<String>, project: &dyn Project) -> Vec<String> {
Expand Down
20 changes: 20 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ pub fn copy_file(from: &Path, to: &Path) -> Result<(), String> {
Ok(())
}

pub fn copy_files(from: &Path, to: &Path, files: Vec<PathBuf>) -> Result<(), String> {
if File::open(to).is_ok() {
if let Err(err) = std::fs::remove_dir_all(to) {
return error!("remove_dir_all failed: {err}");
}
print_verbose!("{to:#?} removed");
}
for file in files {
let from = from.join(&file);
let to = to.join(&file);
let to_path = to.parent().unwrap();
if let Err(err) = std::fs::create_dir_all(to_path) {
return error!("create_dir_all({to_path:#?}) failed: {err}");
}
copy_file(&from, &to)?;
}
print_verbose!("Files copied from {from:#?} to {to:#?}");
Ok(())
}

pub fn write_file(file_path: &Path, content: &str) -> Result<(), String> {
match File::create(file_path) {
Ok(mut file) => {
Expand Down

0 comments on commit 21e67e5

Please sign in to comment.