10

I have a little game engine in which I need to define some custom macros when it is building in Debug or Release mode.

Here are a few lines of my CMake script which is supposed to do that:

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(OE_BUILD_TYPE_DEFINE "OE_DEBUG")
endif()
    
if (CMAKE_BUILD_TYPE STREQUAL "Release")
    set(OE_BUILD_TYPE_DEFINE "OE_RELEASE")
endif()

if (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
    set(OE_BUILD_TYPE_DEFINE "OE_DEBUG")
endif()

if (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
    set(OE_BUILD_TYPE_DEFINE "OE_RELEASE")
endif()

target_compile_definitions(OverEngine PRIVATE
    "_CRT_SECURE_NO_WARNINGS"
    "GLFW_INCLUDE_NONE"

    PUBLIC
    OE_BUILD_TYPE_DEFINE
)

It works great using make or ninja in Linux however on Windows using VisualStudio, the CMAKE_BUILD_TYPE variable is always empty. I know the reason. It is because VS can switch build type without re-running CMake unlike make or ninja generators.

Premake has something called filter which works just fine but for some other reason, I am not using it right now.

How do I set this up?

I am using VisualStudio 2019 16.7.2 and CMake 3.18.2 if it is needed.

EDIT: fixed by replacing those lines with these lines:

target_compile_definitions(OverEngine PRIVATE
    "_CRT_SECURE_NO_WARNINGS"
    "GLFW_INCLUDE_NONE"

    PUBLIC
    $<$<CONFIG:Debug>:OE_DEBUG>
    $<$<CONFIG:Release>:OE_RELEASE>

    $<$<CONFIG:RelWithDebInfo>:OE_DEBUG>
    $<$<CONFIG:MinSizeRel>:OE_RELEASE>
)
OverShifted
  • 457
  • 1
  • 7
  • 17

2 Answers2

9

You may want to use generator expressions here. They are evaluated at build time and therefore the build type is set even for multi-configuration generators as Visual Studio or Xcode.

target_compile_definitions(OverEngine
    PRIVATE
      _CRT_SECURE_NO_WARNINGS
      GLFW_INCLUDE_NONE
    PUBLIC
      $<$<CONFIG:Debug>:OE_DEBUG>
      $<$<CONFIG:RelWithDebInfo>:OE_DEBUG>
      $<$<CONFIG:Release>:OE_RELEASE>
      $<$<CONFIG:MinSizeRel>:OE_RELEASE>
)
vre
  • 6,041
  • 1
  • 25
  • 39
  • this code gave me this error: `$ expression requires one or zero parameters.` how ever using 4 generator expressions for each CONFIG, works for me. – OverShifted Jan 08 '21 at 10:47
  • My bad, I posted the code for CMake 3.19 which supports more than one variable in `$` generator expression. I'll edit the answer. – vre Jan 08 '21 at 10:52
-1

I have a little game engine in which I need to define some custom macros when it is building in Debug or Release mode.

Do not use CMake functionality for this

I personally do not use a lot CMake generator expressions. The possible alternative in addition to CMake generator expression is performing this not via CMake, but directly from the MSVC compiler:

  • First, you may want to detect that you're using the MSVC toolchain. It can be done by checking _MSC_VER macros (https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170).

  • Next you may want to use the following heuristic. When the source code is compiled with DEBUG version of C and C++ runtime via specifying for the compiler the flags /MTd /MDd the _DEBUG macro is defined automatically by the compiler. You can use this heuristic to distinguish Debug builds and Release builds.

References

  1. List of compiler options
  2. About _DEBUG macro
  3. Toolchain-specific macro (mixed with standard C and C++ macroses) used by MSVC:
Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40