Skip to content

cmake FEATURE LibYANG CMake config package - #2651

Open
Roytak wants to merge 14 commits into
develfrom
cmake-config
Open

Roytak wants to merge 14 commits into
develfrom
cmake-config

Conversation

@Roytak

@Roytak Roytak commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Installs a LibYANG CMake package so that dependent projects can use find_package(LibYANG CONFIG) and link the imported target LibYANG::yang instead of a copied Find module or pkg-config. The package version is the SO version with SameMajorVersion compatibility.

It contains these related changes:

  • FindLibYANG.cmake is removed and the README describes the package instead (copies kept in other projects still work),
  • the minimum CMake version is now 3.12 (the declared 2.8.12 no longer worked),
  • private dependencies of yang no longer leak to linking targets, and the DLL is installed into bin on Windows,
  • the internal lcbor.h is no longer installed, so <cbor.h> is not part of the public interface,
  • pkg-config --cflags libyang works without the PCRE2 and xxHash development packages, because private dependencies are listed only in Libs.private, which now also includes the ones that were missing,
  • the build refreshes its copies of the public headers whenever a header changes, without a manual CMake rerun,
  • the deb and rpm -dev packages ship the CMake package, the unused apkg tests are removed, and the CI package jobs build the PR's own sources instead of cloning the target branch.

Comment thread CMakeLists.txt Resolved
Comment thread CMakeModules/LibYANGConfigPackage.cmake Resolved
@Roytak Roytak mentioned this pull request Nov 27, 2026
@Roytak
Roytak force-pushed the cmake-config branch 3 times, most recently from 81ab2ed to bc2e38f Compare December 1, 2026 15:34
Comment thread tools/update/CMakeLists.txt Outdated
# yang update library
add_library(${LIB_NAME} STATIC ${lib_src})
# yang update library, uses also internal libyang functions
add_library(${LIB_NAME} STATIC ${lib_src} $<TARGET_OBJECTS:yang>)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are duplicating the yang functions in ${lib_src} and then $<TARGET_OBJECTS:yang>. Only the latter should be needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib_src was actually the sources of yang update, not yang, so I just renamed the variable to avoid future confusion.

Comment thread CMakeLists.txt Outdated
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
# link pthreads, found by use_compat()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not rely on use_compat(), it may be changed or reordered but mainly is in a separate subordinate List. But you can first check it (if(TARGET Threads::Threads)...) and search for the threads yourself in case the check fails.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread CMakeLists.txt Outdated
message(STATUS "Static build, CMake config package is not installed")
endif()

# pkg-config file paths, the install directories may be absolute

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not by default and even if set that way manually, it should not really be an issue (and is a user mistake).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment thread CMakeLists.txt Resolved
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.
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.
@Roytak
Roytak marked this pull request as ready for review December 2, 2026 09:07
Roytak added 12 commits December 3, 2026 15:32
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.
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 <libyang/libyang.h> both in the build tree and after
installation, and the DLL is installed into bin on Windows.
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.
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 <cbor.h> part of the public interface. It is
now documented with the other internal headers.
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.
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.
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.
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.
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.
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.
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.
The <dirent.h> 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants