11# Julia compiler wrapper script
22# NOTE: The interface and location of this script are considered unstable/experimental
33
4+ using LazyArtifacts
5+
46module JuliaConfig
57 include (joinpath (@__DIR__ , " julia-config.jl" ))
68end
@@ -28,6 +30,57 @@ if help !== nothing
2830 exit (0 )
2931end
3032
33+ # Copied from PackageCompiler
34+ # https://github.com/JuliaLang/PackageCompiler.jl/blob/1c35331d8ef81494f054bbc71214811253101993/src/PackageCompiler.jl#L147-L190
35+ function get_compiler_cmd (; cplusplus:: Bool = false )
36+ cc = get (ENV , " JULIA_CC" , nothing )
37+ path = nothing
38+ @static if Sys. iswindows ()
39+ path = joinpath (LazyArtifacts. artifact " mingw-w64" ,
40+ " extracted_files" ,
41+ (Int== Int64 ? " mingw64" : " mingw32" ),
42+ " bin" ,
43+ cplusplus ? " g++.exe" : " gcc.exe" )
44+ compiler_cmd = ` $path `
45+ end
46+ if cc != = nothing
47+ compiler_cmd = Cmd (Base. shell_split (cc))
48+ path = nothing
49+ elseif ! Sys. iswindows ()
50+ compilers_cpp = (" g++" , " clang++" )
51+ compilers_c = (" gcc" , " clang" )
52+ found_compiler = false
53+ if cplusplus
54+ for compiler in compilers_cpp
55+ if Sys. which (compiler) != = nothing
56+ compiler_cmd = ` $compiler `
57+ found_compiler = true
58+ break
59+ end
60+ end
61+ end
62+ if ! found_compiler
63+ for compiler in compilers_c
64+ if Sys. which (compiler) != = nothing
65+ compiler_cmd = ` $compiler `
66+ found_compiler = true
67+ if cplusplus && ! WARNED_CPP_COMPILER[]
68+ @warn " could not find a c++ compiler (g++ or clang++), falling back to $compiler , this might cause link errors"
69+ WARNED_CPP_COMPILER[] = true
70+ end
71+ break
72+ end
73+ end
74+ end
75+ found_compiler || error (" could not find a compiler, looked for " ,
76+ join (((cplusplus ? compilers_cpp : ()). .. , compilers_c... ), " , " , " and " ))
77+ end
78+ if path != = nothing
79+ compiler_cmd = addenv (compiler_cmd, " PATH" => string (ENV [" PATH" ], " ;" , dirname (path)))
80+ end
81+ return compiler_cmd
82+ end
83+
3184# arguments to forward to julia compilation process
3285julia_args = []
3386enable_trim:: Bool = false
@@ -80,6 +133,7 @@ function get_rpath(; relative::Bool = false)
80133 end
81134end
82135
136+ cc = get_compiler_cmd ()
83137absfile = abspath (file)
84138cflags = JuliaConfig. cflags (; framework= false )
85139cflags = Base. shell_split (cflags)
@@ -91,7 +145,6 @@ tmpdir = mktempdir(cleanup=false)
91145img_path = joinpath (tmpdir, " img.a" )
92146bc_path = joinpath (tmpdir, " img-bc.a" )
93147
94-
95148function precompile_env ()
96149 # Pre-compile the environment
97150 # (otherwise obscure error messages will occur)
@@ -119,7 +172,6 @@ function compile_products(enable_trim::Bool)
119172 println (stderr , " \n Failed to compile $file " )
120173 exit (1 )
121174 end
122-
123175end
124176
125177function link_products ()
@@ -135,11 +187,11 @@ function link_products()
135187 julia_libs = Base. shell_split (Base. isdebugbuild () ? " -ljulia-debug -ljulia-internal-debug" : " -ljulia -ljulia-internal" )
136188 try
137189 if output_type == " --output-lib"
138- cmd2 = ` cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
190+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
139191 elseif output_type == " --output-sysimage"
140- cmd2 = ` cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
192+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
141193 else
142- cmd2 = ` cc $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
194+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
143195 end
144196 verbose && println (" Running: $cmd2 " )
145197 run (cmd2)
0 commit comments