@@ -27,13 +27,22 @@ s = ArgParseSettings()
2727 help = " Hermetic Python version."
2828 default = " 3.10"
2929 arg_type = String
30- # For GCC < 13 we need to disable these flags
31- " --xnn_disable_avx512fp16"
32- help = " Disable AVX512 FP16 support in XNNPACK."
33- action = :store_true
34- " --xnn_disable_avxvnniint8"
35- help = " Disable AVX VNNI INT8 support in XNNPACK."
36- action = :store_true
30+ " --jobs"
31+ help = " Number of parallel jobs."
32+ default = Sys. CPU_THREADS
33+ arg_type = Int
34+ " --copt"
35+ help = " Options to be passed to the C compiler. Can be used multiple times."
36+ action = :append_arg
37+ arg_type = String
38+ " --cxxopt"
39+ help = " Options to be passed to the C++ compiler. Can be used multiple times."
40+ action = :append_arg
41+ arg_type = String
42+ " --extraopt"
43+ help = " Extra options to be passed to Bazel. Can be used multiple times."
44+ action = :append_arg
45+ arg_type = String
3746end
3847# ! format: on
3948parsed_args = parse_args (ARGS , s)
@@ -44,27 +53,8 @@ for (k, v) in parsed_args
4453end
4554println ()
4655
47- using Pkg, Scratch, Preferences, Libdl
48-
49- # 1. Get a scratch directory
50- scratch_dir = get_scratch! (Reactant_jll, " build" )
51- isdir (scratch_dir) && rm (scratch_dir; recursive= true )
52-
5356source_dir = joinpath (@__DIR__ , " ReactantExtra" )
5457
55- # 2. Ensure that an appropriate LLVM_full_jll is installed
56- Pkg. activate (; temp= true )
57-
58- # Build!
59- @info " Building" source_dir scratch_dir
60- run (` mkdir -p $(scratch_dir) ` )
61- run (
62- Cmd (
63- ` $(Base. julia_cmd (). exec[1 ]) --project=. -e "using Pkg; Pkg.instantiate()"` ;
64- dir= source_dir,
65- ),
66- )
67-
6858# --repo_env TF_NEED_ROCM=1
6959# --define=using_rocm=true --define=using_rocm_hipcc=true
7060# --action_env TF_ROCM_AMDGPU_TARGETS="gfx900,gfx906,gfx908,gfx90a,gfx1030"
@@ -115,21 +105,55 @@ gcc_host_compiler_path = parsed_args["gcc_host_compiler_path"]
115105cc = parsed_args[" cc" ]
116106hermetic_python_version = parsed_args[" hermetic_python_version" ]
117107
108+ # Try to guess if `cc` is GCC and get its version number.
109+ cc_is_gcc, gcc_version = let
110+ io = IOBuffer ()
111+ run (pipeline (ignorestatus (` $(cc) --version` ); stdout = io))
112+ version_string = String (take! (io))
113+ # Detecing GCC is hard, the name "gcc" may not appear anywhere in the
114+ # version string, but on the second line there should be FSF.
115+ m = match (
116+ r" \( [^)]+\) (\d +\.\d +\.\d +).*\n .*Free Software Foundation, Inc\. " ,
117+ version_string,
118+ )
119+ if ! isnothing (m)
120+ true , VersionNumber (m[1 ])
121+ else
122+ false , v " 0"
123+ end
124+ end
125+
118126build_cmd_list = [bazel_cmd, " build" ]
119127! isempty (arg) && push! (build_cmd_list, arg)
120128append! (build_cmd_list, [" -c" , " $(build_kind) " ])
121129push! (build_cmd_list, " --action_env=JULIA=$(Base. julia_cmd (). exec[1 ]) " )
122- if parsed_args[" xnn_disable_avx512fp16" ]
123- push! (build_cmd_list, " --define=xnn_enable_avx512fp16=false" )
124- end
125- if parsed_args[" xnn_disable_avxvnniint8" ]
126- push! (build_cmd_list, " --define=xnn_enable_avxvnniint8=false" )
127- end
128130push! (build_cmd_list, " --repo_env=HERMETIC_PYTHON_VERSION=$(hermetic_python_version) " )
129131push! (build_cmd_list, " --repo_env=GCC_HOST_COMPILER_PATH=$(gcc_host_compiler_path) " )
130132push! (build_cmd_list, " --repo_env=CC=$(cc) " )
131133push! (build_cmd_list, " --check_visibility=false" )
132134push! (build_cmd_list, " --verbose_failures" )
135+ push! (build_cmd_list, " --jobs=$(parsed_args[" jobs" ]) " )
136+ for opt in parsed_args[" copt" ]
137+ push! (build_cmd_list, " --copt=$(opt) " )
138+ end
139+ for opt in parsed_args[" cxxopt" ]
140+ push! (build_cmd_list, " --cxxopt=$(opt) " )
141+ end
142+ for opt in parsed_args[" extraopt" ]
143+ push! (build_cmd_list, opt)
144+ end
145+ # Some versions of GCC can't deal with some components of XLA, disable them if necessary.
146+ if cc_is_gcc && build_backend == " cuda"
147+ arch = Base. BinaryPlatforms. arch (Base. BinaryPlatforms. HostPlatform ())
148+ if arch == " x86_64"
149+ if gcc_version < v " 13"
150+ push! (build_cmd_list, " --define=xnn_enable_avxvnniint8=false" )
151+ end
152+ if gcc_version < v " 12"
153+ push! (build_cmd_list, " --define=xnn_enable_avx512fp16=false" )
154+ end
155+ end
156+ end
133157push! (build_cmd_list, " :libReactantExtra.so" )
134158
135159run (Cmd (Cmd (build_cmd_list); dir= source_dir))
@@ -158,7 +182,10 @@ if build_backend == "cuda"
158182 )
159183 end
160184end
185+
161186# Tell ReactantExtra_jll to load our library instead of the default artifact one
187+ using Preferences
188+
162189set_preferences! (
163190 joinpath (dirname (@__DIR__ ), " LocalPreferences.toml" ),
164191 " Reactant_jll" ,
0 commit comments