33# Julia compiler wrapper script
44# NOTE: The interface and location of this script are considered unstable/experimental
55
6+ using LazyArtifacts
7+
68module JuliaConfig
7- include (joinpath (@__DIR__ , " julia-config.jl" ))
9+ include (joinpath (@__DIR__ , " .. " , " julia-config.jl" ))
810end
911
1012julia_cmd = ` $(Base. julia_cmd ()) --startup-file=no --history-file=no`
@@ -30,6 +32,57 @@ if help !== nothing
3032 exit (0 )
3133end
3234
35+ # Copied from PackageCompiler
36+ # https://github.com/JuliaLang/PackageCompiler.jl/blob/1c35331d8ef81494f054bbc71214811253101993/src/PackageCompiler.jl#L147-L190
37+ function get_compiler_cmd (; cplusplus:: Bool = false )
38+ cc = get (ENV , " JULIA_CC" , nothing )
39+ path = nothing
40+ @static if Sys. iswindows ()
41+ path = joinpath (LazyArtifacts. artifact " mingw-w64" ,
42+ " extracted_files" ,
43+ (Int== Int64 ? " mingw64" : " mingw32" ),
44+ " bin" ,
45+ cplusplus ? " g++.exe" : " gcc.exe" )
46+ compiler_cmd = ` $path `
47+ end
48+ if cc != = nothing
49+ compiler_cmd = Cmd (Base. shell_split (cc))
50+ path = nothing
51+ elseif ! Sys. iswindows ()
52+ compilers_cpp = (" g++" , " clang++" )
53+ compilers_c = (" gcc" , " clang" )
54+ found_compiler = false
55+ if cplusplus
56+ for compiler in compilers_cpp
57+ if Sys. which (compiler) != = nothing
58+ compiler_cmd = ` $compiler `
59+ found_compiler = true
60+ break
61+ end
62+ end
63+ end
64+ if ! found_compiler
65+ for compiler in compilers_c
66+ if Sys. which (compiler) != = nothing
67+ compiler_cmd = ` $compiler `
68+ found_compiler = true
69+ if cplusplus && ! WARNED_CPP_COMPILER[]
70+ @warn " could not find a c++ compiler (g++ or clang++), falling back to $compiler , this might cause link errors"
71+ WARNED_CPP_COMPILER[] = true
72+ end
73+ break
74+ end
75+ end
76+ end
77+ found_compiler || error (" could not find a compiler, looked for " ,
78+ join (((cplusplus ? compilers_cpp : ()). .. , compilers_c... ), " , " , " and " ))
79+ end
80+ if path != = nothing
81+ compiler_cmd = addenv (compiler_cmd, " PATH" => string (ENV [" PATH" ], " ;" , dirname (path)))
82+ end
83+ return compiler_cmd
84+ end
85+
3386# arguments to forward to julia compilation process
3487julia_args = []
3588enable_trim:: Bool = false
@@ -82,6 +135,7 @@ function get_rpath(; relative::Bool = false)
82135 end
83136end
84137
138+ cc = get_compiler_cmd ()
85139absfile = abspath (file)
86140cflags = JuliaConfig. cflags (; framework= false )
87141cflags = Base. shell_split (cflags)
@@ -93,7 +147,6 @@ tmpdir = mktempdir(cleanup=false)
93147img_path = joinpath (tmpdir, " img.a" )
94148bc_path = joinpath (tmpdir, " img-bc.a" )
95149
96-
97150function precompile_env ()
98151 # Pre-compile the environment
99152 # (otherwise obscure error messages will occur)
@@ -121,7 +174,6 @@ function compile_products(enable_trim::Bool)
121174 println (stderr , " \n Failed to compile $file " )
122175 exit (1 )
123176 end
124-
125177end
126178
127179function link_products ()
@@ -137,11 +189,11 @@ function link_products()
137189 julia_libs = Base. shell_split (Base. isdebugbuild () ? " -ljulia-debug -ljulia-internal-debug" : " -ljulia -ljulia-internal" )
138190 try
139191 if output_type == " --output-lib"
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 elseif output_type == " --output-sysimage"
142- cmd2 = ` cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
194+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
143195 else
144- cmd2 = ` cc $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
196+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
145197 end
146198 verbose && println (" Running: $cmd2 " )
147199 run (cmd2)
0 commit comments