forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_executable.jl
278 lines (237 loc) · 9.26 KB
/
build_executable.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env julia
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Builds an executable that doesn't require any julia source code.
# The user needs to provide a julia script that contains a function main(),
# taking no argument, which will be run when executing the
# produced executable.
# Note on packages:
# Even if the script contains using statements, exported functions
# will not be available in main(). Full qualification of names is
# required. It's suggested to replace using statements with import
# statements to produce a consistent result between running main() in
# the REPL and running the executable.
# Windows note:
# gcc is required to compile the program. gcc can be installed using WinRPM.
# $ Pkg.add("WinRPM")
# $ using WinRPM
# $ WinRPM.install("gcc")
type Executable
name
filename
buildfile
targetfile
libjulia
end
function Executable(exename, targetdir, debug)
if debug
exename = exename * "-debug"
end
filename = exename
@windows_only filename = filename * ".exe"
buildfile = abspath(joinpath(JULIA_HOME, filename))
targetfile = targetdir == nothing ? buildfile : joinpath(targetdir, filename)
libjulia = debug ? "-ljulia-debug" : "-ljulia"
Executable(exename, filename, buildfile, targetfile, libjulia)
end
type SysFile
buildpath
buildfile
buildfile0
end
function SysFile(exename)
buildpath = abspath(dirname(Libdl.dlpath("libjulia")))
buildfile = joinpath(buildpath, "lib"*exename)
buildfile0 = joinpath(buildpath, "sys0")
SysFile(buildpath, buildfile, buildfile0)
end
function build_executable(exename, script_file, targetdir=nothing, cpu_target="native"; force=false)
julia = abspath(joinpath(JULIA_HOME, "julia"))
build_sysimg = abspath(joinpath(dirname(@__FILE__), "build_sysimg.jl"))
if targetdir != nothing
patchelf = find_patchelf()
if patchelf == nothing && !(OS_NAME == :Windows)
println("ERROR: Using the 'targetdir' option requires the 'patchelf' utility. Please install it.")
return 1
end
end
if !isfile(script_file)
println("ERROR: $(script_file) not found.")
return 1
end
tmpdir = mktempdir()
cfile = joinpath(tmpdir, "start_func.c")
userimgjl = joinpath(tmpdir, "userimg.jl")
script_file = abspath(script_file)
if targetdir != nothing
targetdir = abspath(targetdir)
if !isdir(targetdir)
println("ERROR: targetdir is not a directory.")
return 1
end
end
exe_release = Executable(exename, targetdir, false)
exe_debug = Executable(exename, targetdir, true)
sys = SysFile(exename)
if !force
for f in [cfile, userimgjl, "$(sys.buildfile).$(Libdl.dlext)", "$(sys.buildfile).ji", exe_release.buildfile, exe_debug.buildfile]
if isfile(f)
println("ERROR: File '$(f)' already exists. Delete it or use --force.")
return 1
end
end
if targetdir != nothing && !isempty(readdir(targetdir))
println("ERROR: targetdir is not an empty diectory. Delete all contained files or use --force.")
return 1
end
end
emit_cmain(cfile, exename, targetdir != nothing)
emit_userimgjl(userimgjl, script_file)
println("running: $(julia) $(build_sysimg) $(sys.buildfile) $(cpu_target) $(userimgjl) --force")
run(`$(julia) $(build_sysimg) $(sys.buildfile) $(cpu_target) $(userimgjl) --force`)
println()
gcc = find_system_gcc()
# This argument is needed for the gcc, see issue #9973
win_arg = @windows ? `-D_WIN32_WINNT=0x0502` : ``
incs = get_includes()
ENV2 = deepcopy(ENV)
@windows_only begin
if contains(gcc, "WinRPM")
# This should not bee necessary, it is done due to WinRPM's gcc's
# include paths is not correct see WinRPM.jl issue #38
ENV2["PATH"] *= ";" * dirname(gcc)
push!(incs, "-I"*abspath(joinpath(dirname(gcc),"..","include")))
end
end
for exe in [exe_release, exe_debug]
println("running: $gcc -g -Wl,--no-as-needed $win_arg $(join(incs, " ")) $(cfile) -o $(exe.buildfile) -Wl,-rpath,$(sys.buildpath) -L$(sys.buildpath) $(exe.libjulia) -l$(exename)")
cmd = setenv(`$gcc -g -Wl,--no-as-needed $win_arg $(incs) $(cfile) -o $(exe.buildfile) -Wl,-rpath,$(sys.buildpath) -L$(sys.buildpath) $(exe.libjulia) -l$(exename)`, ENV2)
run(cmd)
println()
end
println("running: rm -rf $(tmpdir) $(sys.buildfile).o $(sys.buildfile0).o $(sys.buildfile0).ji")
map(f-> rm(f, recursive=true), [tmpdir, sys.buildfile*".o", sys.buildfile0*".o", sys.buildfile0*".ji"])
println()
if targetdir != nothing
# Move created files to target directory
for file in [exe_release.buildfile, exe_debug.buildfile, sys.buildfile*".$(Libdl.dlext)", sys.buildfile*".ji"]
mv(file,joinpath(targetdir, basename(file)))
end
# Copy needed shared libraries to the target directory
tmp = ".*\.$(Libdl.dlext).*"
shlibs = filter(Regex(tmp),readdir(sys.buildpath))
for shlib in shlibs
cp(joinpath(sys.buildpath, shlib), joinpath(targetdir, shlib))
end
@unix_only begin
# Fix rpath in executable and shared libraries
shlibs = filter(Regex(tmp),readdir(targetdir))
push!(shlibs, exe_release.filename)
push!(shlibs, exe_debug.filename)
println(shlibs)
for shlib in shlibs
rpath = readall(`$(patchelf) --print-rpath $(joinpath(targetdir, shlib))`)[1:end-1]
if Base.samefile(rpath, sys.buildpath)
run(`$(patchelf) --set-rpath $(targetdir) $(joinpath(targetdir, shlib))`)
end
end
end
end
for exe in [exe_release, exe_debug]
println("$(exe.targetfile) successfully created.")
end
return 0
end
function find_patchelf()
for patchelf in [joinpath(JULIA_HOME, "patchelf"), "patchelf"]
try
if success(`$(patchelf) --version`)
return patchelf
end
end
end
end
function get_includes()
ret = []
# binary install
incpath = abspath(joinpath(JULIA_HOME, "..", "include", "julia"))
push!(ret, "-I$(incpath)")
# Git checkout
julia_root = abspath(joinpath(JULIA_HOME, "..", ".."))
push!(ret, "-I$(julia_root)src")
push!(ret, "-I$(julia_root)src/support")
push!(ret, "-I$(julia_root)usr/include")
ret
end
function emit_cmain(cfile, exename, relocation)
if relocation
sysji = joinpath("lib"*exename)
else
sysji = joinpath(dirname(Libdl.dlpath("libjulia")), "lib"*exename)
end
sysji = escape_string(sysji)
f = open(cfile, "w")
write( f, """
#include <julia.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char sysji[] = \"$(sysji).ji\";
char mainfunc[] = \"main()\";
jl_init_with_image(NULL, sysji);
jl_eval_string(mainfunc);
int ret = 0;
if (jl_exception_occurred())
{
jl_show(jl_stderr_obj(), jl_exception_occurred());
jl_printf(jl_stderr_stream(), \"\\n\");
ret = 1;
}
jl_atexit_hook();
return ret;
}
"""
)
close(f)
end
function emit_userimgjl(userimgjl, script_file)
open(userimgjl, "w") do f
write( f, "include(\"$(escape_string(script_file))\")")
end
end
function find_system_gcc()
# On Windows, check to see if WinRPM is installed, and if so, see if gcc is installed
@windows_only try
require("WinRPM")
winrpmgcc = joinpath(WinRPM.installdir,"usr","$(Sys.ARCH)-w64-mingw32",
"sys-root","mingw","bin","gcc.exe")
if success(`$winrpmgcc --version`)
return winrpmgcc
end
end
# See if `gcc` exists
@unix_only try
if success(`gcc -v`)
return "gcc"
end
end
error( "GCC not found on system: " * @windows? "GCC can be installed via `Pkg.add(\"WinRPM\"); WinRPM.install(\"gcc\")`" : "" )
end
if !isinteractive()
if length(ARGS) < 2 || ("--help" in ARGS || "-h" in ARGS)
println("Usage: build_executable.jl <exename> <script_file> [targetdir] <cpu_target> [--help]")
println(" <exename> is the filename of the resulting executable and the resulting sysimg")
println(" <script_file> is the path to a jl file containing a main() function.")
println(" [targetdir] (optional) is the path to a directory to put the executable and other")
println(" <cpu_target> is an LLVM cpu target to build the system image against")
println(" needed files into (default: julia directory structure)")
println(" --force Set if you wish to overwrite existing files")
println(" --help Print out this help text and exit")
println()
println(" Example:")
println(" julia build_executable.jl standalone_test hello_world.jl targetdir core2")
return 0
end
force_flag = "--force" in ARGS
filter!(x -> x != "--force", ARGS)
build_executable(ARGS..., force=force_flag)
end