diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19789b3bc8..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 }} @@ -266,9 +251,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 && \ @@ -315,7 +301,6 @@ jobs: build-packages: name: ${{ matrix.config.name }} runs-on: ubuntu-latest - needs: git-branch strategy: fail-fast: false matrix: @@ -333,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: @@ -349,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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 005742a8b7..8b14a40944 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 @@ -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 @@ -205,6 +208,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 @@ -235,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 @@ -342,11 +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 headers src/lcbor.h) 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() @@ -371,10 +375,13 @@ if(LY_HAVE_REGEX_H) check_symbol_exists(REG_STARTEND "regex.h" HAVE_REG_STARTEND) 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) +# 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}/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}/include/libyang/${h_name} COPYONLY) +endforeach() # DOC-only target with no extra dependencies if("${BUILD_TYPE_UPPER}" STREQUAL "DOCONLY") @@ -395,6 +402,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) @@ -408,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() @@ -422,56 +433,59 @@ else() set(CMAKE_DL_LIBS dlfcn-win32::dl) endif() - #link dl - target_link_libraries(yang ${CMAKE_DL_LIBS}) + #link dl, only the shared library loads plugins + 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() endif() -if(WIN32) - find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED) - message(STATUS "Found at ${DIRENT_INCLUDE_DIR}") +# compat functions, hidden in the shared library +target_link_libraries(yang PRIVATE yangcompat) - set(COMPAT_POSIX_INCLUDES - ${CMAKE_CURRENT_SOURCE_DIR}/compat/posix-shims - ${DIRENT_INCLUDE_DIR}) - - target_include_directories(yang PRIVATE ${COMPAT_POSIX_INCLUDES}) - include_directories(${COMPAT_POSIX_INCLUDES}) - - find_package(pthreads REQUIRED) - set(COMPAT_WIN_LIBRARIES PThreads4W::PThreads4W shlwapi.lib ws2_32) - target_link_libraries(yang ${COMPAT_WIN_LIBRARIES}) +if(WIN32) + # ws2_32 is public, ly_config.h includes winsock2.h + target_link_libraries(yang 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 +# link pthreads set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads REQUIRED) -target_link_libraries(yang ${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() # 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() # 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}) + target_link_libraries(yang PRIVATE ${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 @@ -483,34 +497,39 @@ 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 DESTINATION ${CMAKE_INSTALL_LIBDIR}) +# 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) -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() +# 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 +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) enable_testing() 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) 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/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake index 53bb0236e7..182b1496a2 100644 --- a/CMakeModules/UseCompat.cmake +++ b/CMakeModules/UseCompat.cmake @@ -1,9 +1,8 @@ # - 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 +# and, on Windows, the POSIX header shims # # Author Michal Vasko # Copyright (c) 2021 CESNET, z.s.p.o. @@ -19,9 +18,6 @@ include(CheckFunctionExists) include(CheckIncludeFile) include(TestBigEndian) include(CheckStructHasMember) -if(POLICY CMP0075) - cmake_policy(SET CMP0075 NEW) -endif() macro(USE_COMPAT) # compatibility checks @@ -43,10 +39,14 @@ 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) - list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + find_package(Threads REQUIRED) + 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) @@ -71,14 +71,30 @@ 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) + + # 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) + target_sources(yangcompat PRIVATE ${PROJECT_SOURCE_DIR}/compat/strptime.c) + endif() + target_include_directories(yangcompat PUBLIC ${PROJECT_BINARY_DIR}/compat) if(WIN32) - include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims) + # 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() - if(NOT HAVE_STRPTIME) - set(compatsrc ${compatsrc} ${PROJECT_SOURCE_DIR}/compat/strptime.c) + 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) + if(WIN32) + find_package(pthreads REQUIRED) + target_link_libraries(yangcompat PUBLIC PThreads4W::PThreads4W) endif() endmacro() 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/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/README.md b/README.md index 036318a0b7..71849c62dd 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 @@ -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 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); 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/ 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.]\+' 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} 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/tests/yangupdate/CMakeLists.txt b/tests/yangupdate/CMakeLists.txt index 17b08d9325..bdbb33bded 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) @@ -11,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) diff --git a/tools/lint/CMakeLists.txt b/tools/lint/CMakeLists.txt index 8e83e24b2b..b386e3f352 100644 --- a/tools/lint/CMakeLists.txt +++ b/tools/lint/CMakeLists.txt @@ -41,8 +41,8 @@ set(format_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.h PARENT_SCOPE) -add_executable(yanglint ${lintsrc} ${compatsrc}) -target_link_libraries(yanglint yang) +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 7b6de226f1..655dd5c4da 100644 --- a/tools/re/CMakeLists.txt +++ b/tools/re/CMakeLists.txt @@ -8,8 +8,8 @@ set(format_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.c PARENT_SCOPE) -add_executable(yangre ${resrc} ${compatsrc}) -target_link_libraries(yangre yang) +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}) 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