Skip to content

Commit 0d84949

Browse files
authored
allow passing on flags with the compiler (#568)
1 parent 8036fe8 commit 0d84949

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

src/PackageCompiler.jl

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,37 @@ function move_default_sysimage_if_windows()
8989
end
9090
end
9191

92-
function run_with_env(cmd, compiler)
93-
if Sys.iswindows()
94-
env = copy(ENV)
95-
env["PATH"] = string(env["PATH"], ";", dirname(compiler))
96-
run(Cmd(cmd; env=env))
97-
else
98-
run(cmd)
99-
end
100-
end
101-
102-
function get_compiler()
92+
function run_compiler(cmd::Cmd)
10393
cc = get(ENV, "JULIA_CC", nothing)
104-
if cc !== nothing
105-
return cc
94+
path = nothing
95+
local compiler_cmd
96+
@static if Sys.iswindows()
97+
if Int == Int64
98+
path = joinpath(LazyArtifacts.artifact"x86_64-w64-mingw32", "mingw64", "bin", "gcc.exe")
99+
compiler_cmd = `$path`
100+
else # Int == Int32
101+
path = joinpath(LazyArtifacts.artifact"i868-w64-mingw32", "mingw32", "bin", "gcc.exe")
102+
compiler_cmd = `$path`
103+
end
106104
end
107-
@static if Sys.iswindows() && Int==Int64
108-
return joinpath(LazyArtifacts.artifact"x86_64-w64-mingw32", "mingw64", "bin", "gcc.exe")
109-
elseif Sys.iswindows() && Int==Int32
110-
return joinpath(LazyArtifacts.artifact"i868-w64-mingw32", "mingw32", "bin", "gcc.exe")
105+
if cc !== nothing
106+
compiler_cmd = Cmd(Base.shell_split(cc))
107+
path = nothing
108+
elseif !Sys.iswindows()
109+
if Sys.which("gcc") !== nothing
110+
compiler_cmd = `gcc`
111+
elseif Sys.which("clang") !== nothing
112+
compiler_cmd = `clang`
113+
else
114+
error("could not find a compiler, looked for `gcc` and `clang`")
115+
end
111116
end
112-
if Sys.which("gcc") !== nothing
113-
return "gcc"
114-
elseif Sys.which("clang") !== nothing
115-
return "clang"
117+
if path !== nothing
118+
compiler_cmd = addenv(compiler_cmd, "PATH" => string(ENV["PATH"], ";", dirname(path)))
116119
end
117-
error("could not find a compiler, looked for `gcc` and `clang`")
120+
full_cmd = `$compiler_cmd $cmd`
121+
@debug "running $full_cmd"
122+
run(full_cmd)
118123
end
119124

120125
function get_julia_cmd()
@@ -421,9 +426,11 @@ end
421426
create_sysimage(packages::Vector{String}; kwargs...)
422427
423428
Create a system image that includes the package(s) in `packages` given as a
424-
string or vector). An attempt to automatically find a compiler will be done but
425-
can also be given explicitly by setting the environment variable `JULIA_CC` to a
426-
path to a compiler.
429+
string or vector).
430+
431+
An attempt to automatically find a compiler will be done but can also be given
432+
explicitly by setting the environment variable `JULIA_CC` to a path to a
433+
compiler (can also include extra arguments to the compiler, like `-g`).
427434
428435
### Keyword arguments:
429436
@@ -617,15 +624,12 @@ end
617624

618625
function compile_c_init_julia(julia_init_c_file::String, sysimage_path::String)
619626
@debug "Compiling $julia_init_c_file"
620-
compiler = get_compiler()
621627
m = something(march(), ``)
622628
flags = Base.shell_split(cflags())
623629

624630
o_init_file = splitext(julia_init_c_file)[1] * ".o"
625-
cmd = `$compiler -c -O2 -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_path)) $TLS_SYNTAX $(bitflag()) $flags $m -o $o_init_file $julia_init_c_file`
626-
627-
@debug "running $cmd"
628-
run_with_env(cmd, compiler)
631+
cmd = `-c -O2 -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_path)) $TLS_SYNTAX $(bitflag()) $flags $m -o $o_init_file $julia_init_c_file`
632+
run_compiler(cmd)
629633
return o_init_file
630634
end
631635

@@ -674,7 +678,6 @@ function create_sysimg_from_object_file(input_object::String,
674678

675679
extra = get_extra_linker_flags(version, compat_level, soname)
676680

677-
compiler = get_compiler()
678681
m = something(march(), ``)
679682
private_libdir = if Base.DARWIN_FRAMEWORK # taken from Libdl tests
680683
if ccall(:jl_is_debugbuild, Cint, ()) != 0
@@ -687,9 +690,8 @@ function create_sysimg_from_object_file(input_object::String,
687690
else
688691
dirname(abspath(Libdl.dlpath("libjulia-internal")))
689692
end
690-
cmd = `$compiler $(bitflag()) $m -shared -L$(julia_libdir) -L$(private_libdir) -o $sysimage_path $o_file_flags -ljulia-internal -ljulia $extra`
691-
@debug "running $cmd"
692-
run_with_env(cmd, compiler)
693+
cmd = `$(bitflag()) $m -shared -L$(julia_libdir) -L$(private_libdir) -o $sysimage_path $o_file_flags -ljulia-internal -ljulia $extra`
694+
run_compiler(cmd)
693695
return nothing
694696
end
695697

@@ -769,7 +771,7 @@ argument, for example:
769771
770772
An attempt to automatically find a compiler will be done but can also be given
771773
explicitly by setting the environment variable `JULIA_CC` to a path to a
772-
compiler.
774+
compiler (can also include extra arguments to the compiler, like `-g`).
773775
774776
### Keyword arguments:
775777
@@ -874,7 +876,7 @@ simply calls `jl_atexit_hook(retcode)`.)
874876
875877
An attempt to automatically find a compiler will be done but can also be given
876878
explicitly by setting the environment variable `JULIA_CC` to a path to a
877-
compiler.
879+
compiler (can also include extra arguments to the compiler, like `-g`).
878880
879881
### Keyword arguments:
880882
@@ -1097,11 +1099,9 @@ function create_executable_from_sysimg(;sysimage_path::String,
10971099
flags = join((cflags(), ldflags(), ldlibs()), " ")
10981100
flags = Base.shell_split(flags)
10991101
wrapper = c_driver_program_path
1100-
compiler = get_compiler()
11011102
m = something(march(), ``)
1102-
cmd = `$compiler -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_path)) $TLS_SYNTAX $(bitflag()) $m -o $(executable_path) $(wrapper) $(sysimage_path) -O2 $(rpath_executable()) $flags`
1103-
@debug "running $cmd"
1104-
run_with_env(cmd, compiler)
1103+
cmd = `-DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_path)) $TLS_SYNTAX $(bitflag()) $m -o $(executable_path) $(wrapper) $(sysimage_path) -O2 $(rpath_executable()) $flags`
1104+
run_compiler(cmd)
11051105
return nothing
11061106
end
11071107

0 commit comments

Comments
 (0)