Skip to content

Commit

Permalink
CMake: Improve build type defaults
Browse files Browse the repository at this point in the history
* Since we have -Og, -O0 (the default) is never a good idea.
* Building the project without a build type (the cmake default) is
  "usually not desirable" (cmake docs), therefore default to Release
  with assertions enabled.
* Document build types.
* Warn if the user somehow managed to build without optimization.
  • Loading branch information
mattkretz authored and wirew0rm committed Oct 16, 2023
1 parent f9c2416 commit 0fc0727
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,49 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

# Determine if fmt is built as a subproject (using add_subdirectory) or if it is the master project.
if (NOT DEFINED GR_TOPLEVEL_PROJECT)
set(GR_TOPLEVEL_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(GR_TOPLEVEL_PROJECT ON)
message(STATUS "CMake version: ${CMAKE_VERSION}")
endif ()
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "(Clang|GNU|Intel)")
# -Og is a much more reasonable default for debugging. Also enable gdb extensions.
set(CMAKE_CXX_FLAGS_DEBUG "-Og -ggdb" CACHE INTERNAL
"Flags used by the compiler during debug builds.")

# Add a build type that keeps runtime checks enabled
set(CMAKE_CXX_FLAGS_RELWITHASSERT "-O3" CACHE INTERNAL
"Flags used by the compiler during release builds containing runtime checks.")

# The default value is often an empty string, but this is usually not desirable and one of the
# other standard build types is usually more appropriate.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithAssert" CACHE STRING
"Choose the type of build. Options are: None Debug Release RelWithAssert RelWithDebInfo MinSizeRel.\n\
- None: no compiler flags, defaults and target-specific flags apply\n\
- Debug: best/complete debugging experience; as optimized as reasonable\n\
- Release: full optimization; some runtime checks disabled\n\
- RelWithAssert: full optimization; runtime checks enabled\n\
- RelWithDebInfo: optimized; debug info; some runtime checks disabled"
FORCE)
endif(NOT CMAKE_BUILD_TYPE)

if (CMAKE_BUILD_TYPE STREQUAL "" AND NOT CMAKE_CXX_FLAGS MATCHES "-O[123gs]")
message(WARNING "It seems you are compiling without optimization. Please set CMAKE_BUILD_TYPE or CMAKE_CXX_FLAGS.")
endif ()
endif ()

# Initialize a variable to hold all the compiler flags -> exported into global config.h(.in)
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES RelWithAssert)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_RELWITHASSERT} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES MinSizeRel)
Expand All @@ -18,15 +56,6 @@ endif()
# Replace ; with space
string(REPLACE ";" " " ALL_COMPILER_FLAGS "${ALL_COMPILER_FLAGS}")

# Determine if fmt is built as a subproject (using add_subdirectory) or if it is the master project.
if (NOT DEFINED GR_TOPLEVEL_PROJECT)
set(GR_TOPLEVEL_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(GR_TOPLEVEL_PROJECT ON)
message(STATUS "CMake version: ${CMAKE_VERSION}")
endif ()
endif ()

# Mainly for FMT
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

Expand Down

0 comments on commit 0fc0727

Please sign in to comment.