#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); }