Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 && \
Expand Down Expand Up @@ -315,7 +301,6 @@ jobs:
build-packages:
name: ${{ matrix.config.name }}
runs-on: ubuntu-latest
needs: git-branch
strategy:
fail-fast: false
matrix:
Expand All @@ -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:
Expand All @@ -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
101 changes: 67 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -372,9 +376,12 @@ if(LY_HAVE_REGEX_H)
endif()

# generate and copy all public header files
configure_file(${PROJECT_SOURCE_DIR}/src/ly_config.h.in ${PROJECT_BINARY_DIR}/libyang/ly_config.h @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/src/version.h.in ${PROJECT_BINARY_DIR}/libyang/version.h @ONLY)
file(COPY ${headers} DESTINATION ${PROJECT_BINARY_DIR}/libyang)
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")
Expand Down Expand Up @@ -424,7 +431,7 @@ else()
endif()

#link dl
target_link_libraries(yang ${CMAKE_DL_LIBS})
target_link_libraries(yang PRIVATE ${CMAKE_DL_LIBS})
endif()

if(WIN32)
Expand All @@ -443,43 +450,44 @@ if(WIN32)

find_package(pthreads REQUIRED)
set(COMPAT_WIN_LIBRARIES PThreads4W::PThreads4W shlwapi.lib ws2_32)
target_link_libraries(yang ${COMPAT_WIN_LIBRARIES})
# ws2_32 is public, ly_config.h includes winsock2.h
target_link_libraries(yang PRIVATE PThreads4W::PThreads4W shlwapi.lib PUBLIC ws2_32)
endif()

set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
target_include_directories(yang PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# link math
if(NOT WIN32)
target_link_libraries(yang m)
target_link_libraries(yang PRIVATE m)
endif()

# find pthreads
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(yang PRIVATE ${CMAKE_THREAD_LIBS_INIT})

# find PCRE2 library
unset(PCRE2_LIBRARY CACHE)
find_package(PCRE2 10.21 REQUIRED)
include_directories(${PCRE2_INCLUDE_DIRS})
target_link_libraries(yang ${PCRE2_LIBRARIES})
target_link_libraries(yang PRIVATE ${PCRE2_LIBRARIES})

# link libcbor if found
if(CBOR_FOUND)
if(TARGET yangobj)
target_include_directories(yangobj PRIVATE ${CBOR_INCLUDE_DIR})
endif()
target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR})
target_link_libraries(yang ${CBOR_LIBRARY})
target_link_libraries(yang PRIVATE ${CBOR_LIBRARY})
endif()

# XXHash include and library
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})
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
Expand All @@ -491,33 +499,58 @@ 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()
# CMake config package, for the build tree and the installation
if(BUILD_SHARED_LIBS)
# the package version is the SO version, which all the dependent projects check
gen_config_package(LibYANG ${LIBYANG_SOVERSION_FULL})
else()
message(STATUS "Static build, CMake config package is not installed")
endif()

# generate and install pkg-config file
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(PC_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
else()
set(PC_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
endif()
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(PC_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
else()
set(PC_LIBDIR "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
endif()
# dependencies only in Libs.private, Requires.private would need their .pc files even for dynamic linking
set(PC_LIBS_PRIVATE "-lpcre2-8")
if(XXHASH_FOUND)
string(APPEND PC_LIBS_PRIVATE " -lxxhash")
endif()
if(CBOR_FOUND)
string(APPEND PC_LIBS_PRIVATE " -lcbor")
endif()
if(NOT WIN32)
string(APPEND PC_LIBS_PRIVATE " -lm")
# only the shared library loads plugins
if(BUILD_SHARED_LIBS AND CMAKE_DL_LIBS)
string(APPEND PC_LIBS_PRIVATE " -l${CMAKE_DL_LIBS}")
Comment thread
michalvasko marked this conversation as resolved.
Resolved
endif()
endif()
if(CMAKE_THREAD_LIBS_INIT)
string(APPEND PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}")
endif()
configure_file("libyang.pc.in" "libyang.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

# tests
if(ENABLE_TESTS)
Expand Down
42 changes: 42 additions & 0 deletions CMakeModules/GenConfigPackage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generate the CMake config package of a library for the build tree and the installation
#
# gen_config_package(LIB_NAME LIB_SOVERSION_FULL [EXPORTS <export-set>...] [PATH_VARS <var>...])
# LIB_NAME package name, the template is <LIB_NAME>Config.cmake.in in the source root
# LIB_SOVERSION_FULL package version, the full SO version of the library
# EXPORTS optional, export sets installed besides <LIB_NAME>Targets
# PATH_VARS optional, template variables with installed paths, <var>_BUILD holds the build tree path
#
# The template reads its other variables from the scope of the caller.
include(CMakePackageConfigHelpers)

function(gen_config_package LIB_NAME LIB_SOVERSION_FULL)
# the optional arguments into LIB_EXPORTS and LIB_PATH_VARS
cmake_parse_arguments(PARSE_ARGV 2 LIB "" "" "EXPORTS;PATH_VARS")
set(LIB_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME})
set(LIB_TEMPLATE ${PROJECT_SOURCE_DIR}/${LIB_NAME}Config.cmake.in)

# create the version file
write_basic_package_version_file(${PROJECT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake
VERSION ${LIB_SOVERSION_FULL} COMPATIBILITY SameMajorVersion)
install(FILES ${PROJECT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake DESTINATION ${LIB_CONFIG_DIR})

# the targets file of each export set, for the build tree and the installation
foreach(LIB_EXPORT IN ITEMS ${LIB_NAME}Targets ${LIB_EXPORTS})
export(EXPORT ${LIB_EXPORT} NAMESPACE ${LIB_NAME}:: FILE ${PROJECT_BINARY_DIR}/${LIB_EXPORT}.cmake)
install(EXPORT ${LIB_EXPORT} NAMESPACE ${LIB_NAME}:: DESTINATION ${LIB_CONFIG_DIR})
endforeach()

# the installed config, generated aside so that it does not replace the build tree one
configure_package_config_file(${LIB_TEMPLATE} ${PROJECT_BINARY_DIR}/CMakeFiles/${LIB_NAME}Config.cmake
INSTALL_DESTINATION ${LIB_CONFIG_DIR} PATH_VARS ${LIB_PATH_VARS})
install(FILES ${PROJECT_BINARY_DIR}/CMakeFiles/${LIB_NAME}Config.cmake DESTINATION ${LIB_CONFIG_DIR})

# each path variable set to its <var>_BUILD value
foreach(LIB_VAR IN LISTS LIB_PATH_VARS)
set(${LIB_VAR} ${${LIB_VAR}_BUILD})
endforeach()

# the build tree config, the build directory is both its location and its prefix
configure_package_config_file(${LIB_TEMPLATE} ${PROJECT_BINARY_DIR}/${LIB_NAME}Config.cmake
INSTALL_DESTINATION ${PROJECT_BINARY_DIR} INSTALL_PREFIX ${PROJECT_BINARY_DIR} PATH_VARS ${LIB_PATH_VARS})
endfunction()
8 changes: 6 additions & 2 deletions CMakeModules/UseCompat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ macro(USE_COMPAT)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
if(CMAKE_THREAD_LIBS_INIT)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
endif()
check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} "") # cmake <= 3.19 needs at least one item
if(CMAKE_THREAD_LIBS_INIT)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
endif()

test_big_endian(IS_BIG_ENDIAN)

Expand Down
Loading
Loading