blob: 8de23df59bf6de39438a46acd0aacae04573ea1b [file] [log] [blame]
Jan Kundrátd9d26a92018-02-22 12:49:21 +01001project(netconf-cli LANGUAGES CXX)
2cmake_minimum_required(VERSION 3.0)
Jan Kundrát763db1e2018-05-25 15:43:51 +02003set(CMAKE_CXX_STANDARD 17)
Jan Kundrátd9d26a92018-02-22 12:49:21 +01004set(CMAKE_CXX_STANDARD_REQUIRED ON)
Jan Kundrátd47f6552018-03-02 13:40:11 +01005
6include(GNUInstallDirs)
7
8# Set a default build type if none was specified. This was shamelessly stolen
9# from VTK's cmake setup because these guys produce both CMake and a project that
10# manipulates this variable, and the web is full of posts where people say that
11# it is apparently evil to just set the build type in a way an earlier version of
12# this patch did. Oh, and the location of this check/update matters, apparently.
13#
14# Yes, this is just plain crazy.
15if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
16 message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
17 set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
18 # Set the possible values of build type for cmake-gui
19 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
20endif()
21
22# -Werror is not a default for sanity reasons (one cannot know what warnings a future compiler
23# might bring along), but it's a default in debug mode. The idea is that developers should care
24# about a warning-free build, and that this is easier than messing with yet another configure option.
25set(CMAKE_CXX_FLAGS_DEBUG "-Werror ${CMAKE_CXX_FLAGS_DEBUG}")
26
27# I don't want to duplicate the compiler's optimizations
28set(CMAKE_CXX_FLAGS "-O2 ${CMAKE_CXX_FLAGS}")
29
30# Build warnings are useful tools (and this project should be warning-free anyway), enable them on all
31# configurations. They are warnings, not errors.
Jan Kundrátfb831902018-05-26 18:58:52 +020032set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Woverloaded-virtual ${CMAKE_CXX_FLAGS}")
33
34if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
35 set(CMAKE_CXX_FLAGS "-Wsuggest-override ${CMAKE_CXX_FLAGS}")
36endif()
Jan Kundrátd47f6552018-03-02 13:40:11 +010037
Jan Kundrátdc2b0722018-03-02 14:13:37 +010038set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
39
40add_custom_target(git-version-cmake-ide
41 cmake/ProjectGitVersion.cmake
42 cmake/ProjectGitVersionRunner.cmake
43 )
44include(cmake/ProjectGitVersion.cmake)
45prepare_git_version(NETCONF_CLI_VERSION "0.0")
46
Jan Kundrát608818b2018-03-02 13:43:53 +010047find_package(Doxygen)
48option(WITH_DOCS "Create and install internal documentation (needs Doxygen)" ${DOXYGEN_FOUND})
49
Jan Kundrátd47f6552018-03-02 13:40:11 +010050find_package(docopt REQUIRED)
Jan Kundrát03caee82018-03-08 14:16:56 +010051find_package(Boost REQUIRED)
Jan Kundrát8d8efe82019-10-18 10:21:36 +020052find_library(REPLXX_LIBRARY NAMES replxx replxx-d REQUIRED)
Václav Kubernáta395d332019-02-13 16:49:20 +010053find_path(REPLXX_PATH replxx.hxx)
54if("${REPLXX_PATH}" STREQUAL REPLXX_PATH-NOTFOUND)
55 message(FATAL_ERROR "Cannot find the \"replxx.hxx\" include file for the replxx library.")
56endif()
Jan Kundrát608818b2018-03-02 13:43:53 +010057
Jan Kundráte05448a2018-06-13 15:54:33 +020058find_package(PkgConfig)
59pkg_check_modules(LIBYANG REQUIRED libyang-cpp>=0.15.111)
Jan Kundrátbd178362019-02-05 19:00:04 +010060# TODO: bump to 0.7.8 once it is tagged
61pkg_check_modules(SYSREPO REQUIRED libSysrepo-cpp>=0.7.7)
Václav Kubernátc31bd602019-03-07 11:44:48 +010062pkg_check_modules(LIBNETCONF2 REQUIRED libnetconf2>=0.12.47)
Jan Kundráte05448a2018-06-13 15:54:33 +020063
Václav Kubernátb96eef72018-05-04 19:10:22 +020064# we don't need filename tracking, and we prefer to use header-only Boost
65add_definitions(-DBOOST_SPIRIT_X3_NO_FILESYSTEM)
66
Jan Kundrát578347c2018-08-09 15:02:06 +020067add_library(schemas STATIC
Václav Kubernátbddbb172018-06-13 16:27:39 +020068 src/static_schema.cpp
Václav Kubernát48fc3832018-05-28 14:21:22 +020069 src/schema.cpp
Jan Kundrát578347c2018-08-09 15:02:06 +020070 )
71target_link_libraries(schemas PUBLIC Boost::boost)
72
Václav Kubernáte7a9fa32018-08-22 13:56:35 +020073add_library(datastoreaccess STATIC
74 src/datastore_access.cpp
75 )
76target_link_libraries(datastoreaccess PUBLIC Boost::boost)
77
Václav Kubernát80aacc02018-08-22 17:41:54 +020078add_library(sysrepoaccess STATIC
79 src/sysrepo_access.cpp
80 )
Václav Kubernátc31bd602019-03-07 11:44:48 +010081
Václav Kubernát80aacc02018-08-22 17:41:54 +020082target_link_libraries(sysrepoaccess datastoreaccess ${SYSREPO_LIBRARIES})
83link_directories(${SYSREPO_LIBRARY_DIRS})
84target_include_directories(sysrepoaccess SYSTEM PRIVATE ${SYSREPO_INCLUDE_DIRS})
85
Václav Kubernátc31bd602019-03-07 11:44:48 +010086add_library(netconfaccess STATIC
87 src/netconf-client.cpp
88 src/netconf_access.cpp
89 )
90
91target_link_libraries(netconfaccess datastoreaccess ${LIBNETCONF2_LIBRARIES})
92link_directories(${LIBNETCONF2_LIBRARY_DIRS})
93target_include_directories(netconfaccess SYSTEM PRIVATE ${LIBNETCONF2_INCLUDE_DIRS})
94
Václav Kubernát0d4db442018-07-18 17:18:43 +020095add_library(yangschema STATIC
96 src/yang_schema.cpp
97 )
98target_link_libraries(yangschema ${LIBYANG_LIBRARIES})
99# Ensure that this doesn't override Boost's -isystem -- see the log for details.
100target_include_directories(yangschema SYSTEM PRIVATE ${LIBYANG_INCLUDEDIR})
101link_directories(${LIBYANG_LIBRARY_DIRS})
102
Jan Kundrát578347c2018-08-09 15:02:06 +0200103add_library(parser STATIC
Václav Kubernát48fc3832018-05-28 14:21:22 +0200104 src/parser.cpp
Václav Kubernát24df80e2018-06-06 15:18:03 +0200105 src/ast_commands.cpp
106 src/ast_path.cpp
Václav Kubernát94938b72018-05-04 15:12:24 +0200107 src/utils.cpp
Václav Kubernát195eeea2018-05-18 13:52:36 +0200108 src/parser_context.cpp
Václav Kubernát96344a12018-05-28 16:33:39 +0200109 src/interpreter.cpp
Václav Kubernát329c6c32019-02-06 16:41:53 +0100110 src/ast_handlers.cpp
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100111 )
Jan Kundrát578347c2018-08-09 15:02:06 +0200112target_link_libraries(parser schemas)
Václav Kubernátd6662962018-03-22 17:41:33 +0100113
Václav Kubernát73109382018-09-14 19:52:03 +0200114
115add_library(sysreposubscription STATIC
116 tests/mock/sysrepo_subscription.cpp
117 )
118
119target_link_libraries(sysreposubscription ${SYSREPO_LIBRARIES})
120link_directories(${SYSREPO_LIBRARY_DIRS})
121target_include_directories(sysreposubscription SYSTEM PRIVATE ${SYSREPO_INCLUDE_DIRS})
122
Jan Kundrátcc4c1f22018-08-09 15:00:03 +0200123add_executable(netconf-cli
Václav Kubernátd6662962018-03-22 17:41:33 +0100124 src/main.cpp
125 )
Václav Kubernáta395d332019-02-13 16:49:20 +0100126target_include_directories(netconf-cli PRIVATE ${REPLXX_PATH})
Václav Kubernátc31bd602019-03-07 11:44:48 +0100127target_link_libraries(netconf-cli netconfaccess sysrepoaccess yangschema docopt parser ${REPLXX_LIBRARY})
Václav Kubernát2b684612018-08-09 18:55:24 +0200128if(CMAKE_CXX_FLAGS MATCHES "-stdlib=libc\\+\\+")
129 target_link_libraries(netconf-cli c++experimental)
130else()
131 target_link_libraries(netconf-cli stdc++fs)
132endif()
133
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100134add_dependencies(netconf-cli target-NETCONF_CLI_VERSION)
135target_include_directories(netconf-cli PRIVATE ${PROJECT_BINARY_DIR})
136
Jan Kundrát5c805212018-03-02 13:57:12 +0100137include(CTest)
138if(BUILD_TESTING)
139 enable_testing()
Jan Kundrátc381e632019-03-14 13:39:11 +0100140 find_package(trompeloeil 33 REQUIRED)
Jan Kundráta33cf082019-03-28 11:55:57 +0100141 find_package(doctest 2.3.1 REQUIRED)
Jan Kundrát5c805212018-03-02 13:57:12 +0100142
Jan Kundráta33cf082019-03-28 11:55:57 +0100143 add_library(DoctestIntegration STATIC
144 tests/doctest_integration.cpp
145 tests/trompeloeil_doctest.h
Jan Kundrátc381e632019-03-14 13:39:11 +0100146 tests/wait-a-bit-longer.cpp
Jan Kundrát5c805212018-03-02 13:57:12 +0100147 )
Jan Kundráta33cf082019-03-28 11:55:57 +0100148 target_include_directories(DoctestIntegration PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tests/ ${CMAKE_CURRENT_SOURCE_DIR}/src/)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100149 target_link_libraries(DoctestIntegration doctest::doctest trompeloeil)
Jan Kundráta33cf082019-03-28 11:55:57 +0100150 target_compile_definitions(DoctestIntegration PUBLIC DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
Jan Kundrát5c805212018-03-02 13:57:12 +0100151
Václav Kubernát73109382018-09-14 19:52:03 +0200152 if (NOT SYSREPOCTL_EXECUTABLE)
153 find_program(SYSREPOCTL_EXECUTABLE sysrepoctl)
154 endif()
155 if (NOT SYSREPOCTL_EXECUTABLE)
156 message(FATAL_ERROR "Unable to find sysrepoctl, set SYSREPOCTL_EXECUTABLE manually.")
157 endif()
158
Václav Kubernát88324812019-06-26 17:36:29 +0200159 if (NOT SYSREPOCFG_EXECUTABLE)
160 find_program(SYSREPOCFG_EXECUTABLE sysrepocfg)
161 endif()
162 if (NOT SYSREPOCFG_EXECUTABLE)
163 message(FATAL_ERROR "Unable to find sysrepocfg, set SYSREPOCFG_EXECUTABLE manually.")
164 endif()
Václav Kubernát73109382018-09-14 19:52:03 +0200165
Václav Kubernátc31bd602019-03-07 11:44:48 +0100166 if (NOT SYSREPOD_EXECUTABLE)
167 find_program(SYSREPOD_EXECUTABLE sysrepod)
168 endif()
169 if (NOT SYSREPOD_EXECUTABLE)
170 message(FATAL_ERROR "Unable to find sysrepod, set SYSREPOD_EXECUTABLE manually.")
171 endif()
172
173 if (NOT SYSREPO_PLUGIND_EXECUTABLE)
174 find_program(SYSREPO_PLUGIND_EXECUTABLE sysrepo-plugind)
175 endif()
176 if (NOT SYSREPO_PLUGIND_EXECUTABLE)
177 message(FATAL_ERROR "Unable to find sysrepo-plugind, set SYSREPO_EXECUTABLE manually.")
178 endif()
179
180 if (NOT NETOPEER2_EXECUTABLE)
181 find_program(NETOPEER2_EXECUTABLE netopeer2-server)
182 endif()
183 if (NOT NETOPEER2_EXECUTABLE)
184 message(FATAL_ERROR "Unable to find netopeer2-server, set NETOPEER2_EXECUTABLE manually.")
185 endif()
186
Jan Kundráte3236272019-09-27 09:45:26 +0200187 if (NOT FAKEROOT_EXECUTABLE)
188 find_program(FAKEROOT_EXECUTABLE fakeroot)
189 endif()
190 if (NOT FAKEROOT_EXECUTABLE)
191 message(FATAL_ERROR "Unable to find fakeroot, set FAKEROOT_EXECUTABLE manually.")
192 endif()
193
Václav Kubernátc31bd602019-03-07 11:44:48 +0100194 set(NETOPEER_SOCKET_PATH "${CMAKE_CURRENT_BINARY_DIR}/netopeer2-server.sock")
195 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/start_daemons.sh.in ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh @ONLY)
196 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/netopeer_vars.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/netopeer_vars.hpp @ONLY)
197
Václav Kubernát88324812019-06-26 17:36:29 +0200198 function(cli_test name)
199 if (${ARGC} GREATER 1) # this is how CMake does optional arguments
200 add_executable(test_${name}
201 tests/${ARGV1}
202 )
203 else()
204 add_executable(test_${name}
205 tests/${name}.cpp
206 )
Jan Kundrát5c805212018-03-02 13:57:12 +0100207 endif()
Václav Kubernát88324812019-06-26 17:36:29 +0200208 target_link_libraries(test_${name} DoctestIntegration parser)
209 if(NOT CMAKE_CROSSCOMPILING)
210 add_test(test_${name} test_${name})
211 endif()
212 target_include_directories(test_${name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Jan Kundrátcc4c1f22018-08-09 15:00:03 +0200213 endfunction()
Václav Kubernát88324812019-06-26 17:36:29 +0200214
215 function(datastore_test name fname model_path)
216 cli_test(${name} ${fname})
217 target_link_libraries(test_${name} sysreposubscription)
218 add_test(NAME test_${name}_init COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} install ${model_path})
219 add_test(NAME test_${name}_cleanup COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} uninstall ${model_path})
220 set_tests_properties(test_${name}_init PROPERTIES FIXTURES_SETUP ${name}-setup)
221 set_tests_properties(test_${name} PROPERTIES FIXTURES_REQUIRED ${name}-setup RESOURCE_LOCK sysrepo)
222 set_tests_properties(test_${name}_cleanup PROPERTIES FIXTURES_CLEANUP ${name}-setup)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100223 target_compile_definitions(test_${name} PRIVATE ${name}_BACKEND)
Václav Kubernát88324812019-06-26 17:36:29 +0200224 endfunction()
225
Václav Kubernátd6662962018-03-22 17:41:33 +0100226 cli_test(cd)
Václav Kubernát11afac72018-07-18 14:59:53 +0200227 cli_test(ls)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200228 cli_test(presence_containers)
Václav Kubernát07204242018-06-04 18:12:09 +0200229 cli_test(leaf_editing)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200230 cli_test(yang)
231 target_link_libraries(test_yang yangschema)
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100232 cli_test(utils)
233 cli_test(path_completion)
Václav Kubernát57272422019-02-08 12:48:24 +0100234 cli_test(command_completion)
Václav Kubernát989b5de2019-02-20 16:28:35 +0100235 cli_test(enum_completion)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100236 cli_test(list_manipulation)
Václav Kubernát1446fe12019-10-02 19:32:51 +0200237 cli_test(parser_methods)
Václav Kubernát88324812019-06-26 17:36:29 +0200238 datastore_test(sysrepo sysrepo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang)
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200239 target_link_libraries(test_sysrepo sysrepoaccess yangschema parser)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100240 datastore_test(netconf sysrepo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang)
Jan Kundráte3236272019-09-27 09:45:26 +0200241 add_test(NAME start_daemons COMMAND ${FAKEROOT_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100242 add_test(NAME setup_netopeer COMMAND ${SYSREPOCFG_EXECUTABLE} ietf-netconf-server -i ${CMAKE_CURRENT_SOURCE_DIR}/netopeer-test-config --datastore=startup --format=xml)
243 add_test(NAME kill_daemons COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/kill_daemons.sh)
Václav Kubernátaf1e1252019-10-09 18:12:44 +0200244 set_tests_properties(test_sysrepo_init PROPERTIES DEPENDS start_daemons)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100245 set_tests_properties(start_daemons PROPERTIES DEPENDS setup_netopeer)
Václav Kubernátaf1e1252019-10-09 18:12:44 +0200246 set_tests_properties(test_netconf_init PROPERTIES DEPENDS test_sysrepo_cleanup)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100247 set_tests_properties(test_netconf_cleanup PROPERTIES DEPENDS kill_daemons)
248 set_tests_properties(kill_daemons PROPERTIES DEPENDS test_netconf)
249
Václav Kubernát88324812019-06-26 17:36:29 +0200250 target_include_directories(test_sysrepo PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100251 target_link_libraries(test_netconf sysreposubscription netconfaccess yangschema parser)
252 target_include_directories(test_netconf PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
Jan Kundrát5c805212018-03-02 13:57:12 +0100253endif()
254
Jan Kundrát608818b2018-03-02 13:43:53 +0100255if(WITH_DOCS)
256 set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
257 set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
258 configure_file(${doxyfile_in} ${doxyfile} @ONLY)
259 add_custom_target(doc
260 COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
261 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
262 COMMENT "Generating API documentation with Doxygen"
263 VERBATIM
264 SOURCES ${doxyfile_in}
265 )
266endif()
Jan Kundrát10f49902018-05-22 17:01:45 +0200267
268install(TARGETS
269 netconf-cli
270 RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/)