@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.8.12)
22project (libyang C)
33
44include (GNUInstallDirs)
5+ include (CheckSymbolExists)
56
67set (LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C." )
78
@@ -10,8 +11,8 @@ set(CMAKE_MACOSX_RPATH TRUE)
1011
1112# set version
1213set (LIBYANG_MAJOR_VERSION 0)
13- set (LIBYANG_MINOR_VERSION 14 )
14- set (LIBYANG_MICRO_VERSION 81 )
14+ set (LIBYANG_MINOR_VERSION 15 )
15+ set (LIBYANG_MICRO_VERSION 119 )
1516set (LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION} .${LIBYANG_MINOR_VERSION} .${LIBYANG_MICRO_VERSION} )
1617set (LIBYANG_SOVERSION ${LIBYANG_MAJOR_VERSION} .${LIBYANG_MINOR_VERSION} )
1718
@@ -20,7 +21,7 @@ if(NOT CMAKE_BUILD_TYPE)
2021 set (CMAKE_BUILD_TYPE debug)
2122endif ()
2223
23- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra" )
24+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=gnu11 " )
2425set (CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG" )
2526set (CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG" )
2627set (CMAKE_C_FLAGS_DEBUG "-g -O0" )
3536endif ()
3637option (ENABLE_CALLGRIND_TESTS "Build performance tests to be run with callgrind" OFF )
3738option (ENABLE_LATEST_REVISIONS "Enable reusing of latest revisions of schemas" ON )
38- option (ENABLE_CACHE "Enable data caching for schemas (time-efficient at the cost of increased space-complexity)" OFF )
39+ option (ENABLE_CACHE "Enable data caching for schemas (time-efficient at the cost of increased space-complexity)" ON )
40+ set (PLUGINS_DIR "${CMAKE_INSTALL_PREFIX} /${CMAKE_INSTALL_LIBDIR} /libyang" CACHE STRING "Directory with libyang plugins (extensions and user types)" )
3941
4042if (ENABLE_LATEST_REVISIONS)
4143 set (ENABLE_LATEST_REVISIONS_MACRO "/**\n * @brief Latest revisions of loaded schemas will be reused.\n */\n #define LY_ENABLED_LATEST_REVISIONS" )
@@ -49,15 +51,32 @@ else()
4951 set (ENABLE_CACHE_MACRO "/**\n * @brief Cache of some temporary information will not be used.\n */" )
5052endif ()
5153
54+ if (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__))" )
61+ elseif (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 ()
66+ set (COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))" )
67+ set (COMPILER_PACKED_ATTR "__attribute__((__packed__))" )
68+ else ()
69+ message (WARNING "You are using an unknown compiler, it must support C11 standard \" _Generic\" statement." )
70+ set (COMPILER_UNUSED_ATTR "UNUSED_ ## x" )
71+ set (COMPILER_PACKED_ATTR "" )
72+ endif ()
73+
5274include_directories (${PROJECT_BINARY_DIR} /src ${PROJECT_SOURCE_DIR} /src)
5375configure_file (${PROJECT_SOURCE_DIR} /src/libyang.h.in ${PROJECT_BINARY_DIR} /src/libyang.h @ONLY)
76+ configure_file (${PROJECT_SOURCE_DIR} /src/common.h.in ${PROJECT_BINARY_DIR} /src/common.h @ONLY)
5477
55- if (PLUGINS_DIR)
56- set (LIBYANG_EXT_PLUGINS_DIR ${PLUGINS_DIR} )
57- else ()
58- set (LIBYANG_EXT_PLUGINS_DIR ${CMAKE_INSTALL_PREFIX} /${CMAKE_INSTALL_LIBDIR} /libyang)
59- endif ()
60- configure_file (${PROJECT_SOURCE_DIR} /src/extensions_config.h.in ${PROJECT_BINARY_DIR} /src/extensions_config.h)
78+ set (EXTENSIONS_PLUGINS_DIR_MACRO "${PLUGINS_DIR} /extensions" )
79+ set (USER_TYPES_PLUGINS_DIR_MACRO "${PLUGINS_DIR} /user_types" )
6180
6281# include custom Modules
6382set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR} /CMakeModules/" )
@@ -72,9 +91,9 @@ set(GEN_JAVASCRIPT_BINDINGS 0 CACHE BOOL "Enable JavaScript bindings.")
7291
7392find_package (FLEX)
7493find_package (BISON)
75- find_program (DEB_BUILDER NAMES debuild)
76- find_program (RPM_BUILDER NAMES rpmbuild)
77- find_program (SED_TOOL NAMES sed)
94+ find_program (DEB_BUILDER NAMES debuild)
95+ find_program (RPM_BUILDER NAMES rpmbuild)
96+ find_program (SED_TOOL NAMES sed)
7897
7998if (NOT BISON_FOUND)
8099 message (WARNING "Missing Bison.\n You won't be able to generate source codes from changed flex/bison files.\n Compiling libyang should still works fine." )
@@ -115,11 +134,13 @@ endif()
115134
116135if ($ENV{TRAVIS_BRANCH} STREQUAL "master" )
117136 set (PACKAGE_NAME "libyang" )
137+ set (PACKAGE_PART_NAME "" )
118138 set (BRANCH "master" )
119139 set (BUILD_TYPE "Package" )
120140 set (CONFLICT_PACKAGE_NAME "libyang-experimental" )
121141else ()
122142 set (PACKAGE_NAME "libyang-experimental" )
143+ set (PACKAGE_PART_NAME "-experimental" )
123144 set (BRANCH "devel" )
124145 set (BUILD_TYPE "debug" )
125146 set (CONFLICT_PACKAGE_NAME "libyang" )
@@ -153,6 +174,10 @@ else ()
153174 configure_file (${PROJECT_SOURCE_DIR} /packages/local-rpm.sh.in ${PROJECT_BINARY_DIR} /build -packages/local-rpm.sh @ONLY)
154175endif ()
155176
177+ # by default build shared library
178+ # static build requires static libpcre library
179+ option (ENABLE_STATIC "Build static (.a) library" OFF )
180+
156181# check the supported platform
157182if (NOT UNIX )
158183 message (FATAL_ERROR "Only *nix like systems are supported." )
@@ -162,7 +187,7 @@ set(libsrc
162187 src/common.c
163188 src/context.c
164189 src/log .c
165- src/dict .c
190+ src/hash_table .c
166191 src/resolve.c
167192 src/validation.c
168193 src/xml.c
@@ -175,7 +200,7 @@ set(libsrc
175200 src/parser_yang.c
176201 src/tree_schema.c
177202 src/tree_data.c
178- src/extensions .c
203+ src/plugins .c
179204 src/printer.c
180205 src/xpath.c
181206 src/printer_yang.c
@@ -204,27 +229,49 @@ set(headers
204229 src/tree_schema.h
205230 src/tree_data.h
206231 src/extensions.h
232+ src/user_types.h
207233 src/xml.h
208234 src/dict.h)
209235
210- set (CMAKE_POSITION_INDEPENDENT_CODE TRUE )
211- add_library (yangobj OBJECT ${libsrc} )
212- add_library (yang SHARED $<TARGET_OBJECTS:yangobj>)
236+ check_symbol_exists(vdprintf stdio.h HAVE_VDPRINTF)
237+ if (HAVE_VDPRINTF)
238+ add_definitions (-DHAVE_VDPRINTF)
239+ endif (HAVE_VDPRINTF)
240+
241+ # create static libyang library
242+ if (ENABLE_STATIC)
243+ add_definitions (-DSTATIC)
244+ set (CMAKE_EXE_LINKER_FLAGS -static )
245+ set (CMAKE_FIND_LIBRARY_SUFFIXES .a)
246+ set (CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
247+ set (CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
248+ add_library (yang STATIC ${libsrc} )
249+ else ()
250+ set (CMAKE_POSITION_INDEPENDENT_CODE TRUE )
251+ add_library (yangobj OBJECT ${libsrc} )
252+ add_library (yang SHARED $<TARGET_OBJECTS:yangobj>)
253+
254+ #link dl
255+ target_link_libraries (yang ${CMAKE_DL_LIBS} )
256+ endif (ENABLE_STATIC)
257+
213258set_target_properties (yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION} )
214259set_target_properties (yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden" )
215260
216261# link math
217262target_link_libraries (yang m)
218263
219- #link dl
220- target_link_libraries (yang ${CMAKE_DL_LIBS} )
221-
222264# find pthreads
223265set (CMAKE_THREAD_PREFER_PTHREAD TRUE )
224266find_package (Threads REQUIRED)
225- target_link_libraries (yang ${CMAKE_THREAD_LIBS_INIT} )
267+ if (ENABLE_STATIC)
268+ target_link_libraries (yang -Wl,--whole-archive ${CMAKE_THREAD_LIBS_INIT} -Wl,--no -whole-archive)
269+ else ()
270+ target_link_libraries (yang ${CMAKE_THREAD_LIBS_INIT} )
271+ endif (ENABLE_STATIC)
226272
227273# find PCRE library
274+ unset (PCRE_LIBRARY CACHE )
228275find_package (PCRE REQUIRED)
229276include_directories (${PCRE_INCLUDE_DIRS} )
230277target_link_libraries (yang ${PCRE_LIBRARIES} )
@@ -266,7 +313,50 @@ add_custom_target(cclean
266313 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
267314
268315# YANG extensions plugins
269- add_subdirectory (src/extensions)
316+ set (EXTENSIONS_LIST "nacm" "metadata" "yangdata" )
317+ # if the tests are enabled, build libyang_ext_test
318+ if (ENABLE_BUILD_TESTS)
319+ find_package (CMocka 1.0.0)
320+ if (CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
321+ list (APPEND EXTENSIONS_LIST "libyang_ext_test" )
322+ endif (CMOCKA_FOUND AND CMAKE_BUILD_TYPE MATCHES debug)
323+ endif (ENABLE_BUILD_TESTS)
324+
325+ if (ENABLE_STATIC)
326+ set (EXTENSIONS_LIST_SIZE " 0 " )
327+ set (ITEM 0)
328+ foreach (EXTENSION ${EXTENSIONS_LIST} )
329+ add_library (${EXTENSION} STATIC "src/extensions/${EXTENSION} .c" )
330+ target_link_libraries (yang ${EXTENSION} )
331+ set (EXTENSIONS_LIST_SIZE "${EXTENSIONS_LIST_SIZE} + lyext_size(${EXTENSION} )" )
332+ set (EXTERN_EXTENSIONS_LIST "${EXTERN_EXTENSIONS_LIST} extern struct lyext_plugin_list ${EXTENSION} [];\n " )
333+ set (MEMCPY_EXTENSIONS_LIST "${MEMCPY_EXTENSIONS_LIST} lyext_add(plugin, count, ${EXTENSION} );\n " )
334+ set (STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \" ${EXTENSION} \" ," )
335+ MATH (EXPR ITEM "${ITEM} +1" )
336+ endforeach ()
337+ else ()
338+ add_subdirectory (src/extensions)
339+ endif (ENABLE_STATIC)
340+
341+ # YANG user types plugins ("user_ipv4" is just an example, not installed by default)
342+ set (USER_TYPE_LIST "user_date_and_time" )
343+ if (ENABLE_STATIC)
344+ set (USER_TYPE_LIST_SIZE " 0 " )
345+ foreach (USER_TYPE ${USER_TYPE_LIST} )
346+ add_library (${USER_TYPE} STATIC "src/user_types/${USER_TYPE} .c" )
347+ target_link_libraries (yang ${USER_TYPE} )
348+ set (USER_TYPE_LIST_SIZE "${USER_TYPE_LIST_SIZE} + lytype_size(${USER_TYPE} )" )
349+ set (EXTERN_USER_TYPE_LIST "${EXTERN_USER_TYPE_LIST} extern struct lytype_plugin_list ${USER_TYPE} [];\n " )
350+ set (MEMCPY_USER_TYPE_LIST "${MEMCPY_USER_TYPE_LIST} lytype_add(plugin, count, ${USER_TYPE} );\n " )
351+ set (STATIC_LOADED_PLUGINS "${STATIC_LOADED_PLUGINS} \" ${USER_TYPE} \" ," )
352+ MATH (EXPR ITEM "${ITEM} +1" )
353+ endforeach ()
354+ set (STATIC_LOADED_PLUGINS_COUNT "${ITEM} " )
355+ else ()
356+ add_subdirectory (src/user_types)
357+ endif (ENABLE_STATIC)
358+
359+ configure_file (${PROJECT_SOURCE_DIR} /src/plugin_config.h.in ${PROJECT_BINARY_DIR} /src/plugin_config.h)
270360
271361# yanglint
272362add_executable (yanglint ${lintsrc} )
@@ -287,17 +377,15 @@ if(ENABLE_VALGRIND_TESTS)
287377endif ()
288378
289379if (ENABLE_BUILD_TESTS)
290- find_package (CMocka 1.0.0)
291380 if (CMOCKA_FOUND)
292381 enable_testing ()
293382 add_subdirectory (tests)
383+ else (CMOCKA_FOUND)
384+ message (STATUS "Disabling tests because of missing CMocka" )
385+ set (ENABLE_BUILD_TESTS NO )
294386 endif (CMOCKA_FOUND)
295387endif (ENABLE_BUILD_TESTS)
296388
297389if (GEN_LANGUAGE_BINDINGS AND GEN_CPP_BINDINGS)
298390 add_subdirectory (swig)
299391endif ()
300-
301- if (GEN_LANGUAGE_BINDINGS AND GEN_JAVASCRIPT_BINDINGS)
302- include (swig/javascript/CMakeLists.txt)
303- endif ()
0 commit comments