Check out my first novel, midnight's simulacra!

CMake: Difference between revisions

From dankwiki
No edit summary
(holy fucking shit cmake sucks ass what the fuck)
Line 9: Line 9:
* Release -- optimized, no symbols
* Release -- optimized, no symbols
* MinSizeRel -- optimized for size
* MinSizeRel -- optimized for size
If <tt>CMAKE_BUILD_TYPE</tt> is not provided, none of these is used -- no debugging flags are set, and no optimization flags are set, in a somewhat stunning bit of unexpected behavior. The following snippet will ensure a default, and allow the value to be selected from a set in the gui:
<pre>
# Set a default build type if none was specified
set(default_build_type "Release")
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
  set(default_build_type "Debug")
endif()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
</pre>

Revision as of 08:52, 6 May 2020

CMake is one of the numerous build systems that seeks to replace Autotools+Make in the twenty-first century.

A CMake build can be configured on the command line with -D switches, or using ccmake/cmake-gui. Either way, the result is build infrastructure for some other tool, such as Make or Ninja.

Build Types

CMAKE_BUILD_TYPE can take any of the values in CMAKE_CONFIGURATION_TYPES. Important ones include:

  • Debug -- no optimization, full debug info, assert (NDEBUG is not defined)
  • RelWithDebInfo -- optimized, symbols
  • Release -- optimized, no symbols
  • MinSizeRel -- optimized for size

If CMAKE_BUILD_TYPE is not provided, none of these is used -- no debugging flags are set, and no optimization flags are set, in a somewhat stunning bit of unexpected behavior. The following snippet will ensure a default, and allow the value to be selected from a set in the gui:

# Set a default build type if none was specified
set(default_build_type "Release")
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
  set(default_build_type "Debug")
endif()
 
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()