-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
73 lines (47 loc) · 2.13 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Set high level project parameters
cmake_minimum_required(VERSION 3.13)
project( code_sample LANGUAGES CXX )
set( CMAKE_CXX_STANDARD 17 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
message( STATUS "cmake build type: ${CMAKE_BUILD_TYPE}")
# Compiler warnings should always be treated as errors. Untreated warnings lead to bugs.
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -O3 -g")
option(BUILD_TESTS "Unit tests for sample code" ON)
# File to include external support stuff
include( ${CMAKE_SOURCE_DIR}/contrib/contrib.cmake )
# The project is broken into separable, testable modules compiled separately as shared object
# files.
set( components
data
operation )
foreach( component IN LISTS components )
add_library( ${component} SHARED src/${component}.cpp )
target_include_directories( ${component} PRIVATE ${CMAKE_BINARY_DIR} )
endforeach()
# all components are compiled and linked to the final executable
add_executable( sample src/sample.cpp )
foreach( link IN LISTS components )
target_link_libraries( sample PRIVATE ${link} )
endforeach()
# build the tests
if( BUILD_TESTS )
enable_testing()
add_library( tests_general SHARED testing/tests_general.hpp )
set_target_properties( tests_general PROPERTIES LINKER_LANGUAGE CXX )
target_link_libraries( tests_general PUBLIC Catch data )
target_include_directories( tests_general PRIVATE ${CMAKE_SOURCE_DIR}/src )
# Link each test file to the implementation it tests
foreach( component IN LISTS components )
add_executable( ${component}_tests src/${component}_tests.cpp)
target_include_directories(${component}_tests PRIVATE ${CMAKE_SOURCE_DIR}/testing)
target_include_directories(${component}_tests PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(${component}_tests PRIVATE ${component} tests_general )
# tests reflect the name of the executable they are given above
add_test( NAME ${component}_test COMMAND ${component}_tests
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} )
endforeach()
# additional libraries
target_link_libraries( operation_tests PRIVATE data )
else()
message( WARNING "No unit tests built")
endif()