How-to use an existing CMakeCache.txt for the "Ninja Multi-Config" generator in Microsoft Visual Studio 2019?

2k Views Asked by At

I am currently trying to improve the CMake user experience in Microsoft Visual Studio (MSVS) 2019 with our CMake-based convention-over-configuration build system.

Until MSVC 2019 we always used a "MSBuild"-generator (e.g. "Visual Studio 2017"). But since Microsoft states that later updates of MSVS are able to open an existing CMakeCache.txt, I wanted to give this MSVS feature a try.

What I would like to achieve: Make MSVS 2019 "understand" CMakeCache.txt files using the build generator "Ninja Multi-Config".

Currently our build system generates a CMakeSettings.json (in the same directory as the CMakeLists.txt file) file during the configure step:

{
  "configurations": [
    {
      "name": "x64-windows-msvc1927-static-md-Debug",
      "cacheRoot": "C:/_bld",
      "cmakeExecutable": "E:/dev/native/cmake/cmake-3.18.2-x64/bin/cmake.exe",
      "configurationType": "Debug"
    },
    {
      "name": "x64-windows-msvc1927-static-md-Release",
      "cacheRoot": "C:/_bld",
      "cmakeExecutable": "E:/dev/native/cmake/cmake-3.18.2-x64/bin/cmake.exe",
      "configurationType": "Release"
    },
    {
      "name": "x64-windows-msvc1927-static-md-RelWithDebInfo",
      "cacheRoot": "C:/_bld",
      "cmakeExecutable": "E:/dev/native/cmake/cmake-3.18.2-x64/bin/cmake.exe",
      "configurationType": "RelWithDebInfo"
    }
  ]
}

Why are we doing this? We have to explicitly set "cmakeExecutable", otherwise MSVS uses the CMake version shipped with MSVS. This fails because we are using v3.18 (and don't want to rely on two different CMake versions for building the software).

Everything looks fine, because all three build configs show up below the "build configuration" combo-box in the MSVS IDE.

But regardless of what is selected, the IDE always builds and runs the Debug build config. Selecting another build config does not have any effect at all.

I'm afraid that it is not possible (yet) to achieve the goal stated above with MSVS because it simply does not understand the "Ninja Multi-Config" generator.

The thing is: I can't change the generator to "Ninja" because that would trigger a CMake warning for an existing cache.

  1. Do I have to use the "Ninja" generator (together with different build trees) in the first place?
  2. Is there any real solution to my problem of using "Ninja" as a multi-config generator in MSVS 2019?
  3. If the answer to question 1 is "yes" or to question 2 is "no": Shouldn't using the generator "Visual Studio 2019" be simpler? I don't see any UX advantage at all not using this generator then.

The following environment is used:

  • CMake v3.18.2
  • Microsoft Visual Studio 2019 v16.7.5
  • Ninja 1.10.1
1

There are 1 best solutions below

0
On

CMake upstream and author of the Ninja Multi-Config generator here. A few things:

  1. As you discovered, you can't change the generator for an existing tree. For example, if the tree was built with Ninja, you can't change it to Ninja Multi-Config later on. The best thing to do is start a new build tree - either wipe the old one and start over or start a new one in a different directory.
  2. AFAIK, Microsoft has not added support for Ninja Multi-Config yet. I certainly hope they do at some point, because that is the generator we recommend for IDEs that use CMake. However, this doesn't really matter, because...
  3. Yes, I'm pretty sure Visual Studio can open a CMakeCache.txt with a generated Visual Studio solution. Even if it can't, you can of course open the Visual Studio solution directly.