11# Script to run in the process that generates juliac's object file output
22
3- inputfile = ARGS [1 ]
4- output_type = ARGS [2 ]
5- add_ccallables = ARGS [3 ] == " true"
6-
73# Run the verifier in the current world (before modifications), so that error
84# messages and types print in their usual way.
95Core. Compiler. _verify_trim_world_age[] = Base. get_world_counter ()
@@ -189,13 +185,37 @@ end
189185
190186import Base. Experimental. entrypoint
191187
192- let mod = Base. include (Base. __toplevel__, inputfile)
193- if ! isa (mod, Module)
194- mod = Main
195- end
188+ # for use as C main if needed
189+ function _main (argc:: Cint , argv:: Ptr{Ptr{Cchar}} ):: Cint
190+ args = ccall (:jl_set_ARGS , Any, (Cint, Ptr{Ptr{Cchar}}), argc, argv):: Vector{String}
191+ return Main. main (args)
192+ end
193+
194+ let mod = Base. include (Main, ARGS [1 ])
196195 Core. @latestworld
197- if output_type == " --output-exe" && isdefined (mod, :main ) && ! add_ccallables
198- entrypoint (mod. main, ())
196+ if ARGS [2 ] == " --output-exe"
197+ have_cmain = false
198+ if isdefined (Main, :main )
199+ for m in methods (Main. main)
200+ if isdefined (m, :ccallable )
201+ # TODO : possibly check signature and return type
202+ have_cmain = true
203+ break
204+ end
205+ end
206+ end
207+ if ! have_cmain
208+ if Base. should_use_main_entrypoint ()
209+ if hasmethod (Main. main, Tuple{Vector{String}})
210+ entrypoint (_main, (Cint, Ptr{Ptr{Cchar}}))
211+ Base. _ccallable (" main" , Cint, Tuple{typeof (_main), Cint, Ptr{Ptr{Cchar}}})
212+ else
213+ error (" `@main` must accept a `Vector{String}` argument." )
214+ end
215+ else
216+ error (" To generate an executable a `@main` function must be defined." )
217+ end
218+ end
199219 end
200220 # entrypoint(join, (Base.GenericIOBuffer{Memory{UInt8}}, Array{Base.SubString{String}, 1}, String))
201221 # entrypoint(join, (Base.GenericIOBuffer{Memory{UInt8}}, Array{String, 1}, Char))
@@ -204,7 +224,7 @@ let mod = Base.include(Base.__toplevel__, inputfile)
204224 entrypoint (Base. wait_forever, ())
205225 entrypoint (Base. trypoptask, (Base. StickyWorkqueue,))
206226 entrypoint (Base. checktaskempty, ())
207- if add_ccallables
227+ if ARGS [ 3 ] == " true "
208228 ccall (:jl_add_ccallable_entrypoints , Cvoid, ())
209229 end
210230end
0 commit comments