Skip to content

Commit 56d00a8

Browse files
jolivier23soumagne
authored andcommitted
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 56d00a8

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-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("Based on NA_INSTALL_PLUGIN_DIR, NA_PLUGIN_PATH_OFFSET is ${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: 47 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,8 @@
2021
# include <Windows.h>
2122
# else
2223
# include <dirent.h>
24+
# include <limits.h>
25+
# include <link.h>
2326
# endif
2427
#endif
2528

@@ -260,20 +263,61 @@ na_finalize(void) NA_DESTRUCTOR;
260263

261264
/*---------------------------------------------------------------------------*/
262265
#ifdef NA_HAS_DYNAMIC_PLUGINS
266+
# ifdef _WIN32
267+
# define resolve_plugin_path(offset) NULL
268+
# else
269+
static char *
270+
resolve_plugin_path(const char *offset)
271+
{
272+
static int placeholder;
273+
Dl_info info;
274+
char *libdir;
275+
char *libpath;
276+
char *slash;
277+
int rc;
278+
279+
if (!dladdr((void *) &placeholder, &info))
280+
return NULL;
281+
282+
libpath = realpath(info.dli_fname, NULL);
283+
if (libpath == NULL)
284+
return NULL;
285+
286+
slash = strrchr(libpath, '/');
287+
if (slash == NULL) {
288+
free(libpath);
289+
return NULL;
290+
}
291+
292+
*slash = '\0';
293+
rc = asprintf(&libdir, "%s/%s", libpath, offset);
294+
free(libpath);
295+
if (rc != -1)
296+
return libdir;
297+
298+
return NULL;
299+
}
300+
# endif
301+
263302
static void
264303
na_initialize(void)
265304
{
266305
const char *plugin_path = getenv("NA_PLUGIN_PATH");
306+
char *relative_plugin_path = NULL;
267307
na_return_t ret;
268-
269-
if (plugin_path == NULL)
270-
plugin_path = NA_DEFAULT_PLUGIN_PATH;
308+
if (plugin_path == NULL) {
309+
relative_plugin_path = resolve_plugin_path(NA_PLUGIN_PATH_OFFSET);
310+
plugin_path = (relative_plugin_path != NULL) ? relative_plugin_path
311+
: NA_DEFAULT_PLUGIN_PATH;
312+
}
271313

272314
ret = na_plugin_scan_path(plugin_path, &na_plugin_dynamic_g);
273315
NA_CHECK_FATAL_DONE(ret != NA_SUCCESS,
274316
"No usable plugin found in path (%s), consider setting NA_PLUGIN_PATH "
275317
"if path indicated is not valid.",
276318
plugin_path);
319+
320+
free(relative_plugin_path);
277321
}
278322

279323
/*---------------------------------------------------------------------------*/

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

src/util/mercury_dl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# include <windows.h>
1616
# define HG_DL_HANDLE HMODULE
1717
#else
18+
# define _GNU_SOURCE
1819
# include <dlfcn.h>
1920
# define HG_DL_HANDLE void *
2021
#endif

0 commit comments

Comments
 (0)