Allow symlinks to be included while collecting exported headers.

Bug: 62380238

Also:
1) Some code clean up to avoid copying set of exported headers.

Test: The following:
      1) change build/soong/cc/builder.go to use built tool instead of prebuilt.
      2) make liblog/include/log.h a symlink to a moved copy -> liblog/log.h.
      3) mm -j64 in liblog, liblog.so.lsdump has enum android_LogPriority, which
         is not present without this change.

Change-Id: Iaecfad5b638ee0db8343bbd80eb60c6aa8f1feac
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/abi_wrappers.cpp b/vndk/tools/header-checker/header-abi-dumper/src/abi_wrappers.cpp
index b0d4e23..391c92a 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/abi_wrappers.cpp
+++ b/vndk/tools/header-checker/header-abi-dumper/src/abi_wrappers.cpp
@@ -61,12 +61,7 @@
   // belonging to the library.
   clang::SourceLocation expansion_location = sm.getExpansionLoc(location);
   llvm::StringRef file_name = sm.getFilename(expansion_location);
-  std::string file_name_adjusted = "";
-  char file_abs_path[PATH_MAX];
-  if (realpath(file_name.str().c_str(), file_abs_path) == nullptr) {
-    return "";
-  }
-  return file_abs_path;
+  return abi_util::RealPath(file_name.str());
 }
 
 static abi_util::AccessSpecifierIR AccessClangToIR(
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/ast_processing.h b/vndk/tools/header-checker/header-abi-dumper/src/ast_processing.h
index 569936b..0a8f7f7 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/ast_processing.h
+++ b/vndk/tools/header-checker/header-abi-dumper/src/ast_processing.h
@@ -87,7 +87,7 @@
   std::string file_name_;
   clang::CompilerInstance *cip_;
   std::string out_dump_name_;
-  std::set<std::string> exported_headers_;
+  const std::set<std::string> &exported_headers_;
 };
 
 #endif // AST_PROCESSING_H_
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.cpp b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.cpp
index 8f17328..764fc23 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.cpp
+++ b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.cpp
@@ -25,16 +25,13 @@
 #include <llvm/Support/Path.h>
 
 HeaderCheckerFrontendAction::HeaderCheckerFrontendAction(
-    const std::string &dump_name, const std::vector<std::string> &exports)
-  : dump_name_(dump_name), exported_header_dirs_(exports) { }
+    const std::string &dump_name, const std::set<std::string> &exported_headers)
+  : dump_name_(dump_name), exported_headers_(exported_headers) { }
 
 std::unique_ptr<clang::ASTConsumer>
 HeaderCheckerFrontendAction::CreateASTConsumer(clang::CompilerInstance &ci,
                                                llvm::StringRef header_file) {
-  std::set<std::string> exported_headers;
-  exported_headers = abi_util::CollectAllExportedHeaders(exported_header_dirs_);
   // Create AST consumers.
-  return llvm::make_unique<HeaderASTConsumer>(header_file,
-                                              &ci, dump_name_,
-                                              exported_headers);
+  return llvm::make_unique<HeaderASTConsumer>(header_file, &ci, dump_name_,
+                                              exported_headers_);
 }
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.h b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.h
index 61e4e4c..1981c73 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.h
+++ b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action.h
@@ -31,12 +31,12 @@
 class HeaderCheckerFrontendAction : public clang::ASTFrontendAction {
  private:
   std::string dump_name_;
-  const std::vector<std::string> &exported_header_dirs_;
+  const std::set<std::string> &exported_headers_;
 
  public:
   HeaderCheckerFrontendAction(
       const std::string &dump_name,
-      const std::vector<std::string> &export_header_dirs);
+      const std::set<std::string> &exported_headers);
 
  protected:
   std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.cpp b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.cpp
index 1640560..3188776 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.cpp
+++ b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.cpp
@@ -20,9 +20,9 @@
 
 HeaderCheckerFrontendActionFactory::HeaderCheckerFrontendActionFactory(
     const std::string &dump_name,
-    const std::vector<std::string> &export_header_dirs)
-  : dump_name_(dump_name), export_header_dirs_(export_header_dirs) { }
+    const std::set<std::string> &exported_headers)
+  : dump_name_(dump_name), exported_headers_(exported_headers) { }
 
 clang::FrontendAction *HeaderCheckerFrontendActionFactory::create() {
-  return new HeaderCheckerFrontendAction(dump_name_, export_header_dirs_);
+  return new HeaderCheckerFrontendAction(dump_name_, exported_headers_);
 }
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.h b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.h
index 947a94a..2de0f0e 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.h
+++ b/vndk/tools/header-checker/header-abi-dumper/src/frontend_action_factory.h
@@ -23,12 +23,12 @@
     : public clang::tooling::FrontendActionFactory {
  private:
   std::string dump_name_;
-  const std::vector<std::string> &export_header_dirs_;
+  const std::set<std::string> &exported_headers_;
 
  public:
   HeaderCheckerFrontendActionFactory(
       const std::string &dump_name,
-      const std::vector<std::string> &exported_header_dirs);
+      const std::set<std::string> &exported_headers);
 
   clang::FrontendAction *create() override;
 };
diff --git a/vndk/tools/header-checker/header-abi-dumper/src/header_checker.cpp b/vndk/tools/header-checker/header-abi-dumper/src/header_checker.cpp
index aaf532f..407573b 100644
--- a/vndk/tools/header-checker/header-abi-dumper/src/header_checker.cpp
+++ b/vndk/tools/header-checker/header-abi-dumper/src/header_checker.cpp
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include "frontend_action_factory.h"
+#include <header_abi_util.h>
 
 #include <clang/Frontend/FrontendActions.h>
 #include <clang/Tooling/CommonOptionsParser.h>
@@ -96,8 +97,10 @@
     ::exit(1);
   }
 
-  if (no_filter) {
-    static_cast<std::vector<std::string> &>(exported_header_dirs).clear();
+  std::set<std::string> exported_headers;
+  if (!no_filter) {
+    exported_headers =
+        abi_util::CollectAllExportedHeaders(exported_header_dirs);
   }
 
   // Initialize clang tools and run front-end action.
@@ -105,7 +108,7 @@
 
   clang::tooling::ClangTool tool(*compilations, header_files);
   std::unique_ptr<clang::tooling::FrontendActionFactory> factory(
-      new HeaderCheckerFrontendActionFactory(out_dump, exported_header_dirs));
+      new HeaderCheckerFrontendActionFactory(out_dump, exported_headers));
 
   return tool.run(factory.get());
 }
diff --git a/vndk/tools/header-checker/header-abi-util/include/header_abi_util.h b/vndk/tools/header-checker/header-abi-util/include/header_abi_util.h
index 5f18ab7..4b50ed8 100644
--- a/vndk/tools/header-checker/header-abi-util/include/header_abi_util.h
+++ b/vndk/tools/header-checker/header-abi-util/include/header_abi_util.h
@@ -32,6 +32,8 @@
 
 namespace abi_util {
 
+std::string RealPath(const std::string &path);
+
 std::set<std::string> CollectAllExportedHeaders(
     const std::vector<std::string> &exported_header_dirs);
 
diff --git a/vndk/tools/header-checker/header-abi-util/src/collect_exported_headers.cpp b/vndk/tools/header-checker/header-abi-util/src/collect_exported_headers.cpp
index 4663208..afdf76b 100644
--- a/vndk/tools/header-checker/header-abi-util/src/collect_exported_headers.cpp
+++ b/vndk/tools/header-checker/header-abi-util/src/collect_exported_headers.cpp
@@ -37,6 +37,14 @@
   return false;
 }
 
+std::string RealPath(const std::string &path) {
+  char file_abs_path[PATH_MAX];
+  if (realpath(path.c_str(), file_abs_path) == nullptr) {
+    return "";
+  }
+  return file_abs_path;
+}
+
 bool CollectExportedHeaderSet(const std::string &dir_name,
                               std::set<std::string> *exported_headers) {
   std::error_code ec;
@@ -66,17 +74,13 @@
       return false;
     }
 
-    if (!llvm::sys::fs::is_regular_file(status)) {
-      // Ignore non regular files. eg: soft links.
+    if ((status.type() != llvm::sys::fs::file_type::symlink_file) &&
+        !llvm::sys::fs::is_regular_file(status)) {
+      // Ignore non regular files, except symlinks.
       continue;
     }
 
-    llvm::SmallString<128> abs_path(file_path);
-    if (llvm::sys::fs::make_absolute(abs_path)) {
-      llvm::errs() << "Failed to get absolute path for : " << file_name << "\n";
-      return false;
-    }
-    exported_headers->insert(abs_path.str());
+    exported_headers->insert(RealPath(file_path));
   }
   return true;
 }