forked from JuliaComputing/static-julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
juliac.jl
68 lines (56 loc) · 2.32 KB
/
juliac.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## Assumptions:
## 1. gcc / x86_64-w64-mingw32-gcc is available and is in path
function julia_compile(julia_program_file)
julia_program_file = abspath(julia_program_file)
if !isfile(julia_program_file)
error("Cannot find file: \"$julia_program_file\"")
end
dir_name = dirname(julia_program_file)
file_name = splitext(basename(julia_program_file))[1]
O_FILE = "$file_name.o"
SO_FILE = "lib$file_name.$(Libdl.dlext)"
C_FILE = joinpath(@__DIR__, "program.c")
E_FILE = file_name * (is_windows() ? ".exe" : "")
build_dir = joinpath(dir_name, "builddir")
if !isdir(build_dir)
println("Make directory:\n\"$build_dir\"")
mkdir(build_dir)
end
if pwd() != build_dir
println("Change directory:\n\"$build_dir\"")
cd(build_dir)
end
julia_pkglibdir = joinpath(dirname(Pkg.dir()), "lib", basename(Pkg.dir()))
if is_windows()
julia_program_file = replace(julia_program_file, "\\", "\\\\")
julia_pkglibdir = replace(julia_pkglibdir, "\\", "\\\\")
end
command = `"$(Base.julia_cmd())" "--startup-file=no" "--output-o" "$O_FILE" "-e"
"include(\"$julia_program_file\"); push!(Base.LOAD_CACHE_PATH, \"$julia_pkglibdir\"); empty!(Base.LOAD_CACHE_PATH)"`
println("Running command:\n$command")
run(command)
command = `$(Base.julia_cmd()) $(joinpath(dirname(JULIA_HOME), "share", "julia", "julia-config.jl"))`
cflags = Base.shell_split(readstring(`$command --cflags`))
ldflags = Base.shell_split(readstring(`$command --ldflags`))
ldlibs = Base.shell_split(readstring(`$command --ldlibs`))
cc = is_windows() ? "x86_64-w64-mingw32-gcc" : "gcc"
command = `$cc -m64 -shared -o $SO_FILE $O_FILE $cflags $ldflags $ldlibs -Wl,-rpath,\$ORIGIN`
if is_windows()
command = `$command -Wl,--export-all-symbols`
end
println("Running command:\n$command")
run(command)
command = `$cc -m64 $C_FILE -o $E_FILE $SO_FILE $cflags $ldflags $ldlibs -Wl,-rpath,\$ORIGIN`
println("Running command:\n$command")
run(command)
end
function main()
if length(ARGS) != 1
println("Usage: $(basename(@__FILE__)) <Julia Program file>")
exit(1)
end
JULIA_PROGRAM_FILE = ARGS[1]
println("Program file:\n$(abspath(JULIA_PROGRAM_FILE))")
julia_compile(JULIA_PROGRAM_FILE)
end
main()