Skip to content

Commit

Permalink
Merge pull request #87 from cpp-best-practices/refactor-detect-arch […
Browse files Browse the repository at this point in the history
…skip ci]
  • Loading branch information
aminya authored Mar 2, 2022
2 parents 2111be3 + d3fd751 commit dbf130c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
44 changes: 44 additions & 0 deletions src/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
26 changes: 1 addition & 25 deletions src/VCEnvironment.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down

0 comments on commit dbf130c

Please sign in to comment.