diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2022-05-27 09:50:35 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2022-06-09 22:29:32 +0200 |
commit | f5b09df9c88d49c95fd0c0ef7b3670b40634ca5c (patch) | |
tree | 3b5e78254b88e5546bf8e041226ad90db1d512c2 /src/search.cc | |
parent | 7a16146600384272baf7fb2fc0fc64f59b17ffe9 (diff) |
WIP: automatic externalsexternals
Diffstat (limited to 'src/search.cc')
-rw-r--r-- | src/search.cc | 120 |
1 files changed, 120 insertions, 0 deletions
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 <iostream> + +#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<std::string>& 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<std::string>& haystack) +{ + auto paths = haystack; + paths.insert(paths.end(), + settings.include_paths.begin(), + settings.include_paths.end()); + return findFile(settings, needle, paths); +} + +std::vector<std::filesystem::path> findBasename(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& haystack) +{ + std::vector<std::filesystem::path> 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<std::filesystem::path> findLibrary(const Settings& settings, + const std::string& needle, + const std::vector<std::string>& 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); +} + |