CMake variables in SCRIPT role
When running CMake standalone scripts like
cmake -P script.cmake
this is the SCRIPT
CMake role.
Not all
CMake information variables
are set in SCRIPT
role, in particular, the CMAKE_HOST*
and CMAKE_SYSTEM*
variables are not set as they are in PROJECT
role.
This is a workaround for cmake -P
SCRIPT role to get the CMAKE_HOST_*
variables.
It uses undocumented CMake-internal scripts, but they’ve been present since 2012 and may be unlikely to change.
message(STATUS "CMake ${CMAKE_VERSION}")
get_property(cmake_role GLOBAL PROPERTY CMAKE_ROLE)
if(cmake_role STREQUAL "SCRIPT")
set(CMAKE_PLATFORM_INFO_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
# define CMAKE_HOST*, CMAKE_SYSTEM*, etc.
include(${CMAKE_ROOT}/Modules/CMakeDetermineSystem.cmake)
# set booleans like CYGWIN
include(${CMAKE_ROOT}/Modules/CMakeSystemSpecificInitialize.cmake)
endif()
message(STATUS "CMAKE_SYSTEM: ${CMAKE_SYSTEM}")
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_VERSION: ${CMAKE_SYSTEM_VERSION}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "CMAKE_HOST_SYSTEM: ${CMAKE_HOST_SYSTEM}")
message(STATUS "CMAKE_HOST_SYSTEM_NAME: ${CMAKE_HOST_SYSTEM_NAME}")
message(STATUS "CMAKE_HOST_SYSTEM_VERSION: ${CMAKE_HOST_SYSTEM_VERSION}")
message(STATUS "CMAKE_HOST_SYSTEM_PROCESSOR: ${CMAKE_HOST_SYSTEM_PROCESSOR}")