From c41e9b52ea539bea4fc4fcfa350d0c6989327105 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:35:42 +0100 Subject: [PATCH 01/14] 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, 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 | 39 ++++++++++++++++++++------------------- libyang.pc.in | 3 +-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 005742a8b7..3dd3edf93d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -395,6 +395,10 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") add_compile_definitions(__EXTENSIONS__) endif() +# private dependencies of a static build, for Libs.private of libyang.pc, +# Requires.private would need their .pc files even for dynamic linking +set(PC_LIBS_PRIVATE "") + # create static libyang library if(NOT BUILD_SHARED_LIBS) add_definitions(-DSTATIC) @@ -422,8 +426,11 @@ else() set(CMAKE_DL_LIBS dlfcn-win32::dl) endif() - #link dl + #link dl, only the shared library loads plugins target_link_libraries(yang ${CMAKE_DL_LIBS}) + if(NOT WIN32 AND CMAKE_DL_LIBS) + string(APPEND PC_LIBS_PRIVATE " -l${CMAKE_DL_LIBS}") + endif() endif() if(WIN32) @@ -447,31 +454,37 @@ set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSIO # link math if(NOT WIN32) target_link_libraries(yang m) + string(APPEND PC_LIBS_PRIVATE " -lm") endif() # find pthreads set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads REQUIRED) target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT}) +if(CMAKE_THREAD_LIBS_INIT) + string(APPEND PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}") +endif() # find PCRE2 library find_package(PCRE2 10.21 REQUIRED) include_directories(${PCRE2_INCLUDE_DIRS}) target_link_libraries(yang ${PCRE2_LIBRARIES}) +string(APPEND PC_LIBS_PRIVATE " -lpcre2-8") # link libcbor if found if(CBOR_FOUND) target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR}) target_link_libraries(yang ${CBOR_LIBRARY}) + string(APPEND PC_LIBS_PRIVATE " -lcbor") endif() # XXHash include and library 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}) + string(APPEND PC_LIBS_PRIVATE " -lxxhash") 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 @@ -493,23 +506,11 @@ 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() - endif() -endif() +string(STRIP "${PC_LIBS_PRIVATE}" PC_LIBS_PRIVATE) + +# 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") # tests if(ENABLE_TESTS) diff --git a/libyang.pc.in b/libyang.pc.in index 63de935029..c0f0624879 100644 --- a/libyang.pc.in +++ b/libyang.pc.in @@ -5,7 +5,6 @@ libdir=${prefix}/@CMAKE_INSTALL_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 2bc957a8495ee6c8dc6709f1aabdb6f9b11a35f1 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:35:49 +0100 Subject: [PATCH 02/14] 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 8cc75b2b196d0a416749696ac1a2548445cb6f40 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 30 Nov 2026 09:52:37 +0100 Subject: [PATCH 03/14] cmake UPDATE require CMake 3.12 The declared minimum 2.8.12 no longer worked. CMake 3.12 is the oldest version that links OBJECT libraries through target_link_libraries(), which the following commits need. It is also the minimum of sysrepo and netopeer2, 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 the libyang objects as a library, which CMake before 3.21 does without a dependency on libyang. The objects of a non-object library are also available only since CMake 3.15, so yangupdate and its tests are disabled with older CMake. The CMP0075 policy guard is always true now. 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 | 11 ++++++----- README.md | 2 +- tests/yangupdate/CMakeLists.txt | 6 ++++++ tools/update/CMakeLists.txt | 13 +++++++++---- 6 files changed, 25 insertions(+), 12 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 3dd3edf93d..4cd7765c98 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..068fc0ea98 100644 --- a/CMakeModules/UseCompat.cmake +++ b/CMakeModules/UseCompat.cmake @@ -19,9 +19,6 @@ include(CheckFunctionExists) include(CheckIncludeFile) include(TestBigEndian) include(CheckStructHasMember) -if(POLICY CMP0075) - cmake_policy(SET CMP0075 NEW) -endif() macro(USE_COMPAT) # compatibility checks @@ -44,9 +41,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/tests/yangupdate/CMakeLists.txt b/tests/yangupdate/CMakeLists.txt index 17b08d9325..7046fc9eff 100644 --- a/tests/yangupdate/CMakeLists.txt +++ b/tests/yangupdate/CMakeLists.txt @@ -4,6 +4,12 @@ set(format_sources ${CMAKE_CURRENT_SOURCE_DIR}/*/*.c PARENT_SCOPE) +if(CMAKE_VERSION VERSION_LESS 3.15) + # yangupdate links the libyang objects, available since CMake 3.15 + message(STATUS "yangupdate tests disabled - requires CMake 3.15") + return() +endif() + function(lyu_add_test) set(YANGUPDATE_PLUGINS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}/plugins) set(YANGUPDATE_TEST_LIB yutest_${ARGV0}_lib) diff --git a/tools/update/CMakeLists.txt b/tools/update/CMakeLists.txt index 144ce6dbd7..2c61852968 100644 --- a/tools/update/CMakeLists.txt +++ b/tools/update/CMakeLists.txt @@ -2,6 +2,11 @@ if(NOT YANGUPDATE_PLUGINS_DIR) message(STATUS "yangupdate disabled - requires YANGUPDATE_PLUGINS_DIR to be set") return() endif() +if(CMAKE_VERSION VERSION_LESS 3.15) + # the objects of a non-object library are available since CMake 3.15 + message(STATUS "yangupdate disabled - requires CMake 3.15") + return() +endif() # get all the plugin files file(GLOB_RECURSE plg_files LIST_DIRECTORIES false CONFIGURE_DEPENDS "${YANGUPDATE_PLUGINS_DIR}/*.c") @@ -23,7 +28,7 @@ endforeach() file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/yang_update_plugins.h "#ifndef LY_YANG_UPDATE_PLUGINS_H_\n#define LY_YANG_UPDATE_PLUGINS_H_\n\n#include \"yang_update_plugins.h\"\n\n${plugins_h_extern}\nstruct lyu_plg *lyu_plugins[] = {\n${plugins_h_structs}};\n\n#endif\n") # sources -set(lib_src +set(updatesrc yang_update.c ${plg_files}) @@ -40,12 +45,12 @@ 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 +add_library(${LIB_NAME} STATIC ${updatesrc} $) 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}) # uses internal functions +target_link_libraries(${LIB_NAME} ${yang_linked_libraries}) if(NOT YANGUPDATE_TEST_LIB) # yangupdate From bcb8a7654a090dbfe1843c3c0435abfa13f27fd5 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:37:44 +0100 Subject: [PATCH 04/14] 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 | 22 +++++++++++++--------- tools/lint/CMakeLists.txt | 3 ++- tools/re/CMakeLists.txt | 3 ++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4cd7765c98..e425de9999 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -427,7 +427,7 @@ else() endif() #link dl, only the shared library loads plugins - target_link_libraries(yang ${CMAKE_DL_LIBS}) + target_link_libraries(yang PRIVATE ${CMAKE_DL_LIBS}) if(NOT WIN32 AND CMAKE_DL_LIBS) string(APPEND PC_LIBS_PRIVATE " -l${CMAKE_DL_LIBS}") endif() @@ -445,22 +445,23 @@ if(WIN32) 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}) + # 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) string(APPEND PC_LIBS_PRIVATE " -lm") 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}) if(CMAKE_THREAD_LIBS_INIT) string(APPEND PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}") endif() @@ -468,13 +469,13 @@ endif() # find PCRE2 library find_package(PCRE2 10.21 REQUIRED) include_directories(${PCRE2_INCLUDE_DIRS}) -target_link_libraries(yang ${PCRE2_LIBRARIES}) +target_link_libraries(yang PRIVATE ${PCRE2_LIBRARIES}) string(APPEND PC_LIBS_PRIVATE " -lpcre2-8") # link libcbor if found if(CBOR_FOUND) target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR}) - target_link_libraries(yang ${CBOR_LIBRARY}) + target_link_libraries(yang PRIVATE ${CBOR_LIBRARY}) string(APPEND PC_LIBS_PRIVATE " -lcbor") endif() @@ -483,7 +484,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}) string(APPEND PC_LIBS_PRIVATE " -lxxhash") message(STATUS "Hash algorithm: xxhash") if (HAVE_XXH3_64BITS_WITHSEED) @@ -503,7 +504,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) string(STRIP "${PC_LIBS_PRIVATE}" PC_LIBS_PRIVATE) 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 7a6ed6df8de84b9d96f3075f0ffdbe911f1c37ea Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:38:29 +0100 Subject: [PATCH 05/14] 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 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e425de9999..d151fe6ce0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -371,10 +371,13 @@ if(LY_HAVE_REGEX_H) check_symbol_exists(REG_STARTEND "regex.h" HAVE_REG_STARTEND) endif() -# generate and copy all public header files +# generate and copy all public header files, configure_file() refreshes the copies whenever they change 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 958039cb2c8e9602112717c1b461321fc2f7c2ef Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:39:03 +0100 Subject: [PATCH 06/14] 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 d151fe6ce0..285683930d 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 e2a536b82f2b1b40283a4ce434be5a49d9f15795 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:02 +0100 Subject: [PATCH 07/14] 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, LibYANG_HAVE_CBOR, LibYANG_YANG_MODULE_DIR, LibYANG_PLUGINS_DIR_EXTENSIONS and LibYANG_PLUGINS_DIR_TYPES. 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. GenConfigPackage.cmake, shared with the other projects, generates the whole config from its arguments, with the documentation in LibYANGConfig.doc as its header. --- CMakeLists.txt | 38 ++++++++++---- CMakeModules/GenConfigPackage.cmake | 81 +++++++++++++++++++++++++++++ LibYANGConfig.doc | 12 +++++ tests/yangupdate/CMakeLists.txt | 2 +- 4 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 CMakeModules/GenConfigPackage.cmake create mode 100644 LibYANGConfig.doc diff --git a/CMakeLists.txt b/CMakeLists.txt index 285683930d..6576be803c 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() refreshes the copies whenever they change -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 @@ -453,7 +457,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) @@ -500,19 +504,33 @@ 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} + VARS + "PROJECT_VERSION" "${LIBYANG_VERSION}" + "HAVE_CBOR" "${LY_HAVE_CBOR}" + "YANG_MODULE_DIR" "${YANG_MODULE_DIR}" + "PLUGINS_DIR_EXTENSIONS" "${PLUGINS_DIR_EXTENSIONS}" + "PLUGINS_DIR_TYPES" "${PLUGINS_DIR_TYPES}") +else() + message(STATUS "Static build, CMake config package is not installed") +endif() + string(STRIP "${PC_LIBS_PRIVATE}" PC_LIBS_PRIVATE) # generate and install pkg-config file diff --git a/CMakeModules/GenConfigPackage.cmake b/CMakeModules/GenConfigPackage.cmake new file mode 100644 index 0000000000..b80637a634 --- /dev/null +++ b/CMakeModules/GenConfigPackage.cmake @@ -0,0 +1,81 @@ +# Generate the CMake config package of a project for the build tree and the installation +# +# gen_config_package(PKG_NAME PKG_VERSION [NO_EXPORT_TARGETS] [DEPENDENCIES ...] +# [VARS ...] [PATH_VARS ...]) +# PKG_NAME package name, expects Config.doc in the source root +# PKG_VERSION package version +# NO_EXPORT_TARGETS optional, the package has no targets, otherwise the Targets export set is used +# DEPENDENCIES optional, dependencies of the package +# VARS optional, variables with the same value in the build tree and the installation +# PATH_VARS optional, paths with a different value in the build tree and the installation +# +# Each variable is set as _. +include(CMakePackageConfigHelpers) + +function(gen_config_package PKG_NAME PKG_VERSION) + cmake_parse_arguments(PARSE_ARGV 2 PKG "NO_EXPORT_TARGETS" "" "DEPENDENCIES;VARS;PATH_VARS") + set(PKG_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PKG_NAME}) + set(PKG_DOC ${PROJECT_SOURCE_DIR}/${PKG_NAME}Config.doc) + set(PKG_TEMPLATE ${PROJECT_BINARY_DIR}/CMakeFiles/${PKG_NAME}Config.cmake.in) + + # the variables of the installed and the build tree config + set(PKG_SET_INSTALL "") + set(PKG_SET_BUILD "") + while(PKG_VARS) + list(GET PKG_VARS 0 PKG_VAR) + list(GET PKG_VARS 1 PKG_VALUE) + list(REMOVE_AT PKG_VARS 0 1) + string(APPEND PKG_SET_INSTALL "set(${PKG_NAME}_${PKG_VAR} \"${PKG_VALUE}\")\n") + string(APPEND PKG_SET_BUILD "set(${PKG_NAME}_${PKG_VAR} \"${PKG_VALUE}\")\n") + endwhile() + while(PKG_PATH_VARS) + list(GET PKG_PATH_VARS 0 PKG_VAR) + list(GET PKG_PATH_VARS 1 PKG_BUILD) + list(GET PKG_PATH_VARS 2 PKG_INSTALL) + list(REMOVE_AT PKG_PATH_VARS 0 1 2) + if(NOT IS_ABSOLUTE "${PKG_INSTALL}") + set(PKG_INSTALL "\${PACKAGE_PREFIX_DIR}/${PKG_INSTALL}") + endif() + string(APPEND PKG_SET_INSTALL "set(${PKG_NAME}_${PKG_VAR} \"${PKG_INSTALL}\")\n") + string(APPEND PKG_SET_BUILD "set(${PKG_NAME}_${PKG_VAR} \"${PKG_BUILD}\")\n") + endwhile() + + # the config template, the documentation header followed by the generated code + file(READ ${PKG_DOC} PKG_CODE) + string(APPEND PKG_CODE "\n# package helpers\n@PACKAGE_INIT@\n") + if(PKG_SET_INSTALL) + string(APPEND PKG_CODE "\n# package variables\n@PACKAGE_VARS@") + endif() + if(PKG_DEPENDENCIES) + string(APPEND PKG_CODE "\n# dependencies\ninclude(CMakeFindDependencyMacro)\n") + foreach(PKG_DEP IN LISTS PKG_DEPENDENCIES) + string(APPEND PKG_CODE "find_dependency(${PKG_DEP} CONFIG)\n") + endforeach() + endif() + if(NOT PKG_NO_EXPORT_TARGETS) + string(APPEND PKG_CODE "\n# targets\ninclude(\"\${CMAKE_CURRENT_LIST_DIR}/${PKG_NAME}Targets.cmake\")\n") + endif() + file(WRITE ${PKG_TEMPLATE} "${PKG_CODE}") + + # create the version file + write_basic_package_version_file(${PROJECT_BINARY_DIR}/${PKG_NAME}ConfigVersion.cmake + VERSION ${PKG_VERSION} COMPATIBILITY SameMajorVersion) + install(FILES ${PROJECT_BINARY_DIR}/${PKG_NAME}ConfigVersion.cmake DESTINATION ${PKG_CMAKE_DIR}) + + # the targets file, for the build tree and the installation + if(NOT PKG_NO_EXPORT_TARGETS) + export(EXPORT ${PKG_NAME}Targets NAMESPACE ${PKG_NAME}:: FILE ${PROJECT_BINARY_DIR}/${PKG_NAME}Targets.cmake) + install(EXPORT ${PKG_NAME}Targets NAMESPACE ${PKG_NAME}:: DESTINATION ${PKG_CMAKE_DIR}) + endif() + + # the installed config, generated aside so that it does not replace the build tree one + set(PACKAGE_VARS "${PKG_SET_INSTALL}") + configure_package_config_file(${PKG_TEMPLATE} ${PROJECT_BINARY_DIR}/CMakeFiles/${PKG_NAME}Config.cmake + INSTALL_DESTINATION ${PKG_CMAKE_DIR}) + install(FILES ${PROJECT_BINARY_DIR}/CMakeFiles/${PKG_NAME}Config.cmake DESTINATION ${PKG_CMAKE_DIR}) + + # the build tree config, the build directory is both its location and its prefix + set(PACKAGE_VARS "${PKG_SET_BUILD}") + configure_package_config_file(${PKG_TEMPLATE} ${PROJECT_BINARY_DIR}/${PKG_NAME}Config.cmake + INSTALL_DESTINATION ${PROJECT_BINARY_DIR} INSTALL_PREFIX ${PROJECT_BINARY_DIR}) +endfunction() diff --git a/LibYANGConfig.doc b/LibYANGConfig.doc new file mode 100644 index 0000000000..bea63284b3 --- /dev/null +++ b/LibYANGConfig.doc @@ -0,0 +1,12 @@ +# 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 +# LibYANG_YANG_MODULE_DIR directory with the YANG modules installed by libyang +# LibYANG_PLUGINS_DIR_EXTENSIONS directory with user extension plugins +# LibYANG_PLUGINS_DIR_TYPES directory with user type plugins diff --git a/tests/yangupdate/CMakeLists.txt b/tests/yangupdate/CMakeLists.txt index 7046fc9eff..bdbb33bded 100644 --- a/tests/yangupdate/CMakeLists.txt +++ b/tests/yangupdate/CMakeLists.txt @@ -17,7 +17,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 77ff5926e058e3ff54ee9f9fe5f8969a3794bb1f Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:16 +0100 Subject: [PATCH 08/14] 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 | 15 ++++++-- 2 files changed, 13 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..71849c62dd 100644 --- a/README.md +++ b/README.md @@ -188,8 +188,19 @@ 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 LibYANG::yang) +``` + +The version passed to `find_package()` is the SO version of the library (`LY_VERSION`). The +package also defines variables holding the values libyang was built with, they are described +in [LibYANGConfig.doc](LibYANGConfig.doc). + +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 4632aedda9abd2c17691c1fd67c6ac899d4fbb59 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:41:42 +0100 Subject: [PATCH 09/14] 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 8a959da672fbeb75d761c26e8ce1b5d020fc6dc5 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 16 Nov 2026 14:48:43 +0100 Subject: [PATCH 10/14] 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 From 2171022f74a8c8de924d74c4e946f4b0507b18c8 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 30 Nov 2026 09:20:48 +0100 Subject: [PATCH 11/14] cmake BUGFIX advanced cache vars of find modules FindCMocka and FindPCRE2 marked their result variables as advanced, which are not cached, instead of the cache variables of find_path() and find_library(). FindCBOR and FindXXHash marked none. The cache variables were thus shown in the basic view of cmake-gui and ccmake. --- CMakeModules/FindCBOR.cmake | 3 +++ CMakeModules/FindCMocka.cmake | 4 ++-- CMakeModules/FindPCRE2.cmake | 2 +- CMakeModules/FindXXHash.cmake | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CMakeModules/FindCBOR.cmake b/CMakeModules/FindCBOR.cmake index 14478f3acf..3b6ad0c5bb 100644 --- a/CMakeModules/FindCBOR.cmake +++ b/CMakeModules/FindCBOR.cmake @@ -35,3 +35,6 @@ find_library(CBOR_LIBRARY include(FindPackageHandleStandardArgs) find_package_handle_standard_args(CBOR FOUND_VAR CBOR_FOUND REQUIRED_VARS CBOR_INCLUDE_DIR CBOR_LIBRARY) + +# show the CBOR_INCLUDE_DIR and CBOR_LIBRARY variables only in the advanced view +mark_as_advanced(CBOR_INCLUDE_DIR CBOR_LIBRARY) diff --git a/CMakeModules/FindCMocka.cmake b/CMakeModules/FindCMocka.cmake index 39dd46fb61..b2d55b7eac 100644 --- a/CMakeModules/FindCMocka.cmake +++ b/CMakeModules/FindCMocka.cmake @@ -64,5 +64,5 @@ endif (CMOCKA_LIBRARY) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR) -# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view -mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES) +# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARY variables only in the advanced view +mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARY) diff --git a/CMakeModules/FindPCRE2.cmake b/CMakeModules/FindPCRE2.cmake index 57313bcbc1..391e611ed9 100644 --- a/CMakeModules/FindPCRE2.cmake +++ b/CMakeModules/FindPCRE2.cmake @@ -68,7 +68,7 @@ else() set(PCRE2_INCLUDE_DIRS ${PCRE2_INCLUDE_DIR}) set(PCRE2_LIBRARIES ${PCRE2_LIBRARY}) - mark_as_advanced(PCRE2_INCLUDE_DIRS PCRE2_LIBRARIES) + mark_as_advanced(PCRE2_INCLUDE_DIR PCRE2_LIBRARY) # Handle the QUIETLY and REQUIRED arguments and set PCRE2_FOUND to TRUE if all listed variables are TRUE. find_package_handle_standard_args(PCRE2 FOUND_VAR PCRE2_FOUND diff --git a/CMakeModules/FindXXHash.cmake b/CMakeModules/FindXXHash.cmake index e311a4fa0a..11a0aa32d9 100644 --- a/CMakeModules/FindXXHash.cmake +++ b/CMakeModules/FindXXHash.cmake @@ -40,3 +40,6 @@ check_symbol_exists(XXH3_64bits_withSeed ${XXHASH_INCLUDE_DIR}/xxhash.h HAVE_XXH include(FindPackageHandleStandardArgs) find_package_handle_standard_args(XXHash FOUND_VAR XXHASH_FOUND REQUIRED_VARS XXHASH_INCLUDE_DIR XXHASH_LIBRARY) + +# show the XXHASH_INCLUDE_DIR and XXHASH_LIBRARY variables only in the advanced view +mark_as_advanced(XXHASH_INCLUDE_DIR XXHASH_LIBRARY) From 338a8fb09e9220367b5914c340c2a054299d844c Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Mon, 30 Nov 2026 09:55:49 +0100 Subject: [PATCH 12/14] cmake REFACTOR compat object library The compat sources were compiled into libyang and again into each tool and unit test. The compat functions are now an object library, compiled once and hidden in the shared libyang. The tools and unit tests link it next to libyang, the objects of an object library reach only the targets linking it directly. It also carries the compat.h include directory and the thread library its functions call. libyang links Threads::Threads instead of the raw thread library flag, which is kept only for libyang.pc. --- CMakeLists.txt | 13 +++++++++---- CMakeModules/UseCompat.cmake | 23 +++++++++++++++-------- tests/CMakeLists.txt | 4 ++-- tools/lint/CMakeLists.txt | 5 ++--- tools/re/CMakeLists.txt | 5 ++--- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6576be803c..7e06d1046a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -419,10 +419,10 @@ if(NOT BUILD_SHARED_LIBS) set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS) - add_library(yang STATIC ${libsrc} ${compatsrc}) + add_library(yang STATIC ${libsrc}) else() set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) - add_library(yang SHARED ${libsrc} ${compatsrc}) + add_library(yang SHARED ${libsrc}) if(NOT WIN32) set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") endif() @@ -440,6 +440,9 @@ else() endif() endif() +# compat functions, hidden in the shared library +target_link_libraries(yang PRIVATE yangcompat) + if(WIN32) find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED) message(STATUS "Found at ${DIRENT_INCLUDE_DIR}") @@ -448,6 +451,7 @@ if(WIN32) ${CMAKE_CURRENT_SOURCE_DIR}/compat/posix-shims ${DIRENT_INCLUDE_DIR}) + target_include_directories(yangcompat PRIVATE ${COMPAT_POSIX_INCLUDES}) target_include_directories(yang PRIVATE ${COMPAT_POSIX_INCLUDES}) include_directories(${COMPAT_POSIX_INCLUDES}) @@ -465,11 +469,12 @@ if(NOT WIN32) string(APPEND PC_LIBS_PRIVATE " -lm") endif() -# find pthreads +# link pthreads set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads REQUIRED) -target_link_libraries(yang PRIVATE ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(yang PRIVATE Threads::Threads) if(CMAKE_THREAD_LIBS_INIT) + # the thread library flag for the .pc file string(APPEND PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}") endif() diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake index 068fc0ea98..8e66df683d 100644 --- a/CMakeModules/UseCompat.cmake +++ b/CMakeModules/UseCompat.cmake @@ -1,9 +1,7 @@ # - Use compat library providing various functions and macros that may be missing on some systems # Once done this will define # -# compatsrc - sources to add to compilation -# -# Additionally, "compat.h" include directory is added and can be included. +# yangcompat - OBJECT library with the compat functions, carrying the "compat.h" include directory # # Author Michal Vasko # Copyright (c) 2021 CESNET, z.s.p.o. @@ -40,7 +38,7 @@ macro(USE_COMPAT) check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME) set(CMAKE_THREAD_PREFER_PTHREAD TRUE) - find_package(Threads) + find_package(Threads REQUIRED) if(CMAKE_THREAD_LIBS_INIT) list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) endif() @@ -72,14 +70,23 @@ macro(USE_COMPAT) list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1) list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_DEFAULT_SOURCE) - # header and source file (adding the source directly allows for hiding its symbols) + # compat.h configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY) - include_directories(${PROJECT_BINARY_DIR}/compat) - set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c) if(WIN32) include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims) endif() + + # compat functions, hidden so that a shared library does not export them + add_library(yangcompat OBJECT ${PROJECT_SOURCE_DIR}/compat/compat.c) if(NOT HAVE_STRPTIME) - set(compatsrc ${compatsrc} ${PROJECT_SOURCE_DIR}/compat/strptime.c) + target_sources(yangcompat PRIVATE ${PROJECT_SOURCE_DIR}/compat/strptime.c) endif() + target_include_directories(yangcompat PUBLIC ${PROJECT_BINARY_DIR}/compat) + set_target_properties(yangcompat PROPERTIES POSITION_INDEPENDENT_CODE ON) + if(NOT WIN32) + set_target_properties(yangcompat PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") + endif() + + # the compat functions call pthread functions + target_link_libraries(yangcompat PUBLIC Threads::Threads) endmacro() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f6e7d98d94..6c90546f59 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,8 +27,8 @@ function(ly_add_utest) list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${TEST_SOURCE}) endforeach() - add_executable(${TEST_NAME} ${TEST_SOURCES} ${compatsrc}) - target_link_libraries(${TEST_NAME} yang ${CMOCKA_LIBRARIES}) + add_executable(${TEST_NAME} ${TEST_SOURCES}) + target_link_libraries(${TEST_NAME} yang yangcompat ${CMOCKA_LIBRARIES}) if(TARGET yang_dll_for_tests) add_dependencies(${TEST_NAME} yang_dll_for_tests) endif() diff --git a/tools/lint/CMakeLists.txt b/tools/lint/CMakeLists.txt index 3fd23f40f4..b386e3f352 100644 --- a/tools/lint/CMakeLists.txt +++ b/tools/lint/CMakeLists.txt @@ -41,9 +41,8 @@ set(format_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.h PARENT_SCOPE) -add_executable(yanglint ${lintsrc} ${compatsrc}) -# the compat sources may call pthread functions -target_link_libraries(yanglint yang ${CMAKE_THREAD_LIBS_INIT}) +add_executable(yanglint ${lintsrc}) +target_link_libraries(yanglint yang yangcompat) 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 f0fe050c5e..655dd5c4da 100644 --- a/tools/re/CMakeLists.txt +++ b/tools/re/CMakeLists.txt @@ -8,9 +8,8 @@ set(format_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.c PARENT_SCOPE) -add_executable(yangre ${resrc} ${compatsrc}) -# the compat sources may call pthread functions -target_link_libraries(yangre yang ${CMAKE_THREAD_LIBS_INIT}) +add_executable(yangre ${resrc}) +target_link_libraries(yangre yang yangcompat) 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 6ead3c7dbecc20694b69df5a2ba87fa7a8d612fa Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Tue, 1 Dec 2026 16:23:18 +0100 Subject: [PATCH 13/14] compat REFACTOR remove unused dirname shim The Windows dirname() implementation using shlwapi was removed from compat, but its declaration in posix-shims/libgen.h and the shlwapi link remained. Nothing includes libgen.h any more. --- CMakeLists.txt | 2 +- compat/posix-shims/libgen.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 compat/posix-shims/libgen.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e06d1046a..3d254d7c5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -457,7 +457,7 @@ if(WIN32) find_package(pthreads REQUIRED) # ws2_32 is public, ly_config.h includes winsock2.h - target_link_libraries(yang PRIVATE PThreads4W::PThreads4W shlwapi.lib PUBLIC ws2_32) + target_link_libraries(yang PRIVATE PThreads4W::PThreads4W PUBLIC ws2_32) endif() set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION}) diff --git a/compat/posix-shims/libgen.h b/compat/posix-shims/libgen.h deleted file mode 100644 index 014bb0756d..0000000000 --- a/compat/posix-shims/libgen.h +++ /dev/null @@ -1 +0,0 @@ -char *dirname(char *path); From d53089dc5e2c535c7befa1308a5c2f1f07989a6d Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Tue, 1 Dec 2026 16:23:23 +0100 Subject: [PATCH 14/14] cmake REFACTOR windows dependencies of compat The and posix-shims include directories and the pthreads library on Windows are dependencies of the compat functions, but were set up for libyang after use_compat(), partly three times over. use_compat() now sets them up on the compat object library, which passes them on to libyang and to the tools and unit tests linking it, those use the POSIX headers too. --- CMakeLists.txt | 14 +------------- CMakeModules/UseCompat.cmake | 14 +++++++++++--- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d254d7c5e..8b14a40944 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -444,20 +444,8 @@ endif() target_link_libraries(yang PRIVATE yangcompat) if(WIN32) - find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED) - message(STATUS "Found at ${DIRENT_INCLUDE_DIR}") - - set(COMPAT_POSIX_INCLUDES - ${CMAKE_CURRENT_SOURCE_DIR}/compat/posix-shims - ${DIRENT_INCLUDE_DIR}) - - target_include_directories(yangcompat PRIVATE ${COMPAT_POSIX_INCLUDES}) - target_include_directories(yang PRIVATE ${COMPAT_POSIX_INCLUDES}) - include_directories(${COMPAT_POSIX_INCLUDES}) - - find_package(pthreads REQUIRED) # ws2_32 is public, ly_config.h includes winsock2.h - target_link_libraries(yang PRIVATE PThreads4W::PThreads4W PUBLIC ws2_32) + target_link_libraries(yang PUBLIC ws2_32) endif() set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION}) diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake index 8e66df683d..182b1496a2 100644 --- a/CMakeModules/UseCompat.cmake +++ b/CMakeModules/UseCompat.cmake @@ -2,6 +2,7 @@ # Once done this will define # # yangcompat - OBJECT library with the compat functions, carrying the "compat.h" include directory +# and, on Windows, the POSIX header shims # # Author Michal Vasko # Copyright (c) 2021 CESNET, z.s.p.o. @@ -72,9 +73,6 @@ macro(USE_COMPAT) # compat.h configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY) - if(WIN32) - include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims) - endif() # compat functions, hidden so that a shared library does not export them add_library(yangcompat OBJECT ${PROJECT_SOURCE_DIR}/compat/compat.c) @@ -82,6 +80,12 @@ macro(USE_COMPAT) target_sources(yangcompat PRIVATE ${PROJECT_SOURCE_DIR}/compat/strptime.c) endif() target_include_directories(yangcompat PUBLIC ${PROJECT_BINARY_DIR}/compat) + if(WIN32) + # POSIX headers missing on Windows + find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED) + message(STATUS "Found at ${DIRENT_INCLUDE_DIR}") + target_include_directories(yangcompat PUBLIC ${PROJECT_SOURCE_DIR}/compat/posix-shims ${DIRENT_INCLUDE_DIR}) + endif() set_target_properties(yangcompat PROPERTIES POSITION_INDEPENDENT_CODE ON) if(NOT WIN32) set_target_properties(yangcompat PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") @@ -89,4 +93,8 @@ macro(USE_COMPAT) # the compat functions call pthread functions target_link_libraries(yangcompat PUBLIC Threads::Threads) + if(WIN32) + find_package(pthreads REQUIRED) + target_link_libraries(yangcompat PUBLIC PThreads4W::PThreads4W) + endif() endmacro()