Figure out the build type on Windows

I assume this question has been asked somewhere on the Web, but I could not find anything usable for my use cases. What is the proper way to handle this logic on all platforms?

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(STEM "Debug")
else()
  set(STEM "Release")
endif()

You shouldn’t use CMAKE_BUILD_TYPE. You should use generator expressions.

As an example:

# Compile foobar library with per-preprocessor macro DEBUG_MODE only on debug builds
add_library(foobar STATIC)
target_compile_definitions(foobar PRIVATE $<$<CONFIG:Debug>:DEBUG_MODE>)

Here is a more in detail stackoverflow post

In fact, it depends on the generator.
For multi-config generators (Visual Studio for instance), CMAKE_BUILD_TYPE cannot be used, so only genex are usable.

For mono-config generators (NMake Makefiles for instance), CMAKE_BUILD_TYPE` is usable but genex are also usable.

As stated, yes, the logic you want isn’t possible for any given generator (because the build type in multi-config generators is chosen after all your CMake code has executed).

However, what are you trying to accomplish? If it is to have different names on libraries based on the configuration, the <CONFIG>_POSTFIX target property is the way to go.

Thank you all for your suggestions. I need to set up the search paths based on the build type, and this step belongs to the configuration stage. I have a workaround by passing an extra parameter from the build script. However, having CMake do this work based on the build config is a cleaner solution.

target_include_directories supports generator expressions, so that’s how I would accomplish this.