Skip to content

Commit f3da94a

Browse files
committed
yokai: detect host architecture
1 parent 8facee1 commit f3da94a

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

cmake/Yokai/Architecture.cmake

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,89 @@
3434
option(USE_ARCH_INTRINSICS "Enable custom code using intrinsics functions or asm declarations" ON)
3535
mark_as_advanced(USE_ARCH_INTRINSICS)
3636

37+
function(yokai_detect_host_arch)
38+
set(arch_name "unknown")
39+
40+
string(TOLOWER "${CMAKE_HOST_SYSTEM_PROCESSOR}" processor_lower)
41+
42+
foreach(name
43+
"amd64"
44+
"arm"
45+
"arm64"
46+
"i686"
47+
"mipsel"
48+
"ppc64"
49+
"riscv64"
50+
)
51+
if ("${processor_lower}" STREQUAL "${name}")
52+
set(arch_name "${processor_lower}")
53+
endif()
54+
endforeach()
55+
56+
if ("${arch_name}" STREQUAL "unknown")
57+
set(ARCHES_amd64 "em64t" "x86_64")
58+
set(ARCHES_arm "armv6l" "armv7l")
59+
set(ARCHES_arm64 "armv8l" "aarch64")
60+
set(ARCHES_loong64 "loongarch64")
61+
set(ARCHES_ppc64el "ppc64le")
62+
63+
foreach(name
64+
"amd64"
65+
"arm"
66+
"arm64"
67+
"loong64"
68+
"ppc64el"
69+
)
70+
if ("${processor_lower}" IN_LIST ARCHES_${name})
71+
set(arch_name "${name}")
72+
endif()
73+
endforeach()
74+
endif()
75+
76+
if ("${arch_name}" STREQUAL "unknown")
77+
if ("${processor_lower}")
78+
message(WARNING "Undocumented host architecture: ${processor_lower}")
79+
else()
80+
message(WARNING "Undocumented host architecture")
81+
endif()
82+
83+
set(arch_name "${processor_lower}")
84+
endif()
85+
86+
string(TOUPPER "${arch_name}" arch_name_upper)
87+
88+
set(YOKAI_HOST_ARCH_NAME "${arch_name}" PARENT_SCOPE)
89+
set(YOKAI_HOST_ARCH_NAME_UPPER "${arch_name_upper}" PARENT_SCOPE)
90+
91+
# Makes possible to do that in CMake code:
92+
# > if (YOKAI_HOST_ARCH_AMD64)
93+
set("YOKAI_HOST_ARCH_${arch_name_upper}" ON PARENT_SCOPE)
94+
95+
if ("${arch_name}" STREQUAL "unknown")
96+
message(WARNING "Unknown host architecture: ${processor_lower}")
97+
98+
set(arch_name "${processor_lower}")
99+
endif()
100+
101+
set(arm_CHILD
102+
"armel"
103+
"armhf"
104+
)
105+
106+
foreach(name
107+
"arm"
108+
)
109+
if ("${arch_name}" STREQUAL "${name}")
110+
message(STATUS "Parent architecture: ${name}, can be ${${name}_CHILD}")
111+
112+
foreach(child_name ${${name}_CHILD})
113+
string(TOUPPER "${child_name}" child_name_upper)
114+
set(YOKAI_HOST_ARCH_${child_name_upper}_PARENT ON PARENT_SCOPE)
115+
endforeach()
116+
endif()
117+
endforeach()
118+
endfunction()
119+
37120
function(yokai_detect_target_arch)
38121
yokai_run_detection("TARGET" "ARCH" "Architecture.c" "")
39122

@@ -75,8 +158,25 @@ function(yokai_set_intrinsics)
75158
endif()
76159
endfunction()
77160

161+
yokai_detect_host_arch()
78162
yokai_detect_target_arch()
79163

164+
if (YOKAI_HOST_ARCH_UNKNOWN AND NOT YOKAI_TARGET_ARCH_UNKNOWN)
165+
message(WARNING "Assuming the host architecture is the same as the target: ${YOKAI_TARGET_ARCH_NAME}")
166+
set(YOKAI_HOST_ARCH_NAME "${YOKAI_TARGET_ARCH_NAME}")
167+
set(YOKAI_HOST_ARCH_NAME_UPPER "${YOKAI_TARGET_ARCH_NAME_UPPER}")
168+
set(YOKAI_HOST_ARCH_${YOKAI_HOST_ARCH_NAME_UPPER} ON)
169+
unset(YOKAI_HOST_ARCH_UNKNOWN)
170+
endif()
171+
172+
if (YOKAI_TARGET_ARCH_UNKNOWN AND NOT YOKAI_HOST_ARCH_UNKNOWN)
173+
message(WARNING "Assuming the target architecture is the same as the host: ${YOKAI_TARGET_ARCH_NAME}")
174+
set(YOKAI_TARGET_ARCH_NAME "${YOKAI_HOST_ARCH_NAME}")
175+
set(YOKAI_TARGET_ARCH_NAME_UPPER "${YOKAI_HOST_ARCH_NAME_UPPER}")
176+
set(YOKAI_TARGET_ARCH_${YOKAI_TARGET_ARCH_NAME_UPPER} ON)
177+
unset(YOKAI_TARGET_ARCH_UNKNOWN)
178+
endif()
179+
80180
if (YOKAI_HOST_ARCH_UNKNOWN)
81181
message(WARNING "Unknown host architecture")
82182
else()
@@ -89,6 +189,17 @@ else()
89189
message(STATUS "Detected target architecture: ${YOKAI_TARGET_ARCH_NAME}")
90190
endif()
91191

192+
if (NOT "${YOKAI_HOST_ARCH_NAME}" STREQUAL "${YOKAI_TARGET_ARCH_NAME}")
193+
if ("${YOKAI_HOST_ARCH_${YOKAI_TARGET_ARCH_NAME}_PARENT}")
194+
message(STATUS "Assuming no architecture cross-compilation")
195+
else()
196+
message(STATUS "Detected architecture cross-compilation")
197+
set(YOKAI_TARGET_CROSS ON)
198+
endif()
199+
else()
200+
message(STATUS "No architecture cross-compilation detected")
201+
endif()
202+
92203
if (YOKAI_SOURCE_GENERATOR)
93204
# Add printable strings to the executable.
94205
yokai_add_buildinfo("char*" "YOKAI_ARCH_STRING" "\"${YOKAI_TARGET_ARCH_NAME}\"")

0 commit comments

Comments
 (0)