blob: 5a745180866aa08c6396d22e7ee8b8e979bf68bc [file] [log] [blame]
Radek Krejcib0cd3942017-04-07 13:31:30 +02001cmake_minimum_required(VERSION 2.8.12)
Radek Krejci63679632019-04-03 14:22:27 +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 Krejcie74ea8c2015-04-09 18:06:20 +02008project(libyang C)
9
Jan Kundrát37df09e2016-11-04 15:42:08 +010010include(GNUInstallDirs)
Renato Westphal63836f72018-06-04 19:37:02 -030011include(CheckSymbolExists)
Jan Kundrát37df09e2016-11-04 15:42:08 +010012
Michal Vaskoe5cf9922015-11-11 11:06:40 +010013set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
14
Radek Krejcib0cd3942017-04-07 13:31:30 +020015# Correct RPATH usage on OS X
16set(CMAKE_MACOSX_RPATH TRUE)
17
Radek Krejci23bc6ea2019-04-05 10:48:44 +020018# Version of the project
19# Generic version of not only the library. Major version is reserved for really big changes of the project,
20# minor version changes with added functionality (new tool, functionality of the tool or library, ...) and
21# micro version is changed with a set of small changes or bugfixes anywhere in the project.
22set(LIBYANG_MAJOR_VERSION 1)
23set(LIBYANG_MINOR_VERSION 0)
Michal Vaskof05fc1e2019-11-07 08:51:27 +010024set(LIBYANG_MICRO_VERSION 89)
Radek Krejci8f7cf402015-08-13 08:05:48 +020025set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
Radek Krejci23bc6ea2019-04-05 10:48:44 +020026
27# Version of the library
28# Major version is changed with every backward non-compatible API/ABI change in libyang, minor version changes
29# with backward compatible change and micro version is connected with any internal change of the library.
30set(LIBYANG_MAJOR_SOVERSION 1)
Michal Vaskoe65f8392019-11-07 08:51:18 +010031set(LIBYANG_MINOR_SOVERSION 3)
32set(LIBYANG_MICRO_SOVERSION 0)
Radek Krejci23bc6ea2019-04-05 10:48:44 +020033set(LIBYANG_SOVERSION_FULL ${LIBYANG_MAJOR_SOVERSION}.${LIBYANG_MINOR_SOVERSION}.${LIBYANG_MICRO_SOVERSION})
34set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_SOVERSION})
Michal Vaskofcd974b2017-08-22 10:17:49 +020035
Michal Vasko8f3160e2017-09-27 11:25:26 +020036# set default build type if not specified by user
37if(NOT CMAKE_BUILD_TYPE)
38 set(CMAKE_BUILD_TYPE debug)
39endif()
40
Renato Westphale32d40b2018-10-22 21:58:40 -020041set(CMAKE_C_STANDARD 11)
42set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
Michal Vasko8f3160e2017-09-27 11:25:26 +020043set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
44set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
45set(CMAKE_C_FLAGS_DEBUG "-g -O0")
46
47# options
48if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
49 option(ENABLE_BUILD_TESTS "Build tests" ON)
50 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
51else()
52 option(ENABLE_BUILD_TESTS "Build tests" OFF)
53 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
54endif()
Michal Vaskocd958c62017-11-03 16:33:59 +010055option(ENABLE_CALLGRIND_TESTS "Build performance tests to be run with callgrind" OFF)
Michal Vaskod025ee32018-06-28 10:04:19 +020056
57option(ENABLE_CACHE "Enable data caching for schemas and hash tables for data (time-efficient at the cost of increased space-complexity)" ON)
Michal Vasko8f3160e2017-09-27 11:25:26 +020058option(ENABLE_LATEST_REVISIONS "Enable reusing of latest revisions of schemas" ON)
Michal Vaskod025ee32018-06-28 10:04:19 +020059option(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)
Juraj Vijtiukfcd6fd82019-01-13 09:41:43 -050060option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
Michal Vaskoc6cd3f02018-03-02 14:07:42 +010061set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang" CACHE STRING "Directory with libyang plugins (extensions and user types)")
Michal Vasko8f3160e2017-09-27 11:25:26 +020062
Michal Vaskofcd974b2017-08-22 10:17:49 +020063if(ENABLE_CACHE)
Michal Vaskod025ee32018-06-28 10:04:19 +020064 set(LY_ENABLED_CACHE 1)
65endif()
66if(ENABLE_LATEST_REVISIONS)
67 set(LY_ENABLED_LATEST_REVISIONS 1)
68endif()
69if(ENABLE_LYD_PRIV)
70 set(LY_ENABLED_LYD_PRIV 1)
Michal Vaskofcd974b2017-08-22 10:17:49 +020071endif()
72
Michal Vasko70bf8e52018-03-26 11:32:33 +020073if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
Michal Vasko70bf8e52018-03-26 11:32:33 +020074 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
75 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
76elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
77 # require at least clang 3.0
78 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
79 message(FATAL_ERROR "Clang version must be at least 3.0!")
80 endif()
Michal Vasko0e9b2972018-03-26 11:53:29 +020081 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
Michal Vasko70bf8e52018-03-26 11:32:33 +020082 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
83else()
Renato Westphale32d40b2018-10-22 21:58:40 -020084 message(WARNING "You are using an unknown compiler, it must support C11 standard \"_Generic\" statement or the \"__builtin_types_compatible_p\" built-in function.")
Michal Vasko0e9b2972018-03-26 11:53:29 +020085 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x")
Michal Vasko70bf8e52018-03-26 11:32:33 +020086 set(COMPILER_PACKED_ATTR "")
87endif()
88
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020089include_directories(${PROJECT_BINARY_DIR}/src ${PROJECT_SOURCE_DIR}/src)
90configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_BINARY_DIR}/src/libyang.h @ONLY)
Michal Vasko70bf8e52018-03-26 11:32:33 +020091configure_file(${PROJECT_SOURCE_DIR}/src/common.h.in ${PROJECT_BINARY_DIR}/src/common.h @ONLY)
Radek Krejci8f7cf402015-08-13 08:05:48 +020092
Michal Vaskoc6cd3f02018-03-02 14:07:42 +010093set(EXTENSIONS_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/extensions")
94set(USER_TYPES_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/user_types")
Radek Krejci858ad952017-01-04 11:16:32 +010095
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020096# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020097set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020098
Mislav Novakovicedc6c082017-05-30 15:21:42 +020099# setup bindings
100set(GEN_LANGUAGE_BINDINGS 0 CACHE BOOL "Enable language bindings generation.")
101set(GEN_CPP_BINDINGS 1 CACHE BOOL "Enable C++ bindings.")
102# Python bindings depend on C++ bindings because of SWIG
103set(GEN_PYTHON_BINDINGS 1 CACHE BOOL "Enable Python bindings.")
104set(GEN_PYTHON_VERSION "3" CACHE STRING "Python version")
105set(GEN_JAVASCRIPT_BINDINGS 0 CACHE BOOL "Enable JavaScript bindings.")
Michal Vasko5edb0ee2018-10-23 08:55:25 +0200106set(GEN_JAVA_BINDINGS 0 CACHE BOOL "Enable Java bindings.")
Mislav Novakovicedc6c082017-05-30 15:21:42 +0200107
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100108find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +0200109find_package(BISON)
Michal Vaskoaa515e82018-03-23 08:45:19 +0100110find_program(DEB_BUILDER NAMES debuild)
111find_program(RPM_BUILDER NAMES rpmbuild)
112find_program(SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100113
Pavol Vicancae54882016-03-30 12:24:43 +0200114if(NOT BISON_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200115 message(WARNING "Missing Bison.\nYou won't be able to generate source codes from changed flex/bison files.\nCompiling libyang should still works fine.")
Pavol Vicancae54882016-03-30 12:24:43 +0200116elseif (NOT FLEX_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200117 message(WARNING "Missing Flex.\nYou won't be able to generate source codes from changed flex/bison files.\nCompiling libyang should still works fine.")
Pavol Vicancae54882016-03-30 12:24:43 +0200118else()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200119 if (BISON_VERSION VERSION_LESS 3)
120 set(EMPTYDIR "")
121 else ()
122 set(EMPTYDIR "%empty")
123 endif()
Jan Kundrátfd7a5c72017-10-26 17:45:06 +0200124 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_BINARY_DIR}/src/yang.y)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200125 add_custom_target(bison
126 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
Radek Krejcif2dc9c32017-10-27 14:17:52 +0200127 COMMAND bison -l -o parser_yang_bis.c --defines=parser_yang_bis.h ${PROJECT_BINARY_DIR}/src/yang.y
Michal Vasko8f3160e2017-09-27 11:25:26 +0200128 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
129 COMMAND ${SED_TOOL} -i 's/int yychar\;/int yychar\;\\nchar *s = NULL, *tmp_s = NULL, *ext_name = NULL\;\\nstruct lys_module *trg = NULL\;\\nstruct lys_node *tpdf_parent = NULL, *data_node = NULL\;\\nstruct lys_ext_instance_complex *ext_instance = NULL\;\\nint is_ext_instance\;\\nvoid *actual = NULL\;\\nenum yytokentype backup_type, actual_type = MODULE_KEYWORD\;\\nint64_t cnt_val = 0\;\\nint is_value = 0\;\\nvoid *yang_type = NULL\;/' parser_yang_bis.c
130 )
Pavol Vicancae54882016-03-30 12:24:43 +0200131endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100132
PavolVican5d377522017-06-07 15:08:14 +0200133if (NOT DEFINED ENV{TRAVIS_BRANCH})
Michal Vasko8f3160e2017-09-27 11:25:26 +0200134 execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
135 OUTPUT_VARIABLE GIT_BRANCH
136 OUTPUT_STRIP_TRAILING_WHITESPACE
137 ERROR_QUIET
138 )
139 if (NOT GIT_BRANCH)
140 set(ENV{TRAVIS_BRANCH} "master")
141 else()
142 if (GIT_BRANCH MATCHES "master|devel")
143 set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
144 else()
145 set(ENV{TRAVIS_BRANCH} "master")
146 endif()
147 endif()
148 set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
PavolVican37396272017-03-25 00:57:20 +0100149endif()
150
PavolVican5d377522017-06-07 15:08:14 +0200151if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200152 set(PACKAGE_NAME "libyang")
PavolVican8738eee2018-02-27 01:47:02 +0100153 set(PACKAGE_PART_NAME "")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200154 set(BRANCH "master")
155 set(BUILD_TYPE "Package")
156 set(CONFLICT_PACKAGE_NAME "libyang-experimental")
PavolVican5d377522017-06-07 15:08:14 +0200157else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200158 set(PACKAGE_NAME "libyang-experimental")
PavolVican8738eee2018-02-27 01:47:02 +0100159 set(PACKAGE_PART_NAME "-experimental")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200160 set(BRANCH "devel")
161 set(BUILD_TYPE "debug")
162 set(CONFLICT_PACKAGE_NAME "libyang")
PavolVican5d377522017-06-07 15:08:14 +0200163endif()
164# change version in config files
165configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
166configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
167configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
168configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
169
PavolVicancf749252017-06-08 09:31:49 +0200170if (NOT DEB_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200171 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.")
PavolVicancf749252017-06-08 09:31:49 +0200172else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200173 # target for local build deb package
174 add_custom_target(build-deb
175 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
176 COMMAND build-packages/local-deb.sh
177 )
178 configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
PavolVicancf749252017-06-08 09:31:49 +0200179endif()
PavolVican5d377522017-06-07 15:08:14 +0200180
PavolVicancf749252017-06-08 09:31:49 +0200181if (NOT RPM_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200182 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.")
PavolVicancf749252017-06-08 09:31:49 +0200183else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200184 # target for local build rpm package
185 string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
186 add_custom_target(build-rpm
187 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
188 COMMAND build-packages/local-rpm.sh
189 )
190 configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
PavolVicancf749252017-06-08 09:31:49 +0200191endif()
PavolVican5d377522017-06-07 15:08:14 +0200192
Mislav Novakovic61c79082018-05-21 13:08:24 +0200193# by default build shared library
194# static build requires static libpcre library
195option(ENABLE_STATIC "Build static (.a) library" OFF)
196
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200197# check the supported platform
198if(NOT UNIX)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200199 message(FATAL_ERROR "Only *nix like systems are supported.")
Radek Krejci61cd5562015-10-05 14:06:34 +0200200endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200201
Radek Krejci5a065542015-05-22 15:02:07 +0200202set(libsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200203 src/common.c
204 src/context.c
205 src/log.c
Michal Vasko6c810702018-03-14 16:23:21 +0100206 src/hash_table.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200207 src/resolve.c
208 src/validation.c
209 src/xml.c
210 src/parser.c
211 src/parser_yin.c
212 src/parser_xml.c
213 src/parser_json.c
Michal Vasko1e82a3b2018-07-03 12:16:58 +0200214 src/parser_lyb.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200215 src/parser_yang_bis.c
216 src/parser_yang_lex.c
217 src/parser_yang.c
218 src/tree_schema.c
219 src/tree_data.c
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100220 src/plugins.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200221 src/printer.c
222 src/xpath.c
223 src/printer_yang.c
224 src/printer_yin.c
Radek Krejci0e67ac42018-07-25 15:46:37 +0200225 src/printer_json_schema.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200226 src/printer_xml.c
227 src/printer_tree.c
228 src/printer_info.c
229 src/printer_json.c
Michal Vasko1e82a3b2018-07-03 12:16:58 +0200230 src/printer_lyb.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200231 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200232
Radek Krejci5a065542015-05-22 15:02:07 +0200233set(lintsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200234 tools/lint/main.c
235 tools/lint/main_ni.c
236 tools/lint/commands.c
237 tools/lint/completion.c
238 tools/lint/configuration.c
239 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200240
Radek Krejcib4a481c2017-07-12 15:12:34 +0200241set(resrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200242 tools/re/main.c)
Radek Krejcib4a481c2017-07-12 15:12:34 +0200243
Michal Vasko9a542742016-06-23 13:04:51 +0200244set(yang2yinsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200245 tools/yang2yin/main.c)
Michal Vasko9a542742016-06-23 13:04:51 +0200246
Tomas Cejka422c0072015-07-21 18:18:25 +0200247set(headers
Michal Vasko8f3160e2017-09-27 11:25:26 +0200248 src/tree_schema.h
249 src/tree_data.h
250 src/extensions.h
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100251 src/user_types.h
Michal Vasko8f3160e2017-09-27 11:25:26 +0200252 src/xml.h
253 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200254
Michal Vasko59b81cd2018-10-12 10:19:51 +0200255check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
Renato Westphal63836f72018-06-04 19:37:02 -0300256if(HAVE_VDPRINTF)
257 add_definitions(-DHAVE_VDPRINTF)
258endif(HAVE_VDPRINTF)
259
Mislav Novakovic61c79082018-05-21 13:08:24 +0200260# create static libyang library
261if(ENABLE_STATIC)
262 add_definitions(-DSTATIC)
263 set(CMAKE_EXE_LINKER_FLAGS -static)
264 set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
265 set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
266 set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
267 add_library(yang STATIC ${libsrc})
Radek Krejciaeccb362019-09-03 11:09:28 +0200268
269 # switch off the tests
270 if(ENABLE_BUILD_TESTS)
271 message(WARNING "Tests are disabled in case of static build")
272 set(ENABLE_BUILD_TESTS OFF)
273 set(ENABLE_VALGRIND_TESTS OFF)
274 endif(ENABLE_BUILD_TESTS)
275 if(ENABLE_VALGRIND_TESTS)
276 message(WARNING "Tests are disabled in case of static build")
277 set(ENABLE_VALGRIND_TESTS OFF)
278 endif(ENABLE_VALGRIND_TESTS)
Mislav Novakovic61c79082018-05-21 13:08:24 +0200279else()
280 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
281 add_library(yangobj OBJECT ${libsrc})
282 add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Mislav Novakovic7073ef52018-05-24 17:23:02 +0200283
Michal Vaskoe41dfe62018-11-08 13:28:07 +0100284 #only for tests with visible internal symbols
285 add_library(yangobj_tests OBJECT ${libsrc})
286
Mislav Novakovic7073ef52018-05-24 17:23:02 +0200287 #link dl
288 target_link_libraries(yang ${CMAKE_DL_LIBS})
Robin Jarry05ad85a2018-11-19 12:53:00 +0100289
290 set_target_properties(yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200291endif(ENABLE_STATIC)
292
Radek Krejci23bc6ea2019-04-05 10:48:44 +0200293set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200294
Radek Krejci89658b32016-02-08 11:14:46 +0100295# link math
296target_link_libraries(yang m)
297
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100298# find pthreads
299set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
300find_package(Threads REQUIRED)
Radek Krejciaeccb362019-09-03 11:09:28 +0200301target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100302
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200303# find PCRE library
Mislav Novakovic4c319552018-05-29 16:27:08 +0200304unset(PCRE_LIBRARY CACHE)
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200305find_package(PCRE REQUIRED)
306include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200307target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200308
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100309install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
Jan Kundrátfd7a5c72017-10-26 17:45:06 +0200310install(FILES ${headers} ${PROJECT_BINARY_DIR}/src/libyang.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200311
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100312find_package(PkgConfig)
313if(PKG_CONFIG_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200314 # generate and install pkg-config file
315 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
316 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
317 # check that pkg-config includes the used path
318 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
319 if(RETURN EQUAL 0)
David Sedlákcc8bd242018-07-03 11:59:31 +0200320 string(STRIP "${PC_PATH}" PC_PATH)
321 set(PC_PATH "${PC_PATH}:$ENV{PKG_CONFIG_PATH}")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200322 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
323 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
324 if(SUBSTR_LEN EQUAL 0)
325 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\".")
326 endif()
327 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100328endif()
329
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200330# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200331find_package(Doxygen)
332if(DOXYGEN_FOUND)
Mislav Novakovicddb2d482018-07-23 14:38:37 +0200333 find_program(DOT_PATH dot PATH_SUFFIXES graphviz2.38/bin graphviz/bin)
334 if(DOT_PATH)
335 set(HAVE_DOT "YES")
336 else()
337 set(HAVE_DOT "NO")
338 message(AUTHOR_WARNING "Doxygen: to generate UML diagrams please install graphviz")
339 endif()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200340 add_custom_target(doc
341 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
342 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
343 configure_file(Doxyfile.in Doxyfile)
Radek Krejcie2626092015-10-08 11:25:44 +0200344endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200345
346# clean cmake cache
347add_custom_target(cclean
Michal Vasko8f3160e2017-09-27 11:25:26 +0200348 COMMAND make clean
349 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
350 COMMAND rm -rf Makefile Doxyfile
351 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200352
Radek Krejcic43058d2017-01-04 16:29:58 +0100353# YANG extensions plugins
Mislav Novakovic61c79082018-05-21 13:08:24 +0200354set(EXTENSIONS_LIST "nacm" "metadata" "yangdata")
Mislav Novakovic609b7482018-05-23 15:01:58 +0200355# if the tests are enabled, build libyang_ext_test
356if(ENABLE_BUILD_TESTS)
357 find_package(CMocka 1.0.0)
Mislav Novakovic675ebb02018-05-25 17:40:19 +0200358 if(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
Mislav Novakovic609b7482018-05-23 15:01:58 +0200359 list(APPEND EXTENSIONS_LIST "libyang_ext_test")
Mislav Novakovic675ebb02018-05-25 17:40:19 +0200360 endif(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
Mislav Novakovic609b7482018-05-23 15:01:58 +0200361endif(ENABLE_BUILD_TESTS)
362
Mislav Novakovic61c79082018-05-21 13:08:24 +0200363if(ENABLE_STATIC)
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200364 set(EXTENSIONS_LIST_SIZE " 0 ")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200365 set(ITEM 0)
366 foreach(EXTENSION ${EXTENSIONS_LIST})
367 add_library(${EXTENSION} STATIC "src/extensions/${EXTENSION}.c")
368 target_link_libraries(yang ${EXTENSION})
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200369 set(EXTENSIONS_LIST_SIZE "${EXTENSIONS_LIST_SIZE} + lyext_size(${EXTENSION})")
370 set(EXTERN_EXTENSIONS_LIST "${EXTERN_EXTENSIONS_LIST}extern struct lyext_plugin_list ${EXTENSION}[];\n")
371 set(MEMCPY_EXTENSIONS_LIST "${MEMCPY_EXTENSIONS_LIST} lyext_add(plugin, count, ${EXTENSION});\n")
Mislav Novakovic74726932018-06-04 13:14:03 +0200372 set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${EXTENSION}\",")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200373 MATH(EXPR ITEM "${ITEM}+1")
374 endforeach()
375else()
376 add_subdirectory(src/extensions)
Mislav Novakovice5bcc132018-05-29 16:28:02 +0200377endif(ENABLE_STATIC)
Radek Krejcic43058d2017-01-04 16:29:58 +0100378
Michal Vasko717cb692018-12-19 15:13:25 +0100379# YANG user types plugins
380set(USER_TYPE_LIST "user_yang_types" "user_inet_types")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200381if(ENABLE_STATIC)
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200382 set(USER_TYPE_LIST_SIZE " 0 ")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200383 foreach(USER_TYPE ${USER_TYPE_LIST})
384 add_library(${USER_TYPE} STATIC "src/user_types/${USER_TYPE}.c")
385 target_link_libraries(yang ${USER_TYPE})
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200386 set(USER_TYPE_LIST_SIZE "${USER_TYPE_LIST_SIZE} + lytype_size(${USER_TYPE})")
387 set(EXTERN_USER_TYPE_LIST "${EXTERN_USER_TYPE_LIST}extern struct lytype_plugin_list ${USER_TYPE}[];\n")
388 set(MEMCPY_USER_TYPE_LIST "${MEMCPY_USER_TYPE_LIST} lytype_add(plugin, count, ${USER_TYPE});\n")
Mislav Novakovic74726932018-06-04 13:14:03 +0200389 set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${USER_TYPE}\",")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200390 MATH(EXPR ITEM "${ITEM}+1")
391 endforeach()
Mislav Novakovic74726932018-06-04 13:14:03 +0200392 set(STATIC_LOADED_PLUGINS_COUNT "${ITEM}")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200393else()
394 add_subdirectory(src/user_types)
Mislav Novakovice5bcc132018-05-29 16:28:02 +0200395endif(ENABLE_STATIC)
Mislav Novakovic61c79082018-05-21 13:08:24 +0200396
397configure_file(${PROJECT_SOURCE_DIR}/src/plugin_config.h.in ${PROJECT_BINARY_DIR}/src/plugin_config.h)
Michal Vasko25b59c72018-03-21 11:49:23 +0100398
Radek Krejcic43058d2017-01-04 16:29:58 +0100399# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200400add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200401target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100402install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
403install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200404
Radek Krejcib4a481c2017-07-12 15:12:34 +0200405#yangre
406add_executable(yangre ${resrc})
407target_link_libraries(yangre yang)
408install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
David Lampartera7893f72018-11-09 16:05:44 +0100409install(FILES ${PROJECT_SOURCE_DIR}/tools/re/yangre.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejcib4a481c2017-07-12 15:12:34 +0200410
Radek Krejcic43058d2017-01-04 16:29:58 +0100411# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200412add_executable(yang2yin ${yang2yinsrc})
413
Michal Vaskoe4edb092018-11-20 10:51:17 +0100414# uninstall
415add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/uninstall.cmake")
416
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200417if(ENABLE_VALGRIND_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200418 set(ENABLE_BUILD_TESTS ON)
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200419endif()
420
421if(ENABLE_BUILD_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200422 if(CMOCKA_FOUND)
423 enable_testing()
424 add_subdirectory(tests)
Radek Krejcibf123af2018-02-21 08:05:21 +0100425 else(CMOCKA_FOUND)
426 message(STATUS "Disabling tests because of missing CMocka")
427 set(ENABLE_BUILD_TESTS NO)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200428 endif(CMOCKA_FOUND)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100429endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100430
Juraj Vijtiukfcd6fd82019-01-13 09:41:43 -0500431if(ENABLE_BUILD_FUZZ_TARGETS)
432 add_subdirectory(tests/fuzz)
433endif(ENABLE_BUILD_FUZZ_TARGETS)
434
Mislav Novakovicedc6c082017-05-30 15:21:42 +0200435if(GEN_LANGUAGE_BINDINGS AND GEN_CPP_BINDINGS)
436 add_subdirectory(swig)
437endif()