bool load_default_backend()

in source/neuropod/internal/backend_registration.cc [155:199]


bool load_default_backend(const std::vector<BackendLoadSpec> &backends,
                          const std::string &                 type,
                          const std::string &                 target_version_range)
{
    // Reverse priority order
    for (auto it = backends.rbegin(); it != backends.rend(); it++)
    {
        const auto &backend = *it;
        if (backend.type != type)
        {
            // Type doesn't match
            continue;
        }

        if (!semver::satisfies(backend.version, target_version_range))
        {
            // Target version range isn't satisifed
            continue;
        }

        // Try to dlopen it
        const auto &sopath = backend.path;
        if (dlopen(sopath.c_str(), RTLD_NOW | RTLD_GLOBAL) == nullptr)
        {
            const auto err = dlerror();
            if (err == nullptr)
            {
                SPDLOG_TRACE("Loading the default backend for type '{}' failed, but no error message was avaliable",
                             type);
            }
            else
            {
                SPDLOG_TRACE("Loading the default backend for type '{}' failed. Error from dlopen: {}", type, err);
            }

            continue;
        }

        // Successfully loaded the backend
        SPDLOG_TRACE("Successfully loaded default backend '{}'", sopath);
        return true;
    }

    return false;
}