Skip to content

Commit

Permalink
Ease the integration of Zenoh-Pico in native Zephyr projects (#180)
Browse files Browse the repository at this point in the history
* Zephyr build integrated with Cmake

* Allow both Zephyr 2.X via PlatformIO and Zephyr 3.X

* Reorganize CMake output messages
  • Loading branch information
cguimaraes authored Mar 27, 2023
1 parent 55913c3 commit a0d3ec6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 89 deletions.
181 changes: 94 additions & 87 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ if(NOT CMAKE_C_STANDARD)
message(STATUS "Setting C99 as the C Standard")
endif()
endif()

set(CMAKE_C_STANDARD_REQUIRED TRUE)
add_definitions(-DZENOH_C_STANDARD=${CMAKE_C_STANDARD})

# while in development, use timestamp for patch version:
string(TIMESTAMP PROJECT_VERSION_PATCH "%Y%m%ddev")
Expand All @@ -35,32 +35,16 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RELEASE)
endif()

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode")

option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON)
message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}")

option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF)
message(STATUS "Produce Debian and RPM packages: ${PACKAGING}")

option(BUILD_EXAMPLES "Use this to also build the examples." ON)
message(STATUS "Build examples: ${BUILD_EXAMPLES}")

option(BUILD_TOOLS "Use this to also build the tools." OFF)
message(STATUS "Build examples: ${BUILD_TOOLS}")

option(BUILD_TESTING "Use this to also build tests." ON)
message(STATUS "Build tests: ${BUILD_TESTING}")

option(BUILD_INTEGRATION "Use this to also build integration tests." OFF)
message(STATUS "Build integration: ${BUILD_INTEGRATION}")

option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0)
message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}")

message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")
option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definitions(-DZENOH_LINUX)
Expand All @@ -76,13 +60,29 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_definitions(-DZENOH_WINDOWS -D_CRT_SECURE_NO_WARNINGS)
set(BUILD_SHARED_LIBS "OFF")
add_definitions(-DZENOH_NO_STDATOMIC)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
if(WITH_ZEPHYR)
add_definitions(-DZENOH_ZEPHYR)
set(BUILD_SHARED_LIBS "OFF")
endif()
else()
message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform")
return()
endif()

add_definitions(-DZENOH_DEBUG=${ZENOH_DEBUG})
add_definitions(-DZENOH_C_STANDARD=${CMAKE_C_STANDARD})

# Print summary of CMAKE configurations
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode")
message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}")
message(STATUS "Produce Debian and RPM packages: ${PACKAGING}")
message(STATUS "Build examples: ${BUILD_EXAMPLES}")
message(STATUS "Build examples: ${BUILD_TOOLS}")
message(STATUS "Build tests: ${BUILD_TESTING}")
message(STATUS "Build integration: ${BUILD_INTEGRATION}")
message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}")
message(STATUS "Build for Zephyr RTOS: ${WITH_ZEPHYR}")
message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")

if(SKBUILD)
set(INSTALL_RPATH "zenoh")
Expand All @@ -96,14 +96,18 @@ find_package(Threads REQUIRED)
if(CMAKE_BUILD_TYPE MATCHES "DEBUG")
if(UNIX)
add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0)
elseif (MSVC)
elseif(MSVC)
add_compile_options(/W4 /WX /Od)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes -pipe -g -O0)
endif()
elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE")
if(UNIX)
add_compile_options(-pipe -O3)
elseif (MSVC)
elseif(MSVC)
add_compile_options(/O2)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
add_compile_options(-pipe -O3)
endif()
endif()

Expand Down Expand Up @@ -149,17 +153,16 @@ file(GLOB Sources "src/*.c"
"src/utils/*.c"
)

if(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD")
if(WITH_ZEPHYR)
file (GLOB Sources_Zephyr "src/system/zephyr/*.c")
list(APPEND Sources ${Sources_Zephyr})
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD")
file (GLOB Sources_Unix "src/system/unix/*.c")
list(APPEND Sources ${Sources_Unix})
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
file (GLOB Sources_Emscripten "src/system/emscripten/*.c")
list(APPEND Sources ${Sources_Emscripten})
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
file (GLOB Sources_Windows "src/system/windows/*.c")
list(APPEND Sources ${Sources_Windows})
endif()
Expand Down Expand Up @@ -220,7 +223,7 @@ if(BUILD_EXAMPLES)
add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_info.c)
add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_scout.c)
endif()
elseif (MSVC)
elseif(MSVC)
add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/windows/z_put.c)
add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/windows/z_pub.c)
add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_pub_st.c)
Expand All @@ -233,76 +236,80 @@ if(BUILD_EXAMPLES)
add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/windows/z_scout.c)
endif()

