blob: 3c6e1cb57d2497efdc0d825810ca187907a64ecc [file] [log] [blame]
Radek Krejcib0cd3942017-04-07 13:31:30 +02001cmake_minimum_required(VERSION 2.8.12)
Radek Krejcie74ea8c2015-04-09 18:06:20 +02002project(libyang C)
3
Jan Kundrát37df09e2016-11-04 15:42:08 +01004include(GNUInstallDirs)
Renato Westphal63836f72018-06-04 19:37:02 -03005include(CheckSymbolExists)
Jan Kundrát37df09e2016-11-04 15:42:08 +01006
Michal Vaskoe5cf9922015-11-11 11:06:40 +01007set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
8
Radek Krejcib0cd3942017-04-07 13:31:30 +02009# Correct RPATH usage on OS X
10set(CMAKE_MACOSX_RPATH TRUE)
11
Radek Krejci8f7cf402015-08-13 08:05:48 +020012# set version
13set(LIBYANG_MAJOR_VERSION 0)
Michal Vaskodd20e912018-08-15 11:31:09 +020014set(LIBYANG_MINOR_VERSION 16)
Michal Vaskoda3592a2018-09-11 08:43:31 +020015set(LIBYANG_MICRO_VERSION 32)
Radek Krejci8f7cf402015-08-13 08:05:48 +020016set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
17set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION})
Michal Vaskofcd974b2017-08-22 10:17:49 +020018
Michal Vasko8f3160e2017-09-27 11:25:26 +020019# set default build type if not specified by user
20if(NOT CMAKE_BUILD_TYPE)
21 set(CMAKE_BUILD_TYPE debug)
22endif()
23
Michal Vasko404aea22018-03-22 14:37:27 +010024set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=gnu11")
Michal Vasko8f3160e2017-09-27 11:25:26 +020025set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
26set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
27set(CMAKE_C_FLAGS_DEBUG "-g -O0")
28
29# options
30if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
31 option(ENABLE_BUILD_TESTS "Build tests" ON)
32 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
33else()
34 option(ENABLE_BUILD_TESTS "Build tests" OFF)
35 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
36endif()
Michal Vaskocd958c62017-11-03 16:33:59 +010037option(ENABLE_CALLGRIND_TESTS "Build performance tests to be run with callgrind" OFF)
Michal Vaskod025ee32018-06-28 10:04:19 +020038
39option(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 +020040option(ENABLE_LATEST_REVISIONS "Enable reusing of latest revisions of schemas" ON)
Michal Vaskod025ee32018-06-28 10:04:19 +020041option(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)
Michal Vaskoc6cd3f02018-03-02 14:07:42 +010042set(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 +020043
Michal Vaskofcd974b2017-08-22 10:17:49 +020044if(ENABLE_CACHE)
Michal Vaskod025ee32018-06-28 10:04:19 +020045 set(LY_ENABLED_CACHE 1)
46endif()
47if(ENABLE_LATEST_REVISIONS)
48 set(LY_ENABLED_LATEST_REVISIONS 1)
49endif()
50if(ENABLE_LYD_PRIV)
51 set(LY_ENABLED_LYD_PRIV 1)
Michal Vaskofcd974b2017-08-22 10:17:49 +020052endif()
53
Michal Vasko70bf8e52018-03-26 11:32:33 +020054if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
55 # require at least gcc 4.9
56 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.9)
57 message(FATAL_ERROR "GCC version must be at least 4.9!")
58 endif()
59 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
60 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
61elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
62 # require at least clang 3.0
63 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
64 message(FATAL_ERROR "Clang version must be at least 3.0!")
65 endif()
Michal Vasko0e9b2972018-03-26 11:53:29 +020066 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
Michal Vasko70bf8e52018-03-26 11:32:33 +020067 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
68else()
69 message(WARNING "You are using an unknown compiler, it must support C11 standard \"_Generic\" statement.")
Michal Vasko0e9b2972018-03-26 11:53:29 +020070 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x")
Michal Vasko70bf8e52018-03-26 11:32:33 +020071 set(COMPILER_PACKED_ATTR "")
72endif()
73
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020074include_directories(${PROJECT_BINARY_DIR}/src ${PROJECT_SOURCE_DIR}/src)
75configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_BINARY_DIR}/src/libyang.h @ONLY)
Michal Vasko70bf8e52018-03-26 11:32:33 +020076configure_file(${PROJECT_SOURCE_DIR}/src/common.h.in ${PROJECT_BINARY_DIR}/src/common.h @ONLY)
Radek Krejci8f7cf402015-08-13 08:05:48 +020077
Michal Vaskoc6cd3f02018-03-02 14:07:42 +010078set(EXTENSIONS_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/extensions")
79set(USER_TYPES_PLUGINS_DIR_MACRO "${PLUGINS_DIR}/user_types")
Radek Krejci858ad952017-01-04 11:16:32 +010080
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020081# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020082set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020083
Mislav Novakovicedc6c082017-05-30 15:21:42 +020084# setup bindings
85set(GEN_LANGUAGE_BINDINGS 0 CACHE BOOL "Enable language bindings generation.")
86set(GEN_CPP_BINDINGS 1 CACHE BOOL "Enable C++ bindings.")
87# Python bindings depend on C++ bindings because of SWIG
88set(GEN_PYTHON_BINDINGS 1 CACHE BOOL "Enable Python bindings.")
89set(GEN_PYTHON_VERSION "3" CACHE STRING "Python version")
90set(GEN_JAVASCRIPT_BINDINGS 0 CACHE BOOL "Enable JavaScript bindings.")
Enbo Zhang7056e642018-09-07 21:05:21 +080091set(GEN_JAVA_BINDINGS 1 CACHE BOOL "Enable Java bindings.")
Mislav Novakovicedc6c082017-05-30 15:21:42 +020092
Pavol Vicanf7cc2852016-03-22 23:27:35 +010093find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020094find_package(BISON)
Michal Vaskoaa515e82018-03-23 08:45:19 +010095find_program(DEB_BUILDER NAMES debuild)
96find_program(RPM_BUILDER NAMES rpmbuild)
97find_program(SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010098
Pavol Vicancae54882016-03-30 12:24:43 +020099if(NOT BISON_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200100 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 +0200101elseif (NOT FLEX_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200102 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 +0200103else()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200104 if (BISON_VERSION VERSION_LESS 3)
105 set(EMPTYDIR "")
106 else ()
107 set(EMPTYDIR "%empty")
108 endif()
Jan Kundrátfd7a5c72017-10-26 17:45:06 +0200109 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_BINARY_DIR}/src/yang.y)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200110 add_custom_target(bison
111 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
Radek Krejcif2dc9c32017-10-27 14:17:52 +0200112 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 +0200113 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
114 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
115 )
Pavol Vicancae54882016-03-30 12:24:43 +0200116endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100117
PavolVican5d377522017-06-07 15:08:14 +0200118if (NOT DEFINED ENV{TRAVIS_BRANCH})
Michal Vasko8f3160e2017-09-27 11:25:26 +0200119 execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
120 OUTPUT_VARIABLE GIT_BRANCH
121 OUTPUT_STRIP_TRAILING_WHITESPACE
122 ERROR_QUIET
123 )
124 if (NOT GIT_BRANCH)
125 set(ENV{TRAVIS_BRANCH} "master")
126 else()
127 if (GIT_BRANCH MATCHES "master|devel")
128 set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
129 else()
130 set(ENV{TRAVIS_BRANCH} "master")
131 endif()
132 endif()
133 set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
PavolVican37396272017-03-25 00:57:20 +0100134endif()
135
PavolVican5d377522017-06-07 15:08:14 +0200136if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200137 set(PACKAGE_NAME "libyang")
PavolVican8738eee2018-02-27 01:47:02 +0100138 set(PACKAGE_PART_NAME "")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200139 set(BRANCH "master")
140 set(BUILD_TYPE "Package")
141 set(CONFLICT_PACKAGE_NAME "libyang-experimental")
PavolVican5d377522017-06-07 15:08:14 +0200142else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200143 set(PACKAGE_NAME "libyang-experimental")
PavolVican8738eee2018-02-27 01:47:02 +0100144 set(PACKAGE_PART_NAME "-experimental")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200145 set(BRANCH "devel")
146 set(BUILD_TYPE "debug")
147 set(CONFLICT_PACKAGE_NAME "libyang")
PavolVican5d377522017-06-07 15:08:14 +0200148endif()
149# change version in config files
150configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
151configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
152configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
153configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
154
PavolVicancf749252017-06-08 09:31:49 +0200155if (NOT DEB_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200156 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 +0200157else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200158 # target for local build deb package
159 add_custom_target(build-deb
160 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
161 COMMAND build-packages/local-deb.sh
162 )
163 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 +0200164endif()
PavolVican5d377522017-06-07 15:08:14 +0200165
PavolVicancf749252017-06-08 09:31:49 +0200166if (NOT RPM_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200167 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 +0200168else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200169 # target for local build rpm package
170 string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
171 add_custom_target(build-rpm
172 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
173 COMMAND build-packages/local-rpm.sh
174 )
175 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 +0200176endif()
PavolVican5d377522017-06-07 15:08:14 +0200177
Mislav Novakovic61c79082018-05-21 13:08:24 +0200178# by default build shared library
179# static build requires static libpcre library
180option(ENABLE_STATIC "Build static (.a) library" OFF)
181
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200182# check the supported platform
183if(NOT UNIX)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200184 message(FATAL_ERROR "Only *nix like systems are supported.")
Radek Krejci61cd5562015-10-05 14:06:34 +0200185endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200186
Radek Krejci5a065542015-05-22 15:02:07 +0200187set(libsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200188 src/common.c
189 src/context.c
190 src/log.c
Michal Vasko6c810702018-03-14 16:23:21 +0100191 src/hash_table.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200192 src/resolve.c
193 src/validation.c
194 src/xml.c
195 src/parser.c
196 src/parser_yin.c
197 src/parser_xml.c
198 src/parser_json.c
Michal Vasko1e82a3b2018-07-03 12:16:58 +0200199 src/parser_lyb.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200200 src/parser_yang_bis.c
201 src/parser_yang_lex.c
202 src/parser_yang.c
203 src/tree_schema.c
204 src/tree_data.c
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100205 src/plugins.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200206 src/printer.c
207 src/xpath.c
208 src/printer_yang.c
209 src/printer_yin.c
Radek Krejci0e67ac42018-07-25 15:46:37 +0200210 src/printer_json_schema.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200211 src/printer_xml.c
212 src/printer_tree.c
213 src/printer_info.c
214 src/printer_json.c
Michal Vasko1e82a3b2018-07-03 12:16:58 +0200215 src/printer_lyb.c
Michal Vasko8f3160e2017-09-27 11:25:26 +0200216 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200217
Radek Krejci5a065542015-05-22 15:02:07 +0200218set(lintsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200219 tools/lint/main.c
220 tools/lint/main_ni.c
221 tools/lint/commands.c
222 tools/lint/completion.c
223 tools/lint/configuration.c
224 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200225
Radek Krejcib4a481c2017-07-12 15:12:34 +0200226set(resrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200227 tools/re/main.c)
Radek Krejcib4a481c2017-07-12 15:12:34 +0200228
Michal Vasko9a542742016-06-23 13:04:51 +0200229set(yang2yinsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200230 tools/yang2yin/main.c)
Michal Vasko9a542742016-06-23 13:04:51 +0200231
Tomas Cejka422c0072015-07-21 18:18:25 +0200232set(headers
Michal Vasko8f3160e2017-09-27 11:25:26 +0200233 src/tree_schema.h
234 src/tree_data.h
235 src/extensions.h
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100236 src/user_types.h
Michal Vasko8f3160e2017-09-27 11:25:26 +0200237 src/xml.h
238 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200239
Renato Westphal63836f72018-06-04 19:37:02 -0300240check_symbol_exists(vdprintf stdio.h HAVE_VDPRINTF)
241if(HAVE_VDPRINTF)
242 add_definitions(-DHAVE_VDPRINTF)
243endif(HAVE_VDPRINTF)
244
Mislav Novakovic61c79082018-05-21 13:08:24 +0200245# create static libyang library
246if(ENABLE_STATIC)
247 add_definitions(-DSTATIC)
248 set(CMAKE_EXE_LINKER_FLAGS -static)
249 set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
250 set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
251 set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
252 add_library(yang STATIC ${libsrc})
253else()
254 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
255 add_library(yangobj OBJECT ${libsrc})
256 add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Mislav Novakovic7073ef52018-05-24 17:23:02 +0200257
258 #link dl
259 target_link_libraries(yang ${CMAKE_DL_LIBS})
Mislav Novakovic61c79082018-05-21 13:08:24 +0200260endif(ENABLE_STATIC)
261
Radek Krejci8f7cf402015-08-13 08:05:48 +0200262set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100263set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200264
Radek Krejci89658b32016-02-08 11:14:46 +0100265# link math
266target_link_libraries(yang m)
267
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100268# find pthreads
269set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
270find_package(Threads REQUIRED)
Mislav Novakovic7073ef52018-05-24 17:23:02 +0200271if(ENABLE_STATIC)
272 target_link_libraries(yang -Wl,--whole-archive ${CMAKE_THREAD_LIBS_INIT} -Wl,--no-whole-archive)
273else()
274 target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
275endif(ENABLE_STATIC)
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100276
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200277# find PCRE library
Mislav Novakovic4c319552018-05-29 16:27:08 +0200278unset(PCRE_LIBRARY CACHE)
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200279find_package(PCRE REQUIRED)
280include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200281target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200282
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100283install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
Jan Kundrátfd7a5c72017-10-26 17:45:06 +0200284install(FILES ${headers} ${PROJECT_BINARY_DIR}/src/libyang.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200285
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100286find_package(PkgConfig)
287if(PKG_CONFIG_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200288 # generate and install pkg-config file
289 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
290 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
291 # check that pkg-config includes the used path
292 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
293 if(RETURN EQUAL 0)
David Sedlákcc8bd242018-07-03 11:59:31 +0200294 string(STRIP "${PC_PATH}" PC_PATH)
295 set(PC_PATH "${PC_PATH}:$ENV{PKG_CONFIG_PATH}")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200296 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
297 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
298 if(SUBSTR_LEN EQUAL 0)
299 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\".")
300 endif()
301 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100302endif()
303
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200304# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200305find_package(Doxygen)
306if(DOXYGEN_FOUND)
Mislav Novakovicddb2d482018-07-23 14:38:37 +0200307 find_program(DOT_PATH dot PATH_SUFFIXES graphviz2.38/bin graphviz/bin)
308 if(DOT_PATH)
309 set(HAVE_DOT "YES")
310 else()
311 set(HAVE_DOT "NO")
312 message(AUTHOR_WARNING "Doxygen: to generate UML diagrams please install graphviz")
313 endif()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200314 add_custom_target(doc
315 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
316 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
317 configure_file(Doxyfile.in Doxyfile)
Radek Krejcie2626092015-10-08 11:25:44 +0200318endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200319
320# clean cmake cache
321add_custom_target(cclean
Michal Vasko8f3160e2017-09-27 11:25:26 +0200322 COMMAND make clean
323 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
324 COMMAND rm -rf Makefile Doxyfile
325 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200326
Radek Krejcic43058d2017-01-04 16:29:58 +0100327# YANG extensions plugins
Mislav Novakovic61c79082018-05-21 13:08:24 +0200328set(EXTENSIONS_LIST "nacm" "metadata" "yangdata")
Mislav Novakovic609b7482018-05-23 15:01:58 +0200329# if the tests are enabled, build libyang_ext_test
330if(ENABLE_BUILD_TESTS)
331 find_package(CMocka 1.0.0)
Mislav Novakovic675ebb02018-05-25 17:40:19 +0200332 if(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
Mislav Novakovic609b7482018-05-23 15:01:58 +0200333 list(APPEND EXTENSIONS_LIST "libyang_ext_test")
Mislav Novakovic675ebb02018-05-25 17:40:19 +0200334 endif(CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
Mislav Novakovic609b7482018-05-23 15:01:58 +0200335endif(ENABLE_BUILD_TESTS)
336
Mislav Novakovic61c79082018-05-21 13:08:24 +0200337if(ENABLE_STATIC)
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200338 set(EXTENSIONS_LIST_SIZE " 0 ")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200339 set(ITEM 0)
340 foreach(EXTENSION ${EXTENSIONS_LIST})
341 add_library(${EXTENSION} STATIC "src/extensions/${EXTENSION}.c")
342 target_link_libraries(yang ${EXTENSION})
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200343 set(EXTENSIONS_LIST_SIZE "${EXTENSIONS_LIST_SIZE} + lyext_size(${EXTENSION})")
344 set(EXTERN_EXTENSIONS_LIST "${EXTERN_EXTENSIONS_LIST}extern struct lyext_plugin_list ${EXTENSION}[];\n")
345 set(MEMCPY_EXTENSIONS_LIST "${MEMCPY_EXTENSIONS_LIST} lyext_add(plugin, count, ${EXTENSION});\n")
Mislav Novakovic74726932018-06-04 13:14:03 +0200346 set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${EXTENSION}\",")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200347 MATH(EXPR ITEM "${ITEM}+1")
348 endforeach()
349else()
350 add_subdirectory(src/extensions)
Mislav Novakovice5bcc132018-05-29 16:28:02 +0200351endif(ENABLE_STATIC)
Radek Krejcic43058d2017-01-04 16:29:58 +0100352
Michal Vasko101e7962018-06-05 10:38:40 +0200353# YANG user types plugins ("user_ipv4" is just an example, not installed by default)
354set(USER_TYPE_LIST "user_date_and_time")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200355if(ENABLE_STATIC)
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200356 set(USER_TYPE_LIST_SIZE " 0 ")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200357 foreach(USER_TYPE ${USER_TYPE_LIST})
358 add_library(${USER_TYPE} STATIC "src/user_types/${USER_TYPE}.c")
359 target_link_libraries(yang ${USER_TYPE})
Mislav Novakovic3bef44a2018-05-23 18:08:52 +0200360 set(USER_TYPE_LIST_SIZE "${USER_TYPE_LIST_SIZE} + lytype_size(${USER_TYPE})")
361 set(EXTERN_USER_TYPE_LIST "${EXTERN_USER_TYPE_LIST}extern struct lytype_plugin_list ${USER_TYPE}[];\n")
362 set(MEMCPY_USER_TYPE_LIST "${MEMCPY_USER_TYPE_LIST} lytype_add(plugin, count, ${USER_TYPE});\n")
Mislav Novakovic74726932018-06-04 13:14:03 +0200363 set(STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \"${USER_TYPE}\",")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200364 MATH(EXPR ITEM "${ITEM}+1")
365 endforeach()
Mislav Novakovic74726932018-06-04 13:14:03 +0200366 set(STATIC_LOADED_PLUGINS_COUNT "${ITEM}")
Mislav Novakovic61c79082018-05-21 13:08:24 +0200367else()
368 add_subdirectory(src/user_types)
Mislav Novakovice5bcc132018-05-29 16:28:02 +0200369endif(ENABLE_STATIC)
Mislav Novakovic61c79082018-05-21 13:08:24 +0200370
371configure_file(${PROJECT_SOURCE_DIR}/src/plugin_config.h.in ${PROJECT_BINARY_DIR}/src/plugin_config.h)
Michal Vasko25b59c72018-03-21 11:49:23 +0100372
Radek Krejcic43058d2017-01-04 16:29:58 +0100373# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200374add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200375target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100376install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
377install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200378
Radek Krejcib4a481c2017-07-12 15:12:34 +0200379#yangre
380add_executable(yangre ${resrc})
381target_link_libraries(yangre yang)
382install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
383
Radek Krejcic43058d2017-01-04 16:29:58 +0100384# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200385add_executable(yang2yin ${yang2yinsrc})
386
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200387if(ENABLE_VALGRIND_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200388 set(ENABLE_BUILD_TESTS ON)
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200389endif()
390
391if(ENABLE_BUILD_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200392 if(CMOCKA_FOUND)
393 enable_testing()
394 add_subdirectory(tests)
Radek Krejcibf123af2018-02-21 08:05:21 +0100395 else(CMOCKA_FOUND)
396 message(STATUS "Disabling tests because of missing CMocka")
397 set(ENABLE_BUILD_TESTS NO)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200398 endif(CMOCKA_FOUND)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100399endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100400
Mislav Novakovicedc6c082017-05-30 15:21:42 +0200401if(GEN_LANGUAGE_BINDINGS AND GEN_CPP_BINDINGS)
402 add_subdirectory(swig)
403endif()