2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
8 #include "IBackendInternal.hpp"
9 #include "DynamicBackend.hpp"
11 #include <armnn/Exceptions.hpp>
17 #include <boost/format.hpp>
19 #if !defined(DYNAMIC_BACKEND_PATHS)
20 #define DYNAMIC_BACKEND_PATHS ""
26 class DynamicBackendUtils
29 static void* OpenHandle(const std::string& sharedObjectPath);
30 static void CloseHandle(const void* sharedObjectHandle);
32 template<typename EntryPointType>
33 static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName);
35 static bool IsBackendCompatible(const BackendVersion& backendVersion);
37 static std::vector<std::string> GetBackendPaths(const std::string& overrideBackendPath = "");
38 static bool IsPathValid(const std::string& path);
39 static std::vector<std::string> GetSharedObjects(const std::vector<std::string>& backendPaths);
41 static std::vector<DynamicBackendPtr> CreateDynamicBackends(const std::vector<std::string>& sharedObjects);
44 /// Protected methods for testing purposes
45 static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
46 static std::vector<std::string> GetBackendPathsImpl(const std::string& backendPaths);
49 static std::string GetDlError();
51 /// This class is to hold utility functions only
52 DynamicBackendUtils() = delete;
55 template<typename EntryPointType>
56 EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
58 if (sharedObjectHandle == nullptr)
60 throw RuntimeException("GetEntryPoint error: invalid handle");
63 if (symbolName == nullptr)
65 throw RuntimeException("GetEntryPoint error: invalid symbol");
68 auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName));
71 throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));