Skip to content

Commit 3c0712b

Browse files
authored
Merge pull request #54 from lucatrv/build-only-explicitly-requested-targets
Build only explicitly requested targets by default
2 parents 05a3345 + 1a334ba commit 3c0712b

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
Helper script to build libraries and executables from Julia code.
44

55
```
6-
usage: juliac.jl [-v] [-q] [-c] [-J <file>] [-C <target>]
6+
usage: juliac.jl [-v] [-q] [-c] [-J <file>]
7+
[--compile {yes|no|all|min}] [-C <target>]
78
[-O {0,1,2,3}] [-g {0,1,2}] [--inline {yes|no}]
89
[--check-bounds {yes|no}] [--math-mode {ieee,fast}]
9-
[--depwarn {yes|no|error}] [-o] [-s] [-e] [-j]
10+
[--depwarn {yes|no|error}] [-a] [-o] [-s] [-e] [-j]
1011
[--version] [-h] juliaprog [cprog] [builddir]
1112
1213
positional arguments:
@@ -24,6 +25,9 @@ optional arguments:
2425
-c, --clean delete builddir
2526
-J, --sysimage <file>
2627
start up with the given system image file
28+
--compile {yes|no|all|min}
29+
enable or disable JIT compiler, or request
30+
exhaustive compilation
2731
-C, --cpu-target <target>
2832
limit usage of CPU features up to <target>
2933
-O, --optimize {0,1,2,3}
@@ -36,6 +40,7 @@ optional arguments:
3640
set floating point optimizations
3741
--depwarn {yes|no|error}
3842
set syntax and method deprecation warnings
43+
-a, --auto automatically build required dependencies
3944
-o, --object build object file
4045
-s, --shared build shared library
4146
-e, --executable build executable file
@@ -44,9 +49,9 @@ optional arguments:
4449
-h, --help show this help message and exit
4550
4651
examples:
47-
juliac.jl -ve hello.jl # verbose, build executable
48-
juliac.jl -ve hello.jl myprog.c # embed into user defined C program
49-
juliac.jl -qo hello.jl # quiet, build object file
52+
juliac.jl -vae hello.jl # verbose, auto, build executable
53+
juliac.jl -vae hello.jl myprog.c # embed into user defined C program
54+
juliac.jl -qo hello.jl # quiet, build object file only
5055
juliac.jl -vosej hello.jl # build all and sync Julia libs
5156
```
5257

@@ -103,3 +108,6 @@ sections in the Julia manual.
103108
With Julia 0.7, a single large binary can be created, which does not
104109
require the driver program to load the shared library. An example of
105110
that is in `program2.c`, where the image file is the binary itself.
111+
112+
For more information on embedding Julia see:\
113+
https://github.com/JuliaLang/julia/blob/master/doc/src/manual/embedding.md

juliac.jl

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ function main(args)
3838
default = nothing
3939
metavar = "<file>"
4040
help = "start up with the given system image file"
41+
"--compile"
42+
arg_type = String
43+
default = nothing
44+
metavar = "{yes|no|all|min}"
45+
range_tester = (x -> x == "yes" || x == "no" || x == "all" || x == "min")
46+
help = "enable or disable JIT compiler, or request exhaustive compilation"
4147
"--cpu-target", "-C"
4248
arg_type = String
4349
default = nothing
@@ -46,40 +52,43 @@ function main(args)
4652
"--optimize", "-O"
4753
arg_type = Int
4854
default = nothing
49-
range_tester = (x -> 0 <= x <= 3)
5055
metavar = "{0,1,2,3}"
56+
range_tester = (x -> 0 <= x <= 3)
5157
help = "set optimization level"
5258
"-g"
5359
arg_type = Int
5460
default = nothing
55-
range_tester = (x -> 0 <= x <= 2)
5661
dest_name = "debug"
5762
metavar = "{0,1,2}"
63+
range_tester = (x -> 0 <= x <= 2)
5864
help = "set debugging information level"
5965
"--inline"
6066
arg_type = String
6167
default = nothing
62-
range_tester = (x -> x == "yes" || x == "no")
6368
metavar = "{yes|no}"
69+
range_tester = (x -> x == "yes" || x == "no")
6470
help = "control whether inlining is permitted"
6571
"--check-bounds"
6672
arg_type = String
6773
default = nothing
68-
range_tester = (x -> x == "yes" || x == "no")
6974
metavar = "{yes|no}"
75+
range_tester = (x -> x == "yes" || x == "no")
7076
help = "emit bounds checks always or never"
7177
"--math-mode"
7278
arg_type = String
7379
default = nothing
74-
range_tester = (x -> x == "ieee" || x == "fast")
7580
metavar = "{ieee,fast}"
81+
range_tester = (x -> x == "ieee" || x == "fast")
7682
help = "set floating point optimizations"
7783
"--depwarn"
7884
arg_type = String
7985
default = nothing
80-
range_tester = (x -> x == "yes" || x == "no" || x == "error")
8186
metavar = "{yes|no|error}"
87+
range_tester = (x -> x == "yes" || x == "no" || x == "error")
8288
help = "set syntax and method deprecation warnings"
89+
"--auto", "-a"
90+
action = :store_true
91+
help = "automatically build required dependencies"
8392
"--object", "-o"
8493
action = :store_true
8594
help = "build object file"
@@ -96,9 +105,9 @@ function main(args)
96105

