Skip to content
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

adding an option to use include instead of using to avoid precompilation #5

Open
thautwarm opened this issue Sep 14, 2022 · 1 comment
Assignees

Comments

@thautwarm
Copy link
Member

jnumpy.init_project(__file__)

@thautwarm
Copy link
Member Author

thautwarm commented Sep 14, 2022

@songjhaha Sysimage-compatible using:

module ImageUsing

if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@compiler_options"))
    @eval Base.Experimental.@compiler_options compile=min optimize=0 infer=no
end

exported_names(m::Module) = filter!(x -> Base.isexported(m, x), names(m; all=true, imported=true))

function _eval_export(__module__::Module, x::Any)
    if x isa Symbol
        return getfield(__module__, x)
    elseif x isa Expr
        if x.head == :. && length(x.args) == 2 && x.args[2] isa QuoteNode && x.args[2].value isa Symbol
            v = _eval_export(__module__, x.args[1])
            return getfield(v, x.args[2].value)
        else
            error("invalid export: $x")
        end
    else
        error("invalid export: $x")
    end
end

"""
    `@image_using A.B`
an alternative to using to compat loading packages from custom sysimages.
"""
macro image_using(sections)
    try
        M = _eval_export(Main, sections) :: Module
        ns = exported_names(M)
        block = Expr(:block)
        for n in ns
            if !isdefined(__module__, n)
                push!(block.args, :(const $n = $M.$n))
            end
        end
        esc(block)
    catch
        esc(Meta.parse("using " * string(sections)))
    end
end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants