Skip to content

embedding_wrapper.c and julia_init.c: Use JULIA_DEPOT_PATH and JULIA_LOAD_PATH from the environment #1032

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion bin/refresh_library_mapping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Downloads
using Libdl
using Glob

v = v"1.9.0"
v = v"1.12.0"
v2 = VersionNumber(v.major, v.minor)

url = download_url(v, Linux(:x86_64; libc = :glibc))
Expand Down
44 changes: 35 additions & 9 deletions src/embedding_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,45 @@ jl_value_t *checked_eval_string(const char *code) {

void set_depot_load_path(const char *root_dir) {
#ifdef _WIN32
char *path_sep = ";";
char *julia_share_subdir = "\\share\\julia";
#else
char *path_sep = ":";
char *julia_share_subdir = "/share/julia";
#endif
char *share_dir =
calloc(sizeof(char), strlen(root_dir) + strlen(julia_share_subdir) + 1);
strcat(share_dir, root_dir);
strcat(share_dir, julia_share_subdir);
int share_path_len = strlen(root_dir) + strlen(julia_share_subdir) + 1;

char *curr_depot_path = getenv("JULIA_DEPOT_PATH");
int curr_depot_path_len = curr_depot_path == NULL ? 0 : strlen(curr_depot_path);
int new_depot_path_len = curr_depot_path_len + 1 + share_path_len;
char *new_depot_path = calloc(sizeof (char), new_depot_path_len);
if (curr_depot_path_len > 0) {
strcat(new_depot_path, curr_depot_path);
strcat(new_depot_path, path_sep);
}
strcat(new_depot_path, root_dir);
strcat(new_depot_path, julia_share_subdir);

char *curr_load_path = getenv("JULIA_LOAD_PATH");
int curr_load_path_len = curr_load_path == NULL ? 0 : strlen(curr_load_path);
int new_load_path_len = curr_load_path_len + 1 + share_path_len;
char *new_load_path = calloc(sizeof (char), new_load_path_len);
if (curr_load_path_len > 0) {
strcat(new_load_path, curr_load_path);
strcat(new_load_path, path_sep);
}
strcat(new_load_path, root_dir);
strcat(new_load_path, julia_share_subdir);

#ifdef _WIN32
_putenv_s("JULIA_DEPOT_PATH", share_dir);
_putenv_s("JULIA_LOAD_PATH", share_dir);
_putenv_s("JULIA_DEPOT_PATH", new_depot_path);
_putenv_s("JULIA_LOAD_PATH", new_load_path);
#else
setenv("JULIA_DEPOT_PATH", share_dir, 1);
setenv("JULIA_LOAD_PATH", share_dir, 1);
setenv("JULIA_DEPOT_PATH", new_depot_path, 1);
setenv("JULIA_LOAD_PATH", new_load_path, 1);
#endif
free(new_load_path);
free(new_depot_path);
}

// main function (windows UTF16 -> UTF8 argument conversion code copied from
Expand Down Expand Up @@ -120,7 +143,10 @@ int main(int argc, char *argv[]) {
jl_value_t *firstarg = checked_eval_string("popfirst!(ARGS)");
JL_GC_PUSH1(&firstarg);
jl_sym_t *var = jl_symbol("PROGRAM_FILE");
#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 11
#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 12
jl_binding_t *bp = jl_get_binding_wr(jl_base_module, var);
jl_checked_assignment(bp, jl_base_module, var, firstarg);
#elif JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 11
jl_binding_t *bp = jl_get_binding_wr(jl_base_module, var, /* alloc */ 1);
jl_checked_assignment(bp, jl_base_module, var, firstarg);
#elif JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR >= 10
Expand Down
44 changes: 35 additions & 9 deletions src/julia_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,45 @@ const char *get_sysimage_path(const char *libname) {

void set_depot_load_path(const char *root_dir) {
#ifdef _WIN32
char *path_sep = ";";
char *julia_share_subdir = "\\share\\julia";
#else
char *path_sep = ":";
char *julia_share_subdir = "/share/julia";
#endif
char *share_dir =
calloc(sizeof(char), strlen(root_dir) + strlen(julia_share_subdir) + 1);
strcat(share_dir, root_dir);
strcat(share_dir, julia_share_subdir);
int share_path_len = strlen(root_dir) + strlen(julia_share_subdir) + 1;

char *curr_depot_path = getenv("JULIA_DEPOT_PATH");
int curr_depot_path_len = curr_depot_path == NULL ? 0 : strlen(curr_depot_path);
int new_depot_path_len = curr_depot_path_len + 1 + share_path_len;
char *new_depot_path = calloc(sizeof (char), new_depot_path_len);
if (curr_depot_path_len > 0) {
strcat(new_depot_path, curr_depot_path);
strcat(new_depot_path, path_sep);
}
strcat(new_depot_path, root_dir);
strcat(new_depot_path, julia_share_subdir);

char *curr_load_path = getenv("JULIA_LOAD_PATH");
int curr_load_path_len = curr_load_path == NULL ? 0 : strlen(curr_load_path);
int new_load_path_len = curr_load_path_len + 1 + share_path_len;
char *new_load_path = calloc(sizeof (char), new_load_path_len);
if (curr_load_path_len > 0) {
strcat(new_load_path, curr_load_path);
strcat(new_load_path, path_sep);
}
strcat(new_load_path, root_dir);
strcat(new_load_path, julia_share_subdir);

#ifdef _WIN32
_putenv_s("JULIA_DEPOT_PATH", share_dir);
_putenv_s("JULIA_LOAD_PATH", share_dir);
_putenv_s("JULIA_DEPOT_PATH", new_depot_path);
_putenv_s("JULIA_LOAD_PATH", new_load_path);
#else
setenv("JULIA_DEPOT_PATH", share_dir, 1);
setenv("JULIA_LOAD_PATH", share_dir, 1);
setenv("JULIA_DEPOT_PATH", new_depot_path, 1);
setenv("JULIA_LOAD_PATH", new_load_path, 1);
#endif
free(share_dir);
free(new_load_path);
free(new_depot_path);
}

void init_julia(int argc, char **argv) {
Expand All @@ -70,8 +92,12 @@ void init_julia(int argc, char **argv) {
set_depot_load_path(root_dir);
free(_sysimage_path);

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR <= 11
jl_options.image_file = sysimage_path;
julia_init(JL_IMAGE_CWD);
#else
jl_init_with_image_file(NULL, sysimage_path);
#endif
}

void shutdown_julia(int retcode) { jl_atexit_hook(retcode); }
1 change: 1 addition & 0 deletions src/library_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const jll_mapping = Dict(
"OpenBLAS_jll" => ["libopenblas64_", "libopenblas"],
"nghttp2_jll" => ["libnghttp2"],
"LibGit2_jll" => ["libgit2"],
"OpenSSL_jll" => ["libcrypto", "libssl"],
"SuiteSparse_jll" => ["libamd", "libbtf", "libcamd", "libccolamd", "libcholmod", "libcolamd", "libklu", "libldl", "librbio", "libspqr", "libsuitesparseconfig", "libumfpack"],
)

Expand Down
9 changes: 8 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ end
rm(joinpath(new_depot, "compiled"); recursive=true, force=true)
rm(joinpath(new_depot, "artifacts"); recursive=true, force=true)
end # try
test_load_path = mktempdir()
test_depot_path = mktempdir()
app_path(app_name) = abspath(app_compiled_dir, "bin", app_name * (Sys.iswindows() ? ".exe" : ""))
app_output = read(`$(app_path("MyApp")) I get --args áéíóú --julia-args --threads=3 --check-bounds=yes -O1`, String)
app_output = withenv("JULIA_DEPOT_PATH" => test_depot_path, "JULIA_LOAD_PATH" => test_load_path) do
read(`$(app_path("MyApp")) I get --args áéíóú --julia-args --threads=3 --check-bounds=yes -O1`, String)
end

# Check stdlib filtering
if filter == true
Expand Down Expand Up @@ -140,6 +144,9 @@ end
# Check app is precompiled in a normal process
@test occursin("outputo: ok", app_output)
@test occursin("myrand: ok", app_output)
# Check env-provided depot and load paths are accepted
@test occursin("DEPOT_PATH = [\"$(escape_string(test_depot_path))", app_output)
@test occursin("LOAD_PATH = [\"$(escape_string(test_load_path))", app_output)
# Check distributed
@test occursin("n = 20000000", app_output)
@test occursin("From worker 2:\t8", app_output)
Expand Down
Loading