CMAKE_SHARED_LIBRARY_SUFFIX in script processing (-P) mode

When I use cmake in -P mode (i.e. executing a script .cmake file) the standard CMake variable for shared library suffixes doesn’t seem to be defined:

~ $ more libsuffix.cmake 
message("CMAKE_SHARED_LIBRARY_SUFFIX: ${CMAKE_SHARED_LIBRARY_SUFFIX}")
~ $ cmake -P libsuffix.cmake 
CMAKE_SHARED_LIBRARY_SUFFIX: 

It seems to work fine in normal usage - the variable is undefined only in -P mode. Can anyone advise on the correct way to access this definition in -P mode?

I suspect this is one of the variables set during platform checks the project() command does, so it just cannot be available in script mode.

Do different build tools use different conventions for this? I thought it was operating system specific, and if so couldn’t that be built in knowledge?

These variables are set by the specific CMake script under ${CMAKE_ROOT}/Modules/Platform

This is undocumented, but this script does work to define CMAKE_SHARED_LIBRARY_SUFFIX

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)

  include(${CMAKE_ROOT}/Modules/CMakeSystemSpecificInformation.cmake)

  include(${CMAKE_ROOT}/Modules/Platform/${CMAKE_SYSTEM_NAME}.cmake)
endif()

include(${CMAKE_ROOT}/Modules/CMakePrintSystemInformation.cmake)

run that in cmake -P and it works

2 Likes