How to detect VS 2019 Minor Version

The latest 16.10 update broke our CMake visual studio builds.

They introduced this new “/external” functionality. Which isn’t backward compatible with what we were doing before.

How can I detect the 16.10 version number of the IDE? I’d like to work around the issue.

I would use the CMAKE_CXX_COMPILER_VERSION variable to detect this.

However, it sounds like we have an env-regression on our hands. @brad.king

You guys probably don’t have an env-regression. We are just doing weird stuff that broke. But we would like to have work.

See what we are doing is we are manually replacing all of the following variables:

        # Set the appropriate binary directories
        set(CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES "")
        list(APPEND CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES ${foobar_MSVC_BIN_TOOLSET})
        list(APPEND CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES ${foobar_SDK_BIN_TOOLSET})
        set(CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES ${CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES} PARENT_SCOPE)

        # Don't allow MSVC defaults
        set(CMAKE_VS_SDK_SOURCE_DIRECTORIES "" PARENT_SCOPE)
        set(CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES "" PARENT_SCOPE)
        set(CMAKE_VS_SDK_REFERENCE_DIRECTORIES "" PARENT_SCOPE)
        set(CMAKE_VS_SDK_EXCLUDE_DIRECTORIES "" PARENT_SCOPE)

        # Set include/library directories
        set(CMAKE_VS_SDK_LIBRARY_DIRECTORIES ${foobar_DK_LIBRARY_DIRECTORIES} PARENT_SCOPE)
        set(CMAKE_VS_SDK_INCLUDE_DIRECTORIES ${foobar_DK_INCLUDE_DIRECTORIES} PARENT_SCOPE)

This roughly allowed us to have toolchains work in Visual Studio builds. It actually worked really well.
Until Microsoft introduced this new external stuff…

The problem is we are using a non 16.10 compatible compiler. And the IDE expects a 16.10 compiler.

This breaks our custom VS builds.

We are using a VS 2019 compiler just not the latest one.

Hmm. I suspect we’ll run into this when we update our CI machines to 16.10 (since we still use 16.8 and 16.9 toolchains). I’m not familiar enough with Visual Studio to know much else here though.

I ended up writing a cmake function that can extract the exact vs version using vswhere.exe

I just tested it locally and it works great! Not sure if the CMake team needs something like this. But it’s worth thinking about at least.

# This function will get the VS Version
function(amd_get_vs_versions version)
    # Return 0 for non-vs generators
    if (NOT (CMAKE_GENERATOR MATCHES "Visual Studio") )
        set(${version} 0)
        return()
    endif()

    # https://github.com/microsoft/vswhere/wiki
    set(CMAKE_VS_WHERE "C:/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe")

    # Get the major version number from the generator name
    # EX: 16 for vs2019
    # EX: 15 for vs2017
    string(REGEX MATCHALL "Visual Studio ([0-9]+) [0-9]+" throwaway ${CMAKE_GENERATOR})
    set(vsMajorVersion ${CMAKE_MATCH_1})

    execute_process(
        # Extract the exact version(s) of Visual Studio
        COMMAND "${CMAKE_VS_WHERE}" -property catalog_productDisplayVersion
        OUTPUT_VARIABLE vs_where_output
    )

    # We are regexing for the string "16.10.0" for example
    if ((${vs_where_output} MATCHES "(${vsMajorVersion}\.[0-9]+\.[0-9]+).*"))
        set(${version} "${CMAKE_MATCH_1}" PARENT_SCOPE)
    else()
        message(FATAL_ERROR "CMAKE | Failed regex vswhere.exe output!")
    endif()
endfunction()