Vscode-cmake-tools: CMake Tools 1.4.0 doesn't define CMAKE_BUILD_TYPE in a project's build directory's CMakeCache.txt

Created on 10 Jun 2020  路  7Comments  路  Source: microsoft/vscode-cmake-tools

Brief Issue Summary

I am using Visual Studio Code and CMake Tools to build a simple C++ project using OpenCV. The project relies on OpenCV's opencv_world430d.dll and OpenCV_world.dll files. To copy them to the build and install directory, I use an if statement to check whether the CMAKE_BUILD_TYPE matches either Debug or Release; then I define a variable equal to the path to the corresponding debug or release dll. Lastly, I use a custom command to copy the files to the build directory and install them to a desired install location. The problem is, CMAKE_BUILD_TYPE is not defined - even though I've configured the project and selected a Debug configuration.

Expected:

If build type is Debug, the syntax if(${CMAKE_BUILD_TYPE) MATCHES Debug) should return true; else, if build type is Release, the syntax if(${CMAKE_BUILD_TYPE) MATCHES Release) should return true.

It is expected that CMAKE_BUILD_TYPE is defined as either Debug or Release.

Apparent Behavior

CMAKE_BUILD_TYPE is not defined.

Platform and Versions

  • Operating System: Windows 10
  • CMake Version: 3.17.3
  • VSCode Version: 1.45.1
  • CMake Tools Extension Version: 1.4.0
  • Compiler/Toolchain: Visual C++ 2019

Other Notes/Information

Looking in the project's build directory's CMakeCache.txt file, CMAKE_BUILD_TYPE is not included. When I use Visual Studio 2019, the project behaves as expected and CMAKE_BUILD_TYPE is included in the CMakeCache.txt file.

configure more info needed

Most helpful comment

Hi!

I think CMAKE_BUILD_TYPE isn't set by default in this case, because CMake supports two types of generators: single-configurations (makefiles, nmake) and multi-configurations (IDEs).

I suggest to add the following lines to settings.json.
This should then update your CMAKE_BUILD_TYPE accordingly and set it as -D CLI option.

"cmake.configureSettings": {
   "CMAKE_BUILD_TYPE": "${buildType}"
}

With this in the CMakeListst.txt to see, if var is set:

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

All 7 comments

If you manually set the preferred generator to be "Ninja", then CMAKE_BUILD_TYPE is defined in the CMakeCache.txt file.

If no preferred generator is specified, Visual Studio is used as the generator and it doesn't specify CMAKE_BUILD_TYPE in the CMakeCache.txt file.

Given that Visual Studio may be the default for a lot of people, it'd be good if it, by default, added CMAKE_BUILD_TYPE to CMakeCache.txt. I'm not sure if it's a bug, a problem with my installation, or something I should do (but didn't know to do). Perhaps if someone can comment, then we can be sure which of these it is.

Hi!

I think CMAKE_BUILD_TYPE isn't set by default in this case, because CMake supports two types of generators: single-configurations (makefiles, nmake) and multi-configurations (IDEs).

I suggest to add the following lines to settings.json.
This should then update your CMAKE_BUILD_TYPE accordingly and set it as -D CLI option.

"cmake.configureSettings": {
   "CMAKE_BUILD_TYPE": "${buildType}"
}

With this in the CMakeListst.txt to see, if var is set:

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

Looks like the problem is the CMake generator: for older CMake Tools it was Ninja but for 1.4.0 it's Visual Studio XXXX. The latter is a multi-configuration generator so CMake does not define CMAKE_BUILD_TYPE for it (CMake generator expressions supposed to be used instead)

I also need CMAKE_BUILD_TYPE in my build to I'm trying to use Ninja generator as I did before. No luck yet - any related vscode settings (cmake.preferredGenerators etc.) are just ignored. Any idea how to do that?

@db4, Are you saying that the Ninja generator is not defining the CMAKE_BUILD_TYPE? Have you reloaded the window/extension since you changed your generator? Sometimes the settings change doesn't automatically get applied. That's something we're investigating. https://github.com/microsoft/vscode-cmake-tools/issues/1127

@db4, Are you saying that the Ninja generator is not defining the CMAKE_BUILD_TYPE?

Quite the opposite: Ninja generator defines CMAKE_BUILD_TYPE, Visual Studio XXXX does not.

So are you saying you can't get the extension to pick Ninja again? Did you set the cmake.generator or cmake.preferredGenerators settings?

So are you saying you can't get the extension to pick Ninja again? Did you set the cmake.generator or cmake.preferredGenerators settings?

Well, I cannot reproduce the issue anymore - after upgrade to 1.4.1 and some cleanups cmake.generator=Ninja works as expected.

Was this page helpful?
0 / 5 - 0 ratings