blob: 6c05afb6e7c9b90c7441bb2ca2257dd875d96989 [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
Jan Kundrátcf65a202021-01-19 09:37:14 +010042pkg_check_modules(SYSREPO REQUIRED sysrepo-cpp>=1.4.100 IMPORTED_TARGET sysrepo)
43pkg_check_modules(LIBYANG REQUIRED libyang-cpp>=1.0.209 IMPORTED_TARGET libyang)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010044
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
Tomáš Pecka9a02c3f2021-01-21 15:27:22 +010050 yang/czechlight-system@2021-01-13.yang
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010051 yang/iana-hardware@2018-03-13.yang
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010052 yang/ietf-hardware@2018-03-13.yang
Tomáš Peckaf98ce032021-01-14 13:35:00 +010053 yang/iana-crypt-hash@2014-08-06.yang
54 yang/ietf-inet-types@2013-07-15.yang
55 yang/ietf-netconf-acm@2018-02-14.yang
56 yang/ietf-system@2014-08-06.yang
57 yang/ietf-yang-types@2013-07-15.yang
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010058 )
59
Tomáš Pecka5123e002020-06-05 15:29:21 +020060# Targets
Tomáš Pecka261c8862020-11-05 11:23:08 +010061# - utils
Tomáš Peckabd828862020-06-15 15:53:35 +020062add_library(velia-utils STATIC
Tomáš Peckaa193c032020-06-22 17:33:31 +020063 src/utils/exceptions.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010064 src/utils/exceptions.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +010065 src/utils/io.cpp
66 src/utils/io.h
Tomáš Peckaa4336812020-07-24 15:32:58 +020067 src/utils/journal.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010068 src/utils/journal.h
69 src/utils/log.h
70 src/utils/log-fwd.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020071 src/utils/log-init.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010072 src/utils/log-init.h
Tomáš Peckaf02671e2021-01-13 09:09:48 +010073 src/utils/log-sysrepo.cpp
74 src/utils/log-sysrepo.h
Tomáš Pecka339bc672020-11-11 15:59:03 +010075 src/utils/time.cpp
76 src/utils/time.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020077 )
Tomáš Peckaf02671e2021-01-13 09:09:48 +010078target_link_libraries(velia-utils PUBLIC spdlog::spdlog PRIVATE PkgConfig::SYSTEMD PkgConfig::SYSREPO)
Tomáš Peckaa4336812020-07-24 15:32:58 +020079
Tomáš Pecka261c8862020-11-05 11:23:08 +010080# - health
81add_library(velia-health STATIC
82 src/health/inputs/AbstractInput.cpp
83 src/health/inputs/DbusSemaphoreInput.cpp
84 src/health/inputs/DbusSystemdInput.cpp
85 src/health/manager/AbstractManager.cpp
86 src/health/manager/StateManager.cpp
87 src/health/outputs/LedSysfsDriver.cpp
88 src/health/outputs/callables.cpp
89 src/health/State.cpp
Tomáš Pecka74b885f2020-06-06 20:02:41 +020090 )
Tomáš Pecka261c8862020-11-05 11:23:08 +010091target_link_libraries(velia-health
92 PUBLIC
93 velia-utils
94 Boost::boost
95 SDBusCpp::sdbus-c++
96 ${STD_FILESYSTEM_LIBRARY}
97 )
Tomáš Pecka74b885f2020-06-06 20:02:41 +020098
Tomáš Pecka491f4f22020-11-05 15:44:00 +010099# - ietf-hardware
100add_library(velia-ietf-hardware STATIC
101 src/ietf-hardware/sysfs/EMMC.cpp
102 src/ietf-hardware/sysfs/EMMC.h
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100103 src/ietf-hardware/sysfs/HWMon.cpp
104 src/ietf-hardware/sysfs/HWMon.h
Tomáš Pecka339bc672020-11-11 15:59:03 +0100105 src/ietf-hardware/IETFHardware.cpp
106 src/ietf-hardware/IETFHardware.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100107 )
108target_link_libraries(velia-ietf-hardware
109 PUBLIC
110 velia-utils
111 ${STD_FILESYSTEM_LIBRARY}
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100112 Boost::boost
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100113 )
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100114
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100115add_library(velia-ietf-hardware-sysrepo STATIC
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100116 src/ietf-hardware/sysrepo/Sysrepo.cpp
117 src/ietf-hardware/sysrepo/Sysrepo.h
118 )
119target_link_libraries(velia-ietf-hardware-sysrepo
120 PUBLIC
121 velia-utils
122 PkgConfig::SYSREPO
123 PkgConfig::LIBYANG
124 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200125
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100126# - ietf-system
127add_library(velia-system STATIC
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100128 src/system/RAUC.cpp
129 src/system/RAUC.h
Tomáš Peckaf976c5b2021-01-23 21:19:52 +0100130 src/system/IETFSystem.cpp
131 src/system/IETFSystem.h
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100132 )
133target_link_libraries(velia-system
134 PUBLIC
135 velia-utils
136 PkgConfig::SYSREPO
137 PkgConfig::LIBYANG
Václav Kubernát6dd52c82021-01-21 08:38:42 +0100138 SDBusCpp::sdbus-c++
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100139 )
140
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100141# - daemons
Tomáš Peckad03044f2020-11-12 14:33:10 +0100142add_executable(veliad
143 src/main.cpp
144 src/Factory.cpp
145 src/Factory.h
146 )
147target_link_libraries(veliad
148 PUBLIC
149 velia-health
150 velia-ietf-hardware
151 velia-ietf-hardware-sysrepo
152 docopt
153 )
154add_dependencies(veliad target-VELIA_VERSION)
155target_include_directories(veliad PUBLIC ${CMAKE_BINARY_DIR})
156
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100157add_executable(veliad-system
158 src/main-system.cpp
159 )
160target_link_libraries(veliad-system
161 PUBLIC
162 velia-system
163 docopt
164 )
165add_dependencies(veliad-system target-VELIA_VERSION)
166target_include_directories(veliad-system PUBLIC ${CMAKE_BINARY_DIR})
167
Tomáš Pecka5123e002020-06-05 15:29:21 +0200168# Testing
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200169include(CTest)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200170if(BUILD_TESTING)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200171 find_package(trompeloeil 33 REQUIRED)
172 find_package(doctest 2.3.1 REQUIRED)
173
Tomáš Peckaefd88432020-06-17 15:31:39 +0200174 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/configure.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/tests/configure.cmake.h)
175
Tomáš Pecka5123e002020-06-05 15:29:21 +0200176 add_library(DoctestIntegration STATIC
Tomáš Pecka261c8862020-11-05 11:23:08 +0100177 tests/doctest_integration.cpp
178 tests/trompeloeil_doctest.h
179 tests/wait-a-bit-longer.cpp
180 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200181 target_link_libraries(DoctestIntegration doctest::doctest trompeloeil spdlog::spdlog)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100182 target_include_directories(DoctestIntegration PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tests/)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200183 target_compile_definitions(DoctestIntegration PUBLIC DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
184
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200185 add_library(DbusTesting STATIC
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100186 tests/dbus-helpers/dbus_rauc_server.cpp
187 tests/dbus-helpers/dbus_rauc_server.h
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200188 tests/dbus-helpers/dbus_semaphore_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100189 tests/dbus-helpers/dbus_semaphore_server.h
Tomáš Peckaccd80c32020-06-22 14:44:32 +0200190 tests/dbus-helpers/dbus_systemd_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100191 tests/dbus-helpers/dbus_systemd_server.h
192 )
193 target_link_libraries(DbusTesting
194 PUBLIC
195 velia-utils
196 SDBusCpp::sdbus-c++
197 )
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200198
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100199 add_library(FsTestUtils STATIC
200 tests/fs-helpers/FileInjector.cpp
201 tests/fs-helpers/FileInjector.h
202 tests/fs-helpers/utils.cpp
203 tests/fs-helpers/utils.h
204 )
205 target_link_libraries(FsTestUtils
206 PUBLIC
207 ${STD_FILESYSTEM_LIBRARY}
208 )
209
Tomáš Pecka5123e002020-06-05 15:29:21 +0200210 function(velia_test name)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100211 add_executable(test-${name} ${CMAKE_SOURCE_DIR}/tests/${name}.cpp)
212 target_link_libraries(test-${name} DoctestIntegration ${ARGN})
213 target_include_directories(test-${name}
214 PUBLIC
215 ${CMAKE_CURRENT_SOURCE_DIR}
216 PRIVATE
217 ${CMAKE_BINARY_DIR}
218 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200219
220 if(NOT CMAKE_CROSSCOMPILING)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100221 add_test(test-${name} test-${name})
Tomáš Pecka5123e002020-06-05 15:29:21 +0200222 endif()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200223 endfunction()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200224
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100225 find_program(SYSREPOCTL sysrepoctl)
226 find_program(SYSREPOCFG sysrepocfg)
227
228 set(sysrepo_previous_fixture_name UNDEFINED)
229 function(sysrepo_fixture_env name)
230 set(test_name_init sysrepo:prep:${name})
231 set(test_name_cleanup sysrepo:clean:${name})
232 set(fixture_name sysrepo:env:${name})
233
234 add_test(NAME ${test_name_init} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} prepare ${ARGN})
235 set_tests_properties(${test_name_init} PROPERTIES FIXTURES_SETUP ${fixture_name} RESOURCE_LOCK sysrepo)
236 add_test(NAME ${test_name_cleanup} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} uninstall ${ARGN})
237 set_tests_properties(${test_name_cleanup} PROPERTIES FIXTURES_CLEANUP ${fixture_name} RESOURCE_LOCK sysrepo)
238
239 if (sysrepo_previous_fixture_name)
240 set_tests_properties(${test_name_init} PROPERTIES DEPENDS sysrepo:clean:${sysrepo_previous_fixture_name})
241 endif ()
242
243 set(sysrepo_previous_fixture_name ${name} PARENT_SCOPE)
244 endfunction()
245
Tomáš Pecka261c8862020-11-05 11:23:08 +0100246 velia_test(health_state-manager velia-health)
247 velia_test(health_input-semaphore velia-health DbusTesting)
248 velia_test(health_input-systemd velia-health DbusTesting)
249 velia_test(health_output-led velia-health)
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100250
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100251 velia_test(system_rauc velia-system DbusTesting)
252
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100253 velia_test(hardware_emmc velia-ietf-hardware FsTestUtils)
254 velia_test(hardware_hwmon velia-ietf-hardware FsTestUtils)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100255
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100256 sysrepo_fixture_env(sysrepo-ietf-system YANG ${CMAKE_CURRENT_SOURCE_DIR}/yang/ietf-system@2014-08-06.yang)
Tomáš Peckaf976c5b2021-01-23 21:19:52 +0100257 velia_test(sysrepo_system-ietfsystem velia-system DbusTesting)
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100258 set_tests_properties(
Tomáš Peckaf976c5b2021-01-23 21:19:52 +0100259 test-sysrepo_system-ietfsystem
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100260 PROPERTIES FIXTURES_REQUIRED sysrepo:env:sysrepo-ietf-system
261 RESOURCE_LOCK sysrepo
262 )
263
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100264 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 +0100265 velia_test(hardware_ietf-hardware velia-ietf-hardware velia-ietf-hardware-sysrepo)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100266 set_tests_properties(
267 test-hardware_ietf-hardware
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100268 PROPERTIES FIXTURES_REQUIRED sysrepo:env:sysrepo-ietf-hardware
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100269 RESOURCE_LOCK sysrepo
270 )
271
272 velia_test(sysrepo_two-daemons velia-ietf-hardware-sysrepo)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100273 # 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 +0100274 add_executable(test-sysrepo_test_merge-daemon ${CMAKE_SOURCE_DIR}/tests/sysrepo_two-daemons_daemon.cpp)
275 target_link_libraries(test-sysrepo_test_merge-daemon PkgConfig::SYSREPO PkgConfig::LIBYANG)
276
277 add_test(NAME sysrepo_test_merge-start COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh start)
278 add_test(NAME sysrepo_test_merge-stop COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh stop)
279 set_tests_properties(sysrepo_test_merge-start PROPERTIES FIXTURES_SETUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
280 set_tests_properties(sysrepo_test_merge-stop PROPERTIES FIXTURES_CLEANUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100281
282 set_tests_properties(
283 test-sysrepo_two-daemons
284 PROPERTIES
285 FIXTURES_REQUIRED "sysrepo:two-daemons;sysrepo:env:sysrepo-ietf-hardware"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100286 RESOURCE_LOCK sysrepo
287 )
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100288 # ensure we run two-daemons merge test and ietf-hardware test sequentially and don't cleanup before everything ends
289 set_tests_properties(sysrepo_test_merge-start PROPERTIES DEPENDS test-hardware_ietf-hardware)
290 set_tests_properties(sysrepo:clean:sysrepo-ietf-hardware PROPERTIES DEPENDS sysrepo_test_merge-stop)
291
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100292
293 find_program(YANGLINT_PATH yanglint)
294 if (NOT YANGLINT_PATH)
295 message(FATAL_ERROR "Cannot find libyang's yanglint")
296 endif()
297 foreach(yang_file ${YANG_SRCS})
298 add_test(NAME yang-tree:${yang_file} COMMAND ${YANGLINT_PATH} -f tree ${CMAKE_CURRENT_SOURCE_DIR}/${yang_file})
299 endforeach()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100300endif()
Tomáš Peckac927d662020-07-13 11:16:16 +0200301
Tomáš Pecka5123e002020-06-05 15:29:21 +0200302if(WITH_DOCS)
303 set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
304 set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
305 configure_file(${doxyfile_in} ${doxyfile} @ONLY)
306 add_custom_target(doc
307 COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
308 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
309 COMMENT "Generating API documentation with Doxygen"
310 VERBATIM
311 SOURCES ${doxyfile_in}
312 )
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200313endif()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100314
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100315set(YANG_DIR ${CMAKE_INSTALL_PREFIX}/share/velia/yang)
316install(FILES ${YANG_SRCS} DESTINATION ${YANG_DIR})
Tomáš Pecka261c8862020-11-05 11:23:08 +0100317install(TARGETS veliad RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/)
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100318install(TARGETS veliad-system RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/)