blob: 5d5dbd8cfe50d09416a5ef5966dea321fa556971 [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
Michal Vasko5aa44c02020-06-29 11:47:02 +020010# include custom Modules
11set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
12
Radek Krejci2c22f122018-09-05 15:08:03 +020013include(GNUInstallDirs)
14include(CheckSymbolExists)
Michal Vasko5aa44c02020-06-29 11:47:02 +020015include(UseCompat)
Radek Krejcib2258822020-07-14 08:39:30 +020016include(ABICheck)
Radek Krejcia198c962020-08-16 10:32:10 +020017include(SourceFormat)
Radek Krejci88ad4be2020-10-03 12:27:05 +020018include(Doc)
19
20# set default build type if not specified by user
21if(NOT CMAKE_BUILD_TYPE)
22 set(CMAKE_BUILD_TYPE Debug)
23endif()
24# normalize build type string
25string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_UPPER)
26if ("${BUILD_TYPE_UPPER}" STREQUAL "RELEASE")
27 set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build Type" FORCE)
28elseif ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
29 set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build Type" FORCE)
30elseif ("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO")
31 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build Type" FORCE)
32elseif ("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBUG")
33 set(CMAKE_BUILD_TYPE "RelWithDebug" CACHE STRING "Build Type" FORCE)
34elseif ("${BUILD_TYPE_UPPER}" STREQUAL "ABICHECK")
35 set(CMAKE_BUILD_TYPE "ABICheck" CACHE STRING "Build Type" FORCE)
36elseif ("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY")
37 set(CMAKE_BUILD_TYPE "DocOnly" CACHE STRING "Build Type" FORCE)
38else ()
39 message(FATAL_ERROR "Unknown CMAKE_BUILD_TYPE \"${CMAKE_BUILD_TYPE}\".")
40endif ()
41
42# check the supported platform
43if(NOT UNIX)
44 message(FATAL_ERROR "Only *nix like systems are supported.")
45endif()
46
47#
48# variables
49#
Radek Krejci2c22f122018-09-05 15:08:03 +020050
51set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
52
53# Correct RPATH usage on OS X
54set(CMAKE_MACOSX_RPATH TRUE)
55
Radek Krejcicb293a32020-11-13 15:36:29 +010056# keep all binaries in the build directory
57set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
58
Radek Krejcia43d69d2019-04-04 10:34:46 +020059# set version of the project
60set(LIBYANG_MAJOR_VERSION 2)
Radek Krejci2c22f122018-09-05 15:08:03 +020061set(LIBYANG_MINOR_VERSION 0)
62set(LIBYANG_MICRO_VERSION 0)
63set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
Radek Krejcia43d69d2019-04-04 10:34:46 +020064# set version of the library
65set(LIBYANG_MAJOR_SOVERSION 2)
66set(LIBYANG_MINOR_SOVERSION 0)
67set(LIBYANG_MICRO_SOVERSION 0)
68set(LIBYANG_SOVERSION_FULL ${LIBYANG_MAJOR_SOVERSION}.${LIBYANG_MINOR_SOVERSION}.${LIBYANG_MICRO_SOVERSION})
69set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_SOVERSION})
Radek Krejci2c22f122018-09-05 15:08:03 +020070
Radek Krejci2c22f122018-09-05 15:08:03 +020071if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
72 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
73 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
74elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
75 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x __attribute__((__unused__))")
76 set(COMPILER_PACKED_ATTR "__attribute__((__packed__))")
77else()
78 set(COMPILER_UNUSED_ATTR "UNUSED_ ## x")
79 set(COMPILER_PACKED_ATTR "")
80endif()
81
Radek Išaa1395bb2020-11-24 21:30:50 +010082set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-missing-field-initializers -std=c99")
Radek Krejcib2258822020-07-14 08:39:30 +020083set(CMAKE_C_FLAGS_DEBUG "-g3 -O0")
84set(CMAKE_C_FLAGS_ABICHECK "-g -Og")
Radek Krejci8c1054b2019-05-17 10:18:54 +020085
Radek Krejci2c22f122018-09-05 15:08:03 +020086include_directories(${PROJECT_BINARY_DIR}/src ${PROJECT_SOURCE_DIR}/src)
Radek Krejci2c22f122018-09-05 15:08:03 +020087
Radek Krejci968d7552021-03-26 20:33:51 +010088configure_file(${PROJECT_SOURCE_DIR}/src/config.h.in ${PROJECT_BINARY_DIR}/src/config.h @ONLY)
89configure_file(${PROJECT_SOURCE_DIR}/src/version.h.in ${PROJECT_BINARY_DIR}/src/version.h @ONLY)
90
Radek Krejci2c22f122018-09-05 15:08:03 +020091set(libsrc
Michal Vasko1324b6c2018-09-07 11:16:23 +020092 src/common.c
Radek Krejci2c22f122018-09-05 15:08:03 +020093 src/log.c
94 src/hash_table.c
Radek Krejciad573502018-09-07 15:26:55 +020095 src/set.c
Michal Vasko004d3152020-06-11 19:59:22 +020096 src/path.c
Michal Vaskod59035b2020-07-08 12:00:06 +020097 src/diff.c
Radek Krejcid4557c62018-09-17 11:42:09 +020098 src/context.c
Radek Krejci50f0c6b2020-06-18 16:31:48 +020099 src/json.c
Radek Krejcie7b95092019-05-15 11:03:07 +0200100 src/tree_data.c
101 src/tree_data_free.c
102 src/tree_data_helpers.c
103 src/tree_data_hash.c
104 src/parser_xml.c
Michal Vasko90932a92020-02-12 14:33:03 +0100105 src/parser_json.c
Michal Vasko60ea6352020-06-29 13:39:39 +0200106 src/parser_lyb.c
Michal Vaskoafac7822020-10-20 14:22:26 +0200107 src/out.c
Radek Krejcie7b95092019-05-15 11:03:07 +0200108 src/printer_data.c
109 src/printer_xml.c
Radek Krejci13a57b62019-07-19 13:04:09 +0200110 src/printer_json.c
Michal Vasko60ea6352020-06-29 13:39:39 +0200111 src/printer_lyb.c
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200112 src/schema_compile.c
113 src/schema_compile_node.c
114 src/schema_compile_amend.c
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100115 src/schema_features.c
Radek Krejci3f5e3db2018-10-11 15:57:47 +0200116 src/tree_schema.c
Radek Krejci19a96102018-11-15 13:38:09 +0100117 src/tree_schema_free.c
Radek Krejci86d106e2018-10-18 09:53:19 +0200118 src/tree_schema_helpers.c
Michal Vaskoafac7822020-10-20 14:22:26 +0200119 src/in.c
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200120 src/parser_yang.c
David Sedlák3b4db242018-10-19 16:11:01 +0200121 src/parser_yin.c
Radek Krejci335332a2019-09-05 13:03:35 +0200122 src/parser_stmt.c
Radek Krejcie7b95092019-05-15 11:03:07 +0200123 src/printer_schema.c
Radek Krejcid3ca0632019-04-16 16:54:54 +0200124 src/printer_yang.c
FredGand944bdc2019-11-05 21:57:07 +0800125 src/printer_yin.c
aPiecek61d062b2020-11-02 11:05:09 +0100126 src/printer_tree.c
Radek Krejci3e6632f2021-03-22 22:08:21 +0100127 src/plugins.c
Radek Krejcie7b95092019-05-15 11:03:07 +0200128 src/plugins_types.c
Radek Krejcif1ca0ac2021-04-12 16:00:06 +0200129 src/plugins_types/binary.c
130 src/plugins_types/bits.c
131 src/plugins_types/boolean.c
132 src/plugins_types/decimal64.c
133 src/plugins_types/empty.c
134 src/plugins_types/enumeration.c
135 src/plugins_types/identityref.c
136 src/plugins_types/instanceid.c
137 src/plugins_types/integer.c
138 src/plugins_types/leafref.c
139 src/plugins_types/string.c
140 src/plugins_types/union.c
Radek Krejci0935f412019-08-20 16:15:18 +0200141 src/plugins_exts.c
Radek Krejcif1ca0ac2021-04-12 16:00:06 +0200142 src/plugins_exts/metadata.c
143 src/plugins_exts/nacm.c
144 src/plugins_exts/yangdata.c
Radek Krejcib1646a92018-11-02 16:08:26 +0100145 src/xml.c
Michal Vaskocde73ac2019-11-14 16:10:27 +0100146 src/xpath.c
147 src/validation.c)
Radek Krejci2c22f122018-09-05 15:08:03 +0200148
Radek Krejci2c22f122018-09-05 15:08:03 +0200149set(headers
150 src/libyang.h
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200151 src/context.h
Radek Krejcica376bd2020-06-11 16:04:06 +0200152 src/dict.h
153 src/log.h
Michal Vaskoafac7822020-10-20 14:22:26 +0200154 src/in.h
Michal Vasko9eb2a372020-07-14 12:18:12 +0200155 src/parser_data.h
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200156 src/parser_schema.h
Radek Krejci3e6632f2021-03-22 22:08:21 +0100157 src/plugins.h
158 src/plugins_exts.h
159 src/plugins_exts_compile.h
160 src/plugins_exts_print.h
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200161 src/plugins_types.h
Michal Vaskoafac7822020-10-20 14:22:26 +0200162 src/out.h
Radek Krejcie7b95092019-05-15 11:03:07 +0200163 src/printer_data.h
Radek Krejcid3ca0632019-04-16 16:54:54 +0200164 src/printer_schema.h
Radek Krejcica376bd2020-06-11 16:04:06 +0200165 src/set.h
166 src/tree.h
167 src/tree_data.h
168 src/tree_schema.h)
Radek Krejci2c22f122018-09-05 15:08:03 +0200169
Radek Krejcicb293a32020-11-13 15:36:29 +0100170# source files to be covered by the 'format' target
171set(format_sources
172 compat/*
173 src/*)
Radek Krejci88ad4be2020-10-03 12:27:05 +0200174#
175# options
176#
177
178if("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY")
179 libyang_doc()
180 return()
181endif()
182
183if(("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG") OR ("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO"))
184 option(ENABLE_BUILD_TESTS "Build tests" ON)
185 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
Radek Krejci7132fc52020-10-26 14:34:06 +0100186 # TODO enable when the internal docs ready
Radek Krejci685ef862020-11-05 12:55:26 +0100187 set(INTERNAL_DOCS NO)
Radek Krejci88ad4be2020-10-03 12:27:05 +0200188else()
189 option(ENABLE_BUILD_TESTS "Build tests" OFF)
190 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
Radek Krejci685ef862020-11-05 12:55:26 +0100191 set(INTERNAL_DOCS NO)
Radek Krejci88ad4be2020-10-03 12:27:05 +0200192endif()
193option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
194option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
Radek Krejci88ad4be2020-10-03 12:27:05 +0200195
Radek Krejci968d7552021-03-26 20:33:51 +0100196set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang" CACHE STRING "Directory with libyang plugins (extensions and user types)")
197set(PLUGINS_DIR_EXTENSIONS "${PLUGINS_DIR}/extensions" CACHE STRING "Directory with libyang user extensions plugins")
198set(PLUGINS_DIR_TYPES "${PLUGINS_DIR}/types" CACHE STRING "Directory with libyang user types plugins")
Radek Krejci88ad4be2020-10-03 12:27:05 +0200199
Radek Krejci88ad4be2020-10-03 12:27:05 +0200200if(ENABLE_COVERAGE)
Radek Išaabec96f2020-11-25 15:46:49 +0100201 if(NOT ENABLE_BUILD_TESTS)
202 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")
203 set(ENABLE_COVERAGE OFF)
204 endif()
205
Radek Krejci88ad4be2020-10-03 12:27:05 +0200206 find_program(PATH_GCOV NAMES gcov)
207 if(NOT PATH_GCOV)
208 message(WARNING "'gcov' executable not found! Disabling building code coverage report.")
209 set(ENABLE_COVERAGE OFF)
210 endif()
211
212 find_program(PATH_LCOV NAMES lcov)
213 if(NOT PATH_LCOV)
214 message(WARNING "'lcov' executable not found! Disabling building code coverage report.")
215 set(ENABLE_COVERAGE OFF)
216 endif()
217
218 find_program(PATH_GENHTML NAMES genhtml)
219 if(NOT PATH_GENHTML)
220 message(WARNING "'genhtml' executable not found! Disabling building code coverage report.")
221 set(ENABLE_COVERAGE OFF)
222 endif()
223
224 if(NOT CMAKE_COMPILER_IS_GNUCC)
225 message(WARNING "Compiler is not gcc! Coverage may break the tests!")
226 endif()
227
228 if(ENABLE_COVERAGE)
Radek Išaa1395bb2020-11-24 21:30:50 +0100229 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
Radek Krejci88ad4be2020-10-03 12:27:05 +0200230 endif()
231endif()
232
233# by default build shared library
234# static build requires static libpcre2 library
235option(ENABLE_STATIC "Build static (.a) library" OFF)
236
237#
238# targets
239#
240
Michal Vasko5aa44c02020-06-29 11:47:02 +0200241# link compat
242use_compat()
243
Radek Krejci2c22f122018-09-05 15:08:03 +0200244# create static libyang library
245if(ENABLE_STATIC)
246 add_definitions(-DSTATIC)
247 set(CMAKE_EXE_LINKER_FLAGS -static)
248 set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
249 set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
250 set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200251 add_library(yang STATIC ${libsrc} $<TARGET_OBJECTS:compat>)
Radek Krejci2c22f122018-09-05 15:08:03 +0200252else()
253 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
254 add_library(yangobj OBJECT ${libsrc})
Michal Vasko5aa44c02020-06-29 11:47:02 +0200255 add_library(yang SHARED $<TARGET_OBJECTS:yangobj> $<TARGET_OBJECTS:compat>)
Radek Krejci2c22f122018-09-05 15:08:03 +0200256
257 #link dl
258 target_link_libraries(yang ${CMAKE_DL_LIBS})
Michal Vasko90932a92020-02-12 14:33:03 +0100259
Juraj Vijtiukdd53e662019-11-15 13:35:33 +0100260 set_target_properties(yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vasko5aa44c02020-06-29 11:47:02 +0200261endif()
Radek Krejci2c22f122018-09-05 15:08:03 +0200262
Radek Krejcia43d69d2019-04-04 10:34:46 +0200263set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejci2c22f122018-09-05 15:08:03 +0200264
265# link math
266target_link_libraries(yang m)
267
268# find pthreads
269set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
270find_package(Threads REQUIRED)
271if(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})
Michal Vasko5aa44c02020-06-29 11:47:02 +0200275endif()
Radek Krejci2c22f122018-09-05 15:08:03 +0200276
Radek Krejci54579462019-04-30 12:47:06 +0200277# find PCRE2 library
278unset(PCRE2_LIBRARY CACHE)
Christian Hoppsdafed222021-03-22 13:02:42 -0400279find_package(PCRE2 10.21 REQUIRED)
Radek Krejci54579462019-04-30 12:47:06 +0200280include_directories(${PCRE2_INCLUDE_DIRS})
281target_link_libraries(yang ${PCRE2_LIBRARIES})
Radek Krejci2c22f122018-09-05 15:08:03 +0200282
283install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
Radek Krejciaa734522020-08-20 15:33:07 +0200284install(FILES ${headers} ${PROJECT_BINARY_DIR}/src/version.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejci2c22f122018-09-05 15:08:03 +0200285
286find_package(PkgConfig)
287if(PKG_CONFIG_FOUND)
288 # 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)
294 string(STRIP "${PC_PATH}" PC_PATH)
295 set(PC_PATH "${PC_PATH}:$ENV{PKG_CONFIG_PATH}")
296 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()
302endif()
303
Radek Krejcie84f12f2018-09-18 14:11:50 +0200304if(ENABLE_BUILD_TESTS)
305 find_package(CMocka 1.0.0)
Radek Krejcie84f12f2018-09-18 14:11:50 +0200306endif(ENABLE_BUILD_TESTS)
Radek Krejci2c22f122018-09-05 15:08:03 +0200307
Radek Krejcicb293a32020-11-13 15:36:29 +0100308# tests
Radek Krejcie84f12f2018-09-18 14:11:50 +0200309if(ENABLE_VALGRIND_TESTS)
310 set(ENABLE_BUILD_TESTS ON)
311endif()
Radek Krejci2c22f122018-09-05 15:08:03 +0200312
Radek Krejcie84f12f2018-09-18 14:11:50 +0200313if(ENABLE_BUILD_TESTS)
314 if(CMOCKA_FOUND)
315 enable_testing()
316 add_subdirectory(tests)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200317 else()
Radek Krejcie84f12f2018-09-18 14:11:50 +0200318 message(STATUS "Disabling tests because of missing CMocka")
Radek Išaabec96f2020-11-25 15:46:49 +0100319 set(ENABLE_BUILD_TESTS OFF)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200320 endif()
321endif()
Radek Krejci2c22f122018-09-05 15:08:03 +0200322
Juraj Vijtiuk04f71272019-08-07 06:49:00 -0400323if(ENABLE_FUZZ_TARGETS)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200324 set(FUZZER "AFL" CACHE STRING "fuzzer type")
325 if(FUZZER STREQUAL "LibFuzzer")
326 if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
327 message(FATAL_ERROR "LibFuzzer works only with clang")
328 endif()
329 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer")
330 endif()
Michal Vasko5aa44c02020-06-29 11:47:02 +0200331endif()
Radek Krejcie868acc2020-11-13 16:42:58 +0100332
333# tools - yanglint, yangre
334add_subdirectory(tools)
335
336# generate doxygen documentation for libyang API
337libyang_doc()
338
339# generate API/ABI report
340if ("${BUILD_TYPE_UPPER}" STREQUAL "ABICHECK")
341 libyang_abicheck()
342endif()
343
344# source code format
345if ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
346 source_format(${format_sources})
347endif()
348
349# clean cmake cache
350add_custom_target(cclean
351 COMMAND make clean
352 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
353 COMMAND rm -rf Makefile Doxyfile
354 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
355