What should I do in CMakeLists.txt w.r.t. the build type?

92 Views Asked by At

I'm the author of some library which gets built using CMake.

If a user specifies a build type they run cmake - I can oblige that, no problem.

But what is the best practice when a user doesn't specify a build type?

  • Should I just ignore it?
  • Should I choose a build type as a fallback/default myself? If so, which?
  • I've read this Kitware blog entry which suggests a certain approach to the matter and places it in a library dependency. The approach is encapsulated into this module. Should I use that?

So far I've been forcing some specific build type and it's been suggested to me perhaps I shouldn't be doing that.

1

There are 1 best solutions below

0
On

I think there are two good options:

  1. Warn the user if CMAKE_BUILD_TYPE is unset and the active generator is single-config.
  2. Do nothing. The warning is only helpful to novice users, anyway. It is possible (however unlikely) that an advanced user intended to do this. In this case, the warning is annoying.

To implement (1) correctly, the following code snippet (placed early, ideally after project()) will work:

cmake_minimum_required(VERSION 3.9)

# ...

get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if (NOT is_multi_config AND NOT DEFINED CMAKE_BUILD_TYPE)
  message(WARNING "Must set CMAKE_BUILD_TYPE for single-config generators.")
endif ()