Search code examples
visual-c++cmakemsbuildwindows-10external-project

CMake - How can I pass the windows SDK version to an ExternalProject built with msbuild?


I have a CMake project which has an external dependency (Crypto++). The external project has a vcxproj file provided, so in order to add it to the build process, I added it like this:

ExternalProject_Add(CryptoPP
    SOURCE_DIR ${CRYPTOPP_SRC_DIR}

    CONFIGURE_COMMAND ""

    BUILD_COMMAND "${MSBUILD_EXE}" 
        "/p:OutDir=<BINARY_DIR>/bin/$<CONFIG>/"
        "/p:IntDir=<BINARY_DIR>/obj/$<CONFIG>/"
        "/p:Configuration=$<CONFIG>"
        "/p:Platform=${CMAKE_VS_PLATFORM_NAME}"
        "<SOURCE_DIR>/${CRYPTOPP_PROJECT}"

    INSTALL_COMMAND "${CMAKE_COMMAND}" -E make_directory <INSTALL_DIR>/lib
            COMMAND "${CMAKE_COMMAND}" -E copy <BINARY_DIR>/bin/$<CONFIG>/cryptlib${CMAKE_STATIC_LIBRARY_SUFFIX} <INSTALL_DIR>/lib
)

This worked fine until I started using Windows 10. Now I'm getting this error: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".

I tried passing the SDK version from CMake, using the only related CMake variable I could find, by adding this flag to msbuild:

"/p:WindowsTargetPlatformVersion=${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}"

However, I'm still getting the same error. I tried displaying the value of the CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION variable, and it always seems empty.

Modifying the dependency vcxproj is not an option. I don't know in advance what SDK version whoever needs to build the project has installed, so that's not an option.

I can't find any way to obtain and pass the SDK version to the external project build, what am I doing wrong?


Solution

  • I found the problem. I forgot to set the platform toolset. I was building my CMake project with v100 so it doesn't need an SDK version. Because I didn't configure the platform toolset for the external project, it used the one set in the project which was the latest (v141) which needed an SDK version.

    So adding this flag fixed my setup:

    "/p:PlatformToolset=${CMAKE_VS_PLATFORM_TOOLSET}"