Skip to content

Commit 4a7e6d6

Browse files
committed
use @main for juliac executable entry point
1 parent adbe096 commit 4a7e6d6

File tree

7 files changed

+53
-34
lines changed

7 files changed

+53
-34
lines changed

contrib/juliac-buildscript.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,23 @@ end
185185

186186
import Base.Experimental.entrypoint
187187

188-
let mod = Base.include(Base.__toplevel__, inputfile)
189-
if !isa(mod, Module)
190-
mod = Main
191-
end
188+
let mod = Base.include(Main, inputfile)
192189
Core.@latestworld
193-
if output_type == "--output-exe" && isdefined(mod, :main) && !add_ccallables
194-
entrypoint(mod.main, ())
190+
if output_type == "--output-exe"
191+
if Base.should_use_main_entrypoint()
192+
if hasmethod(Main.main, Tuple{Vector{String}})
193+
mainsig = which(Main.main, (Vector{String},)).sig
194+
if mainsig != Tuple{typeof(Main.main), Vector{String}}
195+
error("The argument to `@main` must be declared as `::Vector{String}`.")
196+
end
197+
entrypoint(Main.main, (Vector{String},))
198+
Base._ccallable("juliac_main", Cint, mainsig)
199+
else
200+
error("`@main` must accept a `Vector{String}` argument.")
201+
end
202+
else
203+
error("To generate an executable a `@main` function must be defined.")
204+
end
195205
end
196206
#entrypoint(join, (Base.GenericIOBuffer{Memory{UInt8}}, Array{Base.SubString{String}, 1}, String))
197207
#entrypoint(join, (Base.GenericIOBuffer{Memory{UInt8}}, Array{String, 1}, Char))

contrib/juliac.jl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,33 @@ function compile_products(enable_trim::Bool)
123123
end
124124

125125
# Compile the initialization code
126-
open(initsrc_path, "w") do io
127-
print(io, """
128-
#include <julia.h>
129-
__attribute__((constructor)) void static_init(void) {
130-
if (jl_is_initialized())
131-
return;
132-
julia_init(JL_IMAGE_IN_MEMORY);
133-
jl_exception_clear();
134-
}
135-
""")
126+
if output_type == "--output-exe"
127+
open(initsrc_path, "w") do io
128+
print(io, """
129+
#include <julia.h>
130+
int juliac_main(void*);
131+
int main(int argc, char *argv[])
132+
{
133+
julia_init(JL_IMAGE_IN_MEMORY);
134+
jl_exception_clear();
135+
jl_set_ARGS(argc, argv);
136+
return juliac_main(jl_get_global(jl_core_module, jl_symbol("ARGS")));
137+
}
138+
""")
139+
end
140+
else
141+
open(initsrc_path, "w") do io
142+
print(io, """
143+
#include <julia.h>
144+
__attribute__((constructor)) void static_init(void)
145+
{
146+
if (jl_is_initialized())
147+
return;
148+
julia_init(JL_IMAGE_IN_MEMORY);
149+
jl_exception_clear();
150+
}
151+
""")
152+
end
136153
end
137154
run(`cc $(cflags) -g -c -o $init_path $initsrc_path`)
138155
end

src/gf.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,8 +4616,6 @@ JL_DLLEXPORT void jl_extern_c(jl_value_t *name, jl_value_t *declrt, jl_tupletype
46164616
jl_error("@ccallable: function object must be a singleton");
46174617

46184618
// compute / validate return type
4619-
if (!jl_is_concrete_type(declrt) || jl_is_kind(declrt))
4620-
jl_error("@ccallable: return type must be concrete and correspond to a C type");
46214619
if (!jl_type_mappable_to_c(declrt))
46224620
jl_error("@ccallable: return type doesn't correspond to a C type");
46234621

src/jlapi.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ JL_DLLEXPORT void jl_set_ARGS(int argc, char **argv)
5353
if (jl_core_module != NULL) {
5454
jl_array_t *args = (jl_array_t*)jl_get_global(jl_core_module, jl_symbol("ARGS"));
5555
if (args == NULL) {
56-
args = jl_alloc_vec_any(0);
57-
JL_GC_PUSH1(&args);
56+
jl_value_t *vecstr = NULL;
57+
JL_GC_PUSH2(&args, &vecstr);
58+
vecstr = jl_apply_array_type((jl_value_t*)jl_string_type, 1);
59+
args = jl_alloc_array_1d(vecstr, 0);
5860
jl_set_const(jl_core_module, jl_symbol("ARGS"), (jl_value_t*)args);
5961
JL_GC_POP();
6062
}

test/trimming/basic_jll.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
module MyApp
2-
31
using Libdl
42
using Zstd_jll
53

6-
Base.@ccallable function main()::Cint
4+
function @main(args::Vector{String})::Cint
75
println(Core.stdout, "Julia! Hello, world!")
86
fptr = dlsym(Zstd_jll.libzstd_handle, :ZSTD_versionString)
97
println(Core.stdout, unsafe_string(ccall(fptr, Cstring, ())))
108
println(Core.stdout, unsafe_string(ccall((:ZSTD_versionString, libzstd), Cstring, ())))
119
return 0
1210
end
13-
14-
end

test/trimming/hello.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
module MyApp
2-
31
world::String = "world!"
42
const str = OncePerProcess{String}() do
53
return "Hello, " * world
64
end
75

8-
Base.@ccallable function main()::Cint
6+
function @main(args::Vector{String})::Cint
97
println(Core.stdout, str())
108
return 0
119
end
12-
13-
end

test/trimming/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <julia.h>
2-
3-
__attribute__((constructor)) void static_init(void)
2+
int juliac_main(void*);
3+
int main(int argc, char *argv[])
44
{
5-
if (jl_is_initialized())
6-
return;
75
julia_init(JL_IMAGE_IN_MEMORY);
86
jl_exception_clear();
7+
jl_set_ARGS(argc, argv);
8+
return juliac_main(jl_get_global(jl_core_module, jl_symbol("ARGS")));
99
}

0 commit comments

Comments
 (0)