IVGCVSW-3543 Implement the backend versioning algorithm
[platform/upstream/armnn.git] / src / backends / backendsCommon / DynamicBackendUtils.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include "IBackendInternal.hpp"
9
10 #include <armnn/Exceptions.hpp>
11
12 #include <string>
13 #include <dlfcn.h>
14
15 #include <boost/format.hpp>
16
17 namespace armnn
18 {
19
20 class DynamicBackendUtils
21 {
22 public:
23     static void* OpenHandle(const std::string& sharedObjectPath);
24     static void CloseHandle(const void* sharedObjectHandle);
25
26     template<typename EntryPointType>
27     static EntryPointType GetEntryPoint(const void* sharedObjectHandle, const char* symbolName);
28
29     static bool IsBackendCompatible(const BackendVersion& backendVersion);
30
31 protected:
32     /// Protected for testing purposes
33     static bool IsBackendCompatibleImpl(const BackendVersion& backendApiVersion, const BackendVersion& backendVersion);
34
35 private:
36     static std::string GetDlError();
37
38     /// This class is to hold utility functions only
39     DynamicBackendUtils() = delete;
40 };
41
42 template<typename EntryPointType>
43 EntryPointType DynamicBackendUtils::GetEntryPoint(const void* sharedObjectHandle, const char* symbolName)
44 {
45     if (sharedObjectHandle == nullptr)
46     {
47         throw RuntimeException("GetEntryPoint error: invalid handle");
48     }
49
50     if (symbolName == nullptr)
51     {
52         throw RuntimeException("GetEntryPoint error: invalid symbol");
53     }
54
55     auto entryPoint = reinterpret_cast<EntryPointType>(dlsym(const_cast<void*>(sharedObjectHandle), symbolName));
56     if (!entryPoint)
57     {
58         throw RuntimeException(boost::str(boost::format("GetEntryPoint error: %1%") % GetDlError()));
59     }
60
61     return entryPoint;
62 }
63
64 } // namespace armnn