97106
s.epilog = """
98107
examples:\n
99-
\ua0\ua0juliac.jl -ve hello.jl # verbose, build executable\n
100-
\ua0\ua0juliac.jl -ve hello.jl myprog.c # embed into user defined C program\n
101-
\ua0\ua0juliac.jl -qo hello.jl # quiet, build object file\n
108+
\ua0\ua0juliac.jl -vae hello.jl # verbose, auto, build executable\n
109+
\ua0\ua0juliac.jl -vae hello.jl myprog.c # embed into user defined C program\n
110+
\ua0\ua0juliac.jl -qo hello.jl # quiet, build object file only\n
102111
\ua0\ua0juliac.jl -vosej hello.jl # build all and sync Julia libs\n
103112
"""
104113

@@ -117,13 +126,15 @@ function main(args)
117126
parsed_args["quiet"],
118127
parsed_args["clean"],
119128
parsed_args["sysimage"],
129+
parsed_args["compile"],
120130
parsed_args["cpu-target"],
121131
parsed_args["optimize"],
122132
parsed_args["debug"],
123133
parsed_args["inline"],
124134
parsed_args["check-bounds"],
125135
parsed_args["math-mode"],
126136
parsed_args["depwarn"],
137+
parsed_args["auto"],
127138
parsed_args["object"],
128139
parsed_args["shared"],
129140
parsed_args["executable"],
@@ -132,12 +143,17 @@ function main(args)
132143
end
133144

134145
function julia_compile(julia_program, c_program=nothing, build_dir="builddir", verbose=false, quiet=false,
135-
clean=false, sysimage = nothing, cpu_target=nothing, optimize=nothing, debug=nothing,
136-
inline=nothing, check_bounds=nothing, math_mode=nothing, depwarn=nothing,
137-
object=false, shared=false, executable=true, julialibs=true)
146+
clean=false, sysimage = nothing, compile=nothing, cpu_target=nothing, optimize=nothing,
147+
debug=nothing, inline=nothing, check_bounds=nothing, math_mode=nothing, depwarn=nothing,
148+
auto=false, object=false, shared=false, executable=true, julialibs=true)
138149

139150
verbose && quiet && (verbose = false)
140151

