From d3fd7510675f441c861119b625816149638abb41 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 28 Feb 2022 19:23:06 -0800 Subject: [PATCH] refactor: make detect_architecture a generic utility function --- src/Utilities.cmake | 44 +++++++++++++++++++++++++++++++++++++++++ src/VCEnvironment.cmake | 26 +----------------------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/Utilities.cmake b/src/Utilities.cmake index 6fa78b28..a35b26a9 100644 --- a/src/Utilities.cmake +++ b/src/Utilities.cmake @@ -137,3 +137,47 @@ function(is_verbose var) PARENT_SCOPE) endif() endfunction() + +# detect the architecture of the target build system or the host system as a fallback +function(detect_architecture arch) + # if the target processor is not known, fallback to the host processor + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "" + AND NOT + "${CMAKE_HOST_SYSTEM_PROCESSOR}" + STREQUAL + "") + set(_arch "${CMAKE_HOST_SYSTEM_PROCESSOR}") + else() + set(_arch "${CMAKE_SYSTEM_PROCESSOR}") + endif() + + # make it lowercase for comparison + string(TOLOWER "${_arch}" _arch) + + if(_arch STREQUAL x86 OR _arch MATCHES "^i[3456]86$") + set(${arch} + x86 + PARENT_SCOPE) + elseif( + _arch STREQUAL x64 + OR _arch STREQUAL x86_64 + OR _arch STREQUAL amd64) + set(${arch} + x64 + PARENT_SCOPE) + elseif(_arch STREQUAL arm) + set(${arch} + arm + PARENT_SCOPE) + elseif(_arch STREQUAL arm64 OR _arch STREQUAL aarch64) + set(${arch} + arm64 + PARENT_SCOPE) + else() + # fallback to the most common architecture + message(STATUS "Unknown architecture ${_arch} - using x64") + set(${arch} + x64 + PARENT_SCOPE) + endif() +endfunction() diff --git a/src/VCEnvironment.cmake b/src/VCEnvironment.cmake index 86866f61..5714b4ee 100644 --- a/src/VCEnvironment.cmake +++ b/src/VCEnvironment.cmake @@ -1,29 +1,5 @@ include("${ProjectOptions_SRC_DIR}/Utilities.cmake") -macro(detect_architecture) - # detect the architecture - string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER) - if(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL x86 OR CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "^i[3456]86$") - set(VCVARSALL_ARCH x86) - elseif( - CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL x64 - OR CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL x86_64 - OR CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL amd64) - set(VCVARSALL_ARCH x64) - elseif(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL arm) - set(VCVARSALL_ARCH arm) - elseif(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL arm64 OR CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL aarch64) - set(VCVARSALL_ARCH arm64) - else() - if(CMAKE_HOST_SYSTEM_PROCESSOR) - set(VCVARSALL_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR}) - else() - set(VCVARSALL_ARCH x64) - message(STATUS "Unkown architecture CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR_LOWER} - using x64") - endif() - endif() -endmacro() - # Run vcvarsall.bat and set CMake environment variables function(run_vcvarsall) # if MSVC but VSCMD_VER is not set, which means vcvarsall has not run @@ -43,7 +19,7 @@ function(run_vcvarsall) if(EXISTS ${VCVARSALL_FILE}) # detect the architecture - detect_architecture() + detect_architecture(VCVARSALL_ARCH) # run vcvarsall and print the environment variables message(STATUS "Running `${VCVARSALL_FILE} ${VCVARSALL_ARCH}` to set up the MSVC environment")