2025-10-07 17:10:37 -04:00
|
|
|
# SPDX-FileCopyrightText: 2025 crueter
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
## DetectArchitecture ##
|
|
|
|
#[[
|
|
|
|
Does exactly as it sounds. Detects common symbols defined for different architectures and
|
|
|
|
adds compile definitions thereof. Namely:
|
|
|
|
- arm64
|
|
|
|
- arm
|
|
|
|
- x86_64
|
|
|
|
- x86
|
|
|
|
- ia64
|
2025-10-08 13:11:30 -04:00
|
|
|
- mips64
|
2025-10-07 17:10:37 -04:00
|
|
|
- mips
|
|
|
|
- ppc64
|
|
|
|
- ppc
|
|
|
|
- riscv
|
2025-10-08 13:11:30 -04:00
|
|
|
- riscv64
|
|
|
|
- loongarch64
|
2025-10-07 17:10:37 -04:00
|
|
|
- wasm
|
|
|
|
|
2025-10-08 00:48:33 -04:00
|
|
|
Unsupported architectures:
|
|
|
|
- ARMv2-6
|
|
|
|
- m68k
|
|
|
|
- PIC
|
|
|
|
|
|
|
|
This file WILL NOT detect endian-ness for you.
|
|
|
|
|
2025-10-07 17:10:37 -04:00
|
|
|
This file is based off of Yuzu and Dynarmic.
|
|
|
|
]]
|
2025-05-31 02:33:02 -04:00
|
|
|
|
2025-10-07 17:10:37 -04:00
|
|
|
# multiarch builds are a special case and also very difficult
|
|
|
|
# this is what I have for now, but it's not ideal
|
|
|
|
|
|
|
|
# Do note that situations where multiple architectures are defined
|
|
|
|
# should NOT be too dependent on the architecture
|
|
|
|
# otherwise, you may end up with duplicate code
|
2025-05-31 02:33:02 -04:00
|
|
|
if (CMAKE_OSX_ARCHITECTURES)
|
2025-10-07 17:10:37 -04:00
|
|
|
set(MULTIARCH_BUILD 1)
|
2025-05-31 02:33:02 -04:00
|
|
|
set(ARCHITECTURE "${CMAKE_OSX_ARCHITECTURES}")
|
2025-10-07 17:10:37 -04:00
|
|
|
|
|
|
|
# hope and pray the architecture names match
|
|
|
|
foreach(ARCH IN ${CMAKE_OSX_ARCHITECTURES})
|
|
|
|
set(ARCHITECTURE_${ARCH} 1 PARENT_SCOPE)
|
|
|
|
add_definitions(-DARCHITECTURE_${ARCH}=1)
|
|
|
|
endforeach()
|
|
|
|
|
2025-05-31 02:33:02 -04:00
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
include(CheckSymbolExists)
|
2025-10-07 23:37:16 -04:00
|
|
|
function(detect_architecture symbol arch)
|
2025-10-08 00:29:06 -04:00
|
|
|
# The output variable needs to be unset between invocations otherwise
|
|
|
|
# CMake's crazy scope rules will keep it defined
|
2025-10-08 00:19:32 -04:00
|
|
|
unset(SYMBOL_EXISTS CACHE)
|
2025-10-08 00:29:06 -04:00
|
|
|
|
2025-05-31 02:33:02 -04:00
|
|
|
if (NOT DEFINED ARCHITECTURE)
|
2025-10-07 17:10:37 -04:00
|
|
|
set(CMAKE_REQUIRED_QUIET 1)
|
2025-10-08 00:19:32 -04:00
|
|
|
check_symbol_exists("${symbol}" "" SYMBOL_EXISTS)
|
2025-05-31 02:33:02 -04:00
|
|
|
unset(CMAKE_REQUIRED_QUIET)
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
if (SYMBOL_EXISTS)
|
|
|
|
set(ARCHITECTURE "${arch}" PARENT_SCOPE)
|
|
|
|
set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
|
|
|
|
add_definitions(-DARCHITECTURE_${arch}=1)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
function(detect_architecture_symbols)
|
|
|
|
if (DEFINED ARCHITECTURE)
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set(oneValueArgs ARCH)
|
|
|
|
set(multiValueArgs SYMBOLS)
|
|
|
|
|
|
|
|
cmake_parse_arguments(ARGS "" "${oneValueArgs}" "${multiValueArgs}"
|
|
|
|
"${ARGN}")
|
|
|
|
|
|
|
|
set(arch "${ARGS_ARCH}")
|
|
|
|
foreach(symbol ${ARGS_SYMBOLS})
|
|
|
|
detect_architecture("${symbol}" "${arch}")
|
|
|
|
|
2025-10-07 17:10:37 -04:00
|
|
|
if (ARCHITECTURE_${arch})
|
2025-10-08 00:29:06 -04:00
|
|
|
message(DEBUG "[DetectArchitecture] Found architecture symbol ${symbol} for ${arch}")
|
2025-10-07 21:30:56 -04:00
|
|
|
set(ARCHITECTURE "${arch}" PARENT_SCOPE)
|
|
|
|
set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
|
2025-10-07 17:10:37 -04:00
|
|
|
add_definitions(-DARCHITECTURE_${arch}=1)
|
2025-10-08 00:19:32 -04:00
|
|
|
|
|
|
|
return()
|
2025-05-31 02:33:02 -04:00
|
|
|
endif()
|
2025-10-08 00:19:32 -04:00
|
|
|
endforeach()
|
2025-10-07 23:37:16 -04:00
|
|
|
endfunction()
|
2025-10-07 21:30:56 -04:00
|
|
|
|
2025-10-07 22:23:16 -04:00
|
|
|
function(DetectArchitecture)
|
2025-10-08 00:29:06 -04:00
|
|
|
# arches here are put in a sane default order of importance
|
|
|
|
# notably, amd64, arm64, and riscv (in order) are BY FAR the most common
|
|
|
|
# mips is pretty popular in embedded
|
|
|
|
# ppc64 is pretty popular in supercomputing
|
2025-10-08 00:48:33 -04:00
|
|
|
# sparc is uh
|
2025-10-08 00:29:06 -04:00
|
|
|
# ia64 exists
|
2025-10-08 00:48:33 -04:00
|
|
|
# the rest exist, but are probably less popular than ia64
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH arm64
|
|
|
|
SYMBOLS
|
|
|
|
"__ARM64__"
|
|
|
|
"__aarch64__"
|
|
|
|
"_M_ARM64")
|
|
|
|
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH x86_64
|
|
|
|
SYMBOLS
|
|
|
|
"__x86_64"
|
|
|
|
"__x86_64__"
|
|
|
|
"__amd64"
|
|
|
|
"_M_X64"
|
|
|
|
"_M_AMD64")
|
|
|
|
|
2025-10-08 13:11:30 -04:00
|
|
|
# riscv is interesting since it generally does not define a riscv64-specific symbol
|
|
|
|
# We can, however, check for the rv32 zcf extension which is good enough of a heuristic on GCC
|
2025-10-08 00:29:06 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH riscv
|
2025-10-08 13:11:30 -04:00
|
|
|
SYMBOLS
|
|
|
|
"__riscv_zcf")
|
|
|
|
|
|
|
|
# if zcf doesn't exist we can safely assume it's riscv64
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH riscv64
|
2025-10-08 00:29:06 -04:00
|
|
|
SYMBOLS
|
|
|
|
"__riscv")
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH x86
|
|
|
|
SYMBOLS
|
|
|
|
"__i386"
|
|
|
|
"__i386__"
|
|
|
|
"_M_IX86")
|
|
|
|
|
2025-10-08 00:29:06 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH arm
|
|
|
|
SYMBOLS
|
|
|
|
"__arm__"
|
|
|
|
"__TARGET_ARCH_ARM"
|
|
|
|
"_M_ARM")
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH ia64
|
|
|
|
SYMBOLS
|
|
|
|
"__ia64"
|
|
|
|
"__ia64__"
|
|
|
|
"_M_IA64")
|
|
|
|
|
2025-10-08 00:48:33 -04:00
|
|
|
# mips is probably the least fun to detect due to microMIPS
|
2025-10-08 13:11:30 -04:00
|
|
|
# Because microMIPS is such cancer I'm considering it out of scope for now
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH mips64
|
|
|
|
SYMBOLS
|
|
|
|
"__mips64")
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH mips
|
|
|
|
SYMBOLS
|
|
|
|
"__mips"
|
|
|
|
"__mips__"
|
|
|
|
"_M_MRX000")
|
|
|
|
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH ppc64
|
|
|
|
SYMBOLS
|
|
|
|
"__ppc64__"
|
2025-10-08 00:48:33 -04:00
|
|
|
"__powerpc64__"
|
|
|
|
"_ARCH_PPC64"
|
|
|
|
"_M_PPC64")
|
2025-10-08 00:19:32 -04:00
|
|
|
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH ppc
|
|
|
|
SYMBOLS
|
|
|
|
"__ppc__"
|
|
|
|
"__ppc"
|
|
|
|
"__powerpc__"
|
|
|
|
"_ARCH_COM"
|
|
|
|
"_ARCH_PWR"
|
|
|
|
"_ARCH_PPC"
|
|
|
|
"_M_MPPC"
|
|
|
|
"_M_PPC")
|
|
|
|
|
2025-10-08 13:11:30 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH sparc64
|
|
|
|
SYMBOLS
|
|
|
|
"__sparc_v9__")
|
|
|
|
|
2025-10-08 00:48:33 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH sparc
|
|
|
|
SYMBOLS
|
|
|
|
"__sparc__"
|
|
|
|
"__sparc")
|
|
|
|
|
2025-10-08 13:11:30 -04:00
|
|
|
# I don't actually know about loongarch32 since crossdev does not support it, only 64
|
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH loongarch64
|
|
|
|
SYMBOLS
|
|
|
|
"__loongarch__"
|
|
|
|
"__loongarch64")
|
|
|
|
|
2025-10-08 00:19:32 -04:00
|
|
|
detect_architecture_symbols(
|
|
|
|
ARCH wasm
|
|
|
|
SYMBOLS
|
|
|
|
"__EMSCRIPTEN__")
|
|
|
|
|
2025-10-08 00:48:33 -04:00
|
|
|
# "generic" target
|
|
|
|
# If you have reached this point, you're on some as-of-yet unsupported architecture.
|
|
|
|
# See the docs up above for known unsupported architectures
|
|
|
|
# If you're not in the list... I think you know what you're doing.
|
2025-10-07 21:30:56 -04:00
|
|
|
if (NOT DEFINED ARCHITECTURE)
|
|
|
|
set(ARCHITECTURE "GENERIC")
|
|
|
|
set(ARCHITECTURE_GENERIC 1)
|
|
|
|
add_definitions(-DARCHITECTURE_GENERIC=1)
|
|
|
|
endif()
|
2025-05-31 02:33:02 -04:00
|
|
|
|
2025-10-08 00:48:33 -04:00
|
|
|
message(STATUS "[DetectArchitecture] Target architecture: ${ARCHITECTURE}")
|
2025-10-07 23:37:16 -04:00
|
|
|
set(ARCHITECTURE "${ARCHITECTURE}" PARENT_SCOPE)
|
|
|
|
set(ARCHITECTURE_${ARCHITECTURE} 1 PARENT_SCOPE)
|
2025-10-07 22:23:16 -04:00
|
|
|
endfunction()
|