Skip to content
Snippets Groups Projects
Commit f858a786 authored by Benny Baumann's avatar Benny Baumann
Browse files

add: Basic CMake build system

Does not yet support gettext translations
parent 71bb14b7
No related branches found
No related tags found
1 merge request!2Draft: Basic CMake build system
Pipeline #7692 failed
cmake_minimum_required(VERSION 3.20)
project(rmrf LANGUAGES C CXX)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
set(SRCDIR "${CMAKE_SOURCE_DIR}/src")
set(TESTDIR "${CMAKE_SOURCE_DIR}/test")
find_package(PkgConfig REQUIRED)
find_package(LibNL 3.5 REQUIRED COMPONENTS route netfilter genl)
find_package(Gettext 0.21 REQUIRED)
find_package(OpenSSL 3.0 REQUIRED COMPONENTS Crypto SSL)
find_package(Threads REQUIRED)
set(CURSES_NEED_NCURSES TRUE)
set(CURSES_NEED_WIDE TRUE)
find_package(Curses 6.3 REQUIRED)
add_library(Curses UNKNOWN IMPORTED)
set_target_properties(Curses PROPERTIES
IMPORTED_LOCATION "${CURSES_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CURSES_INCLUDE_DIRS}"
)
find_library(ev_LIBRARY NAMES ev libev)
add_library(ev UNKNOWN IMPORTED)
set_target_properties(ev PROPERTIES
IMPORTED_LOCATION "${ev_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${ev_INCLUDE_DIR}"
)
pkg_search_module(systemd REQUIRED libsystemd)
#set(Boost_DEBUG 1)
#set(Boost_VERBOSE 1)
# Hack to make it work on Ubuntu
set(BOOST_ROOT /usr)
find_package(Boost 1.74)
add_custom_target(build)
set(CMAKE_CXX_STANDARD 17)
file(GLOB APPS ${SRCDIR}/app/*.cpp ${SRCDIR}/app/*.c)
file(GLOB_RECURSE SOURCES ${SRCDIR}/*.cpp ${SRCDIR}/*.c EXCLUDE ${SRCDIR}/app/)
list(REMOVE_ITEM SOURCES ${APPS})
add_library(source OBJECT ${SOURCES})
target_include_directories(source PUBLIC ${SRCDIR})
target_link_libraries(source LibNL::NL)
target_link_libraries(source Curses)
target_link_libraries(source ev)
target_link_libraries(source OpenSSL::SSL)
target_link_libraries(source OpenSSL::Crypto)
if(systemd_FOUND)
target_link_libraries(source systemd)
endif()
foreach(APP ${APPS})
get_filename_component(APP_NAME ${APP} NAME_WLE)
message("-- Found App: ${APP_NAME} in ${APP}")
add_executable(${APP_NAME} ${APP})
target_include_directories(${APP_NAME} PUBLIC ${SRCDIR})
target_link_libraries(${APP_NAME} source)
add_dependencies(build ${APP_NAME})
endforeach()
if(Boost_FOUND)
message("-- Enabling unit tests")
file(GLOB TESTS ${TESTDIR}/*.cpp)
foreach(TEST ${TESTS})
get_filename_component(TEST_NAME ${TEST} NAME_WLE)
message("-- Found Test: ${TEST_NAME} in ${TEST}")
add_executable(test_${TEST_NAME} ${TEST})
set_property(SOURCE ${TEST} APPEND PROPERTY COMPILE_DEFINITIONS BOOST_AUTO_TEST_MAIN)
set_property(SOURCE ${TEST} APPEND PROPERTY COMPILE_DEFINITIONS BOOST_TEST_MODULE=${TEST_NAME})
add_test(NAME ${TEST_NAME} COMMAND test_${TEST_NAME})
target_include_directories(test_${TEST_NAME} PUBLIC ${SRCDIR})
target_link_libraries(test_${TEST_NAME} LINK_PUBLIC ${Boost_LIBRARIES})
endforeach()
endif()
add_custom_target(style EXCLUDE_FROM_ALL
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${SRCDIR}/*.c
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${SRCDIR}/*.h
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${SRCDIR}/*.cpp
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${SRCDIR}/*.hpp
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${TESTDIR}/*.c
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${TESTDIR}/*.h
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${TESTDIR}/*.cpp
COMMAND astyle --mode=c --options=none --recursive -q -Q -s4 -f -H -j -J -k1 -W3 -p -U -xb -y ${TESTDIR}/*.hpp
)
include(CTest)
# message("Searching for Linux netlink library")
#if (LibNL_FIND_REQUIRED)
# message(" (required)")
#else (LibNL_FIND_REQUIRED)
# message(" (not required)")
#endif (LibNL_FIND_REQUIRED)
find_package(PkgConfig)
pkg_search_module(PC_LibNL QUIET nl nl-3 nl-3.0 libnl-3 libnl-3.0)
pkg_search_module(PC_LibNL-Route QUIET nl-route nl-route-3 nl-route-3.0 libnl-route-3 libnl-route-3.0)
pkg_search_module(PC_LibNL-NetFilter QUIET nl-nf nl-nf-3 nl-nf-3.0 libnl-nf-3 libnl-nf-3.0)
pkg_search_module(PC_LibNL-GenL QUIET nl-genl nl-genl-3 nl-genl-3.0 libnl-genl-3 libnl-genl-3.0)
find_path(LibNL_INCLUDE_DIR netlink/netlink.h
/usr/include
/usr/include/libnl3
/usr/local/include
/usr/local/include/libnl3
${PC_LibNL_LIBRARY}
)
find_path(LibNL_ROUTE_INCLUDE_DIR netlink/route/route.h
/usr/include
/usr/include/libnl3
/usr/local/include
/usr/local/include/libnl3
${PC_LibNL_Route_LIBRARY}
)
find_path(LibNL_NETFILTER_INCLUDE_DIR netlink/netfilter/netfilter.h
/usr/include
/usr/include/libnl3
/usr/local/include
/usr/local/include/libnl3
${PC_LibNL_NetFilter_LIBRARY}
)
find_path(LibNL_GENL_INCLUDE_DIR netlink/genl/genl.h
/usr/include
/usr/include/libnl3
/usr/local/include
/usr/local/include/libnl3
${PC_LibNL_GenL_LIBRARY}
)
find_library(LibNL_LIBRARY NAMES nl nl-3 PATHS ${PC_LibNL_LIBRARY_DIRS})
find_library(LibNL_ROUTE_LIBRARY NAMES nl-route nl-route-3 PATHS ${PC_LibNL_Route_LIBRARY_DIRS})
find_library(LibNL_NETFILTER_LIBRARY NAMES nl-nf nl-nf-3 PATHS ${PC_LibNL_NetFilter_LIBRARY_DIRS})
find_library(LibNL_GENL_LIBRARY NAMES nl-genl nl-genl-3 PATHS ${PC_LibNL_GenL_LIBRARY_DIRS})
if (LibNL_INCLUDE_DIR AND LibNL_LIBRARY)
set(LibNL_FOUND TRUE)
set(LibNL_VERSION ${PC_LibNL_VERSION})
endif (LibNL_INCLUDE_DIR AND LibNL_LIBRARY)
if (LibNL_FOUND)
set(LibNL_LIBRARIES ${LibNL_LIBRARY} ${LibNL_ROUTE_LIBRARY} ${LibNL_NETFILTER_LIBRARY} ${LibNL_GENL_LIBRARY})
#if (NOT LibNL_FIND_QUIETLY)
# message("-- Found Linux Netlink Library: ${LibNL_VERSION}")
#endif (NOT LibNL_FIND_QUIETLY)
ELSE (LibNL_FOUND)
if (LibNL_FIND_REQUIRED)
message("Netlink version 3 development packages cannot be found.")
message("In Debian/Ubuntu, they may be called:")
message("libnl-3-dev libnl-genl-3dev libnl-nf-3-dev libnl-route-3-dev")
message(FATAL_ERROR "Could not find netlink library.")
endif (LibNL_FIND_REQUIRED)
endif (LibNL_FOUND)
if(LibNL_FOUND AND NOT TARGET LibNL::NL)
add_library(LibNL::NL UNKNOWN IMPORTED)
set_target_properties(LibNL::NL PROPERTIES
IMPORTED_LOCATION "${LibNL_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibNL_INCLUDE_DIR}"
)
endif()
if(LibNL_FOUND AND LibNL_ROUTE_LIBRARY AND NOT TARGET LibNL::NL-Route)
set(LibNL_route_FOUND TRUE)
add_library(LibNL::NL-Route UNKNOWN IMPORTED)
set_target_properties(LibNL::NL-Route PROPERTIES
IMPORTED_LOCATION "${LibNL_ROUTE_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibNL_INCLUDE_DIR}"
DEPENDS LibNL::NL
)
endif()
if(LibNL_FOUND AND LibNL_NETFILTER_LIBRARY AND NOT TARGET LibNL::NL-NetFilter)
set(LibNL_netfilter_FOUND TRUE)
add_library(LibNL::NL-NetFilter UNKNOWN IMPORTED)
set_target_properties(LibNL::NL-NetFilter PROPERTIES
IMPORTED_LOCATION "${LibNL_NETFILTER_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibNL_INCLUDE_DIR}"
DEPENDS LibNL::NL
)
endif()
if(LibNL_FOUND AND LibNL_GENL_LIBRARY AND NOT TARGET LibNL::NL-GenL)
set(LibNL_genl_FOUND TRUE)
add_library(LibNL::NL-GenL UNKNOWN IMPORTED)
set_target_properties(LibNL::NL-GenL PROPERTIES
IMPORTED_LOCATION "${LibNL_GENL_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibNL_INCLUDE_DIR}"
DEPENDS LibNL::NL
)
endif()
mark_as_advanced(
LibNL_LIBRARIES
LibNL_LIBRARY
LibNL_ROUTE_LIBRARY
LibNL_NETFILTER_LIBRARY
LibNL_GENL_LIBRARY
LibNL_INCLUDE_DIR
LibNL_VERSION
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibNL
FOUND_VAR LibNL_FOUND
REQUIRED_VARS
LibNL_INCLUDE_DIR
LibNL_LIBRARIES
VERSION_VAR LibNL_VERSION
HANDLE_COMPONENTS
)
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_MODULE RMRF_TESTS
#include <boost/test/included/unit_test.hpp>
#include "net/socketaddress.hpp"
......
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_MODULE RMRF_TESTS
#include <boost/test/included/unit_test.hpp>
#include <algorithm>
......
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_MODULE RMRF_TESTS
#include <boost/test/included/unit_test.hpp>
#include <chrono>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment