@@ -79,51 +79,86 @@ function compile_products(recipe::ImageRecipe)
7979
8080 project_arg = recipe. project == " " ? Base. active_project () : recipe. project
8181 env_overrides = Dict {String,Any} (" JULIA_LOAD_PATH" => nothing )
82+ tmp_prefs_env = nothing
83+ if is_trim_enabled (recipe)
84+ # Create a temporary empty environment with a LocalPreferences.toml that will be added to JULIA_LOAD_PATH.
85+ tmp_prefs_env = mktempdir ()
86+ # It needs to have a Project.toml so it is recognized as an environment
87+ open (joinpath (tmp_prefs_env, " Project.toml" ), " w" ) do io
88+ # Empty project - just needs to exist for LOAD_PATH
89+ end
90+ # Write LocalPreferences.toml with the trim preferences
91+ open (joinpath (tmp_prefs_env, " LocalPreferences.toml" ), " w" ) do io
92+ println (io, " [Preferences]" )
93+ println (io, " trim_enabled = true" )
94+ println (io, " " )
95+ end
96+ # Prepend the temp env to JULIA_LOAD_PATH
97+ env_overrides[" JULIA_LOAD_PATH" ] = " $tmp_prefs_env :@:@stdlib"
98+ end
99+
82100 inst_cmd = addenv (` $(Base. julia_cmd (cpu_target= precompile_cpu_target)) --project=$project_arg -e "using Pkg; Pkg.instantiate(); Pkg.precompile()"` , env_overrides... )
83101 recipe. verbose && println (" Running: $inst_cmd " )
84102 precompile_time = time_ns ()
85- if ! success (pipeline (inst_cmd; stdout , stderr ))
86- error (" Error encountered during instantiate/precompile of app project." )
87- end
88- recipe. verbose && println (" Precompilation took $((time_ns () - precompile_time)/ 1e9 ) s" )
89- # Compile the Julia code
90- if recipe. img_path == " "
91- tmpdir = mktempdir ()
92- recipe. img_path = joinpath (tmpdir, " image.o.a" )
93- end
94- project_arg = recipe. project == " " ? Base. active_project () : recipe. project
95- # Build command incrementally to guarantee proper token separation
96- cmd = julia_cmd
97- cmd = ` $cmd --project=$project_arg $(image_arg) $(recipe. img_path) --output-incremental=no`
98- for a in strip_args
99- cmd = ` $cmd $a `
100- end
101- for a in recipe. julia_args
102- cmd = ` $cmd $a `
103- end
104- cmd = ` $cmd $(joinpath (JuliaC. SCRIPTS_DIR, " juliac-buildscript.jl" )) --scripts-dir $(JuliaC. SCRIPTS_DIR) --source $(abspath (recipe. file)) $(recipe. output_type) `
105- if recipe. add_ccallables
106- cmd = ` $cmd --compile-ccallable`
107- end
108- if recipe. use_loaded_libs
109- cmd = ` $cmd --use-loaded-libs`
110- end
111-
112- # Threading
113- cmd = addenv (cmd, env_overrides... )
114- recipe. verbose && println (" Running: $cmd " )
115- # Show a spinner while the compiler runs
116- spinner_done, spinner_task = _start_spinner (" Compiling..." )
117- compile_time = time_ns ()
118103 try
119- if ! success (pipeline (cmd; stdout , stderr ))
120- error (" Failed to compile $(recipe. file) " )
104+ if ! success (pipeline (inst_cmd; stdout , stderr ))
105+ error (" Error encountered during instantiate/precompile of app project." )
106+ end
107+ recipe. verbose && println (" Precompilation took $((time_ns () - precompile_time)/ 1e9 ) s" )
108+ # Compile the Julia code
109+ if recipe. img_path == " "
110+ tmpdir = mktempdir ()
111+ recipe. img_path = joinpath (tmpdir, " image.o.a" )
112+ end
113+ project_arg = recipe. project == " " ? Base. active_project () : recipe. project
114+ # Build command incrementally to guarantee proper token separation
115+ cmd = julia_cmd
116+ cmd = ` $cmd --project=$project_arg $(image_arg) $(recipe. img_path) --output-incremental=no`
117+ for a in strip_args
118+ cmd = ` $cmd $a `
121119 end
120+ for a in recipe. julia_args
121+ cmd = ` $cmd $a `
122+ end
123+ cmd = ` $cmd $(joinpath (JuliaC. SCRIPTS_DIR, " juliac-buildscript.jl" )) --scripts-dir $(JuliaC. SCRIPTS_DIR) --source $(abspath (recipe. file)) $(recipe. output_type) `
124+ if recipe. add_ccallables
125+ cmd = ` $cmd --compile-ccallable`
126+ end
127+ if recipe. use_loaded_libs
128+ cmd = ` $cmd --use-loaded-libs`
129+ end
130+
131+ # Threading
132+ cmd = addenv (cmd, env_overrides... )
133+ recipe. verbose && println (" Running: $cmd " )
134+ # Show a spinner while the compiler runs
135+ spinner_done, spinner_task = _start_spinner (" Compiling..." )
136+ compile_time = time_ns ()
137+ try
138+ if ! success (pipeline (cmd; stdout , stderr ))
139+ error (" Failed to compile $(recipe. file) " )
140+ end
141+ finally
142+ spinner_done[] = true
143+ wait (spinner_task)
144+ end
145+ recipe. verbose && println (" Compilation took $((time_ns () - compile_time)/ 1e9 ) s" )
122146 finally
123- spinner_done[] = true
124- wait (spinner_task)
147+ # Cleanup LocalPreferences.toml if we created/modified it
148+ if local_prefs_file != = nothing
149+ try
150+ if local_prefs_backup != = nothing
151+ # Restore the original file
152+ write (local_prefs_file, local_prefs_backup)
153+ else
154+ # Remove the file we created
155+ rm (local_prefs_file; force= true )
156+ end
157+ catch e
158+ @warn " Failed to cleanup LocalPreferences.toml: $local_prefs_file " exception= e
159+ end
160+ end
125161 end
126- recipe. verbose && println (" Compilation took $((time_ns () - compile_time)/ 1e9 ) s" )
127162 # Print compiled image size
128163 if recipe. verbose
129164 @assert isfile (recipe. img_path)
0 commit comments