Skip to content

Commit

Permalink
use type declaration for globals on 1.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Sep 28, 2022
1 parent cb51595 commit be826e5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 25 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.8.0"

# We need to glue expressions together a lot
function excat(exs::Union{Expr,Nothing}...)
ex = Expr(:block)
Expand Down
16 changes: 10 additions & 6 deletions src/products/executable_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ function declare_old_executable_product(product_name)
JLLWrappers.withenv_executable_wrapper,
f,
$(Symbol("$(product_name)_path")),
PATH[],
LIBPATH[],
@static($global_typeassert_available ? PATH : PATH[]),
@static($global_typeassert_available ? LIBPATH : LIBPATH[]),
adjust_PATH,
adjust_LIBPATH,
)
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 Expand Up @@ -61,8 +65,8 @@ function declare_new_executable_product(product_name)
env = Base.invokelatest(
JLLWrappers.adjust_ENV!,
copy(ENV),
PATH[],
LIBPATH[],
@static($global_typeassert_available ? PATH : PATH[]),
@static($global_typeassert_available ? LIBPATH : LIBPATH[]),
adjust_PATH,
adjust_LIBPATH,
)
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) = ""
$(path_name) = $(emit_preference_path_load(string(product_name, "_path")))
else
$(product_name)::String = ""
$(path_name)::Union{Nothing,String} = $(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
16 changes: 13 additions & 3 deletions src/toplevel_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ function generate_toplevel_definitions(src_name, __source__)
"""
function is_available end

const PATH = Ref{String}("")
const LIBPATH = Ref{String}("")
@static if $global_typeassert_available
PATH::String = ""
LIBPATH::String = ""
else
const PATH = Ref{String}("")
const LIBPATH = Ref{String}("")
end
# We put these inter-JLL-package API values here so that they are always defined, even if there
# is no underlying wrapper held within this JLL package.
const PATH_list = String[]
Expand Down Expand Up @@ -115,7 +120,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 +206,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
9 changes: 7 additions & 2 deletions src/wrapper_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ macro generate_init_footer()
# Filter out duplicate and empty entries in our PATH and LIBPATH entries
unique!(PATH_list)
unique!(LIBPATH_list)
PATH[] = join(PATH_list, $(pathsep))
LIBPATH[] = join(vcat(LIBPATH_list, Base.invokelatest(JLLWrappers.get_julia_libpaths))::Vector{String}, $(pathsep))
@static if $global_typeassert_available
global PATH = join(PATH_list, $(pathsep))
global LIBPATH = join(vcat(LIBPATH_list, Base.invokelatest(JLLWrappers.get_julia_libpaths))::Vector{String}, $(pathsep))
else
PATH[] = join(PATH_list, $(pathsep))
LIBPATH[] = join(vcat(LIBPATH_list, Base.invokelatest(JLLWrappers.get_julia_libpaths))::Vector{String}, $(pathsep))
end
end)
end

Expand Down
35 changes: 27 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ module TestJLL end
@test isfile(@eval TestJLL Vulkan_Headers_jll.get_vulkan_hpp_path())
end
@test isdir(@eval TestJLL Vulkan_Headers_jll.artifact_dir)
@test isempty(@eval TestJLL Vulkan_Headers_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL Vulkan_Headers_jll.LIBPATH[])
if JLLWrappers.global_typeassert_available
@test isempty(@eval TestJLL Vulkan_Headers_jll.PATH)
@test occursin(Sys.BINDIR, @eval TestJLL Vulkan_Headers_jll.LIBPATH)
else
@test isempty(@eval TestJLL Vulkan_Headers_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL Vulkan_Headers_jll.LIBPATH[])
end

# Package with an ExecutableProduct
Pkg.develop(PackageSpec(path=joinpath(@__DIR__, "HelloWorldC_jll")))
Expand All @@ -54,8 +59,13 @@ module TestJLL end
@test isfile(@eval TestJLL HelloWorldC_jll.hello_world_path)
@test isfile(@eval TestJLL HelloWorldC_jll.get_hello_world_path())
@test isdir(@eval TestJLL HelloWorldC_jll.artifact_dir)
@test !isempty(@eval TestJLL HelloWorldC_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL HelloWorldC_jll.LIBPATH[])
if JLLWrappers.global_typeassert_available
@test !isempty(@eval TestJLL HelloWorldC_jll.PATH)
@test occursin(Sys.BINDIR, @eval TestJLL HelloWorldC_jll.LIBPATH)
else
@test !isempty(@eval TestJLL HelloWorldC_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL HelloWorldC_jll.LIBPATH[])
end
@test !isfile(@eval TestJLL HelloWorldC_jll.goodbye_world_path)
@test !isfile(@eval TestJLL HelloWorldC_jll.get_goodbye_world_path())
@static if VERSION >= v"1.6.0-DEV"
Expand All @@ -73,8 +83,13 @@ module TestJLL end
@test isfile(@eval TestJLL OpenLibm_jll.libopenlibm_path)
@test isfile(@eval TestJLL OpenLibm_jll.get_libopenlibm_path())
@test isdir(@eval TestJLL OpenLibm_jll.artifact_dir)
@test isempty(@eval TestJLL OpenLibm_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL OpenLibm_jll.LIBPATH[])
if JLLWrappers.global_typeassert_available
@test isempty(@eval TestJLL OpenLibm_jll.PATH)
@test occursin(Sys.BINDIR, @eval TestJLL OpenLibm_jll.LIBPATH)
else
@test isempty(@eval TestJLL OpenLibm_jll.PATH[])
@test occursin(Sys.BINDIR, @eval TestJLL OpenLibm_jll.LIBPATH[])
end
@test C_NULL == @eval TestJLL OpenLibm_jll.libnonexisting_handle

@static if VERSION >= v"1.6.0-DEV"
Expand All @@ -101,9 +116,13 @@ module TestJLL end
@test @eval TestJLL LAMMPS_jll.abi == (Sys.iswindows() ? :MicrosoftMPI : :MPICH)
@test isfile(@eval TestJLL LAMMPS_jll.liblammps_path)
@test isfile(@eval TestJLL LAMMPS_jll.get_liblammps_path())
@test isdir(@eval TestJLL LAMMPS_jll.artifact_dir)
@test occursin(Sys.BINDIR, @eval TestJLL LAMMPS_jll.LIBPATH[])

@test isdir(@eval TestJLL LAMMPS_jll.artifact_dir)
if JLLWrappers.global_typeassert_available
@test occursin(Sys.BINDIR, @eval TestJLL LAMMPS_jll.LIBPATH)
else
@test occursin(Sys.BINDIR, @eval TestJLL LAMMPS_jll.LIBPATH[])
end
artifact_dir = @eval TestJLL LAMMPS_jll.artifact_dir

if !Sys.iswindows()
Expand Down

0 comments on commit be826e5

Please sign in to comment.