blob: 438be2574740b354d3469501184a7af96991d61f [file] [log] [blame]
Radek Krejci183f7e72015-09-01 17:25:47 +02001cmake_minimum_required(VERSION 2.8)
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)
Radek Krejci183f7e72015-09-01 17:25:47 +02007
8# check the supported platform
9if(NOT UNIX)
10 message(FATAL_ERROR "Only *nix like systems are supported.")
11endif()
12
Radek Krejci183f7e72015-09-01 17:25:47 +020013# set default build type if not specified by user
14if(NOT CMAKE_BUILD_TYPE)
15 set(CMAKE_BUILD_TYPE debug)
16endif()
17
18set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden")
19set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
20set(CMAKE_C_FLAGS_DEBUG "-g -O0")
21
Radek Krejci4f2d40d2015-10-08 12:55:01 +020022# set version
23set(LIBNETCONF2_MAJOR_VERSION 0)
Radek Krejci5bb46622016-01-19 14:34:00 +010024set(LIBNETCONF2_MINOR_VERSION 1)
Michal Vasko0a8f9d92016-01-27 16:08:26 +010025set(LIBNETCONF2_MICRO_VERSION 6)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020026set(LIBNETCONF2_VERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION}.${LIBNETCONF2_MICRO_VERSION})
27set(LIBNETCONF2_SOVERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION})
28
Radek Krejciac6d3472015-10-22 15:47:18 +020029# build options
Michal Vaskofb2fb762015-10-27 11:44:32 +010030option(ENABLE_SSH "Enable NETCONF over SSH support (via libssh)" ON)
Radek Krejciac6d3472015-10-22 15:47:18 +020031option(ENABLE_TLS "Enable NETCONF over TLS support (via OpenSSL)" OFF)
32
Radek Krejci4f2d40d2015-10-08 12:55:01 +020033# source files
Radek Krejci183f7e72015-09-01 17:25:47 +020034set(libsrc
Radek Krejci206fcd62015-10-07 15:42:48 +020035 src/io.c
Radek Krejcid0d19522015-09-02 13:49:25 +020036 src/log.c
Michal Vasko7bcb48e2016-01-15 10:28:54 +010037 src/messages_client.c
38 src/messages_server.c
Radek Krejci206fcd62015-10-07 15:42:48 +020039 src/session.c
Michal Vasko086311b2016-01-08 09:53:11 +010040 src/session_client.c
41 src/session_server.c
Radek Krejcid0d19522015-09-02 13:49:25 +020042 src/time.c)
Radek Krejci183f7e72015-09-01 17:25:47 +020043
Michal Vaskofb2fb762015-10-27 11:44:32 +010044if(ENABLE_SSH)
Radek Krejciac6d3472015-10-22 15:47:18 +020045 set(libsrc ${libsrc}
Michal Vasko086311b2016-01-08 09:53:11 +010046 src/session_client_ssh.c
47 src/session_server_ssh.c)
48 set(SSH_MACRO "#ifndef ENABLE_SSH\n#define ENABLE_SSH\n#endif")
Radek Krejciac6d3472015-10-22 15:47:18 +020049endif()
50
Radek Krejci9f03b482015-10-22 16:02:10 +020051if(ENABLE_TLS)
52 set(libsrc ${libsrc}
Michal Vasko086311b2016-01-08 09:53:11 +010053 src/session_client_tls.c
54 src/session_server_tls.c)
55 set(TLS_MACRO "#ifndef ENABLE_TLS\n#define ENABLE_TLS\n#endif")
Radek Krejci9f03b482015-10-22 16:02:10 +020056endif()
57
Radek Krejci183f7e72015-09-01 17:25:47 +020058set(headers
Radek Krejci695d4fa2015-10-22 13:23:54 +020059 src/log.h
60 src/netconf.h
61 src/session.h
Michal Vasko7bcb48e2016-01-15 10:28:54 +010062 src/messages_client.h
Michal Vasko495c9462016-01-15 11:27:43 +010063 src/messages_server.h
64 src/session_client.h
65 src/session_server.h)
Radek Krejci183f7e72015-09-01 17:25:47 +020066
Radek Krejci4f2d40d2015-10-08 12:55:01 +020067# libnetconf2 target
Radek Krejci183f7e72015-09-01 17:25:47 +020068add_library(netconf2 SHARED ${libsrc})
69set_target_properties(netconf2 PROPERTIES VERSION ${LIBNETCONF2_VERSION} SOVERSION ${LIBNETCONF2_SOVERSION})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020070
Radek Krejcice24ab82015-10-08 15:37:02 +020071if(CMAKE_BUILD_TYPE STREQUAL debug)
72 option(ENABLE_BUILD_TESTS "Build tests" ON)
73 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
74else()
75 option(ENABLE_BUILD_TESTS "Build tests" OFF)
76 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
77endif()
78
Radek Krejci4f2d40d2015-10-08 12:55:01 +020079# dependencies - libssh
Michal Vaskofb2fb762015-10-27 11:44:32 +010080if(ENABLE_SSH)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020081 find_package(LibSSH 0.6.4 REQUIRED)
Michal Vaskofb2fb762015-10-27 11:44:32 +010082 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_SSH ${LIBSSH_DEFINITIONS}")
Michal Vasko086311b2016-01-08 09:53:11 +010083 target_link_libraries(netconf2 ${LIBSSH_LIBRARIES} -lssh_threads -lcrypt)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020084 include_directories(${LIBSSH_INCLUDE_DIRS})
85endif()
86
87# dependencies - openssl
88if(ENABLE_TLS)
89 find_package(OpenSSL REQUIRED)
90 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_TLS")
91 target_link_libraries(netconf2 ${OPENSSL_LIBRARIES})
92 include_directories(${OPENSSL_INCLUDE_DIR})
93endif()
94
95# dependencies - libyang
96find_package(LibYANG REQUIRED)
97target_link_libraries(netconf2 ${LIBYANG_LIBRARIES})
98include_directories(${LIBYANG_INCLUDE_DIRS})
99
100if(NOT LIB_INSTALL_DIR)
Radek Krejcib574e032015-10-21 14:19:12 +0200101 set(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200102endif()
103
104if(NOT INCLUDE_INSTALL_DIR)
Radek Krejcib574e032015-10-21 14:19:12 +0200105 set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200106endif()
107set(INCLUDE_INSTALL_SUBDIR ${INCLUDE_INSTALL_DIR}/libnetconf2)
Radek Krejci183f7e72015-09-01 17:25:47 +0200108
Radek Krejci14b14032015-10-21 15:19:43 +0200109if(NOT DATA_INSTALL_DIR)
110 set(DATA_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/share/libnetconf2)
111endif()
112
Radek Krejci183f7e72015-09-01 17:25:47 +0200113# install library
114install(TARGETS netconf2 DESTINATION ${LIB_INSTALL_DIR})
115
116# install headers
Radek Krejci5fc4c392016-01-08 14:23:22 +0100117install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_client.h DESTINATION ${INCLUDE_INSTALL_DIR})
118install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_server.h DESTINATION ${INCLUDE_INSTALL_DIR})
119#install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nc_transapi.h DESTINATION ${INCLUDE_INSTALL_DIR})
Radek Krejci183f7e72015-09-01 17:25:47 +0200120install(FILES ${headers} DESTINATION ${INCLUDE_INSTALL_SUBDIR})
121
Radek Krejci14b14032015-10-21 15:19:43 +0200122# install schemas
123install(
124 CODE "file(GLOB yin_schemas \"${CMAKE_SOURCE_DIR}/schemas/*.yin\")"
125 CODE "file(INSTALL \${yin_schemas} DESTINATION ${DATA_INSTALL_DIR})"
126)
127
Radek Krejcice24ab82015-10-08 15:37:02 +0200128if(ENABLE_VALGRIND_TESTS)
129 set(ENABLE_BUILD_TESTS ON)
130endif()
131
132if(ENABLE_BUILD_TESTS)
133 enable_testing()
134 add_subdirectory(tests)
135endif()
136
Radek Krejci68824962016-01-19 14:03:09 +0100137configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_SOURCE_DIR}/src/config.h" ESCAPE_QUOTES @ONLY)
Michal Vasko1128e112015-10-26 15:34:24 +0100138configure_file(nc_client.h.in nc_client.h)
Michal Vasko086311b2016-01-08 09:53:11 +0100139configure_file(nc_server.h.in nc_server.h)
Radek Krejci14b14032015-10-21 15:19:43 +0200140
Radek Krejci183f7e72015-09-01 17:25:47 +0200141# clean cmake cache
142add_custom_target(cleancache
143 COMMAND make clean
144 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} +
145 COMMAND rm -rf Makefile Doxyfile
146 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})