Merge "Collect header files in subdirectories" am: 78a68df634 am: f939d664a1

Original change: https://android-review.googlesource.com/c/platform/development/+/2469200

Change-Id: Ieb5cc1cfbf00c0ba52837b55b49b1eef9f214f9f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Hsin-Yi Chen
2023-03-06 02:30:23 +00:00
committed by Automerger Merge Worker
3 changed files with 55 additions and 8 deletions

View File

@@ -198,10 +198,12 @@ cc_test_host {
],
static_libs: [
"libbase",
"libgtest",
"libgtest_main",
"libheader-checker",
"libjsoncpp",
"liblog",
],
shared_libs: [

View File

@@ -29,15 +29,11 @@ namespace utils {
static const std::vector<std::string> header_extensions{
".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"};
static bool ShouldSkipFile(llvm::StringRef &file_name) {
// Look for header files only
if (file_name.empty() || file_name.startswith(".")) {
return true;
}
static bool HasHeaderExtension(llvm::StringRef &file_name) {
return std::find_if(header_extensions.begin(), header_extensions.end(),
[file_name](const std::string &e) {
return file_name.endswith(e);
}) == header_extensions.end();
}) != header_extensions.end();
}
static std::string GetCwd() {
@@ -135,11 +131,14 @@ static bool CollectExportedHeaderSet(const std::string &dir_name,
const std::string &file_path = walker->path();
llvm::StringRef file_name(llvm::sys::path::filename(file_path));
// Ignore non header files.
if (ShouldSkipFile(file_name)) {
if (file_name.empty() || file_name.startswith(".")) {
// Ignore hidden files and directories.
walker.no_push();
continue;
}
if (!HasHeaderExtension(file_name)) {
continue;
}
llvm::ErrorOr<llvm::sys::fs::basic_file_status> status = walker->status();
if (!status) {

View File

@@ -14,12 +14,58 @@
#include "utils/source_path_utils.h"
#include <android-base/file.h>
#include <gtest/gtest.h>
#include <filesystem>
#include <vector>
namespace header_checker {
namespace utils {
TEST(SourcePathUtilsTest, CollectAllExportedHeaders) {
// Prepare a header directory.
TemporaryDir temp_dir;
const std::filesystem::path header_dir = temp_dir.path;
const std::filesystem::path header = header_dir / "header.h";
ASSERT_TRUE(android::base::WriteStringToFile("// test", header));
std::error_code ec;
const std::filesystem::path subdir = header_dir / "subdir";
ASSERT_TRUE(std::filesystem::create_directory(subdir, ec));
ASSERT_FALSE(ec);
const std::filesystem::path subdir_link = header_dir / "subdir_link";
std::filesystem::create_directory_symlink(subdir, subdir_link, ec);
ASSERT_FALSE(ec);
const std::filesystem::path hidden_subdir_link = header_dir / ".subdir_link";
std::filesystem::create_directory_symlink(subdir, hidden_subdir_link, ec);
ASSERT_FALSE(ec);
const std::filesystem::path header_link = subdir / "header_link.h";
std::filesystem::create_symlink(header, header_link, ec);
ASSERT_FALSE(ec);
const std::filesystem::path hidden_header_link = subdir / ".header_link.h";
std::filesystem::create_symlink(header, hidden_header_link, ec);
ASSERT_FALSE(ec);
const std::filesystem::path non_header_link = subdir / "header_link.txt";
std::filesystem::create_symlink(header, non_header_link, ec);
ASSERT_FALSE(ec);
// Test the function.
std::vector<std::string> exported_header_dirs{header_dir};
std::vector<RootDir> root_dirs{{header_dir, "include"}};
std::set<std::string> headers =
CollectAllExportedHeaders(exported_header_dirs, root_dirs);
std::set<std::string> expected_headers{"include/header.h",
"include/subdir/header_link.h",
"include/subdir_link/header_link.h"};
ASSERT_EQ(headers, expected_headers);
}
TEST(SourcePathUtilsTest, NormalizeAbsolutePaths) {
const std::vector<std::string> args{"/root/dir"};