From 402bc6bcee8bebdc0fbb5a62264f2f79c79da695 Mon Sep 17 00:00:00 2001
From: Carsten Patzke <carsten.patzke@desy.de>
Date: Tue, 29 Sep 2020 13:00:07 +0200
Subject: [PATCH] Added env ASAPO_LIBFABRIC_LIBRARY to override lib path

---
 common/cpp/src/asapo_fabric/asapo_fabric.cpp | 41 +++++++++++++-------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/common/cpp/src/asapo_fabric/asapo_fabric.cpp b/common/cpp/src/asapo_fabric/asapo_fabric.cpp
index a74a25c93..4ace5a773 100644
--- a/common/cpp/src/asapo_fabric/asapo_fabric.cpp
+++ b/common/cpp/src/asapo_fabric/asapo_fabric.cpp
@@ -4,6 +4,7 @@
 #ifdef LIBFABRIC_ENABLED
 #include <dlfcn.h>
 #include <mutex>
+#include <iostream>
 #include "fabric_factory_impl.h"
 #include "fabric_function_map.h"
 #endif
@@ -19,26 +20,36 @@ std::unique_ptr<FabricFactory> asapo::fabric::GenerateDefaultFabricFactory() {
         return std::unique_ptr<FabricFactory>(new FabricFactoryImpl());
     }
 
-    void* handle = dlopen("libfabric.so.1", RTLD_LAZY);
-    if (handle) {
+    const char* libfabric_path_override = std::getenv("ASAPO_LIBFABRIC_LIBRARY");
+    void* handle = nullptr;
+    if (libfabric_path_override) {
+        handle = dlopen(libfabric_path_override, RTLD_LAZY);
+        if (!handle) {
+            std::cerr << "WARN: 'ASAPO_LIBFABRIC_LIBRARY' was set, but failed to open. Reason: '" << dlerror() << "'. Fallback to normal path." << std::endl;
+        }
+    }
+    if (!handle) {
+        handle = dlopen("libfabric.so.1", RTLD_LAZY); // Try release v1.xx
+    }
+    if (!handle) {
+        return std::unique_ptr<FabricFactory>(new FabricFactoryNotSupported(FabricErrorTemplates::kLibraryNotFoundError));
+    }
+
 #define ADD_FABRIC_CALL(fName) do { if (!(*((void**)&gffm().fName) = dlsym(handle, #fName))) goto functionNotFoundError; } while(0)
-        ADD_FABRIC_CALL(fi_version);
-        ADD_FABRIC_CALL(fi_dupinfo);
-        ADD_FABRIC_CALL(fi_freeinfo);
-        ADD_FABRIC_CALL(fi_getinfo);
-        ADD_FABRIC_CALL(fi_fabric);
-        ADD_FABRIC_CALL(fi_strerror);
+    ADD_FABRIC_CALL(fi_version);
+    ADD_FABRIC_CALL(fi_dupinfo);
+    ADD_FABRIC_CALL(fi_freeinfo);
+    ADD_FABRIC_CALL(fi_getinfo);
+    ADD_FABRIC_CALL(fi_fabric);
+    ADD_FABRIC_CALL(fi_strerror);
 #undef ADD_FABRIC_CALL
 
-        gffm().is_init_ = true;
+    gffm().is_init_ = true;
 
-        return std::unique_ptr<FabricFactory>(new FabricFactoryImpl());
+    return std::unique_ptr<FabricFactory>(new FabricFactoryImpl());
 functionNotFoundError:
-        dlclose(handle);
-        return std::unique_ptr<FabricFactory>(new FabricFactoryNotSupported(FabricErrorTemplates::kLibraryCompatibilityError));
-    } else {
-        return std::unique_ptr<FabricFactory>(new FabricFactoryNotSupported(FabricErrorTemplates::kLibraryNotFoundError));
-    }
+    dlclose(handle);
+    return std::unique_ptr<FabricFactory>(new FabricFactoryNotSupported(FabricErrorTemplates::kLibraryCompatibilityError));
 #endif
     return std::unique_ptr<FabricFactory>(new FabricFactoryNotSupported(FabricErrorTemplates::kNotSupportedOnBuildError));
 }
-- 
GitLab