Skip to content

Commit a9af8ed

Browse files
authored
embedding_wrapper.c and julia_init.c: Use JULIA_DEPOT_PATH and JULIA_LOAD_PATH from the environment (#1032)
* Use `JULIA_DEPOT_PATH` and `JULIA_LOAD_PATH` from the environment When these environment variables are set, use them.
1 parent 57b988f commit a9af8ed

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

src/embedding_wrapper.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,45 @@ jl_value_t *checked_eval_string(const char *code) {
3737

3838
void set_depot_load_path(const char *root_dir) {
3939
#ifdef _WIN32
40+
char *path_sep = ";";
4041
char *julia_share_subdir = "\\share\\julia";
4142
#else
43+
char *path_sep = ":";
4244
char *julia_share_subdir = "/share/julia";
4345
#endif
44-
char *share_dir =
45-
calloc(sizeof(char), strlen(root_dir) + strlen(julia_share_subdir) + 1);
46-
strcat(share_dir, root_dir);
47-
strcat(share_dir, julia_share_subdir);
46+
int share_path_len = strlen(root_dir) + strlen(julia_share_subdir) + 1;
47+
48+
char *curr_depot_path = getenv("JULIA_DEPOT_PATH");
49+
int curr_depot_path_len = curr_depot_path == NULL ? 0 : strlen(curr_depot_path);
50+
int new_depot_path_len = curr_depot_path_len + 1 + share_path_len;
51+
char *new_depot_path = calloc(sizeof (char), new_depot_path_len);
52+
if (curr_depot_path_len > 0) {
53+
strcat(new_depot_path, curr_depot_path);
54+
strcat(new_depot_path, path_sep);
55+
}
56+
strcat(new_depot_path, root_dir);
57+
strcat(new_depot_path, julia_share_subdir);
58+
59+
char *curr_load_path = getenv("JULIA_LOAD_PATH");
60+
int curr_load_path_len = curr_load_path == NULL ? 0 : strlen(curr_load_path);
61+
int new_load_path_len = curr_load_path_len + 1 + share_path_len;
62+
char *new_load_path = calloc(sizeof (char), new_load_path_len);
63+
if (curr_load_path_len > 0) {
64+
strcat(new_load_path, curr_load_path);
65+
strcat(new_load_path, path_sep);
66+
}
67+
strcat(new_load_path, root_dir);
68+
strcat(new_load_path, julia_share_subdir);
4869

4970
#ifdef _WIN32
50-
_putenv_s("JULIA_DEPOT_PATH", share_dir);
51-
_putenv_s("JULIA_LOAD_PATH", share_dir);
71+
_putenv_s("JULIA_DEPOT_PATH", new_depot_path);
72+
_putenv_s("JULIA_LOAD_PATH", new_load_path);
5273
#else
53-
setenv("JULIA_DEPOT_PATH", share_dir, 1);
54-
setenv("JULIA_LOAD_PATH", share_dir, 1);
74+
setenv("JULIA_DEPOT_PATH", new_depot_path, 1);
75+
setenv("JULIA_LOAD_PATH", new_load_path, 1);
5576
#endif
77+
free(new_load_path);
78+
free(new_depot_path);
5679
}
5780

5881
// main function (windows UTF16 -> UTF8 argument conversion code copied from

src/julia_init.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,45 @@ const char *get_sysimage_path(const char *libname) {
4242

4343
void set_depot_load_path(const char *root_dir) {
4444
#ifdef _WIN32
45+
char *path_sep = ";";
4546
char *julia_share_subdir = "\\share\\julia";
4647
#else
48+
char *path_sep = ":";
4749
char *julia_share_subdir = "/share/julia";
4850
#endif
49-
char *share_dir =
50-
calloc(sizeof(char), strlen(root_dir) + strlen(julia_share_subdir) + 1);
51-
strcat(share_dir, root_dir);
52-
strcat(share_dir, julia_share_subdir);
51+
int share_path_len = strlen(root_dir) + strlen(julia_share_subdir) + 1;
52+
53+
char *curr_depot_path = getenv("JULIA_DEPOT_PATH");
54+
int curr_depot_path_len = curr_depot_path == NULL ? 0 : strlen(curr_depot_path);
55+
int new_depot_path_len = curr_depot_path_len + 1 + share_path_len;
56+
char *new_depot_path = calloc(sizeof (char), new_depot_path_len);
57+
if (curr_depot_path_len > 0) {
58+
strcat(new_depot_path, curr_depot_path);
59+
strcat(new_depot_path, path_sep);
60+
}
61+
strcat(new_depot_path, root_dir);
62+
strcat(new_depot_path, julia_share_subdir);
63+
64+
char *curr_load_path = getenv("JULIA_LOAD_PATH");
65+
int curr_load_path_len = curr_load_path == NULL ? 0 : strlen(curr_load_path);
66+
int new_load_path_len = curr_load_path_len + 1 + share_path_len;
67+
char *new_load_path = calloc(sizeof (char), new_load_path_len);
68+
if (curr_load_path_len > 0) {
69+
strcat(new_load_path, curr_load_path);
70+
strcat(new_load_path, path_sep);
71+
}
72+
strcat(new_load_path, root_dir);
73+
strcat(new_load_path, julia_share_subdir);
5374

5475
#ifdef _WIN32
55-
_putenv_s("JULIA_DEPOT_PATH", share_dir);
56-
_putenv_s("JULIA_LOAD_PATH", share_dir);
76+
_putenv_s("JULIA_DEPOT_PATH", new_depot_path);
77+
_putenv_s("JULIA_LOAD_PATH", new_load_path);
5778
#else
58-
setenv("JULIA_DEPOT_PATH", share_dir, 1);
59-
setenv("JULIA_LOAD_PATH", share_dir, 1);
79+
setenv("JULIA_DEPOT_PATH", new_depot_path, 1);
80+
setenv("JULIA_LOAD_PATH", new_load_path, 1);
6081
#endif
61-
free(share_dir);
82+
free(new_load_path);
83+
free(new_depot_path);
6284
}
6385

6486
void init_julia(int argc, char **argv) {

test/runtests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ end
110110
rm(joinpath(new_depot, "compiled"); recursive=true, force=true)
111111
rm(joinpath(new_depot, "artifacts"); recursive=true, force=true)
112112
end # try
113+
test_load_path = mktempdir()
114+
test_depot_path = mktempdir()
113115
app_path(app_name) = abspath(app_compiled_dir, "bin", app_name * (Sys.iswindows() ? ".exe" : ""))
114-
app_output = read(`$(app_path("MyApp")) I get --args áéíóú --julia-args --threads=3 --check-bounds=yes -O1`, String)
116+
app_output = withenv("JULIA_DEPOT_PATH" => test_depot_path, "JULIA_LOAD_PATH" => test_load_path) do
117+
read(`$(app_path("MyApp")) I get --args áéíóú --julia-args --threads=3 --check-bounds=yes -O1`, String)
118+
end
115119

116120
# Check stdlib filtering
117121
if filter == true
@@ -140,6 +144,9 @@ end
140144
# Check app is precompiled in a normal process
141145
@test occursin("outputo: ok", app_output)
142146
@test occursin("myrand: ok", app_output)
147+
# Check env-provided depot and load paths are accepted
148+
@test occursin("DEPOT_PATH = [\"$(escape_string(test_depot_path))", app_output)
149+
@test occursin("LOAD_PATH = [\"$(escape_string(test_load_path))", app_output)
143150
# Check distributed
144151
@test occursin("n = 20000000", app_output)
145152
@test occursin("From worker 2:\t8", app_output)

0 commit comments

Comments
 (0)