Skip to content

Commit b506bfc

Browse files
authored
[deps] Some improvements to the build_local.jl script (#689)
* [deps] Some improvements to the `build_local.jl` script Remove unused packages from the environment, add more arguments to the script. * [deps] Remove more unused stuff * [deps] Automatically disable component depending on GCC version * [deps] Try to improve the GCC regex * [deps] Some fixes to gcc version checks logic
1 parent d15980a commit b506bfc

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

deps/Project.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
[deps]
22
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
3-
BinaryBuilderBase = "7f725544-6523-48cd-82d1-3fa08ff4056e"
4-
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
5-
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
6-
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
73
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
8-
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
9-
10-
[compat]
11-
Clang = "0.18"

deps/build_local.jl

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3746
end
3847
#! format: on
3948
parsed_args = parse_args(ARGS, s)
@@ -44,27 +53,8 @@ for (k, v) in parsed_args
4453
end
4554
println()
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-
5356
source_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"]
115105
cc = parsed_args["cc"]
116106
hermetic_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+
118126
build_cmd_list = [bazel_cmd, "build"]
119127
!isempty(arg) && push!(build_cmd_list, arg)
120128
append!(build_cmd_list, ["-c", "$(build_kind)"])
121129
push!(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
128130
push!(build_cmd_list, "--repo_env=HERMETIC_PYTHON_VERSION=$(hermetic_python_version)")
129131
push!(build_cmd_list, "--repo_env=GCC_HOST_COMPILER_PATH=$(gcc_host_compiler_path)")
130132
push!(build_cmd_list, "--repo_env=CC=$(cc)")
131133
push!(build_cmd_list, "--check_visibility=false")
132134
push!(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
133157
push!(build_cmd_list, ":libReactantExtra.so")
134158

135159
run(Cmd(Cmd(build_cmd_list); dir=source_dir))
@@ -158,7 +182,10 @@ if build_backend == "cuda"
158182
)
159183
end
160184
end
185+
161186
# Tell ReactantExtra_jll to load our library instead of the default artifact one
187+
using Preferences
188+
162189
set_preferences!(
163190
joinpath(dirname(@__DIR__), "LocalPreferences.toml"),
164191
"Reactant_jll",

0 commit comments

Comments
 (0)