152+
if auto
153+
executable && (shared = true)
154+
shared && (object = true)
155+
end
156+
141157
julia_program = abspath(julia_program)
142158
isfile(julia_program) || error("Cannot find file:\n \"$julia_program\"")
143159
quiet || println("Julia program file:\n \"$julia_program\"")
@@ -174,23 +190,24 @@ function julia_compile(julia_program, c_program=nothing, build_dir="builddir", v
174190
verbose && println("Already in build directory")
175191
end
176192

177-
file_name = splitext(basename(julia_program))[1]
178-
o_file = file_name * ".o"
179-
s_file = "lib" * file_name * ".$(Libdl.dlext)"
180-
e_file = file_name * (is_windows() ? ".exe" : "")
193+
julia_program_basename = splitext(basename(julia_program))[1]
194+
o_file = julia_program_basename * ".o"
195+
s_file = "lib" * julia_program_basename * ".$(Libdl.dlext)"
196+
e_file = julia_program_basename * (is_windows() ? ".exe" : "")
197+
tmp_dir = "tmp_v$VERSION"
181198

182199
# TODO: these should probably be emitted from julia-config also:
183200
shlibdir = is_windows() ? JULIA_HOME : abspath(JULIA_HOME, Base.LIBDIR)
184201
private_shlibdir = abspath(JULIA_HOME, Base.PRIVATE_LIBDIR)
185202

186-
delete_object = false
187-
if object || shared || executable
203+
if object
188204
julia_cmd = `$(Base.julia_cmd())`
189205
if length(julia_cmd.exec) != 5 || !all(startswith.(julia_cmd.exec[2:5], ["-C", "-J", "--compile", "--depwarn"]))
190206
error("Unexpected format of \"Base.julia_cmd()\", you may be using an incompatible version of Julia")
191207
end
192208
sysimage == nothing || (julia_cmd.exec[3] = "-J$sysimage")
193209
push!(julia_cmd.exec, "--startup-file=no")
210+
compile == nothing || (julia_cmd.exec[4] = "--compile=$compile")
194211
cpu_target == nothing || (julia_cmd.exec[2] = "-C$cpu_target")
195212
optimize == nothing || push!(julia_cmd.exec, "-O$optimize")
196213
debug == nothing || push!(julia_cmd.exec, "-g$debug")
@@ -202,16 +219,15 @@ function julia_compile(julia_program, c_program=nothing, build_dir="builddir", v
202219
expr = "
203220
VERSION >= v\"0.7+\" && Base.init_load_path($(repr(JULIA_HOME))) # initialize location of site-packages
204221
empty!(Base.LOAD_CACHE_PATH) # reset / remove any builtin paths
205-
push!(Base.LOAD_CACHE_PATH, abspath(\"cache_ji_v$VERSION\")) # enable usage of precompiled files
222+
push!(Base.LOAD_CACHE_PATH, abspath(\"$tmp_dir\")) # enable usage of precompiled files
206223
include($(repr(julia_program))) # include \"julia_program\" file
207224
empty!(Base.LOAD_CACHE_PATH) # reset / remove build-system-relative paths"
208225
command = `$julia_cmd -e $expr`
209-
verbose && println("Build \".ji\" files local cache:\n $command")
226+
verbose && println("Build \".ji\" files:\n $command")
210227
run(command)
211-
command = `$julia_cmd --output-o $o_file -e $expr`
228+
command = `$julia_cmd --output-o $(joinpath(tmp_dir, o_file)) -e $expr`
212229
verbose && println("Build object file \"$o_file\":\n $command")
213230
run(command)
214-
object || (delete_object = true)
215231
end
216232

217233
if shared || executable
@@ -222,10 +238,10 @@ function julia_compile(julia_program, c_program=nothing, build_dir="builddir", v
222238
cc = is_windows() ? "x86_64-w64-mingw32-gcc" : "gcc"
223239
end
224240

225-
if shared || executable
226-
command = `$cc -m64 -shared -o $s_file $o_file $cflags $ldflags $ldlibs`
241+
if shared
242+
command = `$cc -m64 -shared -o $s_file $(joinpath(tmp_dir, o_file)) $cflags $ldflags $ldlibs`
227243
if is_apple()
228-
command = `$command -Wl,-install_name,@rpath/lib$file_name.dylib`
244+
command = `$command -Wl,-install_name,@rpath/lib$julia_program_basename.dylib`
229245
elseif is_windows()
230246
command = `$command -Wl,--export-all-symbols`
231247
end
@@ -244,11 +260,6 @@ function julia_compile(julia_program, c_program=nothing, build_dir="builddir", v
244260
run(command)
245261
end
246262

247-
if delete_object && isfile(o_file)
248-
verbose && println("Delete object file \"$o_file\"")
249-
rm(o_file)
250-
end
251-
252263
if julialibs
253264
verbose && println("Sync Julia libraries:")
254265
libfiles = String[]

0 commit comments

Comments
 (0)