From f5b09df9c88d49c95fd0c0ef7b3670b40634ca5c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 27 May 2022 09:50:35 +0200 Subject: WIP: automatic externals --- src/search.cc | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/search.cc (limited to 'src/search.cc') diff --git a/src/search.cc b/src/search.cc new file mode 100644 index 0000000..e33fc04 --- /dev/null +++ b/src/search.cc @@ -0,0 +1,120 @@ +#include "search.h" + +#include + +#include "libctor.h" + +// https://stackoverflow.com/questions/17939930/finding-out-what-the-gcc-include-path-is + +// /usr/include/ +// /usr/local/include +std::filesystem::path findFile(const Settings& settings, + const std::string& needle, + const std::vector& haystack) +{ + if(settings.verbose > 0) + { + std::cout << "Looking for '" << needle << "'\n"; + } + for(const auto& path : haystack) + { + std::filesystem::path candidate = std::filesystem::path(path) / needle; + if(settings.verbose > 1) + { + std::cout << " in '" << candidate.string() << "' ... "; + } + if(std::filesystem::exists(candidate)) + { + if(settings.verbose > 1) + { + std::cout << "yes\n"; + } + return path; + } + else + { + if(settings.verbose > 1) + { + std::cout << "no\n"; + } + } + } + + if(settings.verbose > 0) + { + std::cout << "Not found.\n"; + } + + return ""; // not found +} + +std::filesystem::path findHeader(const Settings& settings, + const std::string& needle, + const std::vector& haystack) +{ + auto paths = haystack; + paths.insert(paths.end(), + settings.include_paths.begin(), + settings.include_paths.end()); + return findFile(settings, needle, paths); +} + +std::vector findBasename(const Settings& settings, + const std::string& needle, + const std::vector& haystack) +{ + std::vector matches; + if(settings.verbose > 0) + { + std::cout << "Looking for basename '" << needle << "'\n"; + } + + for(const auto& path : haystack) + { + if(settings.verbose > 1) + { + std::cout << "Looking in '" << path << "\n"; + } + + for(const auto& dir_entry : std::filesystem::directory_iterator{path}) + { + if(std::filesystem::is_regular_file(dir_entry)) + { + if(dir_entry.path().stem() == needle) + { + if(settings.verbose > 1) + { + std::cout << "Found " << path / dir_entry.path() << "\n"; + } + matches.push_back(path / dir_entry.path()); + } + } + } + } + + if(matches.empty() && settings.verbose > 0) + { + std::cout << "Not found.\n"; + } + + return matches; +} + +std::vector findLibrary(const Settings& settings, + const std::string& needle, + const std::vector& haystack) +{ + using namespace std::string_literals; + + std::string lib = needle; + if(!lib.starts_with("lib")) + { + lib = "lib"s + needle; + } + auto paths = haystack; + paths.insert(paths.end(), + settings.library_paths.begin(), + settings.library_paths.end()); + return findBasename(settings, lib, paths); +} + -- cgit v1.2.3