Skip to content

Commit 883a833

Browse files
committed
NA: Use relative path for NA plugin search
Calculate relative path at build time and use it at runtime to find the plugin directory Signed-off-by: Jeff Olivier <[email protected]>
1 parent ff63d8f commit 883a833

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/na/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,16 @@ if(NA_USE_DYNAMIC_PLUGINS)
8282
if(NOT BUILD_SHARED_LIBS)
8383
message(FATAL_ERROR "Using dynamic plugins requires BUILD_SHARED_LIBS to be ON.")
8484
endif()
85+
execute_process (
86+
COMMAND realpath -m ${NA_INSTALL_PLUGIN_DIR} --relative-to=${NA_INSTALL_LIB_DIR}
87+
OUTPUT_VARIABLE NA_PLUGIN_PATH_OFFSET
88+
OUTPUT_STRIP_TRAILING_WHITESPACE
89+
)
90+
message("NA_PLUGIN_PATH_OFFSET=${NA_PLUGIN_PATH_OFFSET}")
8591
set(NA_HAS_DYNAMIC_PLUGINS 1)
8692
set(NA_DEFAULT_PLUGIN_PATH ${NA_INSTALL_PLUGIN_DIR} CACHE PATH "Default path used to load plugins.")
8793
mark_as_advanced(NA_DEFAULT_PLUGIN_PATH)
94+
mark_as_advanced(NA_PLUGIN_PATH_OFFSET)
8895
endif()
8996

9097
# BMI

src/na/na.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: BSD-3-Clause
66
*/
77

8+
#define _GNU_SOURCE /* For dladdr */
89
#include "na_plugin.h"
910

1011
#include "mercury_atomic_queue.h"
@@ -20,6 +21,9 @@
2021
# include <Windows.h>
2122
# else
2223
# include <dirent.h>
24+
# include <dlfcn.h>
25+
# include <limits.h>
26+
# include <link.h>
2327
# endif
2428
#endif
2529

@@ -260,20 +264,68 @@ na_finalize(void) NA_DESTRUCTOR;
260264

261265
/*---------------------------------------------------------------------------*/
262266
#ifdef NA_HAS_DYNAMIC_PLUGINS
267+
# ifdef _WIN32
268+
# define resolve_plugin_path(offset) NULL
269+
# else
270+
static char *
271+
resolve_plugin_path(const char *offset)
272+
{
273+
Dl_info info;
274+
char *libdir;
275+
char *libpath;
276+
char *slash;
277+
int rc;
278+
279+
if (!dladdr(resolve_plugin_path, &info)) {
280+
return NULL;
281+
}
282+
283+
libpath = realpath(info.dli_fname, NULL);
284+
if (libpath == NULL) {
285+
return NULL;
286+
}
287+
288+
slash = strrchr(libpath, '/');
289+
if (slash == NULL) {
290+
free(libpath);
291+
return NULL;
292+
}
293+
294+
*slash = '\0';
295+
rc = asprintf(&libdir, "%s/%s", libpath, offset);
296+
free(libpath);
297+
if (rc != -1) {
298+
return libdir;
299+
}
300+
301+
return NULL;
302+
}
303+
# endif
304+
263305
static void
264306
na_initialize(void)
265307
{
266308
const char *plugin_path = getenv("NA_PLUGIN_PATH");
309+
char *relative_plugin_path = NULL;
267310
na_return_t ret;
268-
269-
if (plugin_path == NULL)
270-
plugin_path = NA_DEFAULT_PLUGIN_PATH;
311+
if (plugin_path == NULL) {
312+
relative_plugin_path = resolve_plugin_path(NA_PLUGIN_PATH_OFFSET);
313+
if (relative_plugin_path == NULL) {
314+
plugin_path = NA_DEFAULT_PLUGIN_PATH;
315+
} else {
316+
plugin_path = relative_plugin_path;
317+
}
318+
}
271319

272320
ret = na_plugin_scan_path(plugin_path, &na_plugin_dynamic_g);
273321
NA_CHECK_FATAL_DONE(ret != NA_SUCCESS,
274322
"No usable plugin found in path (%s), consider setting NA_PLUGIN_PATH "
275323
"if path indicated is not valid.",
276324
plugin_path);
325+
326+
if (relative_plugin_path) {
327+
free(relative_plugin_path);
328+
}
277329
}
278330

279331
/*---------------------------------------------------------------------------*/

src/na/na_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
# define NA_PLUGIN_VISIBILITY NA_PRIVATE
8282
#endif
8383
#cmakedefine NA_DEFAULT_PLUGIN_PATH "@NA_DEFAULT_PLUGIN_PATH@"
84+
#cmakedefine NA_PLUGIN_PATH_OFFSET "@NA_PLUGIN_PATH_OFFSET@"
8485

8586
/* Build Options */
8687
#cmakedefine NA_HAS_DEBUG

0 commit comments

Comments
 (0)