Is there a way to integrate the call to vcvarsall.bat into the CMakePresets.json file?

I am currently exploring the presets feature. I am trying to make a windows, ninja, clang-cl based build work.
I managed to do it but it only works if I call Visual Studios vcvarsall.bat file first to get the correct environment.

I never liked having to make the extra call to that file though, so I am wondering if there is a way to embedd that call into the CMakePresets.json file or maybe set the environment by hand in that file. So here are my questions:

  • Is it possible to specify running a command in the CMakePresets.json file?
  • Is there an working example of an CMakePresets.json that sets the same environment as the vcvarsall.bat file?
  • Side question… Why is it not necessary to call vsvarsall.bat when using the visual studio generator?

No, it has always been the job of either the generator (Visual Studio and Xcode do it) or the caller to set up the environment properly to use the compiler. CMake is not a cmd.exe program and cannot read vcvarsall.bat meaningfully without implementing the .bat language itself.

Sounds like you want CMakeSettings.json

CMake isn’t responsible for setting up your compiler environment. It tries to help as much as it can but it’s an unreasonable burden to keep with the decision of the Visual Studio team. If they did decide to parse those bat files, and then visual studio 2021 comes out and breaks it, then it’s wasted effort.

Instead if you want an easy Ninja + VS + Clang environment then you basically have to do what the VS team wants you to do. CMakeSettings.json is the VS team’s answer to people who want the workflow you just expressed.