aboutsummaryrefslogtreecommitdiff
path: root/src/backends/backendsCommon/DynamicBackendUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/backends/backendsCommon/DynamicBackendUtils.cpp')
-rw-r--r--src/backends/backendsCommon/DynamicBackendUtils.cpp86
1 files changed, 85 insertions, 1 deletions
diff --git a/src/backends/backendsCommon/DynamicBackendUtils.cpp b/src/backends/backendsCommon/DynamicBackendUtils.cpp
index ae36a24b30..1dea802016 100644
--- a/src/backends/backendsCommon/DynamicBackendUtils.cpp
+++ b/src/backends/backendsCommon/DynamicBackendUtils.cpp
@@ -5,10 +5,12 @@
#include "DynamicBackendUtils.hpp"
-#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/log/trivial.hpp>
+#include <regex>
+
namespace armnn
{
@@ -151,4 +153,86 @@ bool DynamicBackendUtils::IsPathValid(const std::string& path)
return true;
}
+std::vector<std::string> DynamicBackendUtils::GetSharedObjects(const std::vector<std::string>& backendPaths)
+{
+ std::unordered_set<std::string> uniqueSharedObjects;
+ std::vector<std::string> sharedObjects;
+
+ for (const std::string& backendPath : backendPaths)
+ {
+ using namespace boost::filesystem;
+
+ // Check if the path is valid. In case of error, IsValidPath will log an error message
+ if (!IsPathValid(backendPath))
+ {
+ continue;
+ }
+
+ // Go through all the files in the current backend path
+ for (directory_iterator fileIterator(backendPath); fileIterator != directory_iterator(); fileIterator++)
+ {
+ path filePath = *fileIterator;
+ std::string filename = filePath.filename().string();
+
+ if (filename.empty())
+ {
+ // Empty filename
+ continue;
+ }
+
+ path canonicalPath;
+ try
+ {
+ // Get the canonical path for the current file, it will throw if for example the file is a
+ // symlink that cannot be resolved
+ canonicalPath = canonical(filePath);
+ }
+ catch (const filesystem_error& e)
+ {
+ BOOST_LOG_TRIVIAL(warning) << "GetSharedObjects warning: " << e.what();
+ }
+ if (canonicalPath.empty())
+ {
+ // No such file or perhaps a symlink that couldn't be resolved
+ continue;
+ }
+
+ // Check if the current filename matches the expected naming convention
+ // The expected format is: <vendor>_<name>_backend.so[<version>]
+ // e.g. "Arm_GpuAcc_backend.so" or "Arm_GpuAcc_backend.so.1.2"
+ const std::regex dynamicBackendRegex("^[a-zA-Z0-9]+_[a-zA-Z0-9]+_backend.so(\\.[0-9]+)*$");
+
+ bool filenameMatch = false;
+ try
+ {
+ // Match the filename to the expected naming scheme
+ filenameMatch = std::regex_match(filename, dynamicBackendRegex);
+ }
+ catch (const std::exception& e)
+ {
+ BOOST_LOG_TRIVIAL(warning) << "GetSharedObjects warning: " << e.what();
+ }
+ if (!filenameMatch)
+ {
+ // Filename does not match the expected naming scheme (or an error has occurred)
+ continue;
+ }
+
+ // Append the valid canonical path to the output list only if it's not a duplicate
+ std::string validCanonicalPath = canonicalPath.string();
+ auto it = uniqueSharedObjects.find(validCanonicalPath);
+ if (it == uniqueSharedObjects.end())
+ {
+ // Not a duplicate, append the canonical path to the output list
+ sharedObjects.push_back(validCanonicalPath);
+
+ // Add the canonical path to the collection of unique shared objects
+ uniqueSharedObjects.insert(validCanonicalPath);
+ }
+ }
+ }
+
+ return sharedObjects;
+}
+
} // namespace armnn