Search code examples
visual-studiocmakeconan

How to specify Visual Studio compiler toolset with Conan for CMake?


Visual Studio offers the notion of toolsets, e.g. for Visual Studio 2015 and the optional "Windows XP Support for C++" package, there are the toolsets v140 and v140_xp. In case, someone added the "Clang with Microsoft CodeGen" package, there is v140_clang_c2.

With CMake, I can use the -T command line flag to specify the toolset to be used for the solution files generated with CMake, e.g. cmake -T v140_clang_c2 will generate the project solution file with "Visual Studio 2015 - Clang with Microsoft CodeGen (v140_clang_c2)" configured as the "Platform Toolset" for all targets.

How can I tell Conan on Windows with an appropriate installed Visual Studio to use a specific toolset? Preferably for conanfiles using CMake as the generator.

The only way I could come up with is to add an additional option to all the projects/conanfile.py I'd like to use with different toolsets and add another package option (e.g. used as -o toolset=v140_clang_c2) to be added to the command line of the initial invocation of CMake.

I'd expect that variability to be part of the package manager itself rather than the responsibility of the package writers.


Solution

  • No, conan does not provide this functionality out of the box.

    (UPDATE: It now does, since conan 0.29)

    If you want to use them now, conan could be customized to handle different toolsets, I'd do the following:

    First, extend the current settings, to account for the toolsets. I wouldn't use options if you are using different toolsets extensively. You could try to define them as global to Visual Studio:

    compiler:
            ...
            Visual Studio:
                runtime: [MD, MT, MTd, MDd]
                version: ["8", "9", "10", "11", "12", "14", "15"]
                toolset: [None, v140, v140_xp] 
    

    Or if you want to be more specific, define them per version, something like:

    compiler:
            ...
            Visual Studio:
                version: 
                     "12": 
                         toolset: [None, v120, v120_xp] 
                     "14": 
                         toolset: [None, v140, v140_xp]
    

    Then, it is true that the responsibility to pass the option to cmake, is for the package creator. I would of course just add

    "-T %s" % self.settings.compiler.toolset
    # or
    "-T %s" % self.settings.compiler.version.toolset
    

    to the cmake command arguments.

    Probably deserves opening an issue and discussing with maintainers if possible to make it a feature: https://github.com/conan-io/conan