Skip to content

Commit

Permalink
cmake UPDATE option to build server as a library
Browse files Browse the repository at this point in the history
... and also to disable executable build.
  • Loading branch information
michalvasko committed May 2, 2024
1 parent 9572455 commit 6187713
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
57 changes: 37 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,33 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99")
#
# options
#
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
option(BUILD_SERVER "Build and install neotpeer2-server" ON)
option(BUILD_CLI "Build and install neotpeer2-cli" ON)
option(ENABLE_TESTS "Build tests" ON)
if(("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG") OR ("${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO"))
if(ENABLE_TESTS AND NOT BUILD_SERVER)
message(WARNING "Tests require the server executable, disabling them.")
set(ENABLE_TESTS OFF)
endif()
if(ENABLE_TESTS AND "${BUILD_TYPE_UPPER}" STREQUAL "DEBUG" OR "${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO")
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
else()
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
endif()
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
option(BUILD_CLI "Build and install neotpeer2-cli" ON)
option(ENABLE_URL "Enable URL capability" ON)
option(ENABLE_URL_FILE "Enable the 'file' URL protocol (security advisory: allows authorized users to access local FS as the server user)" OFF)
set(SSH_AUTHORIZED_KEYS_FORMAT "%h/.ssh/authorized_keys" CACHE STRING "sshd-like pattern (with '%h', '%u', '%U') for determining path to users' SSH authorized_keys file.")
set(THREAD_COUNT 3 CACHE STRING "Number of threads accepting new sessions and handling requests")
set(POLL_IO_TIMEOUT 10 CACHE STRING "Timeout in milliseconds of polling sessions for new data. It is also used for synchronization of low level IO such as sending a reply while a notification is being sent")
set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules/netopeer2" CACHE STRING "Directory where to copy the YANG modules to")
set(DATA_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/netopeer2" CACHE STRING "Directory with shared netopeer2 data")
set(NETOPEER2_LIB "no" CACHE STRING "Build netopeer2 library with the setup function or also with the full server functionality, allowed values \"no\", \"setup\", and \"server\"")
if(NOT NETOPEER2_LIB STREQUAL "no" AND NOT NETOPEER2_LIB STREQUAL "setup" AND NOT NETOPEER2_LIB STREQUAL "server")
message(FATAL_ERROR "Invalid value \"${NETOPEER2_LIB}\" of NETOPEER2_LIB.")
endif()

# script options
option(SYSREPO_SETUP "Install required modules with their default configuration into sysrepo using a script. If disabled, a small library is built with the same functionality" ON)
option(SYSREPO_SETUP "Install required modules with their default configuration into sysrepo using a script" ON)
set(MODULES_PERMS 600 CACHE STRING "File access permissions set for all the server modules")
if(NOT MODULES_OWNER)
execute_process(COMMAND id -un RESULT_VARIABLE RET
Expand Down Expand Up @@ -230,7 +239,10 @@ use_compat()

# netopeer2-server
add_library(serverobj OBJECT ${SERVER_SRC})
add_executable(netopeer2-server $<TARGET_OBJECTS:serverobj> src/main.c ${compatsrc})
set(serverlibs "")
if(BUILD_SERVER)
add_executable(netopeer2-server $<TARGET_OBJECTS:serverobj> src/main.c ${compatsrc})
endif()

#
# dependencies
Expand All @@ -239,15 +251,15 @@ add_executable(netopeer2-server $<TARGET_OBJECTS:serverobj> src/main.c ${compats
# librt (not required on OSX or QNX)
find_library(LIBRT rt)
if(LIBRT)
target_link_libraries(netopeer2-server ${LIBRT})
set(serverlibs ${serverlibs} ${LIBRT})
endif()

# libnetconf2 (was already found)
target_link_libraries(netopeer2-server ${LIBNETCONF2_LIBRARIES})
set(serverlibs ${serverlibs} ${LIBNETCONF2_LIBRARIES})

# libssh (was already found, if exists)
if(LIBSSH_FOUND AND LIBNETCONF2_ENABLED_SSH_TLS)
target_link_libraries(netopeer2-server ${LIBSSH_LIBRARIES})
set(serverlibs ${serverlibs} ${LIBSSH_LIBRARIES})
include_directories(${LIBSSH_INCLUDE_DIRS})
endif()

Expand All @@ -257,9 +269,11 @@ if(ENABLE_URL)
if(CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
if(TARGET CURL::libcurl)
target_link_libraries(netopeer2-server CURL::libcurl)
if(BUILD_SERVER)
target_link_libraries(netopeer2-server CURL::libcurl)
endif()
else()
target_link_libraries(netopeer2-server ${CURL_LIBRARIES})
set(serverlibs ${serverlibs} ${CURL_LIBRARIES})
endif()
set(NP2SRV_URL_CAPAB 1)
if(ENABLE_URL_FILE)
Expand All @@ -280,7 +294,7 @@ endif()
find_package(LibSystemd)
if(LIBSYSTEMD_FOUND)
set(NP2SRV_HAVE_SYSTEMD 1)
target_link_libraries(netopeer2-server ${LIBSYSTEMD_LIBRARIES})
set(serverlibs ${serverlibs} ${LIBSYSTEMD_LIBRARIES})
include_directories(${LIBSYSTEMD_INCLUDE_DIRS})
message(STATUS "systemd system service unit path: ${SYSTEMD_UNIT_DIR}")
else()
Expand All @@ -290,19 +304,19 @@ endif()
# pthread
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(netopeer2-server ${CMAKE_THREAD_LIBS_INIT})
set(serverlibs ${serverlibs} ${CMAKE_THREAD_LIBS_INIT})
list(APPEND CMAKE_REQUIRED_FLAGS ${CMAKE_THREAD_LIBS_INIT})

# libyang
find_package(LibYANG ${LIBYANG_DEP_SOVERSION} REQUIRED)
target_link_libraries(netopeer2-server ${LIBYANG_LIBRARIES})
set(serverlibs ${serverlibs} ${LIBYANG_LIBRARIES})
include_directories(${LIBYANG_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_INCLUDES ${LIBYANG_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBYANG_LIBRARIES})

# sysrepo
find_package(Sysrepo ${SYSREPO_DEP_SOVERSION} REQUIRED)
target_link_libraries(netopeer2-server ${SYSREPO_LIBRARIES})
set(serverlibs ${serverlibs} ${SYSREPO_LIBRARIES})
include_directories(${SYSREPO_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_INCLUDES ${SYSREPO_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${SYSREPO_LIBRARIES})
Expand Down Expand Up @@ -335,10 +349,13 @@ install(DIRECTORY "${PROJECT_SOURCE_DIR}/modules/" DESTINATION ${YANG_MODULE_DIR
install(DIRECTORY "${PROJECT_SOURCE_DIR}/scripts/" DESTINATION ${DATA_DIR}/scripts USE_SOURCE_PERMISSIONS)

# install the binary, required modules, and default configuration
install(TARGETS netopeer2-server DESTINATION ${CMAKE_INSTALL_SBINDIR})
install(FILES ${PROJECT_SOURCE_DIR}/doc/netopeer2-server.8 DESTINATION ${CMAKE_INSTALL_MANDIR}/man8)
if(NP2SRV_HAVE_SYSTEMD)
install(FILES ${PROJECT_BINARY_DIR}/netopeer2-server.service DESTINATION ${SYSTEMD_UNIT_DIR})
if(BUILD_SERVER)
target_link_libraries(netopeer2-server ${serverlibs})
install(TARGETS netopeer2-server DESTINATION ${CMAKE_INSTALL_SBINDIR})
install(FILES ${PROJECT_SOURCE_DIR}/doc/netopeer2-server.8 DESTINATION ${CMAKE_INSTALL_MANDIR}/man8)
if(NP2SRV_HAVE_SYSTEMD)
install(FILES ${PROJECT_BINARY_DIR}/netopeer2-server.service DESTINATION ${SYSTEMD_UNIT_DIR})
endif()
endif()

install(FILES "${CMAKE_SOURCE_DIR}/pam/netopeer2.conf" DESTINATION "${CMAKE_INSTALL_SYSCONFDIR}/pam.d")
Expand Down Expand Up @@ -419,8 +436,8 @@ if(BUILD_CLI)
add_subdirectory(cli)
endif()

# np2 sysrepo setup lib
if(NOT SYSREPO_SETUP)
# netopeer2 lib
if(NOT NETOPEER2_LIB STREQUAL "no")
add_subdirectory(lib)
endif()

Expand Down
40 changes: 33 additions & 7 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,53 @@ if(NOT NP2SRV_VERSION)
message(FATAL_ERROR "Please use the root CMakeLists file instead.")
endif()

project(np2_sr_setup C)
project(libnetopeer2 C)

include(GNUInstallDirs)

# source files
set(LIB_SRC
np2_sr_setup.c)

if(NETOPEER2_LIB STREQUAL "server")
# build server main function as a library function
set(LIB_SRC ${LIB_SRC} ${serverobj} ${PROJECT_SOURCE_DIR}/../src/main.c ${compatsrc})
add_definitions(-Dmain=np2_server)
set(NETOPEER2_SERVER_FUNC "\
/**
* @brief netopeer2-server main function.
*
* @param[in] argc Argument count.
* @param[in] argv Argument list, should always be at least the path to the binary.
* @return EXIT_SUCCESS on success.
* @return EXIT_FAILURE on error.
*/
int np2_server(int argc, char *argv[]);")
endif()

configure_file(${PROJECT_SOURCE_DIR}/netopeer2.h.in ${PROJECT_BINARY_DIR}/include/netopeer2.h ESCAPE_QUOTES @ONLY)

# generate YANG header files
add_custom_command(OUTPUT np2_sr_yang.h
COMMAND ${CMAKE_COMMAND} -E env
NP2_MODULE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/../modules
NP2_MODULE_DIR=${PROJECT_SOURCE_DIR}/../modules
LN2_MODULE_DIR=${LN2_YANG_MODULE_DIR}
NP2_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/generate.sh
${PROJECT_SOURCE_DIR}/generate.sh
COMMENT "Generating YANG header files (generate.sh)..."
)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

# lib target
add_library(np2_sr_setup ${LIB_SRC} np2_sr_yang.h)
add_library(netopeer2 ${LIB_SRC} np2_sr_yang.h)

# add dependencies
target_link_libraries(netopeer2 ${serverlibs})
if(TARGET CURL::libcurl)
target_link_libraries(netopeer2 CURL::libcurl)
endif()

# reuse server variables
target_link_libraries(np2_sr_setup ${LIBYANG_LIBRARIES})
target_link_libraries(np2_sr_setup ${SYSREPO_LIBRARIES})
# install
install(TARGETS netopeer2 DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${PROJECT_BINARY_DIR}/include/netopeer2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
12 changes: 7 additions & 5 deletions lib/np2_sr_setup.h → lib/netopeer2.h.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file np2_sr_setup.h
* @file netopeer2.h
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief netopeer2-server sysrepo YANG module setup library header
* @brief netopeer2-server library header
*
* @copyright
* Copyright (c) 2024 Deutsche Telekom AG.
Expand All @@ -14,8 +14,8 @@
* https://opensource.org/licenses/BSD-3-Clause
*/

#ifndef NP2_SR_SETUP_H_
#define NP2_SR_SETUP_H_
#ifndef NETOPEER2_H_
#define NETOPEER2_H_

#include <sys/stat.h>

Expand All @@ -32,4 +32,6 @@
*/
int np2_sr_setup(const char *owner, const char *group, mode_t perm);

#endif /* NP2_SR_SETUP_H_ */
@NETOPEER2_SERVER_FUNC@

#endif /* NETOPEER2_H_ */

0 comments on commit 6187713

Please sign in to comment.