forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
491 lines (430 loc) · 15.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
if(WIN32)
cmake_minimum_required(VERSION 3.22.0)
else()
cmake_minimum_required(VERSION 2.8.12...3.28.1)
endif()
# force out-of-source build
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
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.")
endif()
project(libyang C)
# include custom Modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
include(GNUInstallDirs)
include(UseCompat)
include(ABICheck)
include(SourceFormat)
include(GenDoc)
include(GenCoverage)
# set default build type if not specified by user
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# normalize build type string
# see https://github.com/CESNET/libyang/pull/1692 for why CMAKE_C_FLAGS_<type> are not used directly
string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_UPPER)
if ("${BUILD_TYPE_UPPER}" STREQUAL "RELEASE")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build Type" FORCE)
set(CMAKE_C_FLAGS "-DNDEBUG -O2 ${CMAKE_C_FLAGS}")
elseif("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build Type" FORCE)
set(CMAKE_C_FLAGS "-g3 -O0 ${CMAKE_C_FLAGS}")
elseif("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build Type" FORCE)
elseif("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBUG")
set(CMAKE_BUILD_TYPE "RelWithDebug" CACHE STRING "Build Type" FORCE)
elseif("${BUILD_TYPE_UPPER}" STREQUAL "ABICHECK")
set(CMAKE_BUILD_TYPE "ABICheck" CACHE STRING "Build Type" FORCE)
set(CMAKE_C_FLAGS "-g -Og ${CMAKE_C_FLAGS}")
elseif("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY")
set(CMAKE_BUILD_TYPE "DocOnly" CACHE STRING "Build Type" FORCE)
endif()
#
# variables
#
set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
# Correct RPATH usage on OS X
set(CMAKE_MACOSX_RPATH TRUE)
# keep all binaries in the build directory
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
# set version of the project
set(LIBYANG_MAJOR_VERSION 3)
set(LIBYANG_MINOR_VERSION 1)
set(LIBYANG_MICRO_VERSION 0)
set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
# set version of the library
set(LIBYANG_MAJOR_SOVERSION 3)
set(LIBYANG_MINOR_SOVERSION 2)
set(LIBYANG_MICRO_SOVERSION 0)
set(LIBYANG_SOVERSION_FULL ${LIBYANG_MAJOR_SOVERSION}.${LIBYANG_MINOR_SOVERSION}.${LIBYANG_MICRO_SOVERSION})
set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_SOVERSION})
if(WIN32)
set(C_STANDARD 11)
set(C_STANDARD_REQUIRED ON)
set(CMAKE_C_FLAGS "/Zc:preprocessor /W3 /wd4711 /w14013 /utf-8 ${CMAKE_C_FLAGS}")
else()
# global C flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic -std=c11")
endif()
include_directories(${PROJECT_BINARY_DIR}/libyang ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/src/plugins_exts)
# type plugins are separate because they have their documentation generated
set(type_plugins
src/plugins_types/binary.c
src/plugins_types/bits.c
src/plugins_types/boolean.c
src/plugins_types/decimal64.c
src/plugins_types/empty.c
src/plugins_types/enumeration.c
src/plugins_types/identityref.c
src/plugins_types/instanceid.c
src/plugins_types/instanceid_keys.c
src/plugins_types/integer.c
src/plugins_types/leafref.c
src/plugins_types/lyds_tree.c
src/plugins_types/string.c
src/plugins_types/union.c
src/plugins_types/ipv4_address.c
src/plugins_types/ipv4_address_no_zone.c
src/plugins_types/ipv6_address.c
src/plugins_types/ipv6_address_no_zone.c
src/plugins_types/ipv4_prefix.c
src/plugins_types/ipv6_prefix.c
src/plugins_types/date_and_time.c
src/plugins_types/hex_string.c
src/plugins_types/xpath1.0.c
src/plugins_types/node_instanceid.c)
set(libsrc
src/ly_common.c
src/log.c
src/hash_table.c
src/dict.c
src/set.c
src/path.c
src/diff.c
src/context.c
src/json.c
src/tree_data.c
src/tree_data_free.c
src/tree_data_common.c
src/tree_data_hash.c
src/tree_data_new.c
src/parser_xml.c
src/parser_json.c
src/parser_lyb.c
src/out.c
src/printer_data.c
src/printer_xml.c
src/printer_json.c
src/printer_lyb.c
src/schema_compile.c
src/schema_compile_node.c
src/schema_compile_amend.c
src/schema_features.c
src/tree_data_sorted.c
src/tree_schema.c
src/tree_schema_free.c
src/tree_schema_common.c
src/in.c
src/lyb.c
src/parser_common.c
src/parser_yang.c
src/parser_yin.c
src/printer_schema.c
src/printer_yang.c
src/printer_yin.c
src/printer_tree.c
src/plugins.c
src/plugins_types.c
src/plugins_exts.c
src/plugins_exts/metadata.c
src/plugins_exts/nacm.c
src/plugins_exts/yangdata.c
src/plugins_exts/schema_mount.c
src/plugins_exts/structure.c
src/xml.c
src/xpath.c
src/validation.c
${type_plugins})
set(headers
src/context.h
src/hash_table.h
src/dict.h
src/in.h
src/libyang.h
src/log.h
src/out.h
src/parser_data.h
src/parser_schema.h
src/plugins.h
src/plugins_exts.h
src/plugins_exts/metadata.h
src/plugins_types.h
src/printer_data.h
src/printer_schema.h
src/set.h
src/tree.h
src/tree_data.h
src/tree_edit.h
src/tree_schema.h)
set(internal_headers
src/ly_common.h
src/diff.h
src/hash_table_internal.h
src/in_internal.h
src/json.h
src/lyb.h
src/out_internal.h
src/parser_internal.h
src/path.h
src/plugins_internal.h
src/printer_internal.h
src/schema_compile.h
src/schema_compile_amend.h
src/schema_compile_node.h
src/schema_features.h
src/tree_data_internal.h
src/tree_schema_internal.h
src/validation.h
src/xml.h
src/xpath.h)
set(gen_headers
version.h
ly_config.h)
# files to generate doxygen from
set(doxy_files
doc/build.dox
doc/transition_1_2.dox
doc/transition_2_3.dox
${headers}
${PROJECT_BINARY_DIR}/libyang/version.h
${type_plugins})
# project (doxygen) logo
set(project_logo
doc/logo.png)
# source files to be covered by the 'format' target
set(format_sources
compat/*.c
compat/*.h*
src/*.c
src/*.h
src/plugins_exts/*
src/plugins_types/*)
#
# options
#
if(("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG") OR ("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO"))
option(ENABLE_TESTS "Build tests" ON)
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
else()
option(ENABLE_TESTS "Build tests" OFF)
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
endif()
option(ENABLE_PERF_TESTS "Build performance tests" OFF)
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
option(ENABLE_INTERNAL_DOCS "Generate doxygen documentation also from internal headers" OFF)
option(ENABLE_YANGLINT_INTERACTIVE "Enable interactive CLI yanglint" ON)
option(ENABLE_TOOLS "Build binary tools 'yanglint' and 'yangre'" ON)
option(ENABLE_COMMON_TARGETS "Define common custom target names such as 'doc' or 'uninstall', may cause conflicts when using add_subdirectory() to build this project" ON)
option(BUILD_SHARED_LIBS "By default, shared libs are enabled. Turn off for a static build." ON)
set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules/libyang" CACHE STRING "Directory where to copy the YANG modules to")
if(ENABLE_INTERNAL_DOCS)
set(doxy_files ${doxy_files} ${internal_headers})
set(INTERNAL_DOCS YES)
else()
set(INTERNAL_DOCS NO)
endif()
set(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")
if(LYD_VALUE_SIZE LESS 8)
message(FATAL_ERROR "Data node value size \"${LYD_VALUE_SIZE}\" is not valid.")
endif()
set(PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang" CACHE STRING "Directory with libyang plugins (extensions and user types)")
set(PLUGINS_DIR_EXTENSIONS "${PLUGINS_DIR}/extensions" CACHE STRING "Directory with libyang user extensions plugins")
set(PLUGINS_DIR_TYPES "${PLUGINS_DIR}/types" CACHE STRING "Directory with libyang user types plugins")
#
# checks
#
if(NOT BUILD_SHARED_LIBS)
message(STATUS "Disabling tests for static build")
set(ENABLE_TESTS OFF)
set(ENABLE_VALGRIND_TESTS OFF)
endif()
if(ENABLE_VALGRIND_TESTS)
if(NOT ENABLE_TESTS)
message(WARNING "Tests are disabled! Disabling memory leak tests.")
set(ENABLE_VALGRIND_TESTS OFF)
else()
find_program(VALGRIND_FOUND valgrind)
if(NOT VALGRIND_FOUND)
message(WARNING "valgrind executable not found! Disabling memory leak tests.")
set(ENABLE_VALGRIND_TESTS OFF)
endif()
endif()
endif()
if(ENABLE_TESTS)
find_package(CMocka 1.0.1)
if(NOT CMOCKA_FOUND)
message(STATUS "Disabling tests because of missing CMocka")
set(ENABLE_TESTS OFF)
endif()
endif()
if(ENABLE_PERF_TESTS)
find_path(VALGRIND_INCLUDE_DIR
NAMES
valgrind/callgrind.h
PATHS
/usr/include
/usr/local/include
/opt/local/include
/sw/include
${CMAKE_INCLUDE_PATH}
${CMAKE_INSTALL_PREFIX}/include)
if(VALGRIND_INCLUDE_DIR)
set(HAVE_CALLGRIND 1)
else()
message(STATUS "Disabling callgrind macros in performance tests because of missing valgrind headers")
endif()
endif()
if(ENABLE_COVERAGE)
gen_coverage_enable(${ENABLE_TESTS})
endif()
if ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
# enable before adding tests to let them detect that format checking is available - one of the tests is format checking
source_format_enable(0.77)
endif()
# generate and copy all public header files
configure_file(${PROJECT_SOURCE_DIR}/src/ly_config.h.in ${PROJECT_BINARY_DIR}/libyang/ly_config.h @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/src/version.h.in ${PROJECT_BINARY_DIR}/libyang/version.h @ONLY)
file(COPY ${headers} DESTINATION ${PROJECT_BINARY_DIR}/libyang)
# DOC-only target with no extra dependencies
if("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY")
gen_doc("${doxy_files}" ${LIBYANG_VERSION} ${LIBYANG_DESCRIPTION} ${project_logo})
return()
endif()
#
# targets
#
# link compat
use_compat()
# create static libyang library
if(NOT BUILD_SHARED_LIBS)
add_definitions(-DSTATIC)
# allow binaries compilation linking both static and dynamic libraries never linking static glibc
#set(CMAKE_EXE_LINKER_FLAGS -static)
# prefer static libraries
set(CMAKE_FIND_LIBRARY_SUFFIXES .a;.so)
set(CMAKE_LINK_SEARCH_START_STATIC TRUE)
set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
add_library(yang STATIC ${libsrc} ${compatsrc})
else()
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
add_library(yangobj OBJECT ${libsrc} ${compatsrc})
if(NOT WIN32)
set_target_properties(yangobj PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
endif()
target_compile_definitions(yangobj PRIVATE LIBYANG_BUILD)
add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
if(WIN32)
find_package(dlfcn-win32 REQUIRED)
set(CMAKE_DL_LIBS dlfcn-win32::dl)
endif()
#link dl
target_link_libraries(yang ${CMAKE_DL_LIBS})
endif()
if(WIN32)
find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED)
message(STATUS "Found <dirent.h> at ${DIRENT_INCLUDE_DIR}")
set(COMPAT_POSIX_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/compat/posix-shims
${DIRENT_INCLUDE_DIR})
if(TARGET yangobj)
target_include_directories(yangobj PRIVATE ${COMPAT_POSIX_INCLUDES})
endif()
target_include_directories(yang PRIVATE ${COMPAT_POSIX_INCLUDES})
include_directories(${COMPAT_POSIX_INCLUDES})
find_package(pthreads REQUIRED)
set(COMPAT_WIN_LIBRARIES PThreads4W::PThreads4W shlwapi.lib ws2_32)
target_link_libraries(yang ${COMPAT_WIN_LIBRARIES})
endif()
set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
# link math
if(NOT WIN32)
target_link_libraries(yang m)
endif()
# find pthreads
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
# find PCRE2 library
unset(PCRE2_LIBRARY CACHE)
find_package(PCRE2 10.21 REQUIRED)
include_directories(${PCRE2_INCLUDE_DIRS})
target_link_libraries(yang ${PCRE2_LIBRARIES})
# generated header list
foreach(h IN LISTS gen_headers)
list(APPEND g_headers ${PROJECT_BINARY_DIR}/libyang/${h})
endforeach()
# install the modules
install(DIRECTORY "${PROJECT_SOURCE_DIR}/models/" DESTINATION ${YANG_MODULE_DIR} FILES_MATCHING PATTERN "*.yang")
# install all library files
install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${headers} ${g_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
# generate and install pkg-config file
configure_file("libyang.pc.in" "libyang.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
# check that pkg-config includes the used path
execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
if(RETURN EQUAL 0)
string(STRIP "${PC_PATH}" PC_PATH)
set(PC_PATH "${PC_PATH}:$ENV{PKG_CONFIG_PATH}")
string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
string(LENGTH "${SUBSTR}" SUBSTR_LEN)
if(SUBSTR_LEN EQUAL 0)
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\".")
endif()
endif()
endif()
# tests
if(ENABLE_TESTS OR ENABLE_PERF_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
if(ENABLE_FUZZ_TARGETS)
set(FUZZER "AFL" CACHE STRING "fuzzer type")
if(FUZZER STREQUAL "LibFuzzer")
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "LibFuzzer works only with clang")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined -fno-omit-frame-pointer")
endif()
endif()
# create coverage target for generating coverage reports
gen_coverage("utest_.*" "utest_.*_valgrind")
# tools - yanglint, yangre
if(ENABLE_TOOLS)
add_subdirectory(tools)
endif()
# generate doxygen documentation for libyang API
if(ENABLE_COMMON_TARGETS)
gen_doc("${doxy_files}" ${LIBYANG_VERSION} ${LIBYANG_DESCRIPTION} ${project_logo})
endif()
# generate API/ABI report
if("${BUILD_TYPE_UPPER}" STREQUAL "ABICHECK")
lib_abi_check(yang "${headers}" ${LIBYANG_SOVERSION_FULL} 708a8bfad983a4095c8d4dc537a9d2a1d5ca65c5)
endif()
# source code format target for Makefile
# - add it after tests which may also update list of sources to format
source_format(${format_sources})
# uninstall
if(ENABLE_COMMON_TARGETS)
add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/uninstall.cmake")
endif()
# clean cmake cache
add_custom_target(cclean
COMMAND make clean
COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
COMMAND rm -rf Makefile Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})