Skip to content

Commit

Permalink
allow building sysimg seperately
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Jul 28, 2020
1 parent 3241358 commit ea8926f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 27 deletions.
99 changes: 72 additions & 27 deletions src/build.jl
Expand Up @@ -2,7 +2,12 @@ module PATH
using Libdl
using ..Comonicon

project(m::Module, xs...) = joinpath(dirname(dirname(pathof(m))), xs...)
function project(m::Module, xs...)
path = pathof(m)
path === nothing && return
return joinpath(dirname(dirname(path)), xs...)
end

project(xs...) = project(Comonicon, xs...)

"""
Expand All @@ -20,6 +25,16 @@ function default_sysimg()
end
end

function default_sysimg(mod, name)
lib = project(mod, "deps", "lib", "lib$name.$(Libdl.dlext)")
if lib !== nothing && isfile(lib)
return lib
else
# fallback to try Comonicon sysimg
return default_sysimg()
end
end

"""
default_exename()
Expand Down Expand Up @@ -121,8 +136,9 @@ Install the CLI defined in module `mod` to the `bin`.
- `compile`: julia compile level, can be [:yes, :no, :all, :min]
- `optimize`: julia optimization level, default is 2.
- `sysimg`: compile a system image using PackageCompiler or not, default is `false`.
- `incremental`: PackageCompiler option.
- `filter_stdlibs`: PackageCompiler option.
- `incremental`: PackageCompiler option, default is `false`.
- `filter_stdlibs`: PackageCompiler option, default is `false`.
- `cpu_target`: PackageCompiler option. default is "native".
- `lib_path`: the `lib` folder that stores the compiled system image, default is the `<project>/deps/lib`.
"""
function install(
Expand All @@ -132,39 +148,25 @@ function install(
exename = PATH.default_exename(),
project = PATH.project(mod),
sysimg::Bool = false,
sysimg_path = PATH.default_sysimg(mod, name),
incremental::Bool = false,
compile = nothing,
filter_stdlibs = false,
lib_path = PATH.project(mod, "deps", "lib"),
cpu_target = "native",
optimize = 2,
)

if sysimg
if !ispath(lib_path)
@info "creating library path: $lib_path"
mkpath(lib_path)
end

precompile_jl = PATH.project(mod, "deps", "precompile.jl")
@info "generating precompile execution file: $precompile_jl"
open(precompile_jl, "w+") do f
print(f, precompile_script(mod))
end

@info "compile under project: $project"
@info "incremental: $incremental"
@info "filter stdlibs: $filter_stdlibs"
sysimg_path = PATH.project(mod, "deps", "lib", "lib$name.$(Libdl.dlext)")
create_sysimage(
nameof(mod);
sysimage_path = sysimg_path,
incremental = incremental,
project = project,
precompile_execution_file = precompile_jl,
filter_stdlibs = filter_stdlibs,
build(mod, name;
sysimg_path=sysimg_path,
incremental=incremental,
compile=compile,
filter_stdlibs=filter_stdlibs,
lib_path=lib_path,
cpu_target=cpu_target,
optimize=optimize
)
else
sysimg_path = nothing
end

shadow = joinpath(bin, name * ".jl")
Expand Down Expand Up @@ -214,6 +216,49 @@ function Base.write(io::IO, x::EntryCommand)
println(io, "command_main()")
end

"""
build(mod[, name=default_name(mod)]; kwargs...)
Build system image for given CLI module. See also [`install`](@ref).
"""
function build(
mod, name=default_name(mod);
sysimg_path=PATH.default_sysimg(mod, name),
project = PATH.project(mod),
sysimg::Bool = false,
incremental::Bool = false,
compile = nothing,
filter_stdlibs = false,
lib_path = PATH.project(mod, "deps", "lib"),
cpu_target = "native",
optimize = 2,
)

if !ispath(lib_path)
@info "creating library path: $lib_path"
mkpath(lib_path)
end

precompile_jl = PATH.project(mod, "deps", "precompile.jl")
@info "generating precompile execution file: $precompile_jl"
open(precompile_jl, "w+") do f
print(f, precompile_script(mod))
end

@info "compile under project: $project"
@info "incremental: $incremental"
@info "filter stdlibs: $filter_stdlibs"
create_sysimage(
nameof(mod);
sysimage_path = sysimg_path,
incremental = incremental,
project = project,
precompile_execution_file = precompile_jl,
cpu_target = cpu_target,
filter_stdlibs = filter_stdlibs,
)
end

"""
build()
Expand Down
4 changes: 4 additions & 0 deletions test/parse.jl
Expand Up @@ -114,3 +114,7 @@ ArgParse example implemented in Comonicon.
@test opt1 == "3"
@test opt2 == 2
end

Comonicon.install(Dummy; bin=Comonicon.PATH.project("test", "bin"))
@test isfile(Comonicon.PATH.project("test", "bin", "dummy"))
@test isfile(Comonicon.PATH.project("test", "bin", "dummy.jl"))

0 comments on commit ea8926f

Please sign in to comment.