Skip to content

Commit aa94a23

Browse files
committed
CMake: add code coverage as build options instead of build type
1 parent fddd0c4 commit aa94a23

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,23 @@ if (NOT COMPILER_SUPPORT_GNUXX11)
3838
endif()
3939

4040
# ------------------------------------------------------------------------------
41-
# Build options
42-
set(TESTS_DEFAULT OFF)
43-
4441
# Set default build type if not specified by user
45-
if (NOT CMAKE_BUILD_TYPE)
46-
set (CMAKE_BUILD_TYPE Release
47-
CACHE STRING "Choose type of build (Release/Debug/Coverage)." FORCE)
48-
endif()
49-
50-
string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UPPER)
51-
# Enable unit tests, by default, for coverage builds
52-
if (${BUILD_TYPE_UPPER} STREQUAL "COVERAGE")
53-
set(TESTS_DEFAULT ON)
42+
set(DEFAULT_BUILD_TYPE "Release")
43+
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
44+
# Use the default build type if not specified
45+
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
46+
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
47+
STRING "Choose the type of build." FORCE)
48+
# Set the possible values of build type for cmake-gui
49+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
50+
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
5451
endif()
5552

5653
option(ENABLE_DOC "Enable documentation building" OFF)
57-
option(ENABLE_TESTS "Build Unit tests (make test)" ${TESTS_DEFAULT})
54+
option(ENABLE_TESTS "Build Unit tests (make test)" OFF)
5855
option(ENABLE_TESTS_INTERNAL "Build Unit tests that don't use public API" OFF)
5956
option(ENABLE_TESTS_VALGRIND "Build Unit tests with Valgrind Memcheck" OFF)
57+
option(ENABLE_TESTS_COVERAGE "Enable support for code coverage" OFF)
6058
option(PACKAGE_BUILDER_RPM "Enable RPM package builder (make rpm)" OFF)
6159
option(PACKAGE_BUILDER_DEB "Enable DEB package builder (make deb)" OFF)
6260
option(USE_SYSTEM_LZ4 "Use system-installed LZ4 library" ON)
@@ -76,11 +74,15 @@ endif()
7674
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
7775
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
7876
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Wall -Wextra -pedantic")
79-
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage")
8077
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
8178
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
8279
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra -pedantic")
83-
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage")
80+
81+
if (ENABLE_TESTS AND ENABLE_TESTS_COVERAGE)
82+
# Additional flags for code coverage
83+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
84+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
85+
endif()
8486

8587
# ------------------------------------------------------------------------------
8688
# Project components

CMakeModules/unit_tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function(coverage_add_target)
167167
COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet
168168

169169
# Run tests
170-
COMMAND "${CMAKE_CTEST_COMMAND}" --quiet
170+
COMMAND "${CMAKE_CTEST_COMMAND}"
171171

172172
# Capture the counters
173173
COMMAND "${PATH_LCOV}"

tests/unit_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unit_tests_register_test(api.cpp)
2121

2222
# Enable code coverage target (i.e. make coverage) when appropriate build
2323
# type is chosen.
24-
if (${BUILD_TYPE_UPPER} STREQUAL "COVERAGE")
24+
if (ENABLE_TESTS AND ENABLE_TESTS_COVERAGE)
2525
coverage_add_target()
2626
endif()
2727

0 commit comments

Comments
 (0)