blob: a3f1893f92280dcb1bcd8f6a81da98d7d3a55b0a [file] [log] [blame]
Radek Krejci2c22f122018-09-05 15:08:03 +02001cmake_minimum_required(VERSION 2.8.12)
Radek Krejcif8482c72019-04-04 10:10:47 +02002
3# force out-of-source build
4if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
5 message(FATAL_ERROR "In-source build is not allowed. Please make a standalone build directory and run CMake from there. You may need to remove CMakeCache.txt.")
6endif()
7
Radek Krejci2c22f122018-09-05 15:08:03 +02008project(libyang C)
9
10include(GNUInstallDirs)
11include(CheckSymbolExists)
12
13set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
14
15# Correct RPATH usage on OS X
16set(CMAKE_MACOSX_RPATH TRUE)
17
18# set version
19set(LIBYANG_MAJOR_VERSION 1)
20set(LIBYANG_MINOR_VERSION 0)
21set(LIBYANG_MICRO_VERSION 0)
22set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
23set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION})
24
25# set default build type if not specified by user
26if(NOT CMAKE_BUILD_TYPE)
27 set(CMAKE_BUILD_TYPE debug)
28endif()
29
Radek Krejcicad716e2018-11-26 15:18:27 +010030set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -std=c99")
Radek Krejci2c22f122018-09-05 15:08:03 +020031set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
32set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
33set(CMAKE_C_FLAGS_DEBUG "-g -O0")
34
35# options
Radek Krejcie84f12f2018-09-18 14:11:50 +020036if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
37 option(ENABLE_BUILD_TESTS "Build tests" ON)
38 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
Radek Krejcie8fb7dd2019-03-08 15:14:38 +010039
40 if(CMAKE_COMPILER_IS_GNUCC)
41 option(ENABLE_COVERAGE "Build code coverage report from tests" ON)
42 else()
43 option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
44 endif()
Radek Krejcie84f12f2018-09-18 14:11:50 +020045else()
46 option(ENABLE_BUILD_TESTS "Build tests" OFF)
47 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
Radek Krejci6208cfa2019-03-08 14:43:58 +010048 option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
Radek Krejcie84f12f2018-09-18 14:11:50 +020049endif()
Radek Krejci2c22f122018-09-05 15:08:03 +020050#option(ENABLE_CALLGRIND_TESTS "Build performance tests to be run with callgrind" OFF)
51
52#option(ENABLE_CACHE "Enable data caching for schemas and hash tables for data (time-efficient at the cost of increased space-complexity)" ON)
53#option(ENABLE_LATEST_REVISIONS "Enable reusing of latest revisions of schemas" ON)
54#option(ENABLE_LYD_PRIV "Add a private pointer also to struct lyd_node (data node structure), just like in struct lys_node, for arbitrary user data" OFF)
55#set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang" CACHE STRING "Directory with libyang plugins (extensions and user types)")
56
57#if(ENABLE_CACHE)
58# set(LY_ENABLED_CACHE 1)
59#endif()
60#if(ENABLE_LATEST_REVISIONS)
61# set(LY_ENABLED_LATEST_REVISIONS 1)
62#endif()
63#if(ENABLE_LYD_PRIV)
64# set(LY_ENABLED_LYD_PRIV 1)
65#endif()
66
67if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
68 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
69 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
70elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
71 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
72 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
73else()
74 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x")
75 set(COMPILER_PACKED_ATTR "")
76endif()
77
78include_directories(${PROJECT_BINARY_DIR}/src ${PROJECT_SOURCE_DIR}/src)
79configure_file(${PROJECT_SOURCE_DIR}/src/config.h.in ${PROJECT_BINARY_DIR}/src/config.h @ONLY)
80
81#set(EXTENSIONS_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/extensions")
82#set(USER_TYPES_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/user_types")
83
84# include custom Modules
85set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
86
87# setup bindings
88#set(GEN_LANGUAGE_BINDINGS 0 CACHE BOOL "Enable language bindings generation.")
89#set(GEN_CPP_BINDINGS 1 CACHE BOOL "Enable C++ bindings.")
90# Python bindings depend on C++ bindings because of SWIG
91#set(GEN_PYTHON_BINDINGS 1 CACHE BOOL "Enable Python bindings.")
92#set(GEN_PYTHON_VERSION "3" CACHE STRING "Python version")
93#set(GEN_JAVASCRIPT_BINDINGS 0 CACHE BOOL "Enable JavaScript bindings.")
94
95#find_program(DEB_BUILDER NAMES debuild)
96#find_program(RPM_BUILDER NAMES rpmbuild)
97
98#if (NOT DEFINED ENV{TRAVIS_BRANCH})
99# execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
100# OUTPUT_VARIABLE GIT_BRANCH
101# OUTPUT_STRIP_TRAILING_WHITESPACE
102# ERROR_QUIET
103# )
104# if (NOT GIT_BRANCH)
105# set(ENV{TRAVIS_BRANCH} "master")
106# else()
107# if (GIT_BRANCH MATCHES "master|devel")
108# set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
109# else()
110# set(ENV{TRAVIS_BRANCH} "master")
111# endif()
112# endif()
113# set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
114#endif()
115#
116#if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
117# set(PACKAGE_NAME "libyang")
118# set(PACKAGE_PART_NAME "")
119# set(BRANCH "master")
120# set(BUILD_TYPE "Package")
121# set(CONFLICT_PACKAGE_NAME "libyang-experimental")
122#else ()
123# set(PACKAGE_NAME "libyang-experimental")
124# set(PACKAGE_PART_NAME "-experimental")
125# set(BRANCH "devel")
126# set(BUILD_TYPE "debug")
127# set(CONFLICT_PACKAGE_NAME "libyang")
128#endif()
129# change version in config files
130#configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
131#configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
132#configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
133#configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
134
135#if (NOT DEB_BUILDER)
136# message(WARNING "Missing tools (devscripts, debhelper package) for building deb package.\nYou won't be able to generate deb package from source code.\nCompiling libyang should still works fine.")
137#else ()
138# # target for local build deb package
139# add_custom_target(build-deb
140# WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
141# COMMAND build-packages/local-deb.sh
142# )
143# configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
144#endif()
145
146#if (NOT RPM_BUILDER)
147# message(WARNING "Missing tools (rpm package) for building rpm package. \nYou won't be able to generate rpm package from source code.\nCompiling libyang should still works fine.")
148#else ()
149# # target for local build rpm package
150# string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
151# add_custom_target(build-rpm
152# WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
153# COMMAND build-packages/local-rpm.sh
154# )
155# configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
156#endif()
157
158# by default build shared library
159# static build requires static libpcre library
160option(ENABLE_STATIC "Build static (.a) library" OFF)
161
162# check the supported platform
163if(NOT UNIX)
164 message(FATAL_ERROR "Only *nix like systems are supported.")
165endif()
166
167set(libsrc
Michal Vasko1324b6c2018-09-07 11:16:23 +0200168 src/common.c
Radek Krejci2c22f122018-09-05 15:08:03 +0200169 src/log.c
170 src/hash_table.c
Radek Krejciad573502018-09-07 15:26:55 +0200171 src/set.c
Radek Krejcid4557c62018-09-17 11:42:09 +0200172 src/context.c
Radek Krejci3f5e3db2018-10-11 15:57:47 +0200173 src/tree_schema.c
Radek Krejci19a96102018-11-15 13:38:09 +0100174 src/tree_schema_free.c
175 src/tree_schema_compile.c
Radek Krejci86d106e2018-10-18 09:53:19 +0200176 src/tree_schema_helpers.c
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200177 src/parser_yang.c
Radek Krejcib1646a92018-11-02 16:08:26 +0100178 src/xml.c
179 src/xpath.c)
Radek Krejci2c22f122018-09-05 15:08:03 +0200180
181#set(lintsrc
182# tools/lint/main.c
183# tools/lint/main_ni.c
184# tools/lint/commands.c
185# tools/lint/completion.c
186# tools/lint/configuration.c
187# linenoise/linenoise.c)
188
189#set(resrc
190# tools/re/main.c)
191
192#set(yang2yinsrc
193# tools/yang2yin/main.c)
194
195set(headers
196 src/libyang.h
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200197 src/context.h
Radek Krejci2c22f122018-09-05 15:08:03 +0200198 src/tree_schema.h
Radek Krejcice8c1592018-10-29 15:35:51 +0100199 src/extensions.h
Radek Krejci2c22f122018-09-05 15:08:03 +0200200 src/dict.h
201 src/log.h
Michal Vasko1324b6c2018-09-07 11:16:23 +0200202 src/set.h)
Radek Krejci2c22f122018-09-05 15:08:03 +0200203
204check_symbol_exists(vdprintf stdio.h HAVE_VDPRINTF)
205if(HAVE_VDPRINTF)
206 add_definitions(-DHAVE_VDPRINTF)
207endif(HAVE_VDPRINTF)
208
209# create static libyang library
210if(ENABLE_STATIC)
211 add_definitions(-DSTATIC)
212 set(CMAKE_EXE_LINKER_FLAGS -static)
213 set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
214 set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
215 set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
216 add_library(yang STATIC ${libsrc})
217else()
218 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
219 add_library(yangobj OBJECT ${libsrc})
220 add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
221
222 #link dl
223 target_link_libraries(yang ${CMAKE_DL_LIBS})
224endif(ENABLE_STATIC)
225
226set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcif3f47842018-11-15 11:22:15 +0100227set_target_properties(yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Radek Krejci2c22f122018-09-05 15:08:03 +0200228
229# link math
230target_link_libraries(yang m)
231
232# find pthreads
233set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
234find_package(Threads REQUIRED)
235if(ENABLE_STATIC)
236 target_link_libraries(yang -Wl,--whole-archive ${CMAKE_THREAD_LIBS_INIT} -Wl,--no-whole-archive)
237else()
238 target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
239endif(ENABLE_STATIC)
240
241# find PCRE library
242unset(PCRE_LIBRARY CACHE)
243find_package(PCRE REQUIRED)
244include_directories(${PCRE_INCLUDE_DIRS})
245target_link_libraries(yang ${PCRE_LIBRARIES})
246
247install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
248install(FILES ${headers} ${PROJECT_BINARY_DIR}/src/config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
249
250find_package(PkgConfig)
251if(PKG_CONFIG_FOUND)
252 # generate and install pkg-config file
253 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
254 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
255 # check that pkg-config includes the used path
256 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
257 if(RETURN EQUAL 0)
258 string(STRIP "${PC_PATH}" PC_PATH)
259 set(PC_PATH "${PC_PATH}:$ENV{PKG_CONFIG_PATH}")
260 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
261 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
262 if(SUBSTR_LEN EQUAL 0)
263 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}/${CMAKE_INSTALL_LIBDIR}/pkgconfig\".")
264 endif()
265 endif()
266endif()
267
268# generate doxygen documentation for libyang API
269find_package(Doxygen)
270if(DOXYGEN_FOUND)
271 find_program(DOT_PATH dot PATH_SUFFIXES graphviz2.38/bin graphviz/bin)
272 if(DOT_PATH)
273 set(HAVE_DOT "YES")
274 else()
275 set(HAVE_DOT "NO")
276 message(AUTHOR_WARNING "Doxygen: to generate UML diagrams please install graphviz")
277 endif()
278 add_custom_target(doc
279 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
280 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
281 configure_file(Doxyfile.in Doxyfile)
282endif()
283
284# clean cmake cache
285add_custom_target(cclean
286 COMMAND make clean
287 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
288 COMMAND rm -rf Makefile Doxyfile
289 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
290
291# YANG extensions plugins
292#set(EXTENSIONS_LIST "nacm" "metadata" "yangdata")
293# if the tests are enabled, build libyang_ext_test
Radek Krejcie84f12f2018-09-18 14:11:50 +0200294if(ENABLE_BUILD_TESTS)
295 find_package(CMocka 1.0.0)
Radek Krejci2c22f122018-09-05 15:08:03 +0200296# if(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
297# list(APPEND EXTENSIONS_LIST "libyang_ext_test")
298# endif(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
Radek Krejcie84f12f2018-09-18 14:11:50 +0200299endif(ENABLE_BUILD_TESTS)
Radek Krejci2c22f122018-09-05 15:08:03 +0200300
301#if(ENABLE_STATIC)
302# set(EXTENSIONS_LIST_SIZE " 0 ")
303# set(ITEM 0)
304# foreach(EXTENSION ${EXTENSIONS_LIST})
305# add_library(${EXTENSION} STATIC "src/extensions/${EXTENSION}.c")
306# target_link_libraries(yang ${EXTENSION})
307# set(EXTENSIONS_LIST_SIZE "${EXTENSIONS_LIST_SIZE} + lyext_size(${EXTENSION})")
308# set(EXTERN_EXTENSIONS_LIST "${EXTERN_EXTENSIONS_LIST}extern struct lyext_plugin_list ${EXTENSION}[];\n")
309# set(MEMCPY_EXTENSIONS_LIST "${MEMCPY_EXTENSIONS_LIST} lyext_add(plugin, count, ${EXTENSION});\n")
310# set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${EXTENSION}\",")
311# MATH(EXPR ITEM "${ITEM}+1")
312# endforeach()
313#else()
314# add_subdirectory(src/extensions)
315#endif(ENABLE_STATIC)
316
317# YANG user types plugins ("user_ipv4" is just an example, not installed by default)
318#set(USER_TYPE_LIST "user_date_and_time")
319#if(ENABLE_STATIC)
320# set(USER_TYPE_LIST_SIZE " 0 ")
321# foreach(USER_TYPE ${USER_TYPE_LIST})
322# add_library(${USER_TYPE} STATIC "src/user_types/${USER_TYPE}.c")
323# target_link_libraries(yang ${USER_TYPE})
324# set(USER_TYPE_LIST_SIZE "${USER_TYPE_LIST_SIZE} + lytype_size(${USER_TYPE})")
325# set(EXTERN_USER_TYPE_LIST "${EXTERN_USER_TYPE_LIST}extern struct lytype_plugin_list ${USER_TYPE}[];\n")
326# set(MEMCPY_USER_TYPE_LIST "${MEMCPY_USER_TYPE_LIST} lytype_add(plugin, count, ${USER_TYPE});\n")
327# set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${USER_TYPE}\",")
328# MATH(EXPR ITEM "${ITEM}+1")
329# endforeach()
330# set(STATIC_LOADED_PLUGINS_COUNT "${ITEM}")
331#else()
332# add_subdirectory(src/user_types)
333#endif(ENABLE_STATIC)
334#
335#configure_file(${PROJECT_SOURCE_DIR}/src/plugin_config.h.in ${PROJECT_BINARY_DIR}/src/plugin_config.h)
336
337# yanglint
338#add_executable(yanglint ${lintsrc})
339#target_link_libraries(yanglint yang)
340#install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
341#install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
342
343#yangre
344#add_executable(yangre ${resrc})
345#target_link_libraries(yangre yang)
346#install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
347
348# yang2yin
349#add_executable(yang2yin ${yang2yinsrc})
350
Radek Krejcie84f12f2018-09-18 14:11:50 +0200351if(ENABLE_VALGRIND_TESTS)
352 set(ENABLE_BUILD_TESTS ON)
353endif()
Radek Krejci2c22f122018-09-05 15:08:03 +0200354
Radek Krejcie84f12f2018-09-18 14:11:50 +0200355if(ENABLE_BUILD_TESTS)
356 if(CMOCKA_FOUND)
357 enable_testing()
358 add_subdirectory(tests)
359 else(CMOCKA_FOUND)
360 message(STATUS "Disabling tests because of missing CMocka")
361 set(ENABLE_BUILD_TESTS NO)
362 endif(CMOCKA_FOUND)
363endif(ENABLE_BUILD_TESTS)
Radek Krejci2c22f122018-09-05 15:08:03 +0200364
365#if(GEN_LANGUAGE_BINDINGS AND GEN_CPP_BINDINGS)
366# add_subdirectory(swig)
367#endif()
368