cmake: fall back to per-cpu test when all-in-one test fails

The 'all-in-one' test fails for Emscripten because try_compile does not copy back the actual binary:
the wasm file contains the machine parseable strings, not the html or js file(s).
This commit is contained in:
Anonymous Maarten 2024-05-30 20:45:14 +02:00
parent 8f88c32ca6
commit 4b0f52b04e
1 changed files with 96 additions and 100 deletions

View File

@ -1,10 +1,10 @@
function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS) function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
set(DETECTABLE_ARCHS ARM32 ARM64 EMSCRIPTEN LOONGARCH64 POWERPC32 POWERPC64 X86 X64) set(known_archs EMSCRIPTEN ARM32 ARM64 LOONGARCH64 POWERPC32 POWERPC64 X86 X64)
if(APPLE AND CMAKE_OSX_ARCHITECTURES) if(APPLE AND CMAKE_OSX_ARCHITECTURES)
foreach(arch IN LISTS DETECTABLE_ARCHS) foreach(known_arch IN LISTS known_archs)
set(SDL_CPU_${arch} "0") set(SDL_CPU_${known_arch} "0")
endforeach() endforeach()
set(detected_archs) set(detected_archs)
foreach(osx_arch IN LISTS CMAKE_OSX_ARCHITECTURES) foreach(osx_arch IN LISTS CMAKE_OSX_ARCHITECTURES)
@ -21,9 +21,9 @@ function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
endif() endif()
set(detected_archs) set(detected_archs)
foreach(arch IN LISTS DETECTABLE_ARCHS) foreach(known_arch IN LISTS known_archs)
if(SDL_CPU_${arch}) if(SDL_CPU_${known_arch})
list(APPEND detected_archs "${arch}") list(APPEND detected_archs "${known_arch}")
endif() endif()
endforeach() endforeach()
@ -32,118 +32,114 @@ function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
return() return()
endif() endif()
set(src_arch_detect [=====[ set(arch_check_ARM32 "defined(__arm__) || defined(_M_ARM)")
set(arch_check_ARM64 "defined(__aarch64__) || defined(_M_ARM64)")
set(arch_check_EMSCRIPTEN "defined(__EMSCRIPTEN__)")
set(arch_check_LOONGARCH64 "defined(__loongarch64)")
set(arch_check_POWERPC32 "(defined(__PPC__) || defined(__powerpc__)) && !defined(__powerpc64__)")
set(arch_check_POWERPC64 "defined(__PPC64__) || defined(__powerpc64__)")
set(arch_check_X86 "defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)")
set(arch_check_X64 "defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)")
#if defined(__arm__) || defined(_M_ARM) set(src_vars "")
#define ARCH_ARM32 "1" set(src_main "")
foreach(known_arch IN LISTS known_archs)
set(detected_${known_arch} "0")
string(APPEND src_vars "
#if ${arch_check_${known_arch}}
#define ARCH_${known_arch} \"1\"
#else #else
#define ARCH_ARM32 "0" #define ARCH_${known_arch} \"0\"
#endif #endif
const char *arch_arm32 = "INFO<ARM32=" ARCH_ARM32 ">"; const char *arch_${known_arch} = \"INFO<${known_arch}=\" ARCH_${known_arch} \">\";
")
#if defined(__aarch64__) || defined(_M_ARM64) string(APPEND src_main "
#define ARCH_ARM64 "1" result += arch_${known_arch}[argc];")
#else endforeach()
#define ARCH_ARM64 "0"
#endif
const char *arch_arm64 = "INFO<ARM64=" ARCH_ARM64 ">";
#if defined(__EMSCRIPTEN__)
#define ARCH_EMSCRIPTEN "1"
#else
#define ARCH_EMSCRIPTEN "0"
#endif
const char *arch_emscripten = "INFO<EMSCRIPTEN=" ARCH_EMSCRIPTEN ">";
#if defined(__loongarch64)
#define ARCH_LOONGARCH64 "1"
#else
#define ARCH_LOONGARCH64 "0"
#endif
const char *arch_loongarch64 = "INFO<LOONGARCH64=" ARCH_LOONGARCH64 ">";
#if (defined(__PPC__) || defined(__powerpc__)) && !defined(__powerpc64__)
#define ARCH_POWERPC32 "1"
#else
#define ARCH_POWERPC32 "0"
#endif
const char *arch_powerpc32 = "INFO<ARCH_POWERPC32=" ARCH_POWERPC32 ">";
#if defined(__PPC64__) || defined(__powerpc64__)
#define ARCH_POWERPC64 "1"
#else
#define ARCH_POWERPC64 "0"
#endif
const char *arch_powerpc64 = "INFO<POWERPC64=" ARCH_POWERPC64 ">";
#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)
#define ARCH_X86 "1"
#else
#define ARCH_X86 "0"
#endif
const char *arch_x86 = "INFO<X86=" ARCH_X86 ">";
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#define ARCH_X64 "1"
#else
#define ARCH_X64 "0"
#endif
const char *arch_x64 = "INFO<X64=" ARCH_X64 ">";
set(src_arch_detect "${src_vars}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
(void) argv; (void)argv;
int result = argc; int result = 0;
result += arch_arm32[argc]; ${src_main}
result += arch_arm64[argc];
result += arch_emscripten[argc];
result += arch_loongarch64[argc];
result += arch_powerpc32[argc];
result += arch_powerpc64[argc];
result += arch_x86[argc];
result += arch_x64[argc];
return result; return result;
} }")
]=====])
set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch.c") set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch.c")
file(WRITE "${path_src_arch_detect}" "${src_arch_detect}") file(WRITE "${path_src_arch_detect}" "${src_arch_detect}")
set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch") set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch")
set(path_bin_arch_detect "${path_dir_arch_detect}/bin") set(path_bin_arch_detect "${path_dir_arch_detect}/bin")
set(detected_archs)
set(msg "Detecting Target CPU Architecture") set(msg "Detecting Target CPU Architecture")
message(STATUS "${msg}") message(STATUS "${msg}")
try_compile(COMPILED_RES include(CMakePushCheckState)
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
SOURCES "${path_src_arch_detect}" cmake_push_check_state(RESET)
COPY_FILE "${path_bin_arch_detect}" try_compile(SDL_CPU_CHECK_ALL
) "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
if(NOT COMPILED_RES) SOURCES "${path_src_arch_detect}"
COPY_FILE "${path_bin_arch_detect}"
)
cmake_pop_check_state()
if(NOT SDL_CPU_CHECK_ALL)
message(STATUS "${msg} - <ERROR>") message(STATUS "${msg} - <ERROR>")
message(WARNING "Failed to compile source detecting the target CPU architecture") message(WARNING "Failed to compile source detecting the target CPU architecture")
else()
set(re "INFO<([A-Z0-9]+)=([01])>")
file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}")
foreach(info_arch_01 IN LISTS infos)
string(REGEX MATCH "${re}" A "${info_arch_01}")
if(NOT "${CMAKE_MATCH_1}" IN_LIST known_archs)
message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"")
continue()
endif()
set(arch "${CMAKE_MATCH_1}")
set(arch_01 "${CMAKE_MATCH_2}")
set(detected_${arch} "${arch_01}")
endforeach()
foreach(known_arch IN LISTS known_archs)
if(detected_${known_arch})
list(APPEND detected_archs ${known_arch})
endif()
endforeach()
endif() endif()
set(re "INFO<([A-Z0-9]+)=([01])>") if(detected_archs)
file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}") foreach(known_arch IN LISTS known_archs)
set("SDL_CPU_${known_arch}" "${detected_${known_arch}}" CACHE BOOL "Detected architecture ${known_arch}")
set(detected_archs) endforeach()
message(STATUS "${msg} - ${detected_archs}")
foreach(info_arch_01 IN LISTS infos) else()
string(REGEX MATCH "${re}" A "${info_arch_01}") include(CheckCSourceCompiles)
if(NOT "${CMAKE_MATCH_1}" IN_LIST DETECTABLE_ARCHS) cmake_push_check_state(RESET)
message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"") foreach(known_arch IN LISTS known_archs)
continue() if(NOT detected_archs)
endif() set(cache_variable "SDL_CPU_${known_arch}")
set(arch "${CMAKE_MATCH_1}") set(test_src "
set(arch_01 "${CMAKE_MATCH_2}") int main(int argc, char *argv[]) {
if(arch_01) #if ${arch_check_${known_arch}}
list(APPEND detected_archs "${arch}") return 0;
endif() #else
set("SDL_CPU_${arch}" "${arch_01}" CACHE BOOL "Detected architecture ${arch}") choke
endforeach() #endif
}
message(STATUS "${msg} - ${detected_archs}") ")
check_c_source_compiles("${test_src}" "${cache_variable}")
if(${cache_variable})
set(SDL_CPU_${known_arch} "1" CACHE BOOL "Detected architecture ${known_arch}")
set(detected_archs ${known_arch})
else()
set(SDL_CPU_${known_arch} "0" CACHE BOOL "Detected architecture ${known_arch}")
endif()
endif()
endforeach()
cmake_pop_check_state()
endif()
set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE) set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
endfunction() endfunction()