cmakevisual-studio-2022conan

How to set exact MSVC toolset version in Conan2 + cmake . Build using ninja


I need to set up conan 2 + cmake C++ project build so it will always use VCToolsVersion "14.40.33807" when several toolsets are installed on the desktop.


Solution

  • These are the ways to define the exact toolchain with Conan 2, MSVC and CMake:

    Default, using compiler.version only

    Using the default approach defining compiler.version=194 will define set(CMAKE_GENERATOR_TOOLSET "v143" CACHE STRING "" FORCE) in the toolchain file, and use the default toolset version for v143, typically the latest.

    conan new cmake_lib
    conan build .
    ...
    Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143
    ...
    The CXX compiler identification is MSVC 19.43.34810.0
    

    Specifying the update

    Specifying the compiler.update=2 (optional), then the toolset defined will be set(CMAKE_GENERATOR_TOOLSET "v143,version=14.42" CACHE STRING "" FORCE)

      -- Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143,version=14.42
      ...
      -- The CXX compiler identification is MSVC 19.42.34441.0
    

    Note: A modern CMake version is necessary for this to work properly.

    The compiler.update=2 has effect on the package_id and binary identification, making one different binary per different compiler update. This seems what you want.

    Exact version via conf

    The conan build . -c tools.microsoft:msvc_update=2.34433 configuration allows to define an arbitrary msvc toolset version, it will map to:

      Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143,version=14.42.34433
    -- Conan toolchain: Setting CMAKE_MSVC_RUNTIME_LIBRARY=$<$<CONFIG:Release>:MultiThreadedDLL>
    -- Conan toolchain: C++ Standard 14 with extensions OFF
    -- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
    -- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.26100.
    -- The CXX compiler identification is MSVC 19.42.34441.0
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe - skipped
    

    Note the compiler version doesn't necessarily exactly match the toolset one.

    Finally, if you want the -c tools.microsoft:msvc_update=2.34433 to become part of the package_id so you perfectly identify distinct binaries, you can do it with the

    tools.info.package_id:confs: List of existing configuration to be part of the package ID
    

    configuration, defining it to contain tools.microsoft:msvc_update will make tools.microsoft:msvc_update part of the package_id

    So a full profile to model 14.42.34433, and make it part of the package_id to identify unique binaries for different exact toolset versions is:

    [settings]
    build_type=Release
    os=Windows
    arch=x86_64
    compiler=msvc
    compiler.version=194
    compiler.runtime=dynamic
    compiler.cppstd=17
    
    [conf]
    tools.microsoft:msvc_update=2.34433
    tools.info.package_id:confs=["tools.microsoft:msvc_update"]
    tools.cmake.cmaketoolchain:generator=Ninja
    

    Recall that in the majority of cases, the binary compatibility doesn't require to model down to the exact toolset version, and the Conan 2 default of modeling the compiler.version=194 is good in most of the cases