-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach
jl_load_dynamic_library()
about @executable_path
on all pla…
…tforms It is useful for us to be able to hard-code a dynamic library's location relative to that of the julia executable. We repurpose the `@executable_path` component from MacOS, allowing all platforms to perform executable-relative binary loading (I'm looking at you, Windows).
- Loading branch information
1 parent
08861c7
commit 00abaf8
Showing
3 changed files
with
24 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,6 @@ static int endswith_extension(const char *path) | |
|
||
#define PATHBUF 4096 | ||
|
||
extern char *julia_bindir; | ||
|
||
#define JL_RTLD(flags, FLAG) (flags & JL_RTLD_ ## FLAG ? RTLD_ ## FLAG : 0) | ||
|
||
#ifdef _OS_WINDOWS_ | ||
|
@@ -136,7 +134,7 @@ JL_DLLEXPORT int jl_dlclose(void *handle) | |
|
||
JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, int throw_err) | ||
{ | ||
char path[PATHBUF]; | ||
char path[PATHBUF], relocated[PATHBUF]; | ||
int i; | ||
#ifdef _OS_WINDOWS_ | ||
int err; | ||
|
@@ -173,6 +171,9 @@ JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, | |
this branch permutes all base paths in DL_LOAD_PATH with all extensions | ||
note: skip when !jl_base_module to avoid UndefVarError(:DL_LOAD_PATH), | ||
and also skip for absolute paths | ||
We also do simple string replacement here for elements starting with `@executable_path/`. | ||
While these exist as OS concepts on Darwin, we want to use them on other platforms | ||
such as Windows, so we emulate them here. | ||
*/ | ||
if (!abspath && jl_base_module != NULL) { | ||
jl_array_t *DL_LOAD_PATH = (jl_array_t*)jl_get_global(jl_base_module, jl_symbol("DL_LOAD_PATH")); | ||
|
@@ -183,13 +184,21 @@ JL_DLLEXPORT void *jl_load_dynamic_library(const char *modname, unsigned flags, | |
size_t len = strlen(dl_path); | ||
if (len == 0) | ||
continue; | ||
|
||
// Is this entry supposed to be relative to the bindir? | ||
if (len >= 16 && strncmp(dl_path, "@executable_path", 16) == 0) { | ||
snprintf(relocated, PATHBUF, "%s%s", jl_options.julia_bindir, dl_path + 16); | ||
len = len - 16 + strlen(jl_options.julia_bindir); | ||
} else { | ||
strncpy(relocated, dl_path, len); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
staticfloat
Author
Member
|
||
} | ||
for (i = 0; i < n_extensions; i++) { | ||
const char *ext = extensions[i]; | ||
path[0] = '\0'; | ||
if (dl_path[len-1] == PATHSEPSTRING[0]) | ||
snprintf(path, PATHBUF, "%s%s%s", dl_path, modname, ext); | ||
if (relocated[len-1] == PATHSEPSTRING[0]) | ||
snprintf(path, PATHBUF, "%s%s%s", relocated, modname, ext); | ||
else | ||
snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", dl_path, modname, ext); | ||
snprintf(path, PATHBUF, "%s" PATHSEPSTRING "%s%s", relocated, modname, ext); | ||
#ifdef _OS_WINDOWS_ | ||
if (i == 0) { // LoadLibrary already tested the extensions, we just need to check the `stat` result | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Should this be
strncpy(relocated, dl_path, PATHBUF);
? The way it is, it doesn't copy the terminating0x00
fromdl_path
, so relies onrelocated
to be initialized with zeros, or am I missing something? (Even withPATHBUF
,relocated
could end up non-null-terminated ifdl_path
is too long, but I have no idea whether that can actually happen.)