target_link_libraries(z_put ${Libname})
target_link_libraries(z_pub ${Libname})
target_link_libraries(z_pub_st ${Libname})
target_link_libraries(z_sub ${Libname})
target_link_libraries(z_sub_st ${Libname})
target_link_libraries(z_pull ${Libname})
target_link_libraries(z_get ${Libname})
target_link_libraries(z_queryable ${Libname})
target_link_libraries(z_info ${Libname})
target_link_libraries(z_scout ${Libname})
endif()

if(BUILD_TOOLS)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools)
add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c)
target_link_libraries(z_keyexpr_canonizer ${Libname})
if(UNIX OR MSVC)
target_link_libraries(z_put ${Libname})
target_link_libraries(z_pub ${Libname})
target_link_libraries(z_pub_st ${Libname})
target_link_libraries(z_sub ${Libname})
target_link_libraries(z_sub_st ${Libname})
target_link_libraries(z_pull ${Libname})
target_link_libraries(z_get ${Libname})
target_link_libraries(z_queryable ${Libname})
target_link_libraries(z_info ${Libname})
target_link_libraries(z_scout ${Libname})
endif()
endif()

if(BUILD_TESTING)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c)
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c)
add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c)

target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_endpoint_test ${Libname})
target_link_libraries(z_iobuf_test ${Libname})
target_link_libraries(z_msgcodec_test ${Libname})
target_link_libraries(z_keyexpr_test ${Libname})

enable_testing()
add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test)
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
endif()
if(UNIX OR MSVC)
if(BUILD_TOOLS)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools)
add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c)
target_link_libraries(z_keyexpr_canonizer ${Libname})
endif()

if(BUILD_MULTICAST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(BUILD_TESTING)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c)
target_link_libraries(z_peer_multicast_test ${Libname})
add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c)
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c)
add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c)

configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY)
target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_endpoint_test ${Libname})
target_link_libraries(z_iobuf_test ${Libname})
target_link_libraries(z_msgcodec_test ${Libname})
target_link_libraries(z_keyexpr_test ${Libname})

enable_testing()
add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test)
add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test)
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
endif()
endif()

if(BUILD_INTEGRATION)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(BUILD_MULTICAST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c)
add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c)
target_link_libraries(z_client_test ${Libname})
target_link_libraries(z_api_alignment_test ${Libname})
if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c)
target_link_libraries(z_peer_multicast_test ${Libname})

configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY)

enable_testing()
add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test)
add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test)
enable_testing()
add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test)
endif()
endif()

if(BUILD_INTEGRATION)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c)
add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c)
target_link_libraries(z_client_test ${Libname})
target_link_libraries(z_api_alignment_test ${Libname})

configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY)

enable_testing()
add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test)
add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test)
endif()
endif()
endif()

Expand Down
2 changes: 1 addition & 1 deletion extra_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"-<system/mbed/>",
"-<system/unix/>",
"-<system/windows/>"]
CPPDEFINES = ["ZENOH_ZEPHYR"]
CPPDEFINES = ["ZENOH_ZEPHYR", "ZENOH_PIO"]

elif FRAMEWORK == 'arduino':
PLATFORM = env.get("PIOPLATFORM")
Expand Down
7 changes: 6 additions & 1 deletion include/zenoh-pico/system/platform/zephyr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
#ifndef ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H
#define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H

#include <pthread.h>
#if defined(ZENOH_PIO)
#include <kernel.h>
#else
#include <zephyr/kernel.h>
#endif

#include <pthread.h>

#include "zenoh-pico/config.h"

Expand Down
5 changes: 5 additions & 0 deletions src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
// ZettaScale Zenoh Team, <[email protected]>
//

#if defined(ZENOH_PIO)
#include <drivers/uart.h>
#else
#include <zephyr/drivers/uart.h>
#endif

#include <netdb.h>
#include <stdbool.h>
#include <stddef.h>
Expand Down
6 changes: 6 additions & 0 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
// ZettaScale Zenoh Team, <[email protected]>
//

#if defined(ZENOH_PIO)
#include <kernel.h>
#include <random/rand32.h>
#else
#include <zephyr/kernel.h>
#include <zephyr/random/rand32.h>
#endif

#include <stddef.h>
#include <sys/time.h>
#include <unistd.h>
Expand Down

0 comments on commit a0d3ec6

Please sign in to comment.