From 21e67e565556605908ec7ac220da1ac76adca847 Mon Sep 17 00:00:00 2001 From: Romaric Jodin Date: Fri, 20 Dec 2024 16:16:10 +0100 Subject: [PATCH] add filter_local_include_dirs for mesa & llvm-project (#82) --- src/project/clspv.rs | 2 +- src/project/llvm_project.rs | 63 +++++------------------------------ src/project/mesa.rs | 66 +++++-------------------------------- src/project/spirv_tools.rs | 2 +- src/soong_package.rs | 40 +++++++++++++++++----- src/utils.rs | 20 +++++++++++ 6 files changed, 71 insertions(+), 122 deletions(-) diff --git a/src/project/clspv.rs b/src/project/clspv.rs index b6f8c7e..f27f1a7 100644 --- a/src/project/clspv.rs +++ b/src/project/clspv.rs @@ -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) } diff --git a/src/project/llvm_project.rs b/src/project/llvm_project.rs index 19326f4..795450c 100644 --- a/src/project/llvm_project.rs +++ b/src/project/llvm_project.rs @@ -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"; @@ -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); diff --git a/src/project/mesa.rs b/src/project/mesa.rs index 04130da..14d7193 100644 --- a/src/project/mesa.rs +++ b/src/project/mesa.rs @@ -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"; @@ -56,7 +53,6 @@ impl Project for Mesa { } else { mesa_test_path.clone() }; - if !ctx.skip_gen_ninja { execute_cmd!( "bash", @@ -70,7 +66,6 @@ impl Project for Mesa { ] )?; } - if !ctx.skip_build { execute_cmd!( "meson", @@ -79,7 +74,6 @@ impl Project for Mesa { } let targets = parse_build_ninja::(&self.build_path)?; - let mut package = SoongPackage::new( &self.src_path, &self.ndk_path, @@ -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) diff --git a/src/project/spirv_tools.rs b/src/project/spirv_tools.rs index 34bface..f0ca042 100644 --- a/src/project/spirv_tools.rs +++ b/src/project/spirv_tools.rs @@ -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) } diff --git a/src/soong_package.rs b/src/soong_package.rs index 27cd8b3..6b4ed95 100644 --- a/src/soong_package.rs +++ b/src/soong_package.rs @@ -69,14 +69,38 @@ impl<'a> SoongPackage<'a> { package } - pub fn get_modules(&mut self) -> &mut Vec { - &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) { + 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( "// @@ -96,12 +120,12 @@ impl<'a> SoongPackage<'a> { package } - pub fn get_gen_deps(&self) -> HashSet { - self.gen_deps.to_owned() + pub fn get_gen_deps(&self) -> Vec { + Vec::from_iter(self.gen_deps.to_owned()) } - pub fn get_generated_libraries(&self) -> HashSet { - self.generated_libraries.to_owned() + pub fn get_generated_libraries(&self) -> Vec { + Vec::from_iter(self.generated_libraries.to_owned()) } fn get_defines(&self, defines: Vec, project: &dyn Project) -> Vec { diff --git a/src/utils.rs b/src/utils.rs index 738f084..7746d6e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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) -> 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) => {