CMake: Improve build type detection
This commit is contained in:
+28
-26
@@ -3,9 +3,6 @@
|
||||
|
||||
# Set project name
|
||||
set(PROJECT "stowage")
|
||||
# Set debugging, ON/OFF
|
||||
set(DEBUG "ON")
|
||||
|
||||
# ------------------------------------------
|
||||
|
||||
# Add 'make run' target
|
||||
@@ -18,29 +15,6 @@ add_custom_target(run
|
||||
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
|
||||
project(${PROJECT} CXX)
|
||||
|
||||
# Check if the build should include debugging symbols
|
||||
option(DEBUG "" ${DEBUG})
|
||||
if(DEBUG)
|
||||
# cmake -DDEBUG=on .. && make
|
||||
message("--- Debug ---")
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
# -Og = Optimizations that do not interfere with debugging
|
||||
# -Wall = All warnings about contructions that are easily avoidable
|
||||
# -Wextra = Extra warning flags not covered by -Wall
|
||||
# -g = Produce debugging information in OS's native format
|
||||
# -pg = Generate profile information for analysis with gprof
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -Wall -Wextra -g -pg")
|
||||
# gprof <PROJECT> gmon.out > profile-data.txt
|
||||
else()
|
||||
# cmake -DDEBUG=off .. && make
|
||||
message("--- Release ---")
|
||||
set(CMAKE_BUILD_TYPE "Release")
|
||||
|
||||
# -O3 = Optimizations that increases compilation time and performance
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
|
||||
endif()
|
||||
|
||||
# ------------------------------------------
|
||||
# Setup C++ compiler
|
||||
|
||||
@@ -48,6 +22,34 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# Compiler flags used for all build types
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
||||
# -Wall = All warnings about contructions that are easily avoidable
|
||||
# -Wextra = Extra warning flags not covered by -Wall
|
||||
# -Wpedantic = Warnings for compiler extensions not part of the standard
|
||||
|
||||
# Set default build type if not specified
|
||||
set(DEFAULT_BUILD_TYPE Release)
|
||||
if(EXISTS ${CMAKE_SOURCE_DIR}/.git)
|
||||
set(DEFAULT_BUILD_TYPE Debug)
|
||||
endif()
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE})
|
||||
endif()
|
||||
|
||||
# Set build type specific compiler flags
|
||||
message("--- ${CMAKE_BUILD_TYPE} ---")
|
||||
if(${CMAKE_BUILD_TYPE} STREQUAL Debug)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -g -pg")
|
||||
# -Og = Optimizations that do not interfere with debugging
|
||||
# -g = Produce debugging information in OS's native format
|
||||
# -pg = Generate profile information for analysis with gprof
|
||||
# $ gprof <PROJECT> gmon.out > profile-data.txt
|
||||
elseif(${CMAKE_BUILD_TYPE} STREQUAL Release)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
|
||||
# -O3 = Optimizations that increase compilation time and performance
|
||||
endif()
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# ------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user