conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.26k stars 981 forks source link

[question] Visual Studio 2022: the wrong toolset is selected #16774

Open db4 opened 3 months ago

db4 commented 3 months ago

What is your question?

I have a VS 2022 setup with both MSVC 19.39 (toolset 1439) and 19.40 (toolset 14.40) installed. (I need it to be able to use both compiler.version=193 and compiler.version=194 with Conan). Version 193 works without issues but when I specify -s compiler.version=194 CMake generator still selects MSVC 19.39. I found a recommendation in https://github.com/conan-io/conan/pull/15588 to use -s compiler.update=0. It works, but effectively disables the compatibility plugin - all dependencies should then be rebuilt with compiler.version=194. Is there any less intrusive solution? Can I ask CMakeToolchain generator to add version=14.40 to CMAKE_GENERATOR_TOOLSET without breaking compatibility with older versions?

Have you read the CONTRIBUTING guide?

memsharded commented 3 months ago

Hi @db4

Thanks for your feedback. I assume you are using latest Conan 2.6?

The main issue for this seems to be outside of Conan. When installing update 17.10 with 19.40, VS uninstall 19.39. Installing it again leaves some files in the VS installation with bad configuration, so the combo CMake + VS (with VIsual Studio CMake generators) doesn't work nicely to select 19.40 or 19.39.

Interestingly, when using some other generator like Ninja, then Conan is able to define the environment via VCVars and everything works fine, it is possible to switch between 19.39 and 19.40 just by setting the compiler version, without having to set the update. You can also check that if you install some other older version like 19.38, you might be able to switch and use it from Conan.

Can you please try those to make sure?

@jcar87 was describing some of the issues in https://github.com/conan-io/conan/pull/15588#issuecomment-2096652138, and also how the upgrade to 17.10 could generate those issues, and https://developercommunity.visualstudio.com/t/MicrosoftVCToolsVersionv143defaulttx/10041951#T-N10601904 as related.

Does this help?

db4 commented 1 week ago

Hi @memsharded

Having the latest Visual Studio 2022 17.11.5 with 14.39 and 14.41 toolsets installed I've made both 193 and 194 compilers work with CMake Visual Studio generator as follows:

  1. patch Microsoft.VCToolsVersion.14.39.17.9.props according to https://gitlab.kitware.com/cmake/cmake/-/issues/25192#note_1534143
--- Microsoft.VCToolsVersion.14.39.17.9.props
+++ Microsoft.VCToolsVersion.14.39.17.9.props
@@ -1,7 +1,7 @@
 <?xml version = "1.0" encoding="utf-8"?>
 <Project ToolsVersion = "4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <Import Project="$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)Microsoft.VCRedistVersion.default.props))"/>
   <PropertyGroup>
     <VCToolsVersion Condition = "'$(VCToolsVersion)' == ''" >14.39.33519</VCToolsVersion>
+    <VCToolsRedistVersion Condition = "'$(VCToolsRedistVersion)' == ''" >14.39.33519</VCToolsRedistVersion>
   </PropertyGroup>
 </Project>
  1. install conan packages:

    conan install . -s compiler.version=193 --build missing
  2. patch generated conan_toolchain.cmake:

    
    --- conan_toolchain.cmake
    +++ conan_toolchain.cmake
    @@ -21,7 +21,7 @@
    set(CMAKE_GENERATOR_PLATFORM "x64" CACHE STRING "" FORCE)
    
    message(STATUS "Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143")
    -set(CMAKE_GENERATOR_TOOLSET "v143" CACHE STRING "" FORCE)
    +set(CMAKE_GENERATOR_TOOLSET "v143,version=14.39" CACHE STRING "" FORCE)
    
    ########## 'compilers' block #############

4. configure and build CMake project:

cmake --preset conan-default cmake --build --preset conan-release


Doing step 3 (patch `conan_toolchain.cmake`) each time is rather tedious, maybe Conan can append `,version=14.39` to `CMAKE_GENERATOR_TOOLSET` itself?