From bfb092c3ecee53ecd4dd6a0b509564dfa997254e Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Mon, 11 Feb 2019 05:35:24 -0500 Subject: [PATCH] Problem: value for _WIN32_WINNT not always known in CMakeLists.txt Solution: calculate from CMAKE_SYSTEM_VERSION Problem: CMAKE_SYSTEM_VERSION might be newer than Windows SDK Version Solution: limit _WIN32_WINNT value to Visual Studio default Windows SDK version --- CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f305c75..a15697c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,9 +264,51 @@ if(NOT CYGWIN) check_include_files(windows.h ZMQ_HAVE_WINDOWS) endif() +if (WIN32) + # from https://stackoverflow.com/a/40217291/2019765 + macro(get_WIN32_WINNT version) + if (CMAKE_SYSTEM_VERSION) + set(ver ${CMAKE_SYSTEM_VERSION}) + string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver}) + string(REGEX MATCH "^([0-9]+)" verMajor ${ver}) + # Check for Windows 10, b/c we'll need to convert to hex 'A'. + if ("${verMajor}" MATCHES "10") + set(verMajor "A") + string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver}) + endif ("${verMajor}" MATCHES "10") + # Remove all remaining '.' characters. + string(REPLACE "." "" ver ${ver}) + # Prepend each digit with a zero. + string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver}) + set(${version} "0x${ver}") + endif(CMAKE_SYSTEM_VERSION) + endmacro(get_WIN32_WINNT) + + get_WIN32_WINNT(ZMQ_WIN32_WINNT_DEFAULT) + message(STATUS "Detected _WIN32_WINNT from CMAKE_SYSTEM_VERSION: ${ZMQ_WIN32_WINNT_DEFAULT}") + + # TODO limit _WIN32_WINNT to the actual Windows SDK version, which might be different from the default version installed with Visual Studio + if(MSVC_VERSION STREQUAL "1500" AND CMAKE_SYSTEM_VERSION VERSION_GREATER "6.0") + set(ZMQ_WIN32_WINNT_LIMIT "0x0600") + elseif(MSVC_VERSION STREQUAL "1600" AND CMAKE_SYSTEM_VERSION VERSION_GREATER "6.1") + set(ZMQ_WIN32_WINNT_LIMIT "0x0601") + elseif(MSVC_VERSION STREQUAL "1700" AND CMAKE_SYSTEM_VERSION VERSION_GREATER "6.1") + set(ZMQ_WIN32_WINNT_LIMIT "0x0601") + elseif(MSVC_VERSION STREQUAL "1800" AND CMAKE_SYSTEM_VERSION VERSION_GREATER "6.2") + set(ZMQ_WIN32_WINNT_LIMIT "0x0602") + endif() + if(ZMQ_WIN32_WINNT_LIMIT) + message(STATUS "Mismatch of Visual Studio Version (${MSVC_VERSION}) and CMAKE_SYSTEM_VERSION (${CMAKE_SYSTEM_VERSION}), limiting _WIN32_WINNT to ${ZMQ_WIN32_WINNT_LIMIT}, you may override this by setting ZMQ_WIN32_WINNT") + set(ZMQ_WIN32_WINNT_DEFAULT "${ZMQ_WIN32_WINNT_LIMIT}") + endif() + + set(ZMQ_WIN32_WINNT "${ZMQ_WIN32_WINNT_DEFAULT}" CACHE STRING "Value to set _WIN32_WINNT to for building [default=autodetect from build environment]") + + add_definitions(-D_WIN32_WINNT=${ZMQ_WIN32_WINNT}) +endif(WIN32) + if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" AND CMAKE_SYSTEM_VERSION STREQUAL "10.0") set(ZMQ_HAVE_WINDOWS_UWP ON) - add_definitions(-D_WIN32_WINNT=_WIN32_WINNT_WIN10) endif() if(NOT MSVC) check_include_files(ifaddrs.h ZMQ_HAVE_IFADDRS)