conan-io / conan

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

[question] MSBuildToolchain does not apply compiler toolset when it's missing from vcxproj files #12314

Open prince-chrismc opened 2 years ago

prince-chrismc commented 2 years ago

Reviewing and playing with https://github.com/conan-io/conan-center-index/pull/13185 I discovered the new toolchain and build helper are able to pass in the compiler toolset from the settings model (or the recipe)... but not how I was expecting.

❓ Is this a limitation given the tribe decided on higher support (where this value should always be present) or is it possibly a bug? ❓ Is this expected when migrating old projects to Conan 2.0?

The recipe is currently building with https://github.com/yasm/yasm/tree/v1.3.0/Mkfiles/vc10 which does not have the PlatformToolset defined in any of the configurations.

Calling conan create all 1.3.0@ -pr msvc2019 results in

Done Building Project "C:\J\w\prod\BuildSingleReference@2\.conan\data\yasm\1.3.0\_\_\build\0a420ff5c47119e668867cdb51baff0eca1fdb68\Mkfiles\vc10\yasm.sln" (default targets) -- FAILED.

Build FAILED.

"C:\J\w\prod\BuildSingleReference@2\.conan\data\yasm\1.3.0\_\_\build\0a420ff5c47119e668867cdb51baff0eca1fdb68\Mkfiles\vc10\genversion\genversion.vcxproj" (default target) (3) ->
(PlatformPrepareForBuild target) -> 
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Platform.targets(65,5): error MSB8020: The build tools for Visual Studio 2010 (Platform Toolset = 'v100') cannot be found. To build using the v100 build tools, please install Visual Studio 2010 build tools.  Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [C:\J\w\prod\BuildSingleReference@2\.conan\data\yasm\1.3.0\_\_\build\0a420ff5c47119e668867cdb51baff0eca1fdb68\Mkfiles\vc10\genversion\genversion.vcxproj]

Modifying the recipe with

        tc = MSBuildToolchain(self)
        if not self.settings.get_safe("compiler.toolset"):
            tc.toolset = "v140"
        tc.generate()

Leads to the same problem as well as conan create all 1.3.0@ -pr msvc2019 -s compiler.toolset=v142

Configuration for profile msvc2019:

[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=Visual Studio
compiler.version=16
build_type=Release
[options]
[conf]
[build_requires]
[env]

https://github.com/conan-io/conan/blob/8aeea2230fd6375ce1da15f2380097bed9098377/conan/tools/microsof


Good news, when the patch is applied https://github.com/conan-io/conan-center-index/blob/404cd727b1387a69dca62a5588596effd1a69f48/recipes/yasm/all/patches/0001-platformtoolset.patch the toolset can be correctly applied

in a 2019 prompt it will fail with any toolset other then v142 (which is expected) and in a 2022 prompt it will only work in with toolset=v143... both work with the default provided toolset given toolset=None

jcar87 commented 2 years ago

Good news, when the patch is applied https://github.com/conan-io/conan-center-index/blob/404cd727b1387a69dca62a5588596effd1a69f48/recipes/yasm/all/patches/0001-platformtoolset.patch the toolset can be correctly applied

mmm is the case here that if the patch defines the <PlatformToolset> (regardless of the value), then the override by the generator will work with the value coming from it? Or is it the case that the patch works because it hardcodes the value in the patched file?

prince-chrismc commented 2 years ago

The case is without <PlatformToolset> build fails (new helper does nothing).

The patch can be override with the build helper

memsharded commented 2 years ago

I think the key is:

        if compiler == "msvc":
            subs_toolset = settings.get_safe("compiler.toolset")
            if subs_toolset:
                return subs_toolset
            return msvc_version_to_toolset_version(compiler_version)

The MSBuildToolchain only implemented support for msvc compiler, not Visual Studio one. The idea was that ConanCenter was going to move to that compiler in 1.X, to be fully ready, profiles, etc, but I am not longer sure this is happening/possible, to be discussed.

If not happening, I guess the Visual Studio compiler management should be added there.