Skip to content

Commit 9d5e194

Browse files
committed
yokai: detect host architecture
1 parent 839cacb commit 9d5e194

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

‎cmake/Yokai/Architecture.cmake‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,87 @@
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+
"arm"
44+
"arm64"
45+
"i686"
46+
"mipsel"
47+
"ppc64"
48+
"riscv64"
49+
)
50+
if ("${processor_lower}" STREQUAL "${name}")
51+
set(arch_name "${processor_lower}")
52+
endif()
53+
endforeach()
54+
55+
if ("${arch_name}" STREQUAL "unknown")
56+
set(ARCH_em64t "amd64")
57+
set(ARCH_x86_64 "amd64")
58+
set(ARCH_armv7l, "arm")
59+
set(ARCH_armv6l, "arm")
60+
set(ARCH_aarch64 "arm64")
61+
set(ARCH_loongarch64, "loong64")
62+
set(ARCH_ppc64le, "ppc64el")
63+
64+
foreach(name
65+
"amd64"
66+
"arm"
67+
)
68+
if ("${ARCH_${processor_lower}}" STREQUAL "${name}")
69+
set(arch_name "${ARCH_${processor_lower}}")
70+
endif()
71+
endforeach()
72+
endif()
73+
74+
if ("${arch_name}" STREQUAL "unknown")
75+
if ("${processor_lower}")
76+
message(WARNING "Undocumented host architecture: ${processor_lower}")
77+
else()
78+
message(WARNING "Undocumented host architecture")
79+
endif()
80+
81+
set(arch_name "${processor_lower}")
82+
endif()
83+
84+
string(TOUPPER "${arch_name}" arch_name_upper)
85+
86+
set(YOKAI_HOST_ARCH_NAME "${arch_name}" PARENT_SCOPE)
87+
set(YOKAI_HOST_ARCH_NAME_UPPER "${arch_name_upper}" PARENT_SCOPE)
88+
89+
# Makes possible to do that in CMake code:
90+
# > if (YOKAI_HOST_ARCH_AMD64)
91+
set("YOKAI_HOST_ARCH_${arch_name_upper}" ON PARENT_SCOPE)
92+
93+
if ("${arch_name}" STREQUAL "unknown")
94+
message(WARNING "Unknown host architecture: ${processor_lower}")
95+
96+
set(arch_name "${processor_lower}")
97+
endif()
98+
99+
set(arm_CHILD
100+
"armel"
101+
"armhf"
102+
)
103+
104+
foreach(name
105+
"arm"
106+
)
107+
if ("${arch_name}" STREQUAL "${name}")
108+
message(STATUS "Parent architecture: ${name}, can be ${${name}_CHILD}")
109+
110+
foreach(child_name ${${name}_CHILD})
111+
string(TOUPPER "${child_name}" child_name_upper)
112+
set(YOKAI_HOST_ARCH_${child_name_upper}_PARENT ON PARENT_SCOPE)
113+
endforeach()
114+
endif()
115+
endforeach()
116+
endfunction()
117+
37118
function(yokai_detect_target_arch)
38119
yokai_run_detection("TARGET" "ARCH" "Architecture.c" "")
39120

@@ -75,8 +156,25 @@ function(yokai_set_intrinsics)
75156
endif()
76157
endfunction()
77158

159+
yokai_detect_host_arch()
78160
yokai_detect_target_arch()
79161

162+
if (YOKAI_HOST_ARCH_UNKNOWN AND NOT YOKAI_TARGET_ARCH_UNKNOWN)
163+
message(WARNING "Assuming the host architecture is the same as the target: ${YOKAI_TARGET_ARCH_NAME}")
164+
set(YOKAI_HOST_ARCH_NAME "${YOKAI_TARGET_ARCH_NAME}")
165+
set(YOKAI_HOST_ARCH_NAME_UPPER "${YOKAI_TARGET_ARCH_NAME_UPPER}")
166+
set(YOKAI_HOST_ARCH_${YOKAI_HOST_ARCH_NAME_UPPER} ON)
167+
unset(YOKAI_HOST_ARCH_UNKNOWN)
168+
endif()
169+
170+
if (YOKAI_TARGET_ARCH_UNKNOWN AND NOT YOKAI_HOST_ARCH_UNKNOWN)
171+
message(WARNING "Assuming the target architecture is the same as the host: ${YOKAI_TARGET_ARCH_NAME}")
172+
set(YOKAI_TARGET_ARCH_NAME "${YOKAI_HOST_ARCH_NAME}")
173+
set(YOKAI_TARGET_ARCH_NAME_UPPER "${YOKAI_HOST_ARCH_NAME_UPPER}")
174+
set(YOKAI_TARGET_ARCH_${YOKAI_TARGET_ARCH_NAME_UPPER} ON)
175+
unset(YOKAI_TARGET_ARCH_UNKNOWN)
176+
endif()
177+
80178
if (YOKAI_HOST_ARCH_UNKNOWN)
81179
message(WARNING "Unknown host architecture")
82180
else()
@@ -89,6 +187,17 @@ else()
89187
message(STATUS "Detected target architecture: ${YOKAI_TARGET_ARCH_NAME}")
90188
endif()
91189

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

0 commit comments

Comments
 (0)