-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
114 lines (94 loc) · 3.03 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
cmake_minimum_required(VERSION 3.12)
# Set the project name
project(tinyrobotics VERSION 1.0)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Option to enable test building or not
option(BUILD_TESTS "Build tests" ON)
# Force coloured compiler output
add_compile_options(-fdiagnostics-color)
# CXX flags
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
# Find required packages
if(BUILD_TESTS)
find_package(Catch2 REQUIRED)
endif()
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
find_package(TinyXML2 REQUIRED)
find_package(NLopt REQUIRED)
# Libraries
set(LIBS Eigen3::Eigen tinyxml2::tinyxml2 ${NLOPT_LIBRARIES})
# Target names
set(TARGET_LIB tinyrobotics_lib)
set(TARGET_BIN tinyrobotics_executable)
set(TARGET_TEST tinyrobotics_tests)
# Source files
file(GLOB SRC_INCLUDES "include/*.hpp")
file(GLOB SRC_EXAMPLES "examples/*.cpp")
file(GLOB SRC_TEST "test/*.cpp")
# Static library
if(SRC_INCLUDES)
add_library(${TARGET_LIB} STATIC ${SRC_INCLUDES})
target_link_libraries(${TARGET_LIB} ${LIBS})
endif()
set_target_properties(${TARGET_LIB} PROPERTIES LINKER_LANGUAGE CXX)
# Examples
foreach(SRC_FILE ${SRC_EXAMPLES})
get_filename_component(EXAMPLE ${SRC_FILE} NAME_WE)
add_executable(${EXAMPLE} ${SRC_FILE})
if(SRC_INCLUDES)
target_link_libraries(${EXAMPLE} ${TARGET_LIB})
endif()
target_link_libraries(${EXAMPLE} ${LIBS})
endforeach()
# Unit tests
if(BUILD_TESTS)
add_executable(${TARGET_TEST} ${SRC_TEST})
if(SRC_INCLUDES)
target_link_libraries(${TARGET_TEST} ${TARGET_LIB})
endif()
target_link_libraries(${TARGET_TEST} ${LIBS})
target_link_libraries(${TARGET_TEST} Catch2::Catch2)
# Run unit tests after building executables
add_custom_target(
run_tests ALL
COMMAND ${TARGET_TEST} --use-colour yes
DEPENDS ${TARGET_TEST} ${TARGET_LIB}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Running tests")
endif()
# Create a library target with the include files
add_library(tinyrobotics SHARED ${SRC_INCLUDES})
# Link libraries to the library target
target_link_libraries(tinyrobotics PUBLIC ${LIBS})
set_target_properties(tinyrobotics PROPERTIES LINKER_LANGUAGE CXX)
# Set the installation rules
install(
TARGETS tinyrobotics
EXPORT tinyrobotics_targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES
DESTINATION include/tinyrobotics)
install(DIRECTORY include/ DESTINATION include/tinyrobotics)
install(
EXPORT tinyrobotics_targets
FILE tinyrobotics_targets.cmake
NAMESPACE tinyrobotics::
DESTINATION lib/cmake/tinyrobotics)
# Create a package configuration file for library
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"tinyroboticsConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
install(FILES "tinyroboticsConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/tinyroboticsConfigVersion.cmake"
DESTINATION lib/cmake/tinyrobotics)