Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake libuci dependency improvement #144

Merged
merged 8 commits into from
Jun 7, 2022
Merged
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/docs")
include(CTest)
include(FetchContent)
include(ExternalProject)
include(CMakeDependentOption)


string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} _sys)
Expand Down Expand Up @@ -60,6 +61,8 @@ option(USE_SQLITE3_LIB "Use sqlite3 library" ON)
option(USE_UUID_LIB "Use uuid library" ON)
option(USE_ZYMKEY4_HSM "Use the Zymkey4 HSM" OFF)

cmake_dependent_option(BUILD_UCI_LIB "Build OpenWRT UCI library" ON USE_UCI_SERVICE OFF)

if (USE_CRYPTO_SERVICE)
add_compile_definitions(TEST_CRYPTO_SERVICE_KEY="12345")
endif ()
Expand Down
72 changes: 72 additions & 0 deletions CMakeModules/FindUCI.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#[=======================================================================[.rst:
FindUCI
-------

Finds the OpenWRT Unified Configuration Interface library.

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found:

``OpenWRT::UCI``
The UCI library

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables:

``UCI_FOUND``
True if the system has the UCI library.
``UCI_INCLUDE_DIRS``
Include directories needed to use UCI.
``UCI_LIBRARIES``
Libraries needed to link to UCI.

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

``UCI_INCLUDE_DIR``
The directory containing ``UCI.h``.
``UCI_LIBRARY``
The path to the UCI library.

#]=======================================================================]
cmake_minimum_required(VERSION 3.13.0)

find_package(PkgConfig)
pkg_check_modules(PC_UCI QUIET uci)

find_path(UCI_INCLUDE_DIR uci.h
HINTS ${PC_UCI_INCLUDEDIR} ${PC_UCI_INCLUDE_DIRS})

find_library(UCI_LIBRARY NAMES libuci uci
HINTS ${PC_UCI_LIBDIR} ${PC_UCI_LIBRARY_DIRS})

find_library(UBOX_LIBRARY NAMES libubox ubox
HINTS ${PC_UBOX_LIBDIR} ${PC_UBOX_LIBRARY_DIRS}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(UCI
FOUND_VAR UCI_FOUND
REQUIRED_VARS
UCI_LIBRARY
UCI_INCLUDE_DIR
UBOX_LIBRARY
)

if(UCI_FOUND AND NOT TARGET OpenWRT::UCI)
add_library(OpenWRT::UCI UNKNOWN IMPORTED)
set_target_properties(OpenWRT::UCI PROPERTIES
IMPORTED_LOCATION "${UCI_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${UCI_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${UBOX_LIBRARY}"
)

set(UCI_LIBRARIES OpenWRT::UCI)
set(UCI_INCLUDE_DIRS ${UCI_INCLUDE_DIR})
endif()
30 changes: 0 additions & 30 deletions lib/compile_ubox.sh

This file was deleted.

44 changes: 0 additions & 44 deletions lib/compile_uci.sh

This file was deleted.

111 changes: 55 additions & 56 deletions lib/uci.cmake
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
# Compile libubox
if (USE_UCI_SERVICE AND NOT (BUILD_ONLY_DOCS))
add_compile_definitions(WITH_UCI_SERVICE)
add_compile_definitions(WITH_UCI_SERVICE)

if (NOT BUILD_UCI_LIB)
find_package(UCI MODULE REQUIRED)
message("Found libuci library: ${UCI_LIBRARIES}")
else()
set(LIBUBOX_INSTALL_ROOT ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(LIBUBOX_INSTALL_DIR ${LIBUBOX_INSTALL_ROOT}/ubox)
set(LIBUBOX_INCLUDE_PATH ${LIBUBOX_INSTALL_DIR}/include)
set(LIBUBOX_LIB_DIR "${LIBUBOX_INSTALL_DIR}/lib")

find_library(LIBUBOX_LIB NAMES libubox.so ubox PATHS "${LIBUBOX_LIB_DIR}" NO_DEFAULT_PATH)
find_library(LIBUBOX_STATIC_LIB NAMES libubox.a ubox PATHS "${LIBUBOX_LIB_DIR}" NO_DEFAULT_PATH)

if (LIBUBOX_LIB AND LIBUBOX_STATIC_LIB)
message("Found shared libubox library: ${LIBUBOX_LIB}")
message("Found static libubox library: ${LIBUBOX_STATIC_LIB}")
else ()
FetchContent_Declare(
libubox
GIT_REPOSITORY https://git.openwrt.org/project/libubox.git
GIT_TAG f2d6752901f2f2d8612fb43e10061570c9198af1 # master as of 2022-02-10
GIT_PROGRESS true
)
FetchContent_Populate(libubox)

execute_process(COMMAND
bash
${CMAKE_SOURCE_DIR}/lib/compile_ubox.sh
${libubox_SOURCE_DIR}
${LIBUBOX_INSTALL_DIR}
${CMAKE_SYSTEM_NAME}
${CMAKE_SYSTEM_PROCESSOR}
${CMAKE_C_COMPILER}
)

find_library(LIBUBOX_LIB NAMES libubox.so ubox PATHS "${LIBUBOX_LIB_DIR}" NO_DEFAULT_PATH)
find_library(LIBUBOX_STATIC_LIB NAMES libubox.a ubox PATHS "${LIBUBOX_LIB_DIR}" NO_DEFAULT_PATH)
endif ()
ExternalProject_Add(
libubox
GIT_REPOSITORY https://git.openwrt.org/project/libubox.git
GIT_TAG f2d6752901f2f2d8612fb43e10061570c9198af1 # master as of 2022-02-10
GIT_PROGRESS true
INSTALL_DIR "${LIBUBOX_INSTALL_DIR}"
CMAKE_ARGS
-DBUILD_LUA=OFF
-DBUILD_EXAMPLES=OFF
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
# need to pass cross-compile toolchain manually
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
set(LIBUBOX_LIB "${LIBUBOX_LIB_DIR}/libubox.so")
set(LIBUBOX_STATIC_LIB "${LIBUBOX_LIB_DIR}/libubox.a")

set(LIBUCI_INSTALL_ROOT ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(LIBUCI_INSTALL_DIR ${LIBUCI_INSTALL_ROOT}/uci)
set(LIBUCI_INCLUDE_PATH ${LIBUCI_INSTALL_DIR}/include)
set(LIBUCI_LIB_DIR "${LIBUCI_INSTALL_DIR}/lib")

find_library(LIBUCI_STATIC_LIB NAMES libuci.a PATHS "${LIBUCI_LIB_DIR}" NO_DEFAULT_PATH)

if (LIBUCI_STATIC_LIB)
message("Found libuci library: ${LIBUCI_STATIC_LIB}")
else ()
FetchContent_Declare(
libuci
GIT_REPOSITORY https://git.openwrt.org/project/uci.git
GIT_TAG f84f49f00fb70364f58b4cce72f1796a7190d370 # master as of 2021-10-22
GIT_PROGRESS true
)
FetchContent_Populate(libuci)

execute_process(COMMAND
bash
${CMAKE_SOURCE_DIR}/lib/compile_uci.sh
${libuci_SOURCE_DIR}
${LIBUCI_INSTALL_DIR}
${LIBUBOX_INCLUDE_PATH}
${LIBUBOX_LIB}
${LIBUBOX_STATIC_LIB}
${CMAKE_SYSTEM_NAME}
${CMAKE_SYSTEM_PROCESSOR}
${CMAKE_C_COMPILER}
)
ExternalProject_Add(
libuci
GIT_REPOSITORY https://git.openwrt.org/project/uci.git
GIT_TAG f84f49f00fb70364f58b4cce72f1796a7190d370 # master as of 2021-10-22
GIT_PROGRESS true
INSTALL_DIR "${LIBUCI_INSTALL_DIR}"
CMAKE_ARGS
-DBUILD_LUA=OFF
# https://git.openwrt.org/?p=project/uci.git;a=blob;f=CMakeLists.txt;h=50e7f51fe5fafa9052c125d54443a8b31599efb6;hb=HEAD#l81-85
# libuci does not install the static lib when running `make install`
# -DBUILD_STATIC=ON
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-Dubox_include_dir=${LIBUBOX_INCLUDE_PATH}
-Dubox=${LIBUBOX_LIB}
-Dubox-static=${LIBUBOX_STATIC_LIB}
# need to pass cross-compile toolchain manually
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
# LibUBOX must be installed for LibUCI to configure properly
ExternalProject_Add_StepDependencies(libuci configure libubox)

find_library(LIBUCI_STATIC_LIB NAMES libuci.a uci PATHS "${LIBUCI_LIB_DIR}" NO_DEFAULT_PATH)
endif ()
add_library(OpenWRT::UCI SHARED IMPORTED)
file(MAKE_DIRECTORY "${LIBUCI_INCLUDE_PATH}")
set_target_properties(OpenWRT::UCI PROPERTIES
IMPORTED_LOCATION "${LIBUCI_LIB_DIR}/libuci.so"
INTERFACE_INCLUDE_DIRECTORIES "${LIBUCI_INCLUDE_PATH}"
)
# tell cmake that we can only use OpenWRT::UCI after we compile it
add_dependencies(OpenWRT::UCI libuci)
target_link_libraries(OpenWRT::UCI INTERFACE "${LIBUBOX_LIB}")
endif()
endif ()
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ else ()
endif ()

add_library(config config.c)
target_include_directories(config PUBLIC $<TARGET_PROPERTY:iface,INCLUDE_DIRECTORIES>)
target_link_libraries(config PUBLIC supervisor_config PRIVATE minIni os log)

if (BUILD_MDNS_SERVICE AND BUILD_CAPTURE_SERVICE)
Expand Down
4 changes: 1 addition & 3 deletions src/ap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ include_directories (

add_library(hostapd hostapd.c)
if (USE_UCI_SERVICE)
target_include_directories(hostapd PRIVATE ${LIBUCI_INCLUDE_PATH})
target_link_libraries(hostapd PRIVATE log os iface ${LIBUCI_STATIC_LIB})
target_link_libraries(hostapd PRIVATE log os iface OpenWRT::UCI)
else ()
target_link_libraries(hostapd PRIVATE log os iface)
endif()

add_library(ap_service ap_service.c)
target_include_directories(ap_service PUBLIC $<TARGET_PROPERTY:iface,INCLUDE_DIRECTORIES>)
target_link_libraries(ap_service PUBLIC supervisor_config PRIVATE domain hostapd log os eloop)
5 changes: 1 addition & 4 deletions src/capture/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ target_link_libraries(capture_config PRIVATE log os)

if (BUILD_CAPTURE_SERVICE)
add_library(mdns_decoder mdns_decoder.c)

# we include packet_decoder.h, so need to include its dependencies
target_include_directories(mdns_decoder PUBLIC $<TARGET_PROPERTY:packet_decoder,INCLUDE_DIRECTORIES> $<TARGET_PROPERTY:iface,INCLUDE_DIRECTORIES>)
target_link_libraries(mdns_decoder PRIVATE squeue log os hash PUBLIC packet_decoder)
target_link_libraries(mdns_decoder PRIVATE squeue log os hash iface PUBLIC packet_decoder)

add_library(dns_decoder dns_decoder.c)
target_link_libraries(dns_decoder PRIVATE log os hash packet_decoder)
Expand Down
3 changes: 1 addition & 2 deletions src/dhcp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ include_directories (

add_library(dnsmasq dnsmasq.c)
if (USE_UCI_SERVICE)
target_include_directories(dnsmasq PRIVATE ${LIBUCI_INCLUDE_PATH})
target_link_libraries(dnsmasq PRIVATE log os squeue ${LIBUCI_STATIC_LIB})
target_link_libraries(dnsmasq PRIVATE log os squeue OpenWRT::UCI)
else ()
target_link_libraries(dnsmasq PRIVATE log os)
endif ()
Expand Down
13 changes: 8 additions & 5 deletions src/supervisor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ add_library(mac_mapper mac_mapper.c)
target_link_libraries(mac_mapper PRIVATE bridge_list net log os)

add_library(subscriber_events subscriber_events.c)
target_include_directories(subscriber_events PUBLIC $<TARGET_PROPERTY:iface,INCLUDE_DIRECTORIES>)
target_link_libraries(subscriber_events PRIVATE log os domain SQLite::SQLite3)
target_link_libraries(subscriber_events PUBLIC supervisor_config PRIVATE log os domain SQLite::SQLite3)

add_library(sqlite_fingerprint_writer sqlite_fingerprint_writer.c)
target_link_libraries(sqlite_fingerprint_writer PRIVATE sqliteu squeue log os SQLite::SQLite3)
Expand Down Expand Up @@ -48,8 +47,12 @@ endif ()

add_library(supervisor_config INTERFACE)
set_target_properties(supervisor_config PROPERTIES PUBLIC_HEADER "supervisor_config.h")
target_link_libraries(supervisor_config INTERFACE SQLite::SQLite3)
target_link_libraries(supervisor_config INTERFACE SQLite::SQLite3 iface)

add_library(supervisor supervisor.c)
target_include_directories(supervisor PUBLIC $<TARGET_PROPERTY:iface,INCLUDE_DIRECTORIES>)
target_link_libraries(supervisor PRIVATE capture_config network_commands sqlite_alert_writer sqlite_fingerprint_writer cmd_processor domain log firewall_service)
target_link_libraries(supervisor
PUBLIC supervisor_config
PRIVATE
capture_config network_commands sqlite_alert_writer sqlite_fingerprint_writer
cmd_processor domain log firewall_service
)
16 changes: 6 additions & 10 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,25 @@ add_library(iptables iptables.c)
target_link_libraries(iptables PRIVATE net os log)

add_library(iface_mapper iface_mapper.c)
target_link_libraries(iface_mapper PRIVATE net log os)
target_link_libraries(iface_mapper PUBLIC os PRIVATE net log os)

add_library(iface iface.c)
target_link_libraries(iface PUBLIC os PRIVATE ifaceu net log)
if (USE_NETLINK_SERVICE)
add_library(nl nl.c)
target_include_directories(nl PRIVATE ${LIBNETLINK_UAPI_INCLUDE_PATH} ${LIBNL_INCLUDE_DIR})
target_link_libraries(nl PRIVATE ifaceu ${LIBNL_LIBRARY} ${LIBNL_GENL_LIBRARY} libnetlink MNL::mnl ll_map utils rt_names log os net)

target_link_libraries(iface PRIVATE ifaceu nl net log os)
target_link_libraries(iface PUBLIC nl)
elseif (USE_UCI_SERVICE)
add_library(uci_wrt uci_wrt.c)
target_include_directories(uci_wrt PUBLIC ${LIBUCI_INCLUDE_PATH})
target_link_libraries(uci_wrt PRIVATE squeue net os log ${LIBUCI_STATIC_LIB})

target_link_libraries(iface PUBLIC ifaceu uci_wrt net log os)
target_link_libraries(uci_wrt PUBLIC OpenWRT::UCI PRIVATE squeue net os log)
target_link_libraries(iface PUBLIC uci_wrt)
elseif (USE_GENERIC_IP_SERVICE)
add_compile_definitions(WITH_IP_GENERIC_SERVICE)
add_library(ipgen ipgen.c)
target_link_libraries(ipgen PRIVATE os log net)

target_link_libraries(iface PUBLIC ifaceu ipgen net log os)
else ()
target_link_libraries(iface PRIVATE ifaceu net log os)
target_link_libraries(iface PUBLIC ipgen)
endif ()

add_library(hashmap hashmap.c)
Expand Down
Loading