blob: 4caf487031b060d03bcc0f805aadbdc43422706e [file] [log] [blame]
Michal Vasko8e08c962016-02-09 14:09:11 +01001cmake_minimum_required(VERSION 2.6)
Radek Krejci183f7e72015-09-01 17:25:47 +02002
Radek Krejci4f2d40d2015-10-08 12:55:01 +02003# include custom Modules
4set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
5
6project(libnetconf2 C)
Michal Vasko6779d182016-02-17 14:36:36 +01007set(LIBNETCONF2_DESCRIPTION "NETCONF server and client library in C.")
Radek Krejci183f7e72015-09-01 17:25:47 +02008
9# check the supported platform
10if(NOT UNIX)
Michal Vasko3d865d22016-01-28 16:00:53 +010011 message(FATAL_ERROR "Only *nix like systems are supported.")
Radek Krejci183f7e72015-09-01 17:25:47 +020012endif()
13
Michal Vasko6779d182016-02-17 14:36:36 +010014if(NOT LIB_INSTALL_DIR)
15 set(LIB_INSTALL_DIR lib)
16endif()
17
18if(NOT INCLUDE_INSTALL_DIR)
19 set(INCLUDE_INSTALL_DIR include)
20endif()
21
Radek Krejci183f7e72015-09-01 17:25:47 +020022# set default build type if not specified by user
23if(NOT CMAKE_BUILD_TYPE)
Michal Vasko3d865d22016-01-28 16:00:53 +010024 set(CMAKE_BUILD_TYPE debug)
Radek Krejci183f7e72015-09-01 17:25:47 +020025endif()
26
27set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden")
28set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
29set(CMAKE_C_FLAGS_DEBUG "-g -O0")
30
Radek Krejci4f2d40d2015-10-08 12:55:01 +020031# set version
32set(LIBNETCONF2_MAJOR_VERSION 0)
Michal Vaskoe6b12cf2016-02-09 14:50:29 +010033set(LIBNETCONF2_MINOR_VERSION 2)
Michal Vaskoc5c52132016-02-18 13:29:56 +010034set(LIBNETCONF2_MICRO_VERSION 3)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020035set(LIBNETCONF2_VERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION}.${LIBNETCONF2_MICRO_VERSION})
36set(LIBNETCONF2_SOVERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION})
37
Radek Krejciac6d3472015-10-22 15:47:18 +020038# build options
Michal Vaskofb2fb762015-10-27 11:44:32 +010039option(ENABLE_SSH "Enable NETCONF over SSH support (via libssh)" ON)
Radek Krejciac6d3472015-10-22 15:47:18 +020040option(ENABLE_TLS "Enable NETCONF over TLS support (via OpenSSL)" OFF)
Michal Vasko3d865d22016-01-28 16:00:53 +010041option(ENABLE_DNSSEC "Enable support for SSHFP retrieval using DNSSEC for SSH (requires OpenSSL and libval)" OFF)
42
43if(ENABLE_DNSSEC AND NOT ENABLE_SSH)
44 message(WARNING "DNSSEC SSHFP retrieval cannot be used without SSH support.")
45 set(ENABLE_DNSSEC OFF)
46endif()
Radek Krejciac6d3472015-10-22 15:47:18 +020047
Radek Krejci4f2d40d2015-10-08 12:55:01 +020048# source files
Radek Krejci183f7e72015-09-01 17:25:47 +020049set(libsrc
Michal Vasko3d865d22016-01-28 16:00:53 +010050 src/io.c
51 src/log.c
52 src/messages_client.c
53 src/messages_server.c
54 src/session.c
55 src/session_client.c
56 src/session_server.c
57 src/time.c)
Radek Krejci183f7e72015-09-01 17:25:47 +020058
Michal Vaskofb2fb762015-10-27 11:44:32 +010059if(ENABLE_SSH)
Michal Vasko3d865d22016-01-28 16:00:53 +010060 set(libsrc ${libsrc}
61 src/session_client_ssh.c
62 src/session_server_ssh.c)
Radek Krejci53691be2016-02-22 13:58:37 +010063 set(SSH_MACRO "#ifndef NC_ENABLED_SSH\n#define NC_ENABLED_SSH\n#endif")
Radek Krejciac6d3472015-10-22 15:47:18 +020064endif()
65
Radek Krejci9f03b482015-10-22 16:02:10 +020066if(ENABLE_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +010067 set(libsrc ${libsrc}
68 src/session_client_tls.c
69 src/session_server_tls.c)
Radek Krejci53691be2016-02-22 13:58:37 +010070 set(TLS_MACRO "#ifndef NC_ENABLED_TLS\n#define NC_ENABLED_TLS\n#endif")
Radek Krejci9f03b482015-10-22 16:02:10 +020071endif()
72
Radek Krejci183f7e72015-09-01 17:25:47 +020073set(headers
Michal Vasko3d865d22016-01-28 16:00:53 +010074 src/log.h
75 src/netconf.h
76 src/session.h
77 src/messages_client.h
78 src/messages_server.h
79 src/session_client.h
Michal Vasko45f298f2016-01-29 10:26:26 +010080 src/session_client_ch.h
81 src/session_server.h
82 src/session_server_ch.h)
Radek Krejci183f7e72015-09-01 17:25:47 +020083
Radek Krejci4f2d40d2015-10-08 12:55:01 +020084# libnetconf2 target
Radek Krejci183f7e72015-09-01 17:25:47 +020085add_library(netconf2 SHARED ${libsrc})
86set_target_properties(netconf2 PROPERTIES VERSION ${LIBNETCONF2_VERSION} SOVERSION ${LIBNETCONF2_SOVERSION})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020087
Radek Krejcice24ab82015-10-08 15:37:02 +020088if(CMAKE_BUILD_TYPE STREQUAL debug)
Michal Vasko3d865d22016-01-28 16:00:53 +010089 option(ENABLE_BUILD_TESTS "Build tests" ON)
90 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
Radek Krejcice24ab82015-10-08 15:37:02 +020091else()
Michal Vasko3d865d22016-01-28 16:00:53 +010092 option(ENABLE_BUILD_TESTS "Build tests" OFF)
93 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
Radek Krejcice24ab82015-10-08 15:37:02 +020094endif()
95
Michal Vasko73ed9672016-01-29 13:15:15 +010096#dependencies - pthread
97set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
98find_package(Threads REQUIRED)
99target_link_libraries(netconf2 ${CMAKE_THREAD_LIBS_INIT})
100
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200101# dependencies - libssh
Michal Vaskofb2fb762015-10-27 11:44:32 +0100102if(ENABLE_SSH)
Michal Vasko3d865d22016-01-28 16:00:53 +0100103 find_package(LibSSH 0.6.4 REQUIRED)
Radek Krejci53691be2016-02-22 13:58:37 +0100104 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_SSH ${LIBSSH_DEFINITIONS}")
Michal Vasko3d865d22016-01-28 16:00:53 +0100105 target_link_libraries(netconf2 ${LIBSSH_LIBRARIES} -lssh_threads -lcrypt)
106 include_directories(${LIBSSH_INCLUDE_DIRS})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200107endif()
108
109# dependencies - openssl
Michal Vasko3d865d22016-01-28 16:00:53 +0100110if(ENABLE_TLS OR ENABLE_DNSSEC)
111 find_package(OpenSSL REQUIRED)
112 if (ENABLE_TLS)
Radek Krejci53691be2016-02-22 13:58:37 +0100113 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_TLS")
Michal Vasko3d865d22016-01-28 16:00:53 +0100114 endif()
115 target_link_libraries(netconf2 ${OPENSSL_LIBRARIES})
116 include_directories(${OPENSSL_INCLUDE_DIR})
117endif()
118
Michal Vasko73ed9672016-01-29 13:15:15 +0100119# dependencies - libval
Michal Vasko3d865d22016-01-28 16:00:53 +0100120if (ENABLE_DNSSEC)
121 find_package(LibVAL REQUIRED)
122 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_DNSSEC")
123 target_link_libraries(netconf2 ${LIBVAL_LIBRARIES})
124 include_directories(${LIBVAL_INCLUDE_DIRS})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200125endif()
126
127# dependencies - libyang
128find_package(LibYANG REQUIRED)
129target_link_libraries(netconf2 ${LIBYANG_LIBRARIES})
130include_directories(${LIBYANG_INCLUDE_DIRS})
131
Michal Vasko51cda3c2016-01-29 14:41:26 +0100132# generate doxygen documentation for libnetconf2 API
133find_package(Doxygen)
134if(DOXYGEN_FOUND)
135 set(DOXYGEN_SKIP_DOT TRUE)
136 add_custom_target(doc
137 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
138 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
139 configure_file(Doxyfile.in Doxyfile)
140endif()
141
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200142if(NOT LIB_INSTALL_DIR)
Michal Vasko3d865d22016-01-28 16:00:53 +0100143 set(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200144endif()
145
146if(NOT INCLUDE_INSTALL_DIR)
Michal Vasko3d865d22016-01-28 16:00:53 +0100147 set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200148endif()
149set(INCLUDE_INSTALL_SUBDIR ${INCLUDE_INSTALL_DIR}/libnetconf2)
Radek Krejci183f7e72015-09-01 17:25:47 +0200150
Radek Krejci14b14032015-10-21 15:19:43 +0200151if(NOT DATA_INSTALL_DIR)
Michal Vasko3d865d22016-01-28 16:00:53 +0100152 set(DATA_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/share/libnetconf2)
Radek Krejci14b14032015-10-21 15:19:43 +0200153endif()
154
Radek Krejci183f7e72015-09-01 17:25:47 +0200155# install library
156install(TARGETS netconf2 DESTINATION ${LIB_INSTALL_DIR})
157
158# install headers
Radek Krejci5fc4c392016-01-08 14:23:22 +0100159install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_client.h DESTINATION ${INCLUDE_INSTALL_DIR})
160install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_server.h DESTINATION ${INCLUDE_INSTALL_DIR})
Radek Krejci183f7e72015-09-01 17:25:47 +0200161install(FILES ${headers} DESTINATION ${INCLUDE_INSTALL_SUBDIR})
162
Radek Krejci14b14032015-10-21 15:19:43 +0200163# install schemas
164install(
Michal Vasko3d865d22016-01-28 16:00:53 +0100165 CODE "file(GLOB yin_schemas \"${CMAKE_SOURCE_DIR}/schemas/*.yin\")"
166 CODE "file(INSTALL \${yin_schemas} DESTINATION ${DATA_INSTALL_DIR})"
Radek Krejci14b14032015-10-21 15:19:43 +0200167)
168
Michal Vasko6779d182016-02-17 14:36:36 +0100169# install pkg-config file
170find_package(PkgConfig)
171if(PKG_CONFIG_FOUND)
172 configure_file("libnetconf2.pc.in" "libnetconf2.pc" @ONLY)
173 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libnetconf2.pc" DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig")
174 # check that pkg-config includes the used path
175 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
176 if(RETURN EQUAL 0)
177 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig" SUBSTR "${PC_PATH}")
178 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
179 if(SUBSTR_LEN EQUAL 0)
180 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_PREFIX}/lib/pkgconfig\".")
181 endif()
182 endif()
183endif()
184
Radek Krejcice24ab82015-10-08 15:37:02 +0200185if(ENABLE_VALGRIND_TESTS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100186 set(ENABLE_BUILD_TESTS ON)
Radek Krejcice24ab82015-10-08 15:37:02 +0200187endif()
188
189if(ENABLE_BUILD_TESTS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100190 enable_testing()
191 add_subdirectory(tests)
Radek Krejcice24ab82015-10-08 15:37:02 +0200192endif()
193
Radek Krejci68824962016-01-19 14:03:09 +0100194configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_SOURCE_DIR}/src/config.h" ESCAPE_QUOTES @ONLY)
Michal Vasko1128e112015-10-26 15:34:24 +0100195configure_file(nc_client.h.in nc_client.h)
Michal Vasko086311b2016-01-08 09:53:11 +0100196configure_file(nc_server.h.in nc_server.h)
Radek Krejci14b14032015-10-21 15:19:43 +0200197
Radek Krejci183f7e72015-09-01 17:25:47 +0200198# clean cmake cache
199add_custom_target(cleancache
200 COMMAND make clean
201 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} +
202 COMMAND rm -rf Makefile Doxyfile
203 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})