Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make detect_architecture a generic utility function #87

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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