Skip to content

Add function to get libjulia path at runtime #5045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/dlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,39 @@ char *jl_dlfind_win32(char *f_name)
}
#endif

static char *libjulia_path = NULL;
DLLEXPORT char *jl_get_libjulia_path()
{
if (libjulia_path != NULL) {
int max_path = 512;
libjulia_path = malloc(max_path);

#ifdef _OS_WINDOWS_
HMODULE hm = NULL;

if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR) &jl_get_libjulia_path,
&hm)) {
int ret = GetLastError();
fprintf(stderr, "GetModuleHandle returned %d\n", ret);
}
GetModuleFileNameA(hm, libjulia_path, max_path);
#else
Dl_info dl_info;
dladdr((void*)jl_get_libjulia_path, &dl_info);
strncpy(libjulia_path, dl_info.dli_fname, max_path);
#endif

// remove libjulia.so/dll/dylib from full path
char *pos = strrchr(libjulia_path, PATHSEP);
if (pos) { // terminate string
pos[1] = '\0';
}
}
return libjulia_path;
}

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ DLLEXPORT int jl_uv_dlopen(const char *filename, uv_lib_t *lib, unsigned flags);
DLLEXPORT uv_lib_t *jl_wrap_raw_dl_handle(void *handle);
char *jl_dlfind_win32(char *name);
DLLEXPORT int add_library_mapping(char *lib, void *hnd);
DLLEXPORT char *jl_get_libjulia_path();

#if defined(__linux__) || defined(__FreeBSD__)
DLLEXPORT const char *jl_lookup_soname(char *pfx, size_t n);
Expand Down