Check out my first novel, midnight's simulacra!

CMake: Difference between revisions

From dankwiki
(Created page with "[https://gitlab.kitware.com/cmake/cmake CMake] is one of the numerous build systems that seeks to replace Autotools+Make in the twenty-first century.")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
[https://gitlab.kitware.com/cmake/cmake CMake] is one of the numerous build systems that seeks to replace [[Autotools]]+[[Make]] in the twenty-first century.
[https://gitlab.kitware.com/cmake/cmake 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 <tt>-D</tt> switches, or using <tt>ccmake</tt>/<tt>cmake-gui</tt>. Either way, the result is build infrastructure for some other tool, such as [[Make]] or [[Ninja]].
==Build Types==
<tt>CMAKE_BUILD_TYPE</tt> can take any of the values in <tt>CMAKE_CONFIGURATION_TYPES</tt>. Important ones include:
* Debug -- no optimization, full debug info, <tt>assert</tt> (<tt>NDEBUG</tt> is not defined)
* RelWithDebInfo -- optimized, symbols
* Release -- optimized, no symbols
* 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 of RelWithDebInfo, and allow the value to be selected from a set in the gui:
<pre>
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build mode." FORCE)
endif()
message(STATUS "Requested build mode: ${CMAKE_BUILD_TYPE}")
</pre>
Build types can be added. I thought this might be useful for a <tt>Coverage</tt> target, but I ended up stuffing the coverage flags into <tt>CMAKE_C_FLAGS_DEBUG</tt>/<tt>CMAKE_CXX_FLAGS_DEBUG</tt>.
==Debugging==
Dump all current variables with:
<pre>
get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
</pre>
Better yet, <tt>include(CMakePrintHelpers)</tt> to get the useful commands <tt>cmake_print_properties()</tt> and <tt>cmake_print_variables()</tt>.

Latest revision as of 01:50, 3 August 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 of RelWithDebInfo, and allow the value to be selected from a set in the gui:

set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build mode." FORCE)
endif()
message(STATUS "Requested build mode: ${CMAKE_BUILD_TYPE}")

Build types can be added. I thought this might be useful for a Coverage target, but I ended up stuffing the coverage flags into CMAKE_C_FLAGS_DEBUG/CMAKE_CXX_FLAGS_DEBUG.

Debugging

Dump all current variables with:

get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

Better yet, include(CMakePrintHelpers) to get the useful commands cmake_print_properties() and cmake_print_variables().