From eb26a1941edd76b11066fe3e013bdf060308a25b Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Fri, 28 Aug 2015 16:55:22 -0300 Subject: [PATCH] CMake: Fix architecture detection on MSVC CMAKE_SYSTEM_ARCHICTETURE always returns the *host* not target arch when using the MSVC generators. (CMake bugs 15170 and 14342.) --- CMakeLists.txt | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ebffc0d85..967e7ff24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,23 @@ # dependent libraries. cmake_minimum_required(VERSION 2.8.11) +include(CheckSymbolExists) +function(detect_architecture symbol arch) + if (NOT DEFINED ARCHITECTURE) + set(CMAKE_REQUIRED_QUIET 1) + check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch}) + unset(CMAKE_REQUIRED_QUIET) + + # The output variable needs to be unique across invocations otherwise + # CMake's crazy scope rules will keep it defined + if (ARCHITECTURE_${arch}) + set(ARCHITECTURE "${arch}" PARENT_SCOPE) + set(ARCHITECTURE_${arch} 1 PARENT_SCOPE) + add_definitions(-DARCHITECTURE_${arch}=1) + endif() + endif() +endfunction() + project(citra) if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit) @@ -10,12 +27,21 @@ if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit) DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks) endif() -# Platform-agnostic definition to check if we are on x86_64 -if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "[xX]86_64" OR - ${CMAKE_SYSTEM_PROCESSOR} MATCHES "[aA][mM][dD]64") - set(ARCHITECTURE_x86_64 1) - add_definitions(-DARCHITECTURE_x86_64=1) +if (MSVC) + detect_architecture("_M_AMD64" x86_64) + detect_architecture("_M_IX86" x86) + detect_architecture("_M_ARM" ARM) +else() + detect_architecture("__x86_64__" x86_64) + detect_architecture("__i386__" x86) + detect_architecture("__arm__" ARM) endif() +if (NOT DEFINED ARCHITECTURE) + set(ARCHITECTURE "GENERIC") + set(ARCHITECTURE_GENERIC 1) + add_definitions(-DARCHITECTURE_GENERIC=1) +endif() +message(STATUS "Target architecture: ${ARCHITECTURE}") if (NOT MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-attributes -pthread")