blob: fcdcc93b97353fa01543c6df8bf0b57462ed0e65 [file] [log] [blame]
Tomáš Pecka5123e002020-06-05 15:29:21 +02001project(velia LANGUAGES CXX)
Tomáš Peckaa06d74d2020-06-14 18:18:13 +02002cmake_minimum_required(VERSION 3.15)
Tomáš Pecka5123e002020-06-05 15:29:21 +02003
4# Build options
5set(CMAKE_CXX_STANDARD 17)
6set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
8set(CMAKE_CXX_FLAGS_DEBUG "-Werror ${CMAKE_CXX_FLAGS_DEBUG}")
9set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Woverloaded-virtual ${CMAKE_CXX_FLAGS}")
10if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
11 set(CMAKE_CXX_FLAGS "-Wsuggest-override ${CMAKE_CXX_FLAGS}")
12endif()
13
Tomáš Pecka5d4cf2a2020-07-13 13:19:41 +020014if(CMAKE_CXX_FLAGS MATCHES "-stdlib=libc\\+\\+" AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
15 set(STD_FILESYSTEM_LIBRARY c++fs)
16elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
17 set(STD_FILESYSTEM_LIBRARY stdc++fs)
18endif()
19
20
Tomáš Pecka5123e002020-06-05 15:29:21 +020021add_custom_target(velia-version-cmake-ide
22 cmake/ProjectGitVersion.cmake
23 cmake/ProjectGitVersionRunner.cmake
24 )
25include(cmake/ProjectGitVersion.cmake)
26prepare_git_version(VELIA_VERSION "0.0")
27
28find_package(Doxygen)
29option(WITH_DOCS "Create and install internal documentation (needs Doxygen)" ${DOXYGEN_FOUND})
30
Tomáš Pecka74b885f2020-06-06 20:02:41 +020031find_package(Boost REQUIRED)
Tomáš Peckaa193c032020-06-22 17:33:31 +020032find_package(docopt REQUIRED)
Tomáš Pecka5123e002020-06-05 15:29:21 +020033find_package(spdlog REQUIRED)
Tomáš Pecka0a2e8902020-06-09 21:11:20 +020034find_package(sdbus-c++ REQUIRED)
Tomáš Peckaa4336812020-07-24 15:32:58 +020035find_package(PkgConfig)
36
37pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
Tomáš Pecka1f42cfe2020-10-21 18:38:27 +020038if(${SYSTEMD_VERSION} VERSION_EQUAL 245)
39 message(FATAL_ERROR "Systemd 245 has a bug affecting DBus method calls using sdbus-c++. Please see https://github.com/Kistler-Group/sdbus-cpp/issues/106.")
40endif()
Tomáš Pecka5123e002020-06-05 15:29:21 +020041
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010042pkg_check_modules(SYSREPO REQUIRED sysrepo-cpp>=1.4.79 IMPORTED_TARGET sysrepo)
43pkg_check_modules(LIBYANG REQUIRED libyang-cpp>=1.0.190 IMPORTED_TARGET libyang)
44
Tomáš Peckac927d662020-07-13 11:16:16 +020045include(GNUInstallDirs)
46
Tomáš Pecka261c8862020-11-05 11:23:08 +010047include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/)
48
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010049set(YANG_SRCS
50 yang/iana-hardware@2018-03-13.yang
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010051 yang/ietf-hardware@2018-03-13.yang
52 )
53
Tomáš Pecka5123e002020-06-05 15:29:21 +020054# Targets
Tomáš Pecka261c8862020-11-05 11:23:08 +010055# - utils
Tomáš Peckabd828862020-06-15 15:53:35 +020056add_library(velia-utils STATIC
Tomáš Peckaa193c032020-06-22 17:33:31 +020057 src/utils/exceptions.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010058 src/utils/exceptions.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +010059 src/utils/io.cpp
60 src/utils/io.h
Tomáš Peckaa4336812020-07-24 15:32:58 +020061 src/utils/journal.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010062 src/utils/journal.h
63 src/utils/log.h
64 src/utils/log-fwd.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020065 src/utils/log-init.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010066 src/utils/log-init.h
Tomáš Pecka339bc672020-11-11 15:59:03 +010067 src/utils/time.cpp
68 src/utils/time.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020069 )
Tomáš Pecka261c8862020-11-05 11:23:08 +010070target_link_libraries(velia-utils PUBLIC spdlog::spdlog PRIVATE PkgConfig::SYSTEMD)
Tomáš Peckaa4336812020-07-24 15:32:58 +020071
Tomáš Pecka261c8862020-11-05 11:23:08 +010072# - health
73add_library(velia-health STATIC
74 src/health/inputs/AbstractInput.cpp
75 src/health/inputs/DbusSemaphoreInput.cpp
76 src/health/inputs/DbusSystemdInput.cpp
77 src/health/manager/AbstractManager.cpp
78 src/health/manager/StateManager.cpp
79 src/health/outputs/LedSysfsDriver.cpp
80 src/health/outputs/callables.cpp
81 src/health/State.cpp
Tomáš Pecka74b885f2020-06-06 20:02:41 +020082 )
Tomáš Pecka261c8862020-11-05 11:23:08 +010083target_link_libraries(velia-health
84 PUBLIC
85 velia-utils
86 Boost::boost
87 SDBusCpp::sdbus-c++
88 ${STD_FILESYSTEM_LIBRARY}
89 )
Tomáš Pecka74b885f2020-06-06 20:02:41 +020090
Tomáš Pecka491f4f22020-11-05 15:44:00 +010091# - ietf-hardware
92add_library(velia-ietf-hardware STATIC
93 src/ietf-hardware/sysfs/EMMC.cpp
94 src/ietf-hardware/sysfs/EMMC.h
Tomáš Pecka9b1c9672020-11-11 15:24:06 +010095 src/ietf-hardware/sysfs/HWMon.cpp
96 src/ietf-hardware/sysfs/HWMon.h
Tomáš Pecka339bc672020-11-11 15:59:03 +010097 src/ietf-hardware/IETFHardware.cpp
98 src/ietf-hardware/IETFHardware.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +010099 )
100target_link_libraries(velia-ietf-hardware
101 PUBLIC
102 velia-utils
103 ${STD_FILESYSTEM_LIBRARY}
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100104 Boost::boost
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100105 )
106target_include_directories(velia-ietf-hardware PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/velia-hardware)
107
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100108add_library(velia-ietf-hardware-sysrepo STATIC
109 src/ietf-hardware/sysrepo/Logging.cpp
110 src/ietf-hardware/sysrepo/Logging.h
111 src/ietf-hardware/sysrepo/Sysrepo.cpp
112 src/ietf-hardware/sysrepo/Sysrepo.h
113 )
114target_link_libraries(velia-ietf-hardware-sysrepo
115 PUBLIC
116 velia-utils
117 PkgConfig::SYSREPO
118 PkgConfig::LIBYANG
119 )
120target_include_directories(velia-ietf-hardware PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/velia-hardware)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200121
Tomáš Peckad03044f2020-11-12 14:33:10 +0100122# - daemon
123add_executable(veliad
124 src/main.cpp
125 src/Factory.cpp
126 src/Factory.h
127 )
128target_link_libraries(veliad
129 PUBLIC
130 velia-health
131 velia-ietf-hardware
132 velia-ietf-hardware-sysrepo
133 docopt
134 )
135add_dependencies(veliad target-VELIA_VERSION)
136target_include_directories(veliad PUBLIC ${CMAKE_BINARY_DIR})
137
Tomáš Pecka5123e002020-06-05 15:29:21 +0200138# Testing
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200139include(CTest)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200140if(BUILD_TESTING)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200141 find_package(trompeloeil 33 REQUIRED)
142 find_package(doctest 2.3.1 REQUIRED)
143
Tomáš Peckaefd88432020-06-17 15:31:39 +0200144 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/configure.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/tests/configure.cmake.h)
145
Tomáš Pecka5123e002020-06-05 15:29:21 +0200146 add_library(DoctestIntegration STATIC
Tomáš Pecka261c8862020-11-05 11:23:08 +0100147 tests/doctest_integration.cpp
148 tests/trompeloeil_doctest.h
149 tests/wait-a-bit-longer.cpp
150 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200151 target_link_libraries(DoctestIntegration doctest::doctest trompeloeil spdlog::spdlog)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100152 target_include_directories(DoctestIntegration PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tests/)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200153 target_compile_definitions(DoctestIntegration PUBLIC DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
154
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200155 add_library(DbusTesting STATIC
156 tests/dbus-helpers/dbus_semaphore_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100157 tests/dbus-helpers/dbus_semaphore_server.h
Tomáš Peckaccd80c32020-06-22 14:44:32 +0200158 tests/dbus-helpers/dbus_systemd_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100159 tests/dbus-helpers/dbus_systemd_server.h
160 )
161 target_link_libraries(DbusTesting
162 PUBLIC
163 velia-utils
164 SDBusCpp::sdbus-c++
165 )
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200166
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100167 add_library(FsTestUtils STATIC
168 tests/fs-helpers/FileInjector.cpp
169 tests/fs-helpers/FileInjector.h
170 tests/fs-helpers/utils.cpp
171 tests/fs-helpers/utils.h
172 )
173 target_link_libraries(FsTestUtils
174 PUBLIC
175 ${STD_FILESYSTEM_LIBRARY}
176 )
177
Tomáš Pecka5123e002020-06-05 15:29:21 +0200178 function(velia_test name)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100179 add_executable(test-${name} ${CMAKE_SOURCE_DIR}/tests/${name}.cpp)
180 target_link_libraries(test-${name} DoctestIntegration ${ARGN})
181 target_include_directories(test-${name}
182 PUBLIC
183 ${CMAKE_CURRENT_SOURCE_DIR}
184 PRIVATE
185 ${CMAKE_BINARY_DIR}
186 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200187
188 if(NOT CMAKE_CROSSCOMPILING)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100189 add_test(test-${name} test-${name})
Tomáš Pecka5123e002020-06-05 15:29:21 +0200190 endif()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200191 endfunction()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200192
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100193 find_program(SYSREPOCTL sysrepoctl)
194 find_program(SYSREPOCFG sysrepocfg)
195
196 set(sysrepo_previous_fixture_name UNDEFINED)
197 function(sysrepo_fixture_env name)
198 set(test_name_init sysrepo:prep:${name})
199 set(test_name_cleanup sysrepo:clean:${name})
200 set(fixture_name sysrepo:env:${name})
201
202 add_test(NAME ${test_name_init} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} prepare ${ARGN})
203 set_tests_properties(${test_name_init} PROPERTIES FIXTURES_SETUP ${fixture_name} RESOURCE_LOCK sysrepo)
204 add_test(NAME ${test_name_cleanup} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} uninstall ${ARGN})
205 set_tests_properties(${test_name_cleanup} PROPERTIES FIXTURES_CLEANUP ${fixture_name} RESOURCE_LOCK sysrepo)
206
207 if (sysrepo_previous_fixture_name)
208 set_tests_properties(${test_name_init} PROPERTIES DEPENDS sysrepo:clean:${sysrepo_previous_fixture_name})
209 endif ()
210
211 set(sysrepo_previous_fixture_name ${name} PARENT_SCOPE)
212 endfunction()
213
Tomáš Pecka261c8862020-11-05 11:23:08 +0100214 velia_test(health_state-manager velia-health)
215 velia_test(health_input-semaphore velia-health DbusTesting)
216 velia_test(health_input-systemd velia-health DbusTesting)
217 velia_test(health_output-led velia-health)
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100218
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100219 velia_test(hardware_emmc velia-ietf-hardware FsTestUtils)
220 velia_test(hardware_hwmon velia-ietf-hardware FsTestUtils)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100221
222 sysrepo_fixture_env(sysrepo-ietf-hardware YANG ${CMAKE_CURRENT_SOURCE_DIR}/yang/iana-hardware@2018-03-13.yang YANG ${CMAKE_CURRENT_SOURCE_DIR}/yang/ietf-hardware@2018-03-13.yang FEATURE hardware-sensor)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100223 velia_test(hardware_ietf-hardware velia-ietf-hardware velia-ietf-hardware-sysrepo)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100224 set_tests_properties(
225 test-hardware_ietf-hardware
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100226 PROPERTIES FIXTURES_REQUIRED sysrepo:env:sysrepo-ietf-hardware
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100227 RESOURCE_LOCK sysrepo
228 )
229
230 velia_test(sysrepo_two-daemons velia-ietf-hardware-sysrepo)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100231 # ctest dance for sysrepo_two-daemons: compile daemon, create a fixture for test, add the test into the fixture
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100232 add_executable(test-sysrepo_test_merge-daemon ${CMAKE_SOURCE_DIR}/tests/sysrepo_two-daemons_daemon.cpp)
233 target_link_libraries(test-sysrepo_test_merge-daemon PkgConfig::SYSREPO PkgConfig::LIBYANG)
234
235 add_test(NAME sysrepo_test_merge-start COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh start)
236 add_test(NAME sysrepo_test_merge-stop COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh stop)
237 set_tests_properties(sysrepo_test_merge-start PROPERTIES FIXTURES_SETUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
238 set_tests_properties(sysrepo_test_merge-stop PROPERTIES FIXTURES_CLEANUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100239
240 set_tests_properties(
241 test-sysrepo_two-daemons
242 PROPERTIES
243 FIXTURES_REQUIRED "sysrepo:two-daemons;sysrepo:env:sysrepo-ietf-hardware"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100244 RESOURCE_LOCK sysrepo
245 )
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100246 # ensure we run two-daemons merge test and ietf-hardware test sequentially and don't cleanup before everything ends
247 set_tests_properties(sysrepo_test_merge-start PROPERTIES DEPENDS test-hardware_ietf-hardware)
248 set_tests_properties(sysrepo:clean:sysrepo-ietf-hardware PROPERTIES DEPENDS sysrepo_test_merge-stop)
249
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100250
251 find_program(YANGLINT_PATH yanglint)
252 if (NOT YANGLINT_PATH)
253 message(FATAL_ERROR "Cannot find libyang's yanglint")
254 endif()
255 foreach(yang_file ${YANG_SRCS})
256 add_test(NAME yang-tree:${yang_file} COMMAND ${YANGLINT_PATH} -f tree ${CMAKE_CURRENT_SOURCE_DIR}/${yang_file})
257 endforeach()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100258endif()
Tomáš Peckac927d662020-07-13 11:16:16 +0200259
Tomáš Pecka5123e002020-06-05 15:29:21 +0200260if(WITH_DOCS)
261 set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
262 set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
263 configure_file(${doxyfile_in} ${doxyfile} @ONLY)
264 add_custom_target(doc
265 COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
266 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
267 COMMENT "Generating API documentation with Doxygen"
268 VERBATIM
269 SOURCES ${doxyfile_in}
270 )
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200271endif()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100272
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100273set(YANG_DIR ${CMAKE_INSTALL_PREFIX}/share/velia/yang)
274install(FILES ${YANG_SRCS} DESTINATION ${YANG_DIR})
Tomáš Pecka261c8862020-11-05 11:23:08 +0100275install(TARGETS veliad RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/)