Skip to content

Commit fad86b6

Browse files
committed
Fix ARM64 cross-compilation target platform detection
When cross-compiling, CMAKE_SYSTEM_PROCESSOR may incorrectly reflect the build host instead of the target. Use the compiler's -dumpmachine output to detect the actual target architecture. Fixes #25201
1 parent 1ed8fd9 commit fad86b6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cmake/detect_onnxruntime_target_platform.cmake

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,27 @@ else()
4646
#image on an aarch64 machine with an aarch64 Ubuntu host OS, in the docker instance cmake may still report
4747
# CMAKE_SYSTEM_PROCESSOR as aarch64 by default. Given compiling this code may need more than 2GB memory, we do not
4848
# support compiling for ARM32 natively(only support cross-compiling), we will ignore this issue for now.
49-
if(NOT CMAKE_SYSTEM_PROCESSOR)
49+
50+
# When cross-compiling, detect target architecture from the compiler's target triple
51+
# since CMAKE_SYSTEM_PROCESSOR may incorrectly reflect the build host.
52+
if(CMAKE_CROSSCOMPILING AND ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"))
53+
execute_process(
54+
COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
55+
OUTPUT_VARIABLE GCC_DUMP_MACHINE_OUT
56+
OUTPUT_STRIP_TRAILING_WHITESPACE
57+
ERROR_VARIABLE _err
58+
RESULT_VARIABLE _res
59+
)
60+
if(_res EQUAL 0)
61+
string(REPLACE "-" ";" GCC_DUMP_MACHINE_OUT_LIST "${GCC_DUMP_MACHINE_OUT}")
62+
list(LENGTH GCC_DUMP_MACHINE_OUT_LIST GCC_TRIPLET_LEN)
63+
if(GCC_TRIPLET_LEN GREATER_EQUAL 1)
64+
list(GET GCC_DUMP_MACHINE_OUT_LIST 0 _detected_arch)
65+
message(STATUS "Cross-compiling: detected target architecture '${_detected_arch}' from compiler")
66+
set(CMAKE_SYSTEM_PROCESSOR ${_detected_arch})
67+
endif()
68+
endif()
69+
elseif(NOT CMAKE_SYSTEM_PROCESSOR)
5070
message(WARNING "CMAKE_SYSTEM_PROCESSOR is not set. Please set it in your toolchain cmake file.")
5171
# Try to detect it
5272
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")

0 commit comments

Comments
 (0)