From 725b04cc76c7137d993138cefe0df07b3e885a3e Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:35:42 +0100 Subject: [PATCH 01/10] pkg-config BUGFIX private deps in Libs.private libyang-dev no longer depends on libpcre2-dev, but libyang.pc kept libpcre2-8 and libxxhash in Requires.private. pkg-config resolves Requires.private even for --cflags, so pkg-config --cflags libyang failed on a system without those -dev packages. The private dependencies are now listed only in Libs.private, which needs no .pc files and is used only for static linking. The file also lists libcbor, libm, libdl and threads, which were missing, works with absolute CMAKE_INSTALL_INCLUDEDIR/LIBDIR and is generated always, the pkg-config binary is not needed for that. The warning about PKG_CONFIG_PATH needed the binary and is dropped. --- CMakeLists.txt | 45 +++++++++++++++++++++++++++++---------------- libyang.pc.in | 7 +++---- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f45fd92287..3935803e18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -477,7 +477,6 @@ endif() find_package(XXHash) if(XXHASH_FOUND) add_definitions(-DLY_XXHASH_SUPPORT) - set(PC_XXHASH_LIB libxxhash) include_directories(${XXHASH_INCLUDE_DIR}) target_link_libraries(yang ${XXHASH_LIBRARY}) message(STATUS "Hash algorithm: xxhash") @@ -501,23 +500,37 @@ install(DIRECTORY "${PROJECT_SOURCE_DIR}/modules/" DESTINATION ${YANG_MODULE_DIR 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() +# generate and install pkg-config file +if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") + set(PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}") +else() + set(PC_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") +endif() +if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}") + set(PC_LIBDIR "${CMAKE_INSTALL_LIBDIR}") +else() + set(PC_LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}") +endif() +# dependencies only in Libs.private, Requires.private would need their .pc files even for dynamic linking +set(PC_LIBS_PRIVATE "-lpcre2-8") +if(XXHASH_FOUND) + string(APPEND PC_LIBS_PRIVATE " -lxxhash") +endif() +if(CBOR_FOUND) + string(APPEND PC_LIBS_PRIVATE " -lcbor") +endif() +if(NOT WIN32) + string(APPEND PC_LIBS_PRIVATE " -lm") + # only the shared library loads plugins + if(BUILD_SHARED_LIBS AND CMAKE_DL_LIBS) + string(APPEND PC_LIBS_PRIVATE " -l${CMAKE_DL_LIBS}") endif() endif() +if(CMAKE_THREAD_LIBS_INIT) + string(APPEND PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}") +endif() +configure_file("libyang.pc.in" "libyang.pc" @ONLY) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") # tests if(ENABLE_TESTS) diff --git a/libyang.pc.in b/libyang.pc.in index 63de935029..69712be6ed 100644 --- a/libyang.pc.in +++ b/libyang.pc.in @@ -1,11 +1,10 @@ prefix=@CMAKE_INSTALL_PREFIX@ -includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ -libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=@PC_INCLUDEDIR@ +libdir=@PC_LIBDIR@ Name: @PROJECT_NAME@ Description: @LIBYANG_DESCRIPTION@ Version: @LIBYANG_VERSION@ -Requires.private: libpcre2-8 @PC_XXHASH_LIB@ Libs: -L${libdir} -lyang -Libs.private: -lpcre2-8 +Libs.private: @PC_LIBS_PRIVATE@ Cflags: -I${includedir} From 546a973d8fa1a3bd15a894c87b052e110b50595d Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:35:49 +0100 Subject: [PATCH 02/10] distro REFACTOR remove package tests The apkg tests were never run, CI only builds and installs the packages, and they still expected version 2 of libyang. They only checked that pkg-config and yanglint print a version, so they are removed instead of fixed. --- distro/tests/control | 1 - distro/tests/test-pkg-config.sh | 5 ----- distro/tests/test-yanglint.sh | 5 ----- 3 files changed, 11 deletions(-) delete mode 100644 distro/tests/control delete mode 100755 distro/tests/test-pkg-config.sh delete mode 100755 distro/tests/test-yanglint.sh diff --git a/distro/tests/control b/distro/tests/control deleted file mode 100644 index 61b906099e..0000000000 --- a/distro/tests/control +++ /dev/null @@ -1 +0,0 @@ -Tests: test-pkg-config.sh test-yanglint.sh diff --git a/distro/tests/test-pkg-config.sh b/distro/tests/test-pkg-config.sh deleted file mode 100755 index 581e4d61f8..0000000000 --- a/distro/tests/test-pkg-config.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -ex - -version=`pkg-config --modversion libyang` -echo "$version" | grep '2\.[0-9.]\+' diff --git a/distro/tests/test-yanglint.sh b/distro/tests/test-yanglint.sh deleted file mode 100755 index 70bf4b9b47..0000000000 --- a/distro/tests/test-yanglint.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -ex - -version=`yanglint --version` -echo "$version" | grep '2\.[0-9.]\+' From 545d6c157f1257ab71eba3d0969f4bd46d69bafc Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:36:56 +0100 Subject: [PATCH 03/10] cmake UPDATE require CMake 3.12 The declared minimum 2.8.12 no longer worked. The new minimum is the one of the dependent projects: sysrepo and netopeer2 link OBJECT libraries through target_link_libraries() and netopeer2 uses CURL::libcurl, both new in CMake 3.12. libyang itself builds with CMake 3.5, but the whole family keeps one minimum. Two places did not work with older CMake. use_compat() removed an empty CMAKE_THREAD_LIBS_INIT from a list, which fails up to CMake 3.19. yang_update linked yangobj objects as a library, which CMake before 3.21 does without a dependency on yangobj, and it failed in a static build, where there is no yangobj. The 32-bit CI job ran in i386/ubuntu, which is Ubuntu 18.04 with CMake 3.10. It runs in the i386 Debian 13 image now. --- .github/workflows/ci.yml | 3 ++- CMakeLists.txt | 2 +- CMakeModules/UseCompat.cmake | 8 ++++++-- README.md | 2 +- tools/update/CMakeLists.txt | 13 +++++++++---- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19789b3bc8..cb982777a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -266,9 +266,10 @@ jobs: - name: Build in 32-bit container run: | docker run --rm \ + --platform linux/386 \ -v ${{ github.workspace }}:${{ github.workspace }} \ -w ${{ github.workspace }} \ - i386/ubuntu \ + debian:13 \ /bin/bash -c " \ apt-get update -q && \ apt-get install -y -q git cmake gcc libcmocka-dev libxxhash-dev libpcre2-dev libcbor-dev && \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 3935803e18..ec00f0f7ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ if(WIN32) cmake_minimum_required(VERSION 3.22.0) else() - cmake_minimum_required(VERSION 2.8.12...3.28.1) + cmake_minimum_required(VERSION 3.12...3.28.1) endif() # force out-of-source build diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake index 53bb0236e7..48c588d226 100644 --- a/CMakeModules/UseCompat.cmake +++ b/CMakeModules/UseCompat.cmake @@ -44,9 +44,13 @@ macro(USE_COMPAT) set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads) - list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + if(CMAKE_THREAD_LIBS_INIT) + list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + endif() check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK) - list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} "") # cmake <= 3.19 needs at least one item + if(CMAKE_THREAD_LIBS_INIT) + list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + endif() test_big_endian(IS_BIG_ENDIAN) diff --git a/README.md b/README.md index 036318a0b7..9ccff24d09 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ libyang & yanglint can be installed using [homebrew](https://brew.sh/) with ```b ### Unix Build Requirements * C compiler -* cmake >= 2.8.12 +* cmake >= 3.12 * libpcre2 >= 10.21 (including devel package) #### Optional diff --git a/tools/update/CMakeLists.txt b/tools/update/CMakeLists.txt index 49bdf2cd64..c0aff02631 100644 --- a/tools/update/CMakeLists.txt +++ b/tools/update/CMakeLists.txt @@ -40,12 +40,17 @@ else() set(LIB_NAME yang_update) endif() -# yang update library -add_library(${LIB_NAME} STATIC ${lib_src}) +# yang update library, uses also internal libyang functions +if(TARGET yangobj) + add_library(${LIB_NAME} STATIC ${lib_src} $) + get_target_property(yang_linked_libraries yang LINK_LIBRARIES) + target_link_libraries(${LIB_NAME} ${yang_linked_libraries}) +else() + add_library(${LIB_NAME} STATIC ${lib_src}) + target_link_libraries(${LIB_NAME} yang) +endif() target_compile_definitions(${LIB_NAME} PUBLIC LIBYANG_BUILD) target_include_directories(${LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${PROJECT_BINARY_DIR}) -get_target_property(yang_linked_libraries yang LINK_LIBRARIES) -target_link_libraries(${LIB_NAME} $ ${yang_linked_libraries}) if(NOT YANGUPDATE_TEST_LIB) # yangupdate From c08a5b9be259e1963c371bb7bb9a32946a5b8f85 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:37:44 +0100 Subject: [PATCH 04/10] cmake REFACTOR keyword link signature of yang Without keywords every dependency of the yang target was transitive and an exported target would carry absolute paths of all the private libraries. All the dependencies are private now except ws2_32 on Windows, because ly_config.h includes winsock2.h. The target also carries its include directory so that a linked target finds both in the build tree and after installation, and the DLL is installed into bin on Windows. --- CMakeLists.txt | 21 +++++++++++++-------- tools/lint/CMakeLists.txt | 3 ++- tools/re/CMakeLists.txt | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec00f0f7ac..02112ac360 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,7 +424,7 @@ else() endif() #link dl - target_link_libraries(yang ${CMAKE_DL_LIBS}) + target_link_libraries(yang PRIVATE ${CMAKE_DL_LIBS}) endif() if(WIN32) @@ -443,26 +443,28 @@ if(WIN32) find_package(pthreads REQUIRED) set(COMPAT_WIN_LIBRARIES PThreads4W::PThreads4W shlwapi.lib ws2_32) - target_link_libraries(yang ${COMPAT_WIN_LIBRARIES}) + # ws2_32 is public, ly_config.h includes winsock2.h + target_link_libraries(yang PRIVATE PThreads4W::PThreads4W shlwapi.lib PUBLIC ws2_32) endif() set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION}) +target_include_directories(yang PUBLIC $ $) # link math if(NOT WIN32) - target_link_libraries(yang m) + target_link_libraries(yang PRIVATE m) endif() # find pthreads set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads REQUIRED) -target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(yang PRIVATE ${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}) +target_link_libraries(yang PRIVATE ${PCRE2_LIBRARIES}) # link libcbor if found if(CBOR_FOUND) @@ -470,7 +472,7 @@ if(CBOR_FOUND) target_include_directories(yangobj PRIVATE ${CBOR_INCLUDE_DIR}) endif() target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR}) - target_link_libraries(yang ${CBOR_LIBRARY}) + target_link_libraries(yang PRIVATE ${CBOR_LIBRARY}) endif() # XXHash include and library @@ -478,7 +480,7 @@ find_package(XXHash) if(XXHASH_FOUND) add_definitions(-DLY_XXHASH_SUPPORT) include_directories(${XXHASH_INCLUDE_DIR}) - target_link_libraries(yang ${XXHASH_LIBRARY}) + target_link_libraries(yang PRIVATE ${XXHASH_LIBRARY}) message(STATUS "Hash algorithm: xxhash") if (HAVE_XXH3_64BITS_WITHSEED) # for 32-bit system compatibility, this may not be available there, so use XXH32 instead @@ -497,7 +499,10 @@ endforeach() install(DIRECTORY "${PROJECT_SOURCE_DIR}/modules/" DESTINATION ${YANG_MODULE_DIR} FILES_MATCHING PATTERN "*.yang") # install all library files -install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR}) +install(TARGETS yang + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${headers} ${g_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang) # generate and install pkg-config file diff --git a/tools/lint/CMakeLists.txt b/tools/lint/CMakeLists.txt index 8e83e24b2b..3fd23f40f4 100644 --- a/tools/lint/CMakeLists.txt +++ b/tools/lint/CMakeLists.txt @@ -42,7 +42,8 @@ set(format_sources PARENT_SCOPE) add_executable(yanglint ${lintsrc} ${compatsrc}) -target_link_libraries(yanglint yang) +# the compat sources may call pthread functions +target_link_libraries(yanglint yang ${CMAKE_THREAD_LIBS_INIT}) install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) target_include_directories(yanglint BEFORE PRIVATE ${PROJECT_BINARY_DIR}) diff --git a/tools/re/CMakeLists.txt b/tools/re/CMakeLists.txt index 7b6de226f1..f0fe050c5e 100644 --- a/tools/re/CMakeLists.txt +++ b/tools/re/CMakeLists.txt @@ -9,7 +9,8 @@ set(format_sources PARENT_SCOPE) add_executable(yangre ${resrc} ${compatsrc}) -target_link_libraries(yangre yang) +# the compat sources may call pthread functions +target_link_libraries(yangre yang ${CMAKE_THREAD_LIBS_INIT}) install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ${PROJECT_SOURCE_DIR}/tools/re/yangre.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) target_include_directories(yangre BEFORE PRIVATE ${PROJECT_BINARY_DIR}) From ec7c12f5972fb3901a02906c68baa97e15470ce8 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:38:29 +0100 Subject: [PATCH 05/10] cmake UPDATE refresh public header copies The public headers were copied into the build tree by file(COPY) only when CMake ran, so after editing a header the build used its stale copy until the next manual reconfiguration. configure_file() makes the build rerun CMake and refresh the copy whenever the header changes. --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 02112ac360..4a910dec5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -374,7 +374,10 @@ 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) +foreach(h IN LISTS headers) + get_filename_component(h_name ${h} NAME) + configure_file(${PROJECT_SOURCE_DIR}/${h} ${PROJECT_BINARY_DIR}/libyang/${h_name} COPYONLY) +endforeach() # DOC-only target with no extra dependencies if("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY") From f46f2e1d6b21ac3989304b9d9cf30d63316e0f40 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:39:03 +0100 Subject: [PATCH 06/10] cmake BUGFIX do not install internal lcbor.h lcbor.h is a private header of the CBOR parser and printer. Its functions are not exported and no public header includes it, but as an installed header it made part of the public interface. It is now documented with the other internal headers. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a910dec5f..66cb9b48d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,6 +205,7 @@ set(internal_headers src/hash_table_internal.h src/in_internal.h src/json.h + src/lcbor.h src/lyb.h src/out_internal.h src/parser_internal.h @@ -344,7 +345,6 @@ find_package(CBOR) if(CBOR_FOUND) set(LY_HAVE_CBOR 1) list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c) - list(APPEND headers src/lcbor.h) list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c) else() message(STATUS "libcbor not found, CBOR support disabled") From b40f79988b6d16106568d5e4cdf3491911b495c8 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:02 +0100 Subject: [PATCH 07/10] cmake FEATURE LibYANG config package A shared build installs the LibYANG CMake package into lib/cmake/LibYANG and exports it from the build tree, so that find_package(LibYANG CONFIG) works without a Find module, pkg-config or hand-set include and library paths. It provides the imported target LibYANG::yang and the variables LibYANG_PROJECT_VERSION and LibYANG_HAVE_CBOR. The package version is the SO version with SameMajorVersion compatibility, the version the Find modules of the dependent projects compare. A static build installs no package, it would need imported targets for all the private dependencies. --- CMakeLists.txt | 32 +++++++++++++++------- CMakeModules/GenConfigPackage.cmake | 42 +++++++++++++++++++++++++++++ LibYANGConfig.cmake.in | 18 +++++++++++++ tests/yangupdate/CMakeLists.txt | 2 +- 4 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 CMakeModules/GenConfigPackage.cmake create mode 100644 LibYANGConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 66cb9b48d7..f15d1a49a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ include(ABICheck) include(SourceFormat) include(GenDoc) include(GenCoverage) +include(GenConfigPackage) include(CheckIncludeFile) # set default build type if not specified by user @@ -80,7 +81,9 @@ else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic -std=c11") endif() -include_directories(${PROJECT_BINARY_DIR} ${PROJECT_BINARY_DIR}/libyang ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/src/plugins_exts) +include_directories(${PROJECT_BINARY_DIR} + ${PROJECT_BINARY_DIR}/include ${PROJECT_BINARY_DIR}/include/libyang + ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/src/plugins_exts) # type plugins are separate because they have their documentation generated set(type_plugins @@ -236,7 +239,7 @@ set(doxy_files doc/transition_3_4.dox doc/transition_4_5.dox ${headers} - ${PROJECT_BINARY_DIR}/libyang/version.h + ${PROJECT_BINARY_DIR}/include/libyang/version.h ${type_plugins}) # project (doxygen) logo @@ -343,10 +346,11 @@ endif() find_package(CBOR) if(CBOR_FOUND) - set(LY_HAVE_CBOR 1) + set(LY_HAVE_CBOR TRUE) list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c) list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c) else() + set(LY_HAVE_CBOR FALSE) message(STATUS "libcbor not found, CBOR support disabled") endif() @@ -372,11 +376,11 @@ if(LY_HAVE_REGEX_H) 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) +configure_file(${PROJECT_SOURCE_DIR}/src/ly_config.h.in ${PROJECT_BINARY_DIR}/include/libyang/ly_config.h @ONLY) +configure_file(${PROJECT_SOURCE_DIR}/src/version.h.in ${PROJECT_BINARY_DIR}/include/libyang/version.h @ONLY) foreach(h IN LISTS headers) get_filename_component(h_name ${h} NAME) - configure_file(${PROJECT_SOURCE_DIR}/${h} ${PROJECT_BINARY_DIR}/libyang/${h_name} COPYONLY) + configure_file(${PROJECT_SOURCE_DIR}/${h} ${PROJECT_BINARY_DIR}/include/libyang/${h_name} COPYONLY) endforeach() # DOC-only target with no extra dependencies @@ -451,7 +455,7 @@ if(WIN32) endif() set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION}) -target_include_directories(yang PUBLIC $ $) +target_include_directories(yang PUBLIC $ $) # link math if(NOT WIN32) @@ -495,19 +499,27 @@ endif() # generated header list foreach(h IN LISTS gen_headers) - list(APPEND g_headers ${PROJECT_BINARY_DIR}/libyang/${h}) + list(APPEND g_headers ${PROJECT_BINARY_DIR}/include/libyang/${h}) endforeach() # install the modules install(DIRECTORY "${PROJECT_SOURCE_DIR}/modules/" DESTINATION ${YANG_MODULE_DIR} FILES_MATCHING PATTERN "*.yang") -# install all library files -install(TARGETS yang +# install all library files, a Windows DLL is RUNTIME and belongs to bin/ +install(TARGETS yang EXPORT LibYANGTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${headers} ${g_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang) +# CMake config package, for the build tree and the installation +if(BUILD_SHARED_LIBS) + # the package version is the SO version, which all the dependent projects check + gen_config_package(LibYANG ${LIBYANG_SOVERSION_FULL}) +else() + message(STATUS "Static build, CMake config package is not installed") +endif() + # generate and install pkg-config file if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") set(PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}") diff --git a/CMakeModules/GenConfigPackage.cmake b/CMakeModules/GenConfigPackage.cmake new file mode 100644 index 0000000000..7e1368362c --- /dev/null +++ b/CMakeModules/GenConfigPackage.cmake @@ -0,0 +1,42 @@ +# Generate the CMake config package of a library for the build tree and the installation +# +# gen_config_package(LIB_NAME LIB_SOVERSION_FULL [EXPORTS ...] [PATH_VARS ...]) +# LIB_NAME package name, the template is Config.cmake.in in the source root +# LIB_SOVERSION_FULL package version, the full SO version of the library +# EXPORTS optional, export sets installed besides Targets +# PATH_VARS optional, template variables with installed paths, _BUILD holds the build tree path +# +# The template reads its other variables from the scope of the caller. +include(CMakePackageConfigHelpers) + +function(gen_config_package LIB_NAME LIB_SOVERSION_FULL) + # the optional arguments into LIB_EXPORTS and LIB_PATH_VARS + cmake_parse_arguments(PARSE_ARGV 2 LIB "" "" "EXPORTS;PATH_VARS") + set(LIB_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME}) + set(LIB_TEMPLATE ${PROJECT_SOURCE_DIR}/${LIB_NAME}Config.cmake.in) + + # create the version file + write_basic_package_version_file(${PROJECT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake + VERSION ${LIB_SOVERSION_FULL} COMPATIBILITY SameMajorVersion) + install(FILES ${PROJECT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake DESTINATION ${LIB_CONFIG_DIR}) + + # the targets file of each export set, for the build tree and the installation + foreach(LIB_EXPORT IN ITEMS ${LIB_NAME}Targets ${LIB_EXPORTS}) + export(EXPORT ${LIB_EXPORT} NAMESPACE ${LIB_NAME}:: FILE ${PROJECT_BINARY_DIR}/${LIB_EXPORT}.cmake) + install(EXPORT ${LIB_EXPORT} NAMESPACE ${LIB_NAME}:: DESTINATION ${LIB_CONFIG_DIR}) + endforeach() + + # the installed config, generated aside so that it does not replace the build tree one + configure_package_config_file(${LIB_TEMPLATE} ${PROJECT_BINARY_DIR}/CMakeFiles/${LIB_NAME}Config.cmake + INSTALL_DESTINATION ${LIB_CONFIG_DIR} PATH_VARS ${LIB_PATH_VARS}) + install(FILES ${PROJECT_BINARY_DIR}/CMakeFiles/${LIB_NAME}Config.cmake DESTINATION ${LIB_CONFIG_DIR}) + + # each path variable set to its _BUILD value + foreach(LIB_VAR IN LISTS LIB_PATH_VARS) + set(${LIB_VAR} ${${LIB_VAR}_BUILD}) + endforeach() + + # the build tree config, the build directory is both its location and its prefix + configure_package_config_file(${LIB_TEMPLATE} ${PROJECT_BINARY_DIR}/${LIB_NAME}Config.cmake + INSTALL_DESTINATION ${PROJECT_BINARY_DIR} INSTALL_PREFIX ${PROJECT_BINARY_DIR} PATH_VARS ${LIB_PATH_VARS}) +endfunction() diff --git a/LibYANGConfig.cmake.in b/LibYANGConfig.cmake.in new file mode 100644 index 0000000000..791e08b259 --- /dev/null +++ b/LibYANGConfig.cmake.in @@ -0,0 +1,18 @@ +# CMake config package of libyang +# +# Imported target: +# LibYANG::yang the libyang library +# +# Variables: +# LibYANG_VERSION SO version of the library, compared by find_package() +# LibYANG_PROJECT_VERSION version of the libyang project +# LibYANG_HAVE_CBOR libyang supports the CBOR data format (LY_HAVE_CBOR) + +@PACKAGE_INIT@ + +set(LibYANG_PROJECT_VERSION "@LIBYANG_VERSION@") +set(LibYANG_HAVE_CBOR @LY_HAVE_CBOR@) + +# the LibYANG::yang target, generated by CMake next to this file +include("${CMAKE_CURRENT_LIST_DIR}/LibYANGTargets.cmake") +check_required_components(LibYANG) diff --git a/tests/yangupdate/CMakeLists.txt b/tests/yangupdate/CMakeLists.txt index 17b08d9325..f5d9e8199b 100644 --- a/tests/yangupdate/CMakeLists.txt +++ b/tests/yangupdate/CMakeLists.txt @@ -11,7 +11,7 @@ function(lyu_add_test) set(TEST_NAME yangupdate_${ARGV0}) add_executable(${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}/test.c) - target_include_directories(${TEST_NAME} PRIVATE ${PROJECT_BINARY_DIR}/libyang ${PROJECT_SOURCE_DIR}/tools/update) + target_include_directories(${TEST_NAME} PRIVATE ${PROJECT_BINARY_DIR}/include/libyang ${PROJECT_SOURCE_DIR}/tools/update) target_link_libraries(${TEST_NAME} ${YANGUPDATE_TEST_LIB} ${CMOCKA_LIBRARIES}) set_target_properties(${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests) From 047dff00db6b4872a89a0f7461ecafd0359dc42a Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:16 +0100 Subject: [PATCH 08/10] cmake UPDATE remove FindLibYANG.cmake The Find module was never installed, projects copied it into their own trees, and the LibYANG CMake package replaces it. The copies keep working, releases without the package are found by pkg-config. README describes the package instead. --- FindLibYANG.cmake | 89 ----------------------------------------------- README.md | 17 +++++++-- 2 files changed, 15 insertions(+), 91 deletions(-) delete mode 100644 FindLibYANG.cmake diff --git a/FindLibYANG.cmake b/FindLibYANG.cmake deleted file mode 100644 index 89cd93ad50..0000000000 --- a/FindLibYANG.cmake +++ /dev/null @@ -1,89 +0,0 @@ -# - Try to find LibYANG -# Once done this will define -# -# LIBYANG_FOUND - system has LibYANG -# LIBYANG_INCLUDE_DIRS - the LibYANG include directory -# LIBYANG_LIBRARIES - Link these to use LibYANG -# LIBYANG_VERSION - SO version of the found libyang library -# -# Author Michal Vasko -# Copyright (c) 2021 CESNET, z.s.p.o. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# 3. The name of the author may not be used to endorse or promote products -# derived from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -include(FindPackageHandleStandardArgs) - -if(LIBYANG_LIBRARIES AND LIBYANG_INCLUDE_DIRS) - # in cache already - set(LIBYANG_FOUND TRUE) -else() - find_path(LIBYANG_INCLUDE_DIR - NAMES - libyang/libyang.h - PATHS - /usr/include - /usr/local/include - /opt/local/include - /sw/include - ${CMAKE_INCLUDE_PATH} - ${CMAKE_INSTALL_PREFIX}/include - ) - - find_library(LIBYANG_LIBRARY - NAMES - yang - libyang - PATHS - /usr/lib - /usr/lib64 - /usr/local/lib - /usr/local/lib64 - /opt/local/lib - /sw/lib - ${CMAKE_LIBRARY_PATH} - ${CMAKE_INSTALL_PREFIX}/lib - ) - - if(LIBYANG_INCLUDE_DIR) - find_path(LY_VERSION_PATH "libyang/version.h" HINTS ${LIBYANG_INCLUDE_DIR}) - if(LY_VERSION_PATH) - file(READ "${LY_VERSION_PATH}/libyang/version.h" LY_VERSION_FILE) - else() - find_path(LY_HEADER_PATH "libyang/libyang.h" HINTS ${LIBYANG_INCLUDE_DIR}) - file(READ "${LY_HEADER_PATH}/libyang/libyang.h" LY_VERSION_FILE) - endif() - string(REGEX MATCH "#define LY_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+\"" LY_VERSION_MACRO "${LY_VERSION_FILE}") - string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" LIBYANG_VERSION "${LY_VERSION_MACRO}") - endif() - - set(LIBYANG_INCLUDE_DIRS ${LIBYANG_INCLUDE_DIR}) - set(LIBYANG_LIBRARIES ${LIBYANG_LIBRARY}) - mark_as_advanced(LIBYANG_INCLUDE_DIRS LIBYANG_LIBRARIES) - - # handle the QUIETLY and REQUIRED arguments and set LIBYANG_FOUND to TRUE - # if all listed variables are TRUE - find_package_handle_standard_args(LibYANG FOUND_VAR LIBYANG_FOUND - REQUIRED_VARS LIBYANG_LIBRARY LIBYANG_INCLUDE_DIR - VERSION_VAR LIBYANG_VERSION) -endif() diff --git a/README.md b/README.md index 9ccff24d09..4b8c5ad7ff 100644 --- a/README.md +++ b/README.md @@ -188,8 +188,21 @@ library was installed into a non-standard path, the path to libyang must be spec linker. To help with setting all the compiler's options, there is `libyang.pc` file for `pkg-config(1)` available in the source tree. The file is installed with the library. -If you are using `cmake` in you project, it is also possible to use the provided -`FindLibYANG.cmake` file to detect presence of the libyang library in the system. +A CMake project finds libyang by its CMake package, installed into `LIBDIR/cmake/LibYANG` +(`LIBDIR` is the library directory under the install prefix, e.g. `/usr/local/lib`): +``` +find_package(LibYANG 5 CONFIG REQUIRED) +target_link_libraries(myapp PRIVATE LibYANG::yang) +``` + +The version passed to `find_package()` is the SO version of the library. The package also +defines the following variables: + +* `LibYANG_PROJECT_VERSION` - project version of libyang, +* `LibYANG_HAVE_CBOR` - whether libyang supports the CBOR data format. + +To use libyang from its build directory, pass `-DLibYANG_DIR=`. +Static builds and older libyang releases provide no CMake package, use `pkg-config` with them. ## Bindings From 04eb038bd6028fc711a4bc33df17ddb1c14e5b0f Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:42 +0100 Subject: [PATCH 09/10] distro UPDATE package the LibYANG CMake package The CMake package belongs to libyang-dev and libyang-devel. dh_missing now fails on installed files missing from all the packages, with debhelper compat 10 it did not even list them, so a new installed file silently fell out of the deb packages. pkg-config is no longer needed for the build. --- distro/pkg/deb/control | 3 +-- distro/pkg/deb/libyang-dev.install | 1 + distro/pkg/deb/rules | 4 ++++ distro/pkg/rpm/libyang.spec | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/distro/pkg/deb/control b/distro/pkg/deb/control index cd967faf34..0f17fd61ac 100644 --- a/distro/pkg/deb/control +++ b/distro/pkg/deb/control @@ -9,8 +9,7 @@ Build-Depends: cmake, libcmocka-dev , libpcre2-dev, libcbor-dev, - libxxhash-dev, - pkg-config + libxxhash-dev Vcs-Browser: https://github.com/CESNET/libyang/tree/master Vcs-Git: https://github.com/CESNET/libyang.git diff --git a/distro/pkg/deb/libyang-dev.install b/distro/pkg/deb/libyang-dev.install index 452d7e0142..ba59834e0c 100644 --- a/distro/pkg/deb/libyang-dev.install +++ b/distro/pkg/deb/libyang-dev.install @@ -1,3 +1,4 @@ usr/include/libyang/*.h usr/lib/*/*.so usr/lib/*/pkgconfig/*.pc +usr/lib/*/cmake/LibYANG diff --git a/distro/pkg/deb/rules b/distro/pkg/deb/rules index 58f845de82..5339ca9ed3 100755 --- a/distro/pkg/deb/rules +++ b/distro/pkg/deb/rules @@ -10,3 +10,7 @@ include /usr/share/dpkg/default.mk override_dh_auto_configure: dh_auto_configure -- \ -DCMAKE_BUILD_TYPE:String="Release" + +# fail on installed files that no package lists +override_dh_missing: + dh_missing --fail-missing diff --git a/distro/pkg/rpm/libyang.spec b/distro/pkg/rpm/libyang.spec index 6322c5022a..c291b7eeb7 100644 --- a/distro/pkg/rpm/libyang.spec +++ b/distro/pkg/rpm/libyang.spec @@ -87,6 +87,7 @@ cp -a doc/html %{buildroot}/%{_docdir}/libyang/html %files devel %{_libdir}/libyang.so %{_libdir}/pkgconfig/libyang.pc +%{_libdir}/cmake/LibYANG/ %{_includedir}/libyang/*.h %dir %{_includedir}/libyang/ From de59b7a081d498695c4427b705b364156c1e8242 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:48:43 +0100 Subject: [PATCH 10/10] actions UPDATE build own sources The package jobs cloned the target branch from GitHub, so a pull request was built without its own changes. They now build the checked out sources and no longer install pkg-config, which the build does not need anymore. --- .github/workflows/ci.yml | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb982777a3..7d0f08640d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,21 +10,6 @@ on: - devel jobs: - git-branch: - name: Get git branch - runs-on: ubuntu-latest - outputs: - branch-name: ${{ steps.get-git-branch.outputs.branch-name }} - steps: - - id: get-git-branch - run: | - if ${{ github.event_name == 'push' }} - then export GIT_BRANCH=`echo ${{ github.ref }} | cut -d'/' -f 3` - else - export GIT_BRANCH=${{ github.base_ref }} - fi - echo "branch-name=$GIT_BRANCH" >> $GITHUB_OUTPUT - build-unix: name: ${{ matrix.config.name }} runs-on: ${{ matrix.config.os }} @@ -316,7 +301,6 @@ jobs: build-packages: name: ${{ matrix.config.name }} runs-on: ubuntu-latest - needs: git-branch strategy: fail-fast: false matrix: @@ -334,7 +318,7 @@ jobs: image: "debian:13", pkg-setup: "apt-get update -q", pkg-cmd: "apt-get install -yq", - pkg-packages: "pipx cmake git debhelper pkg-config libcmocka-dev libpcre2-dev libcbor-dev libxxhash-dev", + pkg-packages: "pipx cmake git debhelper libcmocka-dev libpcre2-dev libcbor-dev libxxhash-dev", pipx-install: "" } container: @@ -350,9 +334,11 @@ jobs: pipx install apkg $HOME/.local/bin/apkg system-setup + - uses: actions/checkout@main + - name: Build-and-install-package run: | - git clone -b ${{ needs.git-branch.outputs.branch-name }} https://github.com/CESNET/libyang.git - cd libyang + # the container runs as root, the checked out workspace belongs to the runner user + git config --global --add safe.directory $GITHUB_WORKSPACE $HOME/.local/bin/apkg build $HOME/.local/bin/apkg install