Skip to content

Commit c2bec03

Browse files
committed
Move work to compile time
Previously, we were loading preferences within `__init__()`; move it out to top-level statements so that the preferences are loaded at compile time and baked into the `.ji` files.
1 parent 0ccd118 commit c2bec03

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/products/executable_generators.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,27 @@ function declare_new_executable_product(product_name)
4949
end
5050

5151
macro declare_executable_product(product_name)
52+
path_name = string(product_name, "_path")
5253
return excat(
5354
# We will continue to support `withenv`-style for as long as we must
5455
declare_old_executable_product(product_name),
5556
# We will, however, urge users to move to the thread-safe `addenv`-style on Julia 1.6+
5657
declare_new_executable_product(product_name),
58+
# Perform a compile-time load of a path preference override
59+
:($(Symbol(path_name)) = $(emit_preference_path_load(path_name))),
5760
)
5861
end
5962

6063
macro init_executable_product(product_name, product_path)
61-
preference_name = string(product_name, "_path")
62-
path_name = Symbol(preference_name)
64+
path_name = Symbol(string(product_name, "_path"))
6365
return esc(quote
66+
global $(path_name)
6467
# Locate the executable on-disk, store into $(path_name)
65-
global $(path_name) = $(emit_preference_path_load(preference_name, product_path))
68+
if $(path_name) === nothing
69+
$(path_name) = joinpath(artifact_dir, $(product_path))
70+
end
6671

6772
# Add this executable's directory onto the list of PATH's that we'll need to expose to dependents
68-
push!(PATH_list, joinpath(artifact_dir, $(dirname(product_path))))
73+
push!(PATH_list, dirname($(path_name)))
6974
end)
7075
end

src/products/file_generators.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ macro declare_file_product(product_name)
33
path_name = Symbol(string(product_name, "_path"))
44
return esc(quote
55
# These will be filled in by init_file_product().
6-
$(path_name) = ""
6+
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
77
$(product_name) = ""
88
function $(get_path_name)()
99
return $(path_name)::String
@@ -12,11 +12,13 @@ macro declare_file_product(product_name)
1212
end
1313

1414
macro init_file_product(product_name, product_path)
15-
preference_name = string(product_name, "_path")
16-
path_name = Symbol(preference_name)
15+
path_name = Symbol(string(product_name, "_path"))
1716
return esc(quote
17+
global $(path_name)
1818
# FileProducts are very simple, and we maintain the `_path` suffix version for consistency
19-
global $(path_name) = $(emit_preference_path_load(preference_name, product_path))
19+
if $(path_name) === nothing
20+
$(path_name) = joinpath(artifact_dir, $(product_path))
21+
end
2022
global $(product_name) = $(path_name)
2123
end)
2224
end

src/products/library_generators.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro declare_library_product(product_name, product_soname)
1818
quote
1919
# These will be filled in by init_library_product()
2020
$(handle_name) = C_NULL
21-
$(path_name) = ""
21+
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
2222
function $(get_path_name)()
2323
return $(path_name)::String
2424
end
@@ -43,7 +43,10 @@ macro init_library_product(product_name, product_path, dlopen_flags)
4343
preference_name = string(product_name, "_path")
4444
path_name = Symbol(preference_name)
4545
return excat(quote
46-
global $(path_name) = $(emit_preference_path_load(preference_name, product_path))
46+
global $(path_name)
47+
if $(path_name) === nothing
48+
$(path_name) = joinpath(artifact_dir, $(product_path))
49+
end
4750
# Manually `dlopen()` this right now so that future invocations
4851
# of `ccall` with its path/SONAME will find this path immediately.
4952
# dlopen_flags === nothing means to not dlopen the library.

src/wrapper_generators.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ end
5555
emit_preference_path_load(pref_name, default_value)
5656
5757
On Julia 1.6+, emits a `load_preference()` call for the given preference name,
58-
with the given default value. On Julia 1.5-, simply emits the default value.
58+
returning `nothing` if it is not loaded. On Julia v1.5-, always returns `nothing`.
5959
"""
60-
function emit_preference_path_load(pref_name, product_path)
60+
function emit_preference_path_load(pref_name)
6161
# Can't use `Preferences.jl` on older Julias, just always use the default value in that case
6262
@static if VERSION < v"1.6.0-DEV"
6363
return quote
64-
joinpath(artifact_dir, $(product_path))
64+
nothing
6565
end
6666
else
6767
return quote
68-
@load_preference($(pref_name), joinpath(artifact_dir, $(product_path)))
68+
@load_preference($(pref_name), nothing)
6969
end
7070
end
7171
end

0 commit comments

Comments
 (0)