Check available generators

Is there a way to check available CMake generators? Some argument to query it from CMake? I cannot find a way to do this. I mean generators that are actually available and will successfully configure projects. For example I’d like to get true for Visual Studio 15 2017 only if VS2017 is installed.

I tried to workaround by just calling cmake -G <generator_name> and parsing output to find “CMake Error: Could not create named generator” message. This works fine on Linux. I don’t even have to create CMakeLists.txt, the response is pretty fast. The problem with this approach is on Windows. For some reason I have to create CMakeLists.txt, otherwise it won’t check the availability of generator. And generating project files take a really long time - about 3 seconds. Too long for checking that many generators.

I tried cmake -E capabilities, but it doesn’t provide information of what’s actually available on current system. Only general metadata.

In contrary, cmake -E capabilities gives you the supported generators on the current platform. See "generators" array.

On the current platform, meaning OS but not on the current machine. I have VS2019 installed and no other version. It should give me true for VS2019 and false for VS2017. A portion of generators array:

    {
      "extraGenerators": [],
      "name": "Visual Studio 16 2019",
      "platformSupport": true,
      "supportedPlatforms": [
        "x64",
        "Win32",
        "ARM",
        "ARM64",
        "ARM64EC"
      ],
      "toolsetSupport": true
    },
    {
      "extraGenerators": [],
      "name": "Visual Studio 15 2017 Win64",
      "platformSupport": true,
      "supportedPlatforms": [
        "x64",
        "Win32",
        "ARM",
        "ARM64"
      ],
      "toolsetSupport": true
    },

For that purpose, you can use vswhere.

Thanks, but this is not just about VS. I’m looking for a generic solution for all generators.

@brad.king ?

This is also discussed in CMake Issue 25397. In general it is not possible to know ahead of time whether a given generator will work.