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 = []
3386
@@ -77,6 +130,7 @@ function get_rpath(; relative::Bool = false)
77130 end
78131end
79132
133+ cc = get_compiler_cmd ()
80134absfile = abspath (file)
81135cflags = JuliaConfig. cflags (; framework= false )
82136cflags = Base. shell_split (cflags)
@@ -90,7 +144,6 @@ init_path = joinpath(tmpdir, "init.a")
90144img_path = joinpath (tmpdir, " img.a" )
91145bc_path = joinpath (tmpdir, " img-bc.a" )
92146
93-
94147function precompile_env ()
95148 # Pre-compile the environment
96149 # (otherwise obscure error messages will occur)
@@ -123,7 +176,7 @@ function compile_products()
123176 }
124177 """ )
125178 end
126- run (` cc $(cflags) -g -c -o $init_path $initsrc_path ` )
179+ run (` $(cc) $(cflags) -g -c -o $init_path $initsrc_path ` )
127180end
128181
129182function link_products ()
@@ -139,11 +192,11 @@ function link_products()
139192 julia_libs = Base. shell_split (Base. isdebugbuild () ? " -ljulia-debug -ljulia-internal-debug" : " -ljulia -ljulia-internal" )
140193 try
141194 if output_type == " --output-lib"
142- cmd2 = ` cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $init_path $(julia_libs) `
195+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $init_path $(julia_libs) `
143196 elseif output_type == " --output-sysimage"
144- cmd2 = ` cc $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
197+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -shared -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $(julia_libs) `
145198 else
146- cmd2 = ` cc $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $init_path $(julia_libs) `
199+ cmd2 = ` $(cc) $(allflags) $(rpath) -o $outname -Wl,$(Base. Linking. WHOLE_ARCHIVE) $img_path -Wl,$(Base. Linking. NO_WHOLE_ARCHIVE) $init_path $(julia_libs) `
147200 end
148201 verbose && println (" Running: $cmd2 " )
149202 run (cmd2)
0 commit comments