Skip to content

Commit

Permalink
use type declaration for globals on 1.9+ (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Carlsson authored Oct 19, 2022
1 parent 9ae1b7c commit f6f6abe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/JLLWrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ if VERSION >= v"1.6.0-DEV"
using Preferences
end

const global_typeassert_available = VERSION >= v"1.9.0"

# We need to glue expressions together a lot
function excat(exs::Union{Expr,Nothing}...)
ex = Expr(:block)
Expand Down
8 changes: 6 additions & 2 deletions src/products/executable_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ function declare_old_executable_product(product_name)
)
end

# This will eventually be replaced with a `Ref{String}`
$(path_name) = ""

@static if $global_typeassert_available
$(path_name)::Union{String,Nothing} = ""
else
$(path_name) = ""
end
function $(Symbol(string("get_", product_name, "_path")))()
return $(path_name)::String
end
Expand Down
9 changes: 7 additions & 2 deletions src/products/file_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ macro declare_file_product(product_name)
path_name = Symbol(string(product_name, "_path"))
return esc(quote
# These will be filled in by init_file_product().
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
$(product_name) = ""
@static if $global_typeassert_available
$(product_name)::String = ""
$(path_name)::Union{Nothing,String} = $(emit_preference_path_load(string(product_name, "_path")))
else
$(product_name) = ""
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
end
function $(get_path_name)()
return $(path_name)::String
end
Expand Down
17 changes: 13 additions & 4 deletions src/products/library_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ macro declare_library_product(product_name, product_soname)
else
lib_declaration = quote
# On Julia 1.6+, this doesn't have to be `const`! Thanks Jeff!
$(product_name) = ""
@static if $global_typeassert_available
$(product_name)::String = ""
else
$(product_name) = ""
end
end
end

return excat(
quote
# These will be filled in by init_library_product()
$(handle_name) = C_NULL
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
@static if $global_typeassert_available
$(handle_name)::Ptr{Cvoid} = C_NULL
$(path_name)::Union{Nothing,String} = $(emit_preference_path_load(string(product_name, "_path")))
else
$(handle_name) = C_NULL
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
end
function $(get_path_name)()
return $(path_name)::String
end
Expand Down
7 changes: 6 additions & 1 deletion src/toplevel_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ function generate_wrapper_load(src_name, pkg_uuid, __source__)
end

return quote
global best_wrapper
@static if $global_typeassert_available
global best_wrapper::Union{Nothing,String}
else
global best_wrapper
end
# Load Artifacts.toml file and select best platform at compile-time, since this is
# running at toplevel, and therefore will be run completely at compile-time. We use
# a `let` block here to avoid storing unnecessary data in our `.ji` files
Expand Down Expand Up @@ -197,6 +201,7 @@ macro generate_main_file_header(src_name)
generate_compiler_options(src_name),
# import Artifacts module
generate_imports(src_name),
global_typeassert_available ? :(global artifact_dir::String) : :()
)
end

Expand Down

0 comments on commit f6f6abe

Please sign in to comment.