Skip to content

Commit 6f8cb53

Browse files
author
Janis Erdmanis
committed
refactoring precompilation
1 parent 8170000 commit 6f8cb53

10 files changed

Lines changed: 100 additions & 148 deletions

File tree

recipes/macos/dmg_settings.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

recipes/macos/main.sh

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,7 @@ SCRIPT_DIR=$(dirname "$0")
44
BUNDLE_DIR=$(realpath $(dirname $(dirname $SCRIPT_DIR)))
55

66
APP_NAME={{APP_NAME}}
7-
WITH_SPLASH_SCREEN={{WITH_SPLASH_SCREEN}}
7+
#WITH_SPLASH_SCREEN={{WITH_SPLASH_SCREEN}}
88

9-
JULIA_HOME="$SCRIPT_DIR/julia"
10-
export JULIA="$JULIA_HOME/bin/julia"
11-
12-
if [ -z "$APP_SANDBOX_CONTAINER_ID" ]; then
13-
echo "Running outside SandBox environment"
14-
15-
CACHE_DIR="$HOME/.cache/$APP_NAME"
16-
export USER_DATA="$HOME/.config/$APP_NAME"
17-
else
18-
echo "Running in a SandBox environment"
19-
20-
CACHE_DIR="$HOME/Library/Caches/depot"
21-
export USER_DATA="$HOME/Library/Application Support/Local"
22-
fi
23-
24-
mkdir -p "$USER_DATA"
25-
26-
export JULIA_LOAD_PATH="$SCRIPT_DIR/packages:@stdlib:@"
27-
export JULIA_PROJECT="$SCRIPT_DIR/$APP_NAME"
28-
29-
JULIA_MAIN="$SCRIPT_DIR/$APP_NAME/main.jl"
30-
31-
export JULIA_DEPOT_PATH="$CACHE_DIR:$SCRIPT_DIR/"
32-
33-
PRECOMPILED="$CACHE_DIR/precompiled"
34-
35-
if [ -e $PRECOMPILED ] || [ -d "$SCRIPT_DIR/compiled" ]; then
36-
37-
$JULIA --startup-file=no -L "$SCRIPT_DIR/startup/init.jl" $JULIA_MAIN
38-
39-
else
40-
41-
if $WITH_SPLASH_SCREEN ; then
42-
$JULIA --startup-file=no "$SCRIPT_DIR/startup/configure.jl"
43-
else
44-
$JULIA --startup-file=no "$SCRIPT_DIR/startup/precompile.jl"
45-
fi
46-
47-
mkdir -p $(dirname $PRECOMPILED)
48-
touch "$PRECOMPILED"
49-
open --new $BUNDLE_DIR # This likelly does not work in a sandbox
50-
51-
fi
9+
JULIA="$SCRIPT_DIR/julia/bin/julia"
10+
$JULIA -L "$SCRIPT_DIR/startup/init.jl" "$SCRIPT_DIR/{{MODULE_NAME}}/main.jl"

recipes/macos/precompile.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

recipes/macos/startup.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This file should contain site-specific commands to be executed on Julia startup;
2+
# Users may store their own personal commands in `~/.julia/config/startup.jl`.
3+
4+
if haskey(ENV, "USER_DATA")
5+
println("Using USER_DATA environment variable to run in custom location")
6+
cache_dir = joinpath(ENV["USER_DATA"], "depot")
7+
elseif !haskey(ENV, "APP_SANDBOX_CONTAINER_ID")
8+
println("Running outside SandBox environment")
9+
cache_dir = joinpath(homedir(), ".cache", "{{APP_NAME}}")
10+
ENV["USER_DATA"] = joinpath(homedir(), ".config", "{{APP_NAME}}")
11+
else
12+
println("Running in a SandBox environment")
13+
cache_dir = joinpath(homedir(), "Library", "Caches", "depot")
14+
ENV["USER_DATA"] = joinpath(homedir(), "Library", "Application Support", "Local")
15+
end
16+
17+
libdir = dirname(dirname(dirname(@__DIR__)))
18+
19+
Base.ACTIVE_PROJECT[] = joinpath(libdir, "{{MODULE_NAME}}")
20+
21+
empty!(LOAD_PATH)
22+
push!(LOAD_PATH, joinpath(libdir, "packages"), "@stdlib", "@")
23+
24+
# Modify DEPOT_PATH (equivalent to JULIA_DEPOT_PATH)
25+
empty!(DEPOT_PATH)
26+
push!(DEPOT_PATH, cache_dir, libdir, joinpath(libdir, "julia/share/julia"))
27+
28+
@info "Active project is $(Base.ACTIVE_PROJECT[])"
29+
@info "LOAD_PATH = $LOAD_PATH"
30+
@info "DEPOT_PATH = $DEPOT_PATH"
31+
@info "USER_DATA = $(ENV["USER_DATA"])"
32+
33+
34+
function _precompile()
35+
popfirst!(DEPOT_PATH)
36+
@eval using {{MODULE_NAME}}
37+
end

recipes/startup/init.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mkpath(ENV["USER_DATA"])
2+
13
LOGFILE_PATH = joinpath(ENV["USER_DATA"], "session.log")
24
rm(LOGFILE_PATH, force=true)
35
logfile = open(LOGFILE_PATH, "w")

src/builder.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ using Xorriso_jll: xorriso
33
using rcodesign_jll: rcodesign
44

55

6-
76
function generate_self_signing_pfx(source, destination; password = "PASSWORD")
87

98
run(`$(rcodesign()) generate-self-signed-certificate --person-name="AppBundler" --p12-file="$destination" --p12-password="$password"`)
@@ -13,7 +12,7 @@ end
1312
# MACOS_PFX_PASSWORD = ...
1413
# if unset a self signing certificate could be used instead
1514
# placing it at the meta folder as macos.pfx seems like a good option
16-
function build_app(platform::MacOS, source, destination; compress::Bool = isext(destination, ".dmg"), compression =:lzma, debug = true, precompile = true)
15+
function build_app(platform::MacOS, source, destination; compress::Bool = isext(destination, ".dmg"), compression =:lzma, debug = true, precompile = true, incremental = true)
1716

1817
if precompile && (!Sys.isapple() || (Sys.ARCH == "x86_64" && arch(platform) != Sys.ARCH))
1918
error("Precompilation can only be done on MacOS as currently Julia does not support cross compilation. Set `precompile=false` to make a bundle without precompilation.")
@@ -37,15 +36,26 @@ function build_app(platform::MacOS, source, destination; compress::Bool = isext(
3736

3837
if precompile
3938
@info "Precompiling"
40-
precompile_script = "$app_stage/Contents/MacOS/precompile"
41-
run(`$precompile_script`)
39+
40+
if !incremental
41+
rm("$app_stage/Contents/Libraries/julia/share/julia/compiled", recursive=true)
42+
end
43+
44+
julia = "$app_stage/Contents/Libraries/julia/bin/julia"
45+
startup = "$app_stage/Contents/Libraries/julia/etc/julia/startup.jl"
46+
47+
# Run the command with the modified environment
48+
# withenv("JULIA_DEBUG" => "loading") do
49+
run(`$julia --eval '_precompile()'`)
50+
# end
51+
4252
else
4353
@info "Precompilation disabled. Precompilation will happen on the desitination system at first launch."
4454
end
4555

4656
# I could write tests and check them with thoose ones
4757
run(`find $app_stage -name "._*" -delete`)
48-
rm("$app_stage/Contents/MacOS/precompile")
58+
#rm("$app_stage/Contents/MacOS/precompile")
4959
end
5060

5161
password = get(ENV, "MACOS_PFX_PASSWORD", "")
@@ -57,7 +67,15 @@ function build_app(platform::MacOS, source, destination; compress::Bool = isext(
5767
generate_self_signing_pfx(source, pfx_path; password = "")
5868
end
5969

60-
run(`$(rcodesign()) sign --shallow --p12-file "$pfx_path" --p12-password "$password" --entitlements-xml-path "$app_stage/Contents/Resources/Entitlements.plist" "$app_stage"`)
70+
entitlements_path = joinpath(source, "meta/macos/Entitlements.plist")
71+
if isfile(entitlements_path)
72+
@info "Using entitlements $entitlements_path"
73+
else
74+
@info "No override found at $entitlements_path; using default override"
75+
entitlements_path = joinpath(dirname(@__DIR__), "recipes/macos/Entitlements.plist")
76+
end
77+
78+
run(`$(rcodesign()) sign --shallow --p12-file "$pfx_path" --p12-password "$password" --entitlements-xml-path "$entitlements_path" "$app_stage"`)
6179

6280
if compress
6381
rm(joinpath(dirname(app_stage), "Applications"); force=true)
@@ -72,5 +90,3 @@ function build_app(platform::MacOS, source, destination; compress::Bool = isext(
7290

7391
return
7492
end
75-
76-

src/bundler.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct Rule
66
dest::String
77
template::Bool
88
executable::Bool
9+
override::Bool
910
end
1011

1112
isvalid(rule::Rule, default::String, override::String) = ispath(joinpath(default, rule.origin)) || ispath(joinpath(override, rule.origin))
@@ -19,7 +20,7 @@ end
1920
Bundle(default::String, override::String) = Bundle(default, override, Rule[])
2021

2122
add_rule!(bundle::Bundle, rule::Rule) = push!(bundle.rules, rule)
22-
add_rule!(bundle::Bundle, origin, dest; template=false, executable=false) = add_rule!(bundle, Rule(origin, dest, template, executable))
23+
add_rule!(bundle::Bundle, origin, dest; template=false, executable=false, override=false) = add_rule!(bundle, Rule(origin, dest, template, executable, override))
2324

2425
function issubpath(path::String, subpath::String)
2526

@@ -138,6 +139,8 @@ function get_bundle_parameters(project_toml)
138139

139140
parameters = Dict{String, Any}()
140141

142+
parameters["MODULE_NAME"] = get(toml_dict, "name", "Main")
143+
141144
app_name = haskey(toml_dict, "APP_NAME") ? toml_dict["APP_NAME"] : haskey(toml_dict, "name") ? toml_dict["name"] : basename(dirname(project_toml))
142145
parameters["APP_NAME"] = lowercase(join(split(app_name, " "), "-"))
143146
#parameters["APP_DIR_NAME"] = haskey(toml_dict, "name") ? toml_dict["name"] : basename(dirname(project_toml))

src/deps.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,28 @@ function copy_app(source, destination)
224224
cp(joinpath(source, i), joinpath(destination, i))
225225
end
226226

227+
# Creating a module if it does not exists
228+
229+
toml_dict = TOML.parsefile(joinpath(source, "Project.toml"))
230+
module_name = get(toml_dict, "name", dirname(source))
231+
232+
path = joinpath(destination, "src/$module_name.jl")
233+
234+
if !isfile(path)
235+
236+
mkpath(joinpath(destination, "src"))
237+
238+
dependencies = toml_dict["deps"]
239+
deps = join(["using $i" for i in keys(dependencies)], "\n")
240+
241+
write(path, """
242+
module $module_name
243+
$deps
244+
end
245+
""")
246+
247+
end
248+
227249
return
228250
end
229251

src/recipes.jl

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
# function get_macos_launcher_path(arch)
2-
# return AppBundlerUtils_jll.macos_launcher_path
3-
# end
4-
51
function bundle_app(platform::MacOS, source, destination; with_splash_screen = nothing)
62

73
rm(destination, recursive=true, force=true)
84

95
parameters = get_bundle_parameters("$source/Project.toml")
106
app_name = parameters["APP_NAME"]
7+
module_name = parameters["MODULE_NAME"]
118

129
if isnothing(with_splash_screen)
1310
with_splash_screen = parse(Bool, parameters["WITH_SPLASH_SCREEN"]) # When not set take a value
@@ -24,32 +21,25 @@ function bundle_app(platform::MacOS, source, destination; with_splash_screen = n
2421
add_rule!(bundle, "macos/Resources", "Resources")
2522
add_rule!(bundle, "icon.icns", "Resources/icon.icns")
2623

27-
add_rule!(bundle, "precompile.jl", "Libraries/startup/precompile.jl")
2824
add_rule!(bundle, "startup", "Libraries/startup")
2925

3026
add_rule!(bundle, "macos/main.sh", "Libraries/main", template=true, executable=true)
31-
add_rule!(bundle, "macos/precompile.sh", "MacOS/precompile", template=true, executable=true)
3227
add_rule!(bundle, "macos/Info.plist", "Info.plist", template=true)
3328

34-
add_rule!(bundle, "macos/launcher.c", "Resources/launcher.c")
35-
add_rule!(bundle, "macos/Entitlements.plist", "Resources/Entitlements.plist")
36-
add_rule!(bundle, "macos/dmg_settings.py", "Resources/dmg_settings.py")
37-
29+
mkpath("$app_dir/Libraries")
30+
copy_app(source, "$app_dir/Libraries/$module_name")
31+
retrieve_julia(platform, "$app_dir/Libraries/julia")
32+
33+
add_rule!(bundle, "macos/startup.jl", "$app_dir/Libraries/julia/etc/julia/startup.jl", template=true, override=true)
34+
3835
build(bundle, app_dir, parameters)
3936

40-
copy_app(source, "$app_dir/Libraries/$app_name")
41-
retrieve_julia(platform, "$app_dir/Libraries/julia")
4237
retrieve_packages(source, "$app_dir/Libraries/packages"; with_splash_screen)
4338
retrieve_artifacts(platform, "$app_dir/Libraries/packages", "$app_dir/Libraries/artifacts")
4439

45-
40+
mkdir(joinpath(destination, "Contents/MacOS"))
4641
retrieve_macos_launcher(platform, joinpath(destination, "Contents/MacOS/$app_name"))
4742

48-
# arch = "arm64"
49-
# launcher_path = get_macos_launcher_path(arch)
50-
# cp(launcher_path, joinpath(destination, "Contents/MacOS/$app_name"); force=true)
51-
# chmod(joinpath(destination, "Contents/MacOS/$app_name"), 0o755)
52-
5343
return
5444
end
5545

0 commit comments

Comments
 (0)