Skip to content

Commit 156763d

Browse files
authored
[BuildToolchains] Always set relevant host system CMake variables (#230)
* [BuildToolchains] Set `CMAKE_HOST_SYSTEM_PROCESSOR` for CMake host toolchain * [BuildToolchains] Always set relevant host system variables If we don't, CMake will use `uname` to infer the host system variables, which will likely give wrong results. The important is to set `CMAKE_SYSTEM_NAME` only when cross-compiling.
1 parent 59b1f52 commit 156763d

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/BuildToolchains.jl

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,65 +31,58 @@ function cmake_os(p::AbstractPlatform)
3131
end
3232
end
3333

34-
function toolchain_file(bt::CMake, p::AbstractPlatform; is_host::Bool=false)
34+
function toolchain_file(bt::CMake, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false)
3535
target = triplet(p)
3636
aatarget = aatriplet(p)
3737

38-
# CMake uses the setting of `HOST_SYSTEM_NAME` and `SYSTEM_NAME` to decide
39-
# whether the current build is a cross-compilation or not:
40-
# <https://cmake.org/cmake/help/latest/variable/CMAKE_CROSSCOMPILING.html>.
41-
# We want to have the host toolchain always setting `HOST_SYSTEM_NAME`, and
42-
# the target toolchain always setting `SYSTEM_NAME`.
43-
system_name_var = if is_host
44-
"CMAKE_HOST_SYSTEM_NAME"
45-
else
46-
"CMAKE_SYSTEM_NAME"
38+
# In order to get the version of the host system we need to call `/bin/uname -r`.
39+
file = """
40+
# CMake toolchain file for $(c_compiler(bt)) running on $(target)
41+
set(CMAKE_HOST_SYSTEM_NAME $(cmake_os(host_platform)))
42+
set(CMAKE_HOST_SYSTEM_PROCESSOR $(cmake_arch(host_platform)))
43+
execute_process(COMMAND /bin/uname -r OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION)
44+
"""
45+
46+
if !is_host
47+
# CMake checks whether `SYSTEM_NAME` is set manually to decide whether the current
48+
# build is a cross-compilation or not:
49+
# <https://cmake.org/cmake/help/latest/variable/CMAKE_CROSSCOMPILING.html>. We
50+
# always set `HOST_SYSTEM_NAME`, but set `SYSTEM_NAME` only for the target
51+
# toolchain.
52+
file *= """
53+
set(CMAKE_SYSTEM_NAME $(cmake_os(p)))
54+
set(CMAKE_SYSTEM_PROCESSOR $(cmake_arch(p)))
55+
"""
4756
end
4857

4958
if Sys.isapple(p)
5059
darwin_ver = something(os_version(p), v"14.5.0")
5160
maj_ver = darwin_ver.major
5261
min_ver = darwin_ver.minor
53-
return """
54-
# CMake toolchain file for $(c_compiler(bt)) running on $(target)
55-
set($(system_name_var) $(cmake_os(p)))
56-
set(CMAKE_SYSTEM_PROCESSOR $(cmake_arch(p)))
62+
file *= """
5763
set(CMAKE_SYSTEM_VERSION $(maj_ver).$(min_ver))
5864
set(DARWIN_MAJOR_VERSION $(maj_ver))
5965
set(DARWIN_MINOR_VERSION $(min_ver))
6066
61-
# Enable rpath support
62-
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
63-
6467
set(CMAKE_SYSROOT /opt/$(aatarget)/$(aatarget)/sys-root)
6568
set(CMAKE_SYSTEM_FRAMEWORK_PATH
6669
\${CMAKE_SYSROOT}/System/Library/Frameworks
6770
\${CMAKE_SYSROOT}/System/Library/PrivateFrameworks
6871
)
69-
set(CMAKE_INSTALL_PREFIX \$ENV{prefix})
70-
71-
set(CMAKE_C_COMPILER /opt/bin/$(target)/$(aatarget)-$(c_compiler(bt)))
72-
set(CMAKE_CXX_COMPILER /opt/bin/$(target)/$(aatarget)-$(cxx_compiler(bt)))
73-
set(CMAKE_Fortran_COMPILER /opt/bin/$(target)/$(aatarget)-$(fortran_compiler(bt)))
74-
75-
set(CMAKE_LINKER /opt/bin/$(target)/$(aatarget)-ld)
76-
set(CMAKE_OBJCOPY /opt/bin/$(target)/$(aatarget)-objcopy)
77-
78-
set(CMAKE_AR /opt/bin/$(target)/$(aatarget)-ar)
79-
set(CMAKE_NM /opt/bin/$(target)/$(aatarget)-nm)
80-
set(CMAKE_RANLIB /opt/bin/$(target)/$(aatarget)-ranlib)
81-
82-
if( \$ENV{CC} MATCHES ccache )
83-
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
84-
endif()
8572
"""
8673
else
87-
return """
88-
# CMake toolchain file for $(c_compiler(bt)) running on $(target)
89-
set($(system_name_var) $(cmake_os(p)))
90-
set(CMAKE_SYSTEM_PROCESSOR $(cmake_arch(p)))
74+
if !is_host
75+
file *= """
76+
execute_process(COMMAND /usr/bin/uname -r OUTPUT_VARIABLE CMAKE_SYSTEM_VERSION)
9177
78+
"""
79+
end
80+
file *= """
9281
set(CMAKE_SYSROOT /opt/$(aatarget)/$(aatarget)/sys-root/)
82+
"""
83+
end
84+
85+
file *= """
9386
set(CMAKE_INSTALL_PREFIX \$ENV{prefix})
9487
9588
set(CMAKE_C_COMPILER /opt/bin/$(target)/$(aatarget)-$(c_compiler(bt)))
@@ -107,7 +100,6 @@ function toolchain_file(bt::CMake, p::AbstractPlatform; is_host::Bool=false)
107100
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
108101
endif()
109102
"""
110-
end
111103
end
112104

113105
meson_c_args(p::AbstractPlatform, envs::Dict{String,String}; is_host::Bool=false) =
@@ -229,12 +221,12 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
229221
for compiler in (:clang, :gcc)
230222
# Target toolchains
231223
if platforms_match(p, platform)
232-
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p; is_host=false))
224+
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false))
233225
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false))
234226
end
235227
# Host toolchains
236228
if platforms_match(p, host_platform)
237-
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p; is_host=true))
229+
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true))
238230
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true))
239231
end
240232
end

0 commit comments

Comments
 (0)