Skip to content

Commit 58f079f

Browse files
authored
build: format and check warnings only when BUILD_TESTING is enabled (#67)
* build: enable formatting only if `BUILD_TESTING` is enabled * build: disable `BUILD_TESTING` on top of the `CMakeLists.txt` * build: enable check warning only if `BUILD_TESTING` is enabled
1 parent f3c21d1 commit 58f079f

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

CMakeLists.txt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.5)
22

33
project(MyFibonacci)
44

5+
# Disable testing build if built as a subproject.
6+
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
7+
set(BUILD_TESTING OFF)
8+
endif()
9+
510
file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.7/CPM.cmake
611
${CMAKE_BINARY_DIR}/_deps/CPM.cmake EXPECTED_MD5 14ea07dfb484cad5db4ee1c75fd6a911)
712
include(${CMAKE_BINARY_DIR}/_deps/CPM.cmake)
@@ -13,34 +18,36 @@ cpmgetpackage(CheckWarning.cmake)
1318
add_library(my_fibonacci src/sequence.cpp)
1419
target_include_directories(my_fibonacci PUBLIC include)
1520
set_property(TARGET my_fibonacci PROPERTY CXX_STANDARD 11)
16-
target_check_warning(my_fibonacci)
21+
if(BUILD_TESTING)
22+
target_check_warning(my_fibonacci)
23+
endif()
1724

1825
add_executable(my_fibonacci_main src/main.cpp)
1926
target_link_libraries(my_fibonacci_main PUBLIC argparse my_fibonacci)
2027
set_property(TARGET my_fibonacci_main PROPERTY CXX_STANDARD 11)
21-
target_check_warning(my_fibonacci_main)
28+
if(BUILD_TESTING)
29+
target_check_warning(my_fibonacci_main)
30+
endif()
2231

23-
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
24-
cpmgetpackage(Format.cmake)
32+
if(BUILD_TESTING)
33+
enable_testing()
2534

26-
if(BUILD_TESTING)
27-
enable_testing()
35+
cpmgetpackage(Format.cmake)
2836

29-
cpmgetpackage(Catch2)
30-
include("${Catch2_SOURCE_DIR}/extras/Catch.cmake")
37+
cpmgetpackage(Catch2)
38+
include("${Catch2_SOURCE_DIR}/extras/Catch.cmake")
3139

32-
get_target_property(my_fibonacci_SOURCES my_fibonacci SOURCES)
33-
add_executable(my_fibonacci_test test/sequence_test.cpp ${my_fibonacci_SOURCES})
40+
get_target_property(my_fibonacci_SOURCES my_fibonacci SOURCES)
41+
add_executable(my_fibonacci_test test/sequence_test.cpp ${my_fibonacci_SOURCES})
3442

35-
get_target_property(my_fibonacci_INCLUDES my_fibonacci INCLUDE_DIRECTORIES)
36-
target_include_directories(my_fibonacci_test PRIVATE ${my_fibonacci_INCLUDES})
43+
get_target_property(my_fibonacci_INCLUDES my_fibonacci INCLUDE_DIRECTORIES)
44+
target_include_directories(my_fibonacci_test PRIVATE ${my_fibonacci_INCLUDES})
3745

38-
target_link_libraries(my_fibonacci_test PRIVATE Catch2::Catch2WithMain)
46+
target_link_libraries(my_fibonacci_test PRIVATE Catch2::Catch2WithMain)
3947

40-
target_check_warning(my_fibonacci_test)
41-
target_compile_options(my_fibonacci_test PRIVATE --coverage -O0)
42-
target_link_options(my_fibonacci_test PRIVATE --coverage)
48+
target_check_warning(my_fibonacci_test)
49+
target_compile_options(my_fibonacci_test PRIVATE --coverage -O0)
50+
target_link_options(my_fibonacci_test PRIVATE --coverage)
4351

44-
catch_discover_tests(my_fibonacci_test)
45-
endif()
52+
catch_discover_tests(my_fibonacci_test)
4653
endif()

0 commit comments

Comments
 (0)