blob: f07caac6e34e4f296b03c227c10ec5adf01aabcc [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
Tomáš Peckaf98ce032021-01-14 13:35:00 +010052 yang/iana-crypt-hash@2014-08-06.yang
53 yang/ietf-inet-types@2013-07-15.yang
54 yang/ietf-netconf-acm@2018-02-14.yang
55 yang/ietf-system@2014-08-06.yang
56 yang/ietf-yang-types@2013-07-15.yang
Tomáš Pecka73a13cc2020-11-17 11:12:19 +010057 )
58
Tomáš Pecka5123e002020-06-05 15:29:21 +020059# Targets
Tomáš Pecka261c8862020-11-05 11:23:08 +010060# - utils
Tomáš Peckabd828862020-06-15 15:53:35 +020061add_library(velia-utils STATIC
Tomáš Peckaa193c032020-06-22 17:33:31 +020062 src/utils/exceptions.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010063 src/utils/exceptions.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +010064 src/utils/io.cpp
65 src/utils/io.h
Tomáš Peckaa4336812020-07-24 15:32:58 +020066 src/utils/journal.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010067 src/utils/journal.h
68 src/utils/log.h
69 src/utils/log-fwd.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020070 src/utils/log-init.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +010071 src/utils/log-init.h
Tomáš Peckaf02671e2021-01-13 09:09:48 +010072 src/utils/log-sysrepo.cpp
73 src/utils/log-sysrepo.h
Tomáš Pecka339bc672020-11-11 15:59:03 +010074 src/utils/time.cpp
75 src/utils/time.h
Tomáš Pecka5123e002020-06-05 15:29:21 +020076 )
Tomáš Peckaf02671e2021-01-13 09:09:48 +010077target_link_libraries(velia-utils PUBLIC spdlog::spdlog PRIVATE PkgConfig::SYSTEMD PkgConfig::SYSREPO)
Tomáš Peckaa4336812020-07-24 15:32:58 +020078
Tomáš Pecka261c8862020-11-05 11:23:08 +010079# - health
80add_library(velia-health STATIC
81 src/health/inputs/AbstractInput.cpp
82 src/health/inputs/DbusSemaphoreInput.cpp
83 src/health/inputs/DbusSystemdInput.cpp
84 src/health/manager/AbstractManager.cpp
85 src/health/manager/StateManager.cpp
86 src/health/outputs/LedSysfsDriver.cpp
87 src/health/outputs/callables.cpp
88 src/health/State.cpp
Tomáš Pecka74b885f2020-06-06 20:02:41 +020089 )
Tomáš Pecka261c8862020-11-05 11:23:08 +010090target_link_libraries(velia-health
91 PUBLIC
92 velia-utils
93 Boost::boost
94 SDBusCpp::sdbus-c++
95 ${STD_FILESYSTEM_LIBRARY}
96 )
Tomáš Pecka74b885f2020-06-06 20:02:41 +020097
Tomáš Pecka491f4f22020-11-05 15:44:00 +010098# - ietf-hardware
99add_library(velia-ietf-hardware STATIC
100 src/ietf-hardware/sysfs/EMMC.cpp
101 src/ietf-hardware/sysfs/EMMC.h
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100102 src/ietf-hardware/sysfs/HWMon.cpp
103 src/ietf-hardware/sysfs/HWMon.h
Tomáš Pecka339bc672020-11-11 15:59:03 +0100104 src/ietf-hardware/IETFHardware.cpp
105 src/ietf-hardware/IETFHardware.h
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100106 )
107target_link_libraries(velia-ietf-hardware
108 PUBLIC
109 velia-utils
110 ${STD_FILESYSTEM_LIBRARY}
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100111 Boost::boost
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100112 )
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100113
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100114add_library(velia-ietf-hardware-sysrepo STATIC
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100115 src/ietf-hardware/sysrepo/Sysrepo.cpp
116 src/ietf-hardware/sysrepo/Sysrepo.h
117 )
118target_link_libraries(velia-ietf-hardware-sysrepo
119 PUBLIC
120 velia-utils
121 PkgConfig::SYSREPO
122 PkgConfig::LIBYANG
123 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200124
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100125# - ietf-system
126add_library(velia-system STATIC
127 src/system/Sysrepo.cpp
128 src/system/Sysrepo.h
129 )
130target_link_libraries(velia-system
131 PUBLIC
132 velia-utils
133 PkgConfig::SYSREPO
134 PkgConfig::LIBYANG
135 )
136
Tomáš Peckad03044f2020-11-12 14:33:10 +0100137# - daemon
138add_executable(veliad
139 src/main.cpp
140 src/Factory.cpp
141 src/Factory.h
142 )
143target_link_libraries(veliad
144 PUBLIC
145 velia-health
146 velia-ietf-hardware
147 velia-ietf-hardware-sysrepo
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100148 velia-system
Tomáš Peckad03044f2020-11-12 14:33:10 +0100149 docopt
150 )
151add_dependencies(veliad target-VELIA_VERSION)
152target_include_directories(veliad PUBLIC ${CMAKE_BINARY_DIR})
153
Tomáš Pecka5123e002020-06-05 15:29:21 +0200154# Testing
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200155include(CTest)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200156if(BUILD_TESTING)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200157 find_package(trompeloeil 33 REQUIRED)
158 find_package(doctest 2.3.1 REQUIRED)
159
Tomáš Peckaefd88432020-06-17 15:31:39 +0200160 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/configure.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/tests/configure.cmake.h)
161
Tomáš Pecka5123e002020-06-05 15:29:21 +0200162 add_library(DoctestIntegration STATIC
Tomáš Pecka261c8862020-11-05 11:23:08 +0100163 tests/doctest_integration.cpp
164 tests/trompeloeil_doctest.h
165 tests/wait-a-bit-longer.cpp
166 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200167 target_link_libraries(DoctestIntegration doctest::doctest trompeloeil spdlog::spdlog)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100168 target_include_directories(DoctestIntegration PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tests/)
Tomáš Pecka5123e002020-06-05 15:29:21 +0200169 target_compile_definitions(DoctestIntegration PUBLIC DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
170
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200171 add_library(DbusTesting STATIC
172 tests/dbus-helpers/dbus_semaphore_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100173 tests/dbus-helpers/dbus_semaphore_server.h
Tomáš Peckaccd80c32020-06-22 14:44:32 +0200174 tests/dbus-helpers/dbus_systemd_server.cpp
Tomáš Pecka261c8862020-11-05 11:23:08 +0100175 tests/dbus-helpers/dbus_systemd_server.h
176 )
177 target_link_libraries(DbusTesting
178 PUBLIC
179 velia-utils
180 SDBusCpp::sdbus-c++
181 )
Tomáš Pecka0a2e8902020-06-09 21:11:20 +0200182
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100183 add_library(FsTestUtils STATIC
184 tests/fs-helpers/FileInjector.cpp
185 tests/fs-helpers/FileInjector.h
186 tests/fs-helpers/utils.cpp
187 tests/fs-helpers/utils.h
188 )
189 target_link_libraries(FsTestUtils
190 PUBLIC
191 ${STD_FILESYSTEM_LIBRARY}
192 )
193
Tomáš Pecka5123e002020-06-05 15:29:21 +0200194 function(velia_test name)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100195 add_executable(test-${name} ${CMAKE_SOURCE_DIR}/tests/${name}.cpp)
196 target_link_libraries(test-${name} DoctestIntegration ${ARGN})
197 target_include_directories(test-${name}
198 PUBLIC
199 ${CMAKE_CURRENT_SOURCE_DIR}
200 PRIVATE
201 ${CMAKE_BINARY_DIR}
202 )
Tomáš Pecka5123e002020-06-05 15:29:21 +0200203
204 if(NOT CMAKE_CROSSCOMPILING)
Tomáš Pecka261c8862020-11-05 11:23:08 +0100205 add_test(test-${name} test-${name})
Tomáš Pecka5123e002020-06-05 15:29:21 +0200206 endif()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200207 endfunction()
Tomáš Pecka5123e002020-06-05 15:29:21 +0200208
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100209 find_program(SYSREPOCTL sysrepoctl)
210 find_program(SYSREPOCFG sysrepocfg)
211
212 set(sysrepo_previous_fixture_name UNDEFINED)
213 function(sysrepo_fixture_env name)
214 set(test_name_init sysrepo:prep:${name})
215 set(test_name_cleanup sysrepo:clean:${name})
216 set(fixture_name sysrepo:env:${name})
217
218 add_test(NAME ${test_name_init} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} prepare ${ARGN})
219 set_tests_properties(${test_name_init} PROPERTIES FIXTURES_SETUP ${fixture_name} RESOURCE_LOCK sysrepo)
220 add_test(NAME ${test_name_cleanup} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL} ${SYSREPOCFG} uninstall ${ARGN})
221 set_tests_properties(${test_name_cleanup} PROPERTIES FIXTURES_CLEANUP ${fixture_name} RESOURCE_LOCK sysrepo)
222
223 if (sysrepo_previous_fixture_name)
224 set_tests_properties(${test_name_init} PROPERTIES DEPENDS sysrepo:clean:${sysrepo_previous_fixture_name})
225 endif ()
226
227 set(sysrepo_previous_fixture_name ${name} PARENT_SCOPE)
228 endfunction()
229
Tomáš Pecka261c8862020-11-05 11:23:08 +0100230 velia_test(health_state-manager velia-health)
231 velia_test(health_input-semaphore velia-health DbusTesting)
232 velia_test(health_input-systemd velia-health DbusTesting)
233 velia_test(health_output-led velia-health)
Tomáš Pecka491f4f22020-11-05 15:44:00 +0100234
Tomáš Pecka9b1c9672020-11-11 15:24:06 +0100235 velia_test(hardware_emmc velia-ietf-hardware FsTestUtils)
236 velia_test(hardware_hwmon velia-ietf-hardware FsTestUtils)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100237
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100238 sysrepo_fixture_env(sysrepo-ietf-system YANG ${CMAKE_CURRENT_SOURCE_DIR}/yang/ietf-system@2014-08-06.yang)
239 velia_test(sysrepo_system velia-system DbusTesting)
240 set_tests_properties(
241 test-sysrepo_system
242 PROPERTIES FIXTURES_REQUIRED sysrepo:env:sysrepo-ietf-system
243 RESOURCE_LOCK sysrepo
244 )
245
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100246 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 +0100247 velia_test(hardware_ietf-hardware velia-ietf-hardware velia-ietf-hardware-sysrepo)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100248 set_tests_properties(
249 test-hardware_ietf-hardware
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100250 PROPERTIES FIXTURES_REQUIRED sysrepo:env:sysrepo-ietf-hardware
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100251 RESOURCE_LOCK sysrepo
252 )
253
254 velia_test(sysrepo_two-daemons velia-ietf-hardware-sysrepo)
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100255 # 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 +0100256 add_executable(test-sysrepo_test_merge-daemon ${CMAKE_SOURCE_DIR}/tests/sysrepo_two-daemons_daemon.cpp)
257 target_link_libraries(test-sysrepo_test_merge-daemon PkgConfig::SYSREPO PkgConfig::LIBYANG)
258
259 add_test(NAME sysrepo_test_merge-start COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh start)
260 add_test(NAME sysrepo_test_merge-stop COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepo_two-daemons_control.sh stop)
261 set_tests_properties(sysrepo_test_merge-start PROPERTIES FIXTURES_SETUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
262 set_tests_properties(sysrepo_test_merge-stop PROPERTIES FIXTURES_CLEANUP sysrepo:two-daemons RESOURCE_LOCK sysrepo)
Tomáš Pecka55f64f82020-12-10 19:03:30 +0100263
264 set_tests_properties(
265 test-sysrepo_two-daemons
266 PROPERTIES
267 FIXTURES_REQUIRED "sysrepo:two-daemons;sysrepo:env:sysrepo-ietf-hardware"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +0100268 RESOURCE_LOCK sysrepo
269 )
Tomáš Pecka83b62e12020-12-16 14:50:49 +0100270 # ensure we run two-daemons merge test and ietf-hardware test sequentially and don't cleanup before everything ends
271 set_tests_properties(sysrepo_test_merge-start PROPERTIES DEPENDS test-hardware_ietf-hardware)
272 set_tests_properties(sysrepo:clean:sysrepo-ietf-hardware PROPERTIES DEPENDS sysrepo_test_merge-stop)
273
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100274
275 find_program(YANGLINT_PATH yanglint)
276 if (NOT YANGLINT_PATH)
277 message(FATAL_ERROR "Cannot find libyang's yanglint")
278 endif()
279 foreach(yang_file ${YANG_SRCS})
280 add_test(NAME yang-tree:${yang_file} COMMAND ${YANGLINT_PATH} -f tree ${CMAKE_CURRENT_SOURCE_DIR}/${yang_file})
281 endforeach()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100282endif()
Tomáš Peckac927d662020-07-13 11:16:16 +0200283
Tomáš Pecka5123e002020-06-05 15:29:21 +0200284if(WITH_DOCS)
285 set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
286 set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
287 configure_file(${doxyfile_in} ${doxyfile} @ONLY)
288 add_custom_target(doc
289 COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
290 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
291 COMMENT "Generating API documentation with Doxygen"
292 VERBATIM
293 SOURCES ${doxyfile_in}
294 )
Tomáš Peckaa06d74d2020-06-14 18:18:13 +0200295endif()
Tomáš Pecka261c8862020-11-05 11:23:08 +0100296
Tomáš Pecka73a13cc2020-11-17 11:12:19 +0100297set(YANG_DIR ${CMAKE_INSTALL_PREFIX}/share/velia/yang)
298install(FILES ${YANG_SRCS} DESTINATION ${YANG_DIR})
Tomáš Pecka261c8862020-11-05 11:23:08 +0100299install(TARGETS veliad RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/)