+static std::string replace_extension(const std::string &path, const std::string &ext)
+{
+ auto result = path;
+ auto lastSlashIdx = result.find_last_of('/');
+ auto lastDotIdx = result.find_last_of('.');
+ if (lastDotIdx != std::string::npos && lastDotIdx > lastSlashIdx)
+ result.erase(lastDotIdx, std::string::npos);
+ if ( ext.size() > 0 && ext[0] == '.' )
+ result.append(ext);
+ else
+ result.append('.' + ext);
+ return result;
+}
+
+bool Options::findFile(const std::string &path, const std::vector<std::string> &tbdExtensions, FileInfo& result) const
+{
+ FileInfo tbdInfo;
+ for ( const auto &ext : tbdExtensions ) {
+ auto newPath = replace_extension(path, ext);
+ bool found = tbdInfo.checkFileExists(*this, newPath.c_str());
+ if ( fTraceDylibSearching )
+ printf("[Logging for XBS]%sfound library: '%s'\n", (found ? " " : " not "), newPath.c_str());
+ if ( found )
+ break;
+ }
+
+ FileInfo dylibInfo;
+ {
+ bool found = dylibInfo.checkFileExists(*this, path.c_str());
+ if ( fTraceDylibSearching )
+ printf("[Logging for XBS]%sfound library: '%s'\n", (found ? " " : " not "), path.c_str());
+ }
+
+ // There is only a text-based stub file or a dynamic library file.
+ if ( tbdInfo.missing() != dylibInfo.missing() ) {
+ result = tbdInfo.missing() ? dylibInfo : tbdInfo;
+ }
+ // There are both - a text-based stub file and a dynamic library file.
+ else if ( !tbdInfo.missing() && !dylibInfo.missing() ) {
+ // Check if we should prefer the text-based stub file (installapi).
+ if (tapi::LinkerInterfaceFile::shouldPreferTextBasedStubFile(tbdInfo.path)) {
+ result = tbdInfo;
+ }
+ // If the files are still in sync we can use and should use the text-based stub file.
+ else if (tapi::LinkerInterfaceFile::areEquivalent(tbdInfo.path, dylibInfo.path)) {
+ result = tbdInfo;
+ }
+ // Otherwise issue a warning and fall-back to the dynamic library file.
+ else {
+ warning("text-based stub file %s and library file %s are out of sync. Falling back to library file for linking.", tbdInfo.path, dylibInfo.path);
+ result = dylibInfo;
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
+static bool startsWith(const std::string& str, const std::string& prefix)
+{
+ return (str.compare(0, prefix.length(), prefix) == 0);
+}
+
+static std::string getDirPath(const std::string& path)
+{
+ std::string::size_type lastSlashPos = path.find_last_of('/');
+ if ( lastSlashPos == std::string::npos )
+ return "./";
+ else
+ return path.substr(0, lastSlashPos+1);
+}
+
+Options::FileInfo Options::findFile(const std::string &path, const ld::dylib::File* fromDylib) const