@@ -16,23 +16,28 @@ include(UseCompat)
1616include (ABICheck)
1717include (SourceFormat)
1818include (GenDoc)
19+ include (GenCoverage)
1920
2021# set default build type if not specified by user
2122if (NOT CMAKE_BUILD_TYPE )
2223 set (CMAKE_BUILD_TYPE Debug)
2324endif ()
2425# normalize build type string
26+ # see https://github.com/CESNET/libyang/pull/1692 for why CMAKE_C_FLAGS_<type> are not used directly
2527string (TOUPPER "${CMAKE_BUILD_TYPE} " BUILD_TYPE_UPPER)
2628if ("${BUILD_TYPE_UPPER} " STREQUAL "RELEASE" )
2729 set (CMAKE_BUILD_TYPE "Release" CACHE STRING "Build Type" FORCE)
30+ set (CMAKE_C_FLAGS "-DNDEBUG -O2 ${CMAKE_C_FLAGS} " )
2831elseif ("${BUILD_TYPE_UPPER} " STREQUAL "DEBUG" )
2932 set (CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build Type" FORCE)
33+ set (CMAKE_C_FLAGS "-g3 -O0 ${CMAKE_C_FLAGS} " )
3034elseif ("${BUILD_TYPE_UPPER} " STREQUAL "RELWITHDEBINFO" )
3135 set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build Type" FORCE)
3236elseif ("${BUILD_TYPE_UPPER} " STREQUAL "RELWITHDEBUG" )
3337 set (CMAKE_BUILD_TYPE "RelWithDebug" CACHE STRING "Build Type" FORCE)
3438elseif ("${BUILD_TYPE_UPPER} " STREQUAL "ABICHECK" )
3539 set (CMAKE_BUILD_TYPE "ABICheck" CACHE STRING "Build Type" FORCE)
40+ set (CMAKE_C_FLAGS "-g -Og ${CMAKE_C_FLAGS} " )
3641elseif ("${BUILD_TYPE_UPPER} " STREQUAL "DOCONLY" )
3742 set (CMAKE_BUILD_TYPE "DocOnly" CACHE STRING "Build Type" FORCE)
3843endif ()
@@ -57,19 +62,17 @@ set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
5762# set version of the project
5863set (LIBYANG_MAJOR_VERSION 2)
5964set (LIBYANG_MINOR_VERSION 0)
60- set (LIBYANG_MICRO_VERSION 7 )
65+ set (LIBYANG_MICRO_VERSION 88 )
6166set (LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION} .${LIBYANG_MINOR_VERSION} .${LIBYANG_MICRO_VERSION} )
6267# set version of the library
6368set (LIBYANG_MAJOR_SOVERSION 2)
64- set (LIBYANG_MINOR_SOVERSION 1 )
65- set (LIBYANG_MICRO_SOVERSION 4 )
69+ set (LIBYANG_MINOR_SOVERSION 8 )
70+ set (LIBYANG_MICRO_SOVERSION 18 )
6671set (LIBYANG_SOVERSION_FULL ${LIBYANG_MAJOR_SOVERSION} .${LIBYANG_MINOR_SOVERSION} .${LIBYANG_MICRO_SOVERSION} )
6772set (LIBYANG_SOVERSION ${LIBYANG_MAJOR_SOVERSION} )
6873
69- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -std=c99" )
70- set (CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2" )
71- set (CMAKE_C_FLAGS_DEBUG "-g3 -O0" )
72- set (CMAKE_C_FLAGS_ABICHECK "-g -Og" )
74+ # global C flags
75+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic -std=c11" )
7376
7477include_directories (${PROJECT_BINARY_DIR} /src ${PROJECT_SOURCE_DIR} /src)
7578
@@ -94,7 +97,8 @@ set(type_plugins
9497 src/plugins_types/ipv4_prefix.c
9598 src/plugins_types/ipv6_prefix.c
9699 src/plugins_types/date_and_time.c
97- src/plugins_types/xpath1.0.c)
100+ src/plugins_types/xpath1.0.c
101+ src/plugins_types/node_instanceid.c)
98102
99103set (libsrc
100104 src/common.c
@@ -145,27 +149,49 @@ set(libsrc
145149 ${type_plugins} )
146150
147151set (headers
148- src/libyang.h
149152 src/context.h
150153 src/dict.h
151- src/log .h
152154 src/in.h
155+ src/libyang.h
156+ src/log .h
157+ src/out.h
153158 src/parser_data.h
154159 src/parser_schema.h
155160 src/plugins.h
156161 src/plugins_exts.h
157162 src/plugins_exts_compile.h
158163 src/plugins_exts_print.h
159164 src/plugins_types.h
160- src/out.h
161165 src/printer_data.h
162166 src/printer_schema.h
163167 src/set.h
164168 src/tree .h
165- src/tree_edit.h
166169 src/tree_data.h
170+ src/tree_edit.h
167171 src/tree_schema.h)
168172
173+ set (internal_headers
174+ src/common.h
175+ src/diff.h
176+ src/hash_table.h
177+ src/in_internal.h
178+ src/json.h
179+ src/lyb.h
180+ src/out_internal.h
181+ src/parser_internal.h
182+ src/path .h
183+ src/plugins_internal.h
184+ src/printer_internal.h
185+ src/schema_compile.h
186+ src/schema_compile_amend.h
187+ src/schema_compile_node.h
188+ src/schema_features.h
189+ src/tree_data_internal.h
190+ src/tree_schema_internal.h
191+ src/validation.h
192+ src/xml.h
193+ src/xpath.h)
194+
169195set (gen_headers
170196 src/version .h
171197 src/config.h)
@@ -195,17 +221,23 @@ set(format_sources
195221#
196222
197223if (("${BUILD_TYPE_UPPER} " STREQUAL "DEBUG" ) OR ("${BUILD_TYPE_UPPER} " STREQUAL "RELWITHDEBINFO" ))
198- option (ENABLE_BUILD_TESTS "Build tests" ON )
224+ option (ENABLE_TESTS "Build tests" ON )
199225 option (ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON )
200- # TODO enable when the internal docs ready
201- set (INTERNAL_DOCS NO )
202226else ()
203- option (ENABLE_BUILD_TESTS "Build tests" OFF )
227+ option (ENABLE_TESTS "Build tests" OFF )
204228 option (ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF )
205- set (INTERNAL_DOCS NO )
206229endif ()
230+ option (ENABLE_PERF_TESTS "Build performance tests" OFF )
207231option (ENABLE_COVERAGE "Build code coverage report from tests" OFF )
208232option (ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF )
233+ option (ENABLE_INTERNAL_DOCS "Generate doxygen documentation also from internal headers" OFF )
234+
235+ if (ENABLE_INTERNAL_DOCS)
236+ set (doxy_files ${doxy_files} ${internal_headers} )
237+ set (INTERNAL_DOCS YES )
238+ else ()
239+ set (INTERNAL_DOCS NO )
240+ endif ()
209241
210242set (LYD_VALUE_SIZE "24" CACHE STRING "Maximum size in bytes of data node values that do not need to be allocated dynamically, minimum is 8" )
211243if (LYD_VALUE_SIZE LESS 8)
@@ -215,42 +247,66 @@ set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang" CACHE
215247set (PLUGINS_DIR_EXTENSIONS "${PLUGINS_DIR} /extensions" CACHE STRING "Directory with libyang user extensions plugins" )
216248set (PLUGINS_DIR_TYPES "${PLUGINS_DIR} /types" CACHE STRING "Directory with libyang user types plugins" )
217249
218- if (ENABLE_COVERAGE)
219- if (NOT ENABLE_BUILD_TESTS)
220- message (WARNING "you cannot generage coverage when tests are disabled. Enable test by additing parameter -DENABLE_BUILD_TESTS=ON or run cmake in some debug mode" )
221- set (ENABLE_COVERAGE OFF )
222- endif ()
250+ # by default build shared library
251+ # static build requires static libpcre2 library
252+ option (ENABLE_STATIC "Build static (.a) library" OFF )
223253
224- find_program (PATH_GCOV NAMES gcov)
225- if (NOT PATH_GCOV)
226- message (WARNING "'gcov' executable not found! Disabling building code coverage report." )
227- set (ENABLE_COVERAGE OFF )
228- endif ()
254+ #
255+ # checks
256+ #
257+ if (ENABLE_STATIC)
258+ message (STATUS "Disabling tests for static build" )
259+ set (ENABLE_TESTS OFF )
260+ set (ENABLE_VALGRIND_TESTS OFF )
261+ endif ()
229262
230- find_program (PATH_LCOV NAMES lcov)
231- if (NOT PATH_LCOV)
232- message (WARNING "'lcov' executable not found! Disabling building code coverage report." )
233- set (ENABLE_COVERAGE OFF )
263+ if (ENABLE_VALGRIND_TESTS)
264+ if (NOT ENABLE_TESTS)
265+ message (WARNING "Tests are disabled! Disabling memory leak tests." )
266+ set (ENABLE_VALGRIND_TESTS OFF )
267+ else ()
268+ find_program (VALGRIND_FOUND valgrind)
269+ if (NOT VALGRIND_FOUND)
270+ message (WARNING "valgrind executable not found! Disabling memory leak tests." )
271+ set (ENABLE_VALGRIND_TESTS OFF )
272+ endif ()
234273 endif ()
274+ endif ()
235275
236- find_program (PATH_GENHTML NAMES genhtml)
237- if (NOT PATH_GENHTML)
238- message (WARNING "'genhtml' executable not found! Disabling building code coverage report." )
239- set (ENABLE_COVERAGE OFF )
276+ if (ENABLE_TESTS)
277+ find_package (CMocka 1.0.0)
278+ if (NOT CMOCKA_FOUND)
279+ message (STATUS "Disabling tests because of missing CMocka" )
280+ set (ENABLE_TESTS OFF )
240281 endif ()
282+ endif ()
241283
242- if (NOT CMAKE_COMPILER_IS_GNUCC)
243- message (WARNING "Compiler is not gcc! Coverage may break the tests!" )
284+ if (ENABLE_PERF_TESTS)
285+ find_path (VALGRIND_INCLUDE_DIR
286+ NAMES
287+ valgrind/callgrind.h
288+ PATHS
289+ /usr/include
290+ /usr/local/include
291+ /opt/local/include
292+ /sw/include
293+ ${CMAKE_INCLUDE_PATH}
294+ ${CMAKE_INSTALL_PREFIX} /include )
295+ if (VALGRIND_INCLUDE_DIR)
296+ set (HAVE_CALLGRIND 1)
297+ else ()
298+ message (STATUS "Disabling callgrind macros in performance tests because of missing valgrind headers" )
244299 endif ()
300+ endif ()
245301
246- if (ENABLE_COVERAGE)
247- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage" )
248- endif ()
302+ if (ENABLE_COVERAGE)
303+ gen_coverage_enable(${ENABLE_TESTS} )
249304endif ()
250305
251- # by default build shared library
252- # static build requires static libpcre2 library
253- option (ENABLE_STATIC "Build static (.a) library" OFF )
306+ if ("${BUILD_TYPE_UPPER} " STREQUAL "DEBUG" )
307+ # enable before adding tests to let them detect that format checking is available - one of the tests is format checking
308+ source_format_enable()
309+ endif ()
254310
255311# generate files
256312configure_file (${PROJECT_SOURCE_DIR} /src/config.h.in ${PROJECT_BINARY_DIR} /src/config.h @ONLY)
@@ -274,18 +330,18 @@ if(ENABLE_STATIC)
274330 add_definitions (-DSTATIC)
275331 set (CMAKE_EXE_LINKER_FLAGS -static )
276332 set (CMAKE_FIND_LIBRARY_SUFFIXES .a)
333+ set (CMAKE_LINK_SEARCH_START_STATIC TRUE )
277334 set (CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
278335 set (CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
279336 add_library (yang STATIC ${libsrc} ${compatsrc} )
280337else ()
281338 set (CMAKE_POSITION_INDEPENDENT_CODE TRUE )
282339 add_library (yangobj OBJECT ${libsrc} ${compatsrc} )
340+ set_target_properties (yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden" )
283341 add_library (yang SHARED $<TARGET_OBJECTS:yangobj>)
284342
285343 #link dl
286344 target_link_libraries (yang ${CMAKE_DL_LIBS} )
287-
288- set_target_properties (yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden" )
289345endif ()
290346
291347set_target_properties (yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION} )
@@ -325,8 +381,8 @@ if(PKG_CONFIG_FOUND)
325381 # check that pkg-config includes the used path
326382 execute_process (COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
327383 if (RETURN EQUAL 0)
328- string (STRIP "${PC_PATH} " PC_PATH)
329- set (PC_PATH "${PC_PATH} :$ENV{PKG_CONFIG_PATH} " )
384+ string (STRIP "${PC_PATH} " PC_PATH)
385+ set (PC_PATH "${PC_PATH} :$ENV{PKG_CONFIG_PATH} " )
330386 string (REGEX MATCH "${CMAKE_INSTALL_PREFIX} /${CMAKE_INSTALL_LIBDIR} /pkgconfig" SUBSTR "${PC_PATH} " )
331387 string (LENGTH "${SUBSTR} " SUBSTR_LEN)
332388 if (SUBSTR_LEN EQUAL 0)
@@ -335,28 +391,10 @@ if(PKG_CONFIG_FOUND)
335391 endif ()
336392endif ()
337393
338- if (ENABLE_BUILD_TESTS)
339- find_package (CMocka 1.0.0)
340- endif (ENABLE_BUILD_TESTS)
341-
342- if ("${BUILD_TYPE_UPPER} " STREQUAL "DEBUG" )
343- # enable before adding tests to let them detect that format checking is available - one of the tests is format checking
344- source_format_enable()
345- endif ()
346-
347394# tests
348- if (ENABLE_VALGRIND_TESTS)
349- set (ENABLE_BUILD_TESTS ON )
350- endif ()
351-
352- if (ENABLE_BUILD_TESTS)
353- if (CMOCKA_FOUND)
354- enable_testing ()
355- add_subdirectory (tests)
356- else ()
357- message (STATUS "Disabling tests because of missing CMocka" )
358- set (ENABLE_BUILD_TESTS OFF )
359- endif ()
395+ if (ENABLE_TESTS OR ENABLE_PERF_TESTS)
396+ enable_testing ()
397+ add_subdirectory (tests)
360398endif ()
361399
362400if (ENABLE_FUZZ_TARGETS)
@@ -369,6 +407,9 @@ if(ENABLE_FUZZ_TARGETS)
369407 endif ()
370408endif ()
371409
410+ # create coverage target for generating coverage reports
411+ gen_coverage("utest_.*" "utest_.*_valgrind" )
412+
372413# tools - yanglint, yangre
373414add_subdirectory (tools)
374415
@@ -377,13 +418,16 @@ gen_doc("${doxy_files}" ${LIBYANG_VERSION} ${LIBYANG_DESCRIPTION} ${project_logo
377418
378419# generate API/ABI report
379420if ("${BUILD_TYPE_UPPER} " STREQUAL "ABICHECK" )
380- lib_abi_check(yang "${headers} " ${LIBYANG_SOVERSION_FULL} b45499b88104d43979f3e2e2a1cf8eba686f8585 )
421+ lib_abi_check(yang "${headers} " ${LIBYANG_SOVERSION_FULL} 4de7d0740b95feb6f540c645c2dad007e3ea7bdc )
381422endif ()
382423
383424# source code format target for Makefile
384425# - add it after tests which may also update list of sources to format
385426source_format(${format_sources} )
386427
428+ # uninstall
429+ add_custom_target (uninstall "${CMAKE_COMMAND} " -P "${CMAKE_MODULE_PATH} /uninstall.cmake" )
430+
387431# clean cmake cache
388432add_custom_target (cclean
389433 COMMAND make clean
0 commit comments