blob: bddd8cc5d044100c7fd5789936a0e50d67ec0274 [file] [log] [blame]
Michal Vasko623d59e2021-01-13 14:11:48 +01001cmake_minimum_required(VERSION 2.8.12)
Claus Klein22091912020-01-20 13:45:47 +01002
Michal Vaskodf67eb42020-02-06 16:22:07 +01003project(libnetconf2 C)
Michal Vasko4cb2d722020-04-23 13:49:16 +02004set(LIBNETCONF2_DESC "NETCONF library in C providing API for both clients and servers.")
Claus Klein22091912020-01-20 13:45:47 +01005
Michal Vasko9e8ac262020-04-07 13:06:45 +02006# include custom Modules
7list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
8
Jan Kundrátb78cdfc2016-11-04 17:11:24 +01009include(GNUInstallDirs)
Michal Vasko9aa3c6a2018-11-12 08:50:10 +010010include(CheckFunctionExists)
Michal Vasko32af6142019-01-23 09:30:42 +010011include(CheckCSourceCompiles)
Michal Vasko352c3902018-11-12 09:16:59 +010012include(CheckIncludeFile)
Michal Vasko9e8ac262020-04-07 13:06:45 +020013include(UseCompat)
Claus Klein22091912020-01-20 13:45:47 +010014
Michal Vaskod6123692019-07-16 16:04:16 +020015if(POLICY CMP0075)
16 cmake_policy(SET CMP0075 NEW)
17endif()
Radek Krejci183f7e72015-09-01 17:25:47 +020018
Michal Vasko6779d182016-02-17 14:36:36 +010019set(LIBNETCONF2_DESCRIPTION "NETCONF server and client library in C.")
Radek Krejci183f7e72015-09-01 17:25:47 +020020
21# check the supported platform
22if(NOT UNIX)
Michal Vasko3d865d22016-01-28 16:00:53 +010023 message(FATAL_ERROR "Only *nix like systems are supported.")
Radek Krejci183f7e72015-09-01 17:25:47 +020024endif()
25
Michal Vasko63b52fc2018-02-16 10:08:17 +010026# osx specific
27set(CMAKE_MACOSX_RPATH TRUE)
28
Radek Krejci183f7e72015-09-01 17:25:47 +020029# set default build type if not specified by user
30if(NOT CMAKE_BUILD_TYPE)
Claus Klein22091912020-01-20 13:45:47 +010031 set(CMAKE_BUILD_TYPE Debug)
Radek Krejci183f7e72015-09-01 17:25:47 +020032endif()
33
Michal Vasko89fff912020-05-27 08:29:11 +020034add_compile_options(-Wall -Wextra -fvisibility=hidden -std=gnu99)
PavolVican384786e2017-07-12 15:31:20 +020035set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
Radek Krejci183f7e72015-09-01 17:25:47 +020036
Michal Vasko08d5cde2019-07-10 14:34:55 +020037# Version of the project
38# Generic version of not only the library. Major version is reserved for really big changes of the project,
39# minor version changes with added functionality (new tool, functionality of the tool or library, ...) and
40# micro version is changed with a set of small changes or bugfixes anywhere in the project.
41set(LIBNETCONF2_MAJOR_VERSION 1)
Michal Vasko8afb2ed2019-11-05 12:07:34 +010042set(LIBNETCONF2_MINOR_VERSION 1)
Michal Vasko6efbb022021-04-14 08:24:28 +020043set(LIBNETCONF2_MICRO_VERSION 45)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020044set(LIBNETCONF2_VERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION}.${LIBNETCONF2_MICRO_VERSION})
Michal Vasko08d5cde2019-07-10 14:34:55 +020045
46# Version of the library
47# Major version is changed with every backward non-compatible API/ABI change in libyang, minor version changes
48# with backward compatible change and micro version is connected with any internal change of the library.
49set(LIBNETCONF2_MAJOR_SOVERSION 1)
Michal Vaskoee9d7402020-04-16 09:03:59 +020050set(LIBNETCONF2_MINOR_SOVERSION 3)
Michal Vasko9bdaf7e2021-04-14 08:24:17 +020051set(LIBNETCONF2_MICRO_SOVERSION 24)
Michal Vasko08d5cde2019-07-10 14:34:55 +020052set(LIBNETCONF2_SOVERSION_FULL ${LIBNETCONF2_MAJOR_SOVERSION}.${LIBNETCONF2_MINOR_SOVERSION}.${LIBNETCONF2_MICRO_SOVERSION})
53set(LIBNETCONF2_SOVERSION ${LIBNETCONF2_MAJOR_SOVERSION})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020054
Michal Vasko819598b2020-07-29 10:05:11 +020055# libyang SO version required
Michal Vasko429a7472021-04-15 10:00:28 +020056set(LIBYANG_DEP_SOVERSION_MAJOR 2)
Michal Vasko819598b2020-07-29 10:05:11 +020057
Radek Krejciac6d3472015-10-22 15:47:18 +020058# build options
Michal Vaskofb2fb762015-10-27 11:44:32 +010059option(ENABLE_SSH "Enable NETCONF over SSH support (via libssh)" ON)
Michal Vasko061dc802016-02-29 10:15:28 +010060option(ENABLE_TLS "Enable NETCONF over TLS support (via OpenSSL)" ON)
Michal Vasko3d865d22016-01-28 16:00:53 +010061option(ENABLE_DNSSEC "Enable support for SSHFP retrieval using DNSSEC for SSH (requires OpenSSL and libval)" OFF)
Michal Vaskof471fa02017-02-15 10:48:12 +010062set(READ_INACTIVE_TIMEOUT 20 CACHE STRING "Maximum number of seconds waiting for new data once some data have arrived")
63set(READ_ACTIVE_TIMEOUT 300 CACHE STRING "Maximum number of seconds for receiving a full message")
64set(MAX_PSPOLL_THREAD_COUNT 6 CACHE STRING "Maximum number of threads that could simultaneously access a ps_poll structure")
Michal Vasko5a9bd9c2019-08-16 15:50:09 +020065set(TIMEOUT_STEP 100 CACHE STRING "Number of microseconds tasks are repeated until timeout elapses")
Michal Vasko5a446472020-05-06 12:43:42 +020066set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/share/yang/modules" CACHE STRING "Directory with common YANG modules")
Michal Vasko3d865d22016-01-28 16:00:53 +010067
68if(ENABLE_DNSSEC AND NOT ENABLE_SSH)
69 message(WARNING "DNSSEC SSHFP retrieval cannot be used without SSH support.")
70 set(ENABLE_DNSSEC OFF)
71endif()
Radek Krejciac6d3472015-10-22 15:47:18 +020072
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020073include_directories(${PROJECT_BINARY_DIR}/src)
74
Radek Krejci4f2d40d2015-10-08 12:55:01 +020075# source files
Radek Krejci183f7e72015-09-01 17:25:47 +020076set(libsrc
Michal Vasko3d865d22016-01-28 16:00:53 +010077 src/io.c
78 src/log.c
79 src/messages_client.c
80 src/messages_server.c
81 src/session.c
82 src/session_client.c
Michal Vaskod7fb6df2021-05-19 11:27:35 +020083 src/session_server.c)
Radek Krejci183f7e72015-09-01 17:25:47 +020084
Michal Vaskofb2fb762015-10-27 11:44:32 +010085if(ENABLE_SSH)
Claus Klein22091912020-01-20 13:45:47 +010086 list(APPEND libsrc
Michal Vasko3d865d22016-01-28 16:00:53 +010087 src/session_client_ssh.c
88 src/session_server_ssh.c)
Radek Krejci53691be2016-02-22 13:58:37 +010089 set(SSH_MACRO "#ifndef NC_ENABLED_SSH\n#define NC_ENABLED_SSH\n#endif")
Radek Krejciac6d3472015-10-22 15:47:18 +020090endif()
91
Radek Krejci9f03b482015-10-22 16:02:10 +020092if(ENABLE_TLS)
Claus Klein22091912020-01-20 13:45:47 +010093 list(APPEND libsrc
Michal Vasko3d865d22016-01-28 16:00:53 +010094 src/session_client_tls.c
95 src/session_server_tls.c)
Radek Krejci53691be2016-02-22 13:58:37 +010096 set(TLS_MACRO "#ifndef NC_ENABLED_TLS\n#define NC_ENABLED_TLS\n#endif")
Radek Krejci9f03b482015-10-22 16:02:10 +020097endif()
98
Radek Krejci183f7e72015-09-01 17:25:47 +020099set(headers
Claus Klein22091912020-01-20 13:45:47 +0100100 ${PROJECT_BINARY_DIR}/src/config.h
Michal Vasko3d865d22016-01-28 16:00:53 +0100101 src/log.h
102 src/netconf.h
103 src/session.h
104 src/messages_client.h
105 src/messages_server.h
106 src/session_client.h
Michal Vasko45f298f2016-01-29 10:26:26 +0100107 src/session_client_ch.h
108 src/session_server.h
109 src/session_server_ch.h)
Radek Krejci183f7e72015-09-01 17:25:47 +0200110
Michal Vasko9e8ac262020-04-07 13:06:45 +0200111# use compat
112use_compat()
113
Claus Klein22091912020-01-20 13:45:47 +0100114# netconf2 target
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200115add_library(netconf2 SHARED ${libsrc} ${compatsrc})
Michal Vaskod8536552020-04-21 14:41:16 +0200116set_target_properties(netconf2 PROPERTIES VERSION ${LIBNETCONF2_SOVERSION_FULL} SOVERSION ${LIBNETCONF2_SOVERSION})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200117
Claus Klein22091912020-01-20 13:45:47 +0100118if((CMAKE_BUILD_TYPE STREQUAL Debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
Michal Vasko3d865d22016-01-28 16:00:53 +0100119 option(ENABLE_BUILD_TESTS "Build tests" ON)
120 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
Radek Krejcice24ab82015-10-08 15:37:02 +0200121else()
Michal Vasko3d865d22016-01-28 16:00:53 +0100122 option(ENABLE_BUILD_TESTS "Build tests" OFF)
123 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
Radek Krejcice24ab82015-10-08 15:37:02 +0200124endif()
Fred Gan5eda9cb2020-12-11 16:32:08 +0800125option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
126
127if(ENABLE_COVERAGE)
128 find_program(PATH_GCOV NAMES gcov)
129 if(NOT PATH_GCOV)
130 message(WARNING "'gcov' executable not found! Disabling building code coverage report.")
131 set(ENABLE_COVERAGE OFF)
132 endif()
133
134 find_program(PATH_LCOV NAMES lcov)
135 if(NOT PATH_LCOV)
136 message(WARNING "'lcov' executable not found! Disabling building code coverage report.")
137 set(ENABLE_COVERAGE OFF)
138 endif()
139
140 find_program(PATH_GENHTML NAMES genhtml)
141 if(NOT PATH_GENHTML)
142 message(WARNING "'genhtml' executable not found! Disabling building code coverage report.")
143 set(ENABLE_COVERAGE OFF)
144 endif()
145
146 if(NOT CMAKE_COMPILER_IS_GNUCC)
147 message(WARNING "Compiler is not gcc! Coverage may break the tests!")
148 endif()
149
150 if(ENABLE_COVERAGE)
151 set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
152 endif()
153endif()
Radek Krejcice24ab82015-10-08 15:37:02 +0200154
Radek Krejci32898d52016-07-14 17:37:02 +0200155# dependencies - pthread
Michal Vasko73ed9672016-01-29 13:15:15 +0100156set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
157find_package(Threads REQUIRED)
158target_link_libraries(netconf2 ${CMAKE_THREAD_LIBS_INIT})
159
Radek Krejci28472922016-07-15 11:51:16 +0200160# check availability for some pthread functions
Radek Krejci32898d52016-07-14 17:37:02 +0200161set(CMAKE_REQUIRED_LIBRARIES pthread)
Michal Vasko7ae909a2018-11-09 09:51:42 +0100162check_include_file(stdatomic.h HAVE_STDATOMIC)
Rosen Penevef2f3ac2019-07-15 18:15:28 -0700163check_function_exists(pthread_rwlockattr_setkind_np HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP)
Radek Krejci32898d52016-07-14 17:37:02 +0200164
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200165# dependencies - openssl
Radek Krejci5c69cdf2017-05-26 16:29:29 +0200166if(ENABLE_TLS OR ENABLE_DNSSEC OR ENABLE_SSH)
Michal Vasko3d865d22016-01-28 16:00:53 +0100167 find_package(OpenSSL REQUIRED)
Claus Klein22091912020-01-20 13:45:47 +0100168 if(ENABLE_TLS)
169 message(STATUS "OPENSSL found, required for TLS")
Fred Gan5eda9cb2020-12-11 16:32:08 +0800170 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DNC_ENABLED_TLS")
Michal Vasko3d865d22016-01-28 16:00:53 +0100171 endif()
Claus Klein22091912020-01-20 13:45:47 +0100172
Michal Vasko3d865d22016-01-28 16:00:53 +0100173 target_link_libraries(netconf2 ${OPENSSL_LIBRARIES})
174 include_directories(${OPENSSL_INCLUDE_DIR})
175endif()
176
Michal Vasko474d3732017-09-27 13:57:00 +0200177# dependencies - libssh
178if(ENABLE_SSH)
Michal Vasko819598b2020-07-29 10:05:11 +0200179 find_package(LibSSH 0.7.1 REQUIRED)
Michal Vaskoed4a4522020-05-19 16:46:12 +0200180 if(LIBSSH_VERSION VERSION_EQUAL 0.9.3 OR LIBSSH_VERSION VERSION_EQUAL 0.9.4)
181 message(FATAL_ERROR "LibSSH ${LIBSSH_VERSION} includes regression bugs and libnetconf2 will NOT work properly, try to use another version")
Michal Vasko899760c2020-01-03 10:13:11 +0100182 endif()
Claus Klein22091912020-01-20 13:45:47 +0100183
Michal Vaskoed4a4522020-05-19 16:46:12 +0200184 target_link_libraries(netconf2 ${LIBSSH_LIBRARIES})
185 list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBSSH_LIBRARIES})
Michal Vasko474d3732017-09-27 13:57:00 +0200186 include_directories(${LIBSSH_INCLUDE_DIRS})
Fred Gan5eda9cb2020-12-11 16:32:08 +0800187 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DNC_ENABLED_SSH")
Michal Vasko71514b32020-04-07 13:36:51 +0200188
189 # crypt
apropp-molex4e903c32020-04-20 03:06:58 -0400190 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "QNX")
191 target_link_libraries(netconf2 -lcrypt)
192 list(APPEND CMAKE_REQUIRED_LIBRARIES crypt)
193 else()
194 target_link_libraries(netconf2 -llogin)
195 list(APPEND CMAKE_REQUIRED_LIBRARIES login)
196 endif()
Michal Vasko474d3732017-09-27 13:57:00 +0200197endif()
198
Michal Vasko73ed9672016-01-29 13:15:15 +0100199# dependencies - libval
Claus Klein22091912020-01-20 13:45:47 +0100200if(ENABLE_DNSSEC)
Michal Vasko3d865d22016-01-28 16:00:53 +0100201 find_package(LibVAL REQUIRED)
Fred Gan5eda9cb2020-12-11 16:32:08 +0800202 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DENABLE_DNSSEC")
Michal Vasko3d865d22016-01-28 16:00:53 +0100203 target_link_libraries(netconf2 ${LIBVAL_LIBRARIES})
204 include_directories(${LIBVAL_INCLUDE_DIRS})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200205endif()
206
207# dependencies - libyang
Michal Vasko429a7472021-04-15 10:00:28 +0200208find_package(LibYANG ${LIBYANG_DEP_SOVERSION_MAJOR} REQUIRED)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200209target_link_libraries(netconf2 ${LIBYANG_LIBRARIES})
210include_directories(${LIBYANG_INCLUDE_DIRS})
211
apropp-molex4e903c32020-04-20 03:06:58 -0400212# header file compatibility - shadow.h and crypt.h
213check_include_file("shadow.h" HAVE_SHADOW)
214check_include_file("crypt.h" HAVE_CRYPT)
215
216# function compatibility - getpeereid on QNX
217if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
218 target_link_libraries(netconf2 -lsocket)
219 list(APPEND CMAKE_REQUIRED_LIBRARIES socket)
220 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES pthread)
221 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
222 check_symbol_exists(getpeereid "sys/types.h;unistd.h" HAVE_GETPEEREID)
223 list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_QNX_SOURCE)
224endif()
225
Michal Vasko51cda3c2016-01-29 14:41:26 +0100226# generate doxygen documentation for libnetconf2 API
227find_package(Doxygen)
228if(DOXYGEN_FOUND)
229 set(DOXYGEN_SKIP_DOT TRUE)
230 add_custom_target(doc
231 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
232 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
233 configure_file(Doxyfile.in Doxyfile)
234endif()
235
Radek Krejci183f7e72015-09-01 17:25:47 +0200236# install library
Jan Kundrátb78cdfc2016-11-04 17:11:24 +0100237install(TARGETS netconf2 DESTINATION ${CMAKE_INSTALL_LIBDIR})
Radek Krejci183f7e72015-09-01 17:25:47 +0200238
239# install headers
Jan Kundrátb78cdfc2016-11-04 17:11:24 +0100240install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_client.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
241install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_server.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Michal Vasko5a446472020-05-06 12:43:42 +0200242install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnetconf2)
Radek Krejci14b14032015-10-21 15:19:43 +0200243
Michal Vasko6779d182016-02-17 14:36:36 +0100244# install pkg-config file
245find_package(PkgConfig)
246if(PKG_CONFIG_FOUND)
247 configure_file("libnetconf2.pc.in" "libnetconf2.pc" @ONLY)
Jan Kundrátb78cdfc2016-11-04 17:11:24 +0100248 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libnetconf2.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko6779d182016-02-17 14:36:36 +0100249 # check that pkg-config includes the used path
250 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
251 if(RETURN EQUAL 0)
Jan Kundrátb78cdfc2016-11-04 17:11:24 +0100252 string(REGEX MATCH "${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko6779d182016-02-17 14:36:36 +0100253 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
254 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátb78cdfc2016-11-04 17:11:24 +0100255 message(WARNING "pkg-config will not detect the new package after installation, adjust PKG_CONFIG_PATH using \"export PKG_CONFIG_PATH=\${PKG_CONFIG_PATH}:${CMAKE_INSTALL_LIBDIR}/pkgconfig\".")
Michal Vasko6779d182016-02-17 14:36:36 +0100256 endif()
257 endif()
258endif()
259
Radek Krejcice24ab82015-10-08 15:37:02 +0200260if(ENABLE_VALGRIND_TESTS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100261 set(ENABLE_BUILD_TESTS ON)
Radek Krejcice24ab82015-10-08 15:37:02 +0200262endif()
263
264if(ENABLE_BUILD_TESTS)
Radek Krejcieaa06722016-02-26 14:27:32 +0100265 find_package(CMocka 1.0.0)
266 if(CMOCKA_FOUND)
267 enable_testing()
268 add_subdirectory(tests)
Claus Klein22091912020-01-20 13:45:47 +0100269 endif()
Radek Krejcice24ab82015-10-08 15:37:02 +0200270endif()
271
Jan Kundrátcf15d6c2017-10-26 18:07:56 +0200272configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_BINARY_DIR}/src/config.h" ESCAPE_QUOTES @ONLY)
Michal Vasko1128e112015-10-26 15:34:24 +0100273configure_file(nc_client.h.in nc_client.h)
Michal Vasko086311b2016-01-08 09:53:11 +0100274configure_file(nc_server.h.in nc_server.h)
Radek Krejci14b14032015-10-21 15:19:43 +0200275
Radek Krejci183f7e72015-09-01 17:25:47 +0200276# clean cmake cache
277add_custom_target(cleancache
278 COMMAND make clean
279 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} +
280 COMMAND rm -rf Makefile Doxyfile
281 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Bi-Ruei, Chiuf5faf832019-09-21 20:15:59 +0800282
283# uninstall
284add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/uninstall.cmake")