Skip to content

Commit

Permalink
split cmakelists, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Mar 2, 2023
1 parent 7e8e556 commit cb1555d
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 166 deletions.
32 changes: 19 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,33 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: build zenoh-c
- name: install zenoh-cpp
shell: bash
run: |
git clone https://github.com/eclipse-zenoh/zenoh-c.git
cd zenoh-c
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../install
cmake --build . --target install
mkdir -p build_install && cd build_install
cmake ../install -DCMAKE_INSTALL_PREFIX=../local
cmake --install .
- name: build zenoh-cpp
- name: make examples
shell: bash
run: |
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../install
cmake --build . --target install
mkdir -p build && cd build
cmake ..
cmake --build . --target examples
- name: make examples
- name: make examples with zenoh-cpp as subbroject
shell: bash
run: |
cd build
cmake --build . --target examples
mkdir -p build_examples_subproj && cd build_examples_subproj
cmake ../examples -DCMAKE_BUILD_TYPE=Debug
cmake --build . --config Debug
- name: make examples with zenoh-c as installed package
shell: bash
run: |
mkdir -p build_examples_findproj && cd build_examples_findproj
cmake ../examples -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/local -DZENOHC_FORCE_TYPE=PACKAGE
cmake --build . --config Release
- name: make tests
shell: bash
Expand Down
90 changes: 7 additions & 83 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,103 +1,27 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.20)
project(
zenohcpp
VERSION 0.7.0.1
DESCRIPTION "C++ bindings for Zenoh"
HOMEPAGE_URL "https://github.com/ZettaScaleLabs/zenoh-cpp"
LANGUAGES C CXX
)
include(cmake/helpers.cmake)
set_default_build_type(Release)

add_library(zenohcpp INTERFACE)
add_dependencies(zenohcpp zenohc::lib)
target_include_directories(zenohcpp INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")

#
# Components included only if project is the root project
#
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
configure_include_project(ZENOHCPP_ZENOHC zenohc zenohc::lib "../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c")
# add_subdirectory(install)
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${cargo_binary_dir}/tests)
# add_subdirectory(tests)
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${cargo_binary_dir}/examples)
enable_testing()
include(cmake/helpers.cmake)
set_default_build_type(Release)
configure_include_project(ZENOHCPP_ZENOHC zenohc zenohc::lib "../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c" "")
add_subdirectory(install)
add_subdirectory(tests)
add_subdirectory(examples)
endif()

# function(add_libraries target)
# target_link_libraries(${target} PUBLIC zenohc::lib)
# if(APPLE)
# find_library(FFoundation Foundation)
# find_library(FSecurity Security)
# target_link_libraries(${target} PUBLIC ${FFoundation} ${FSecurity})
# elseif(UNIX)
# target_link_libraries(${target} PUBLIC rt pthread m dl)
# elseif(WIN32)
# target_link_libraries(${target} PUBLIC ws2_32 crypt32 secur32 bcrypt ncrypt userenv ntdll iphlpapi runtimeobject)
# endif()
# endfunction()

# function(add_example file suffix is_cpp)
# get_filename_component(filename ${file} NAME_WE)
# set(target ${filename}_${suffix})
# add_executable(${target} EXCLUDE_FROM_ALL ${file})
# add_dependencies(examples ${target})
# if (${is_cpp})
# target_link_libraries(${target} PUBLIC zenohcpp)
# set_property(TARGET ${target} PROPERTY LANGUAGE CXX)
# set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
# else()
# set_property(TARGET ${target} PROPERTY LANGUAGE C)
# set_property(TARGET ${target} PROPERTY C_STANDARD 11)
# endif()
# set_property(TARGET ${target} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/examples")
# add_libraries(${target})
# endfunction()

# function(add_examples glob suffix is_cpp)
# file(GLOB files ${glob})
# foreach(file ${files})
# add_example(${file} ${suffix} ${is_cpp})
# endforeach()
# endfunction()

# function(add_test_instance file)
# get_filename_component(filename ${file} NAME_WE)
# set(target ${filename})
# add_executable(${target} EXCLUDE_FROM_ALL ${file})
# add_dependencies(tests ${target})
# target_link_libraries(${target} PUBLIC zenohcpp)
# set_property(TARGET ${target} PROPERTY LANGUAGE CXX)
# set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
# set_property(TARGET ${target} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests")
# add_libraries(${target})
# add_test(NAME "test_${filename}" COMMAND ${target})
# endfunction()

# function(add_tests glob)
# file(GLOB files ${glob})
# foreach(file ${files})
# add_test_instance(${file})
# endforeach()
# endfunction()

# if(APPLE OR UNIX OR WIN32)
# enable_testing()
# add_custom_target(tests)
# add_tests("${CMAKE_SOURCE_DIR}/tests/*.cpp")

# add_custom_target(examples)
# # C examples compiled with C compiler
# add_examples("${CMAKE_SOURCE_DIR}/examples/c/*.c" "c" FALSE)
# # C examples compiled with C++ compiler
# add_examples("${CMAKE_SOURCE_DIR}/examples/c/*.c" "c_cpp" TRUE)
# # C++ examples compiled with C++ compiler
# add_examples("${CMAKE_SOURCE_DIR}/examples/cpp/*.cpp" "cpp" TRUE)
# else()
# message(WARNING "You platform doesn't seem to support building the examples or tests.")
# endif()

# #
# # Configure installation
# #
Expand Down
78 changes: 31 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,100 +25,84 @@ C++ bindings are still so the Zenoh team will highly appreciate any help in test

> :warning: **WARNING** :warning: : Zenoh and its ecosystem are under active development. When you build from git, make sure you also build from git any other Zenoh repository you plan to use (e.g. binding, plugin, backend, etc.). It may happen that some changes in git are not compatible with the most recent packaged Zenoh release (e.g. deb, docker, pip). We put particular effort in mantaining compatibility between the various git repositories in the Zenoh project.
The zenoh C++ API is a set of C++ header files wrapping the [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) (and [zenoh-pico(https://github.com/eclipse-zenoh/zenoh-pico)] in nearest future) library.
The zenoh C++ API is a set of C++ header files wrapping the [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) (and [zenoh-pico] in nearest future) library.
So to install and use zenoh-cpp, [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) should be installed in the system.
So to use zenoh-cpp, [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) should be available to your project.

[zenoh-c]: https://github.com/eclipse-zenoh/zenoh-c
[zenoh-cpp]: https://github.com/eclipse-zenoh/zenoh-cpp
[zenoh-pico]: https://github.com/eclipse-zenoh/zenoh-pico
[zenohcpp.h]: https://github.com/eclipse-zenoh/zenoh-cpp/blob/main/include/zenohcpp.h
To install [zenoh-cpp] do the following steps:

The steps to install [zenoh-cpp]:
1. Clone the sources

1. Make sure that [rust](https://www.rust-lang.org) is available on your platform:

* Ubuntu
```bash
sudo apt-get install rustc
```
```bash
git clone https://github.com/eclipse-zenoh/zenoh-cpp.git
```

* MacOS
```bash
brew install rust
```
2. Do install. Notice, that the [zenoh-c] is not required for installation, but it's necessary for building tests and examples. So instead of the main project which depends on [zenoh-c], it's faster to directly use `install` subproject.

2. Install [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) library. If you don't want to use root privileges and install it into system `/usr/local` directory
add [CMAKE_INSTALL_PREFIX](https://cmake.org/cmake/help/v3.0/variable/CMAKE_INSTALL_PREFIX.html) parameter to `cmake` arguments.
Use option `CMAKE_INSTALL_PREFIX` for specifying installation location. Without this parameter installation is performed to default system location `/usr/local` which requires root privileges.

```sh
git clone https://github.com/eclipse-zenoh/zenoh-c.git &&
cd zenoh-c && mkdir -p build && cd build &&
cmake .. -DCMAKE_INSTALL_PREFIX=/home/username/local &&
cmake --build . --target install
```bash
mkdir build && cd build
cmake ../zenoh-cpp/install -DCMAKE_INSTALL_PREFIX=~/.local
cmake --install .
```

3. Install [zenoh-cpp]. Use the same `CMAKE_INSTALL_PREFIX` parameter as for [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c). The key point is that this
parameter is not only install destination path, but is also included into `CMAKE_SYSTEM_PREFIX_PATH`. So the CMAKE's
[find_package](https://cmake.org/cmake/help/latest/command/find_package.html) command is able to find [zenoh-c].
The path must be absolute, without environment variables and home directory shortcut `~`, otherwise cmake may not be able to
find it.

```sh
git clone https://github.com/eclipse-zenoh/zenoh-cpp.git &&
cd zenoh-cpp && mkdir -p build && cd build &&
cmake .. -DCMAKE_INSTALL_PREFIX=/home/username/local &&
cmake --build . --target install
```
[zenoh-c]: https://github.com/eclipse-zenoh/zenoh-c
[zenoh-cpp]: https://github.com/eclipse-zenoh/zenoh-cpp
[zenoh-pico]: https://github.com/eclipse-zenoh/zenoh-pico
[zenohcpp.h]: https://github.com/eclipse-zenoh/zenoh-cpp/blob/main/include/zenohcpp.h

## Building and running tests

```bash
cd /path/to/zenoh-cpp
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake ../zenoh-cpp
cmake --build . --target tests
ctest
```

## Building the Examples

```bash
cd /path/to/zenoh-cpp
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake ../zenoh-cpp
cmake --build . --target examples
```

You may alternatively use other CMAKE_BUILD_TYPE configurations, such as `Debug` or `RelWithDebInfo` if you wish to keep the debug symbols.
You may also directly build the `examples` project, which includes `zenoh-cpp` as subproject in this case.

```bash
mkdir -p build && cd build
cmake ../zenoh-cpp/examples
cmake --build .
```

## Running the Examples

### Basic Pub/Sub Example
```bash
./target/release/examples/z_sub_cpp
./z_sub_cpp
```

```bash
./target/release/examples/z_pub_cpp
./z_pub_cpp
```

### Queryable and Query Example
```bash
./target/release/examples/z_queryable_cpp
./z_queryable_cpp
```

```bash
./target/release/examples/z_get_cpp
./z_get_cpp
```

### Throughput Examples
```bash
./target/release/examples/z_sub_thgr_cpp
./z_sub_thgr_cpp
```

```bash
./target/release/examples/z_pub_thgr_cpp
./z_pub_thgr_cpp
```

## Library usage and API Conventions
Expand Down
50 changes: 36 additions & 14 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,43 @@ function(__include_project project_name)
endfunction()

#
# Configure set of cache variables and includes requested project accordingly to these variables
#
function(configure_include_project var_prefix project target path package git_url)
declare_cache_var(${var_prefix}_SOURCE_TYPE "" STRING "${project} source type. Can be PATH, PACKAGE or GIT_URL. If empty, tries all these variants in order")
declare_cache_var(${var_prefix}_SOURCE "" STRING "${project} source: filesystem path, package name or git url. If empty, uses the default value from corresponding variable")
declare_cache_var(${var_prefix}_PATH ${path} STRING "PATH to ${project} source directory")
declare_cache_var(${var_prefix}_PACKAGE ${package} STRING "name of ${project} PACKAGE")
declare_cache_var(${var_prefix}_GIT_URL ${git_url} STRING "GIT_URL of ${project} repository")
if(${var_prefix}_SOURCE_TYPE STREQUAL "")
# Configure set of cache variables
# Include external project accordingly to these variables
#
# Example:
#
# configure_include_project(ZENOHC zenohc zenohc::lib ".." zenohc "https://github.com/eclipse-zenoh/zenoh-c" "")
#
# This command defines cache variables
#
# ZENOHC_SOURCE = ""
# ZENOHC_PATH = ".."
# ZENOHC_PACKAGE = "zenohc"
# ZENOHC_GIT_URL = "https://github.com/eclipse-zenoh/zenoh-c"
# ZENOHC_GIT_TAG = ""
#
# Then it tries to include the project with name 'zenohc' from first available source in order (PATH,PACKAGE,GIT_URL).
# Project should define target `zenohc::lib`, otherwise cmake configuration step fails with error.
#
# If ZENOHC_SOURCE is set by user to value PATH, PACKAGE or GIT_URL, then the project is included from this source only.
#
# For example:
#
# cmake ../zenoh-c/examples -DZENOHC_SOURCE=GIT_URL -DZENOHC_GIT_URL=https://github.com/username/zenoh-c
#
# makes 'examples' project to compile with zenoh-c from username's zenoh-c git repository
#
function(configure_include_project var_prefix project target path package git_url git_tag)
declare_cache_var(${var_prefix}_SOURCE "" STRING "Explicit ${project} source type. Can be PATH, PACKAGE or GIT_URL. If empty, tries all these variants in order")
declare_cache_var(${var_prefix}_PATH "${path}" STRING "PATH to ${project} source directory")
declare_cache_var(${var_prefix}_PACKAGE "${package}" STRING "name of ${project} PACKAGE")
declare_cache_var(${var_prefix}_GIT_URL "${git_url}" STRING "GIT_URL of ${project} repository")
declare_cache_var(${var_prefix}_GIT_TAG "${git_tag}" STRING "GIT_TAG of ${project} repository")
if(${var_prefix}_SOURCE STREQUAL "")
include_project(${project} TARGET ${target} PATH ${${var_prefix}_PATH} QUIET)
include_project(${project} TARGET ${target} PACKAGE ${${var_prefix}_PACKAGE} QUIET)
include_project(${project} TARGET ${target} GIT_URL ${${var_prefix}_GIT_URL})
elseif(${var_prefix}_SOURCE STREQUAL "")
include_project(${project} TARGET ${target} ${${var_prefix}_SOURCE_TYPE} ${ZENOHCPP_ZENOHC_${ZENOHCPP_ZENOHC_SOURCE_TYPE}})
include_project(${project} TARGET ${target} GIT_URL ${${var_prefix}_GIT_URL} GIT_TAG ${${var_prefix}_GIT_TAG})
else()
include_project(${project} TARGET ${target} ${${var_prefix}_SOURCE_TYPE} ${ZENOHCPP_ZENOHC_SOURCE})
include_project(${project} TARGET ${target} ${${var_prefix}_SOURCE} ${${var_prefix}_${${var_prefix}_SOURCE}} GIT_TAG ${${var_prefix}_GIT_TAG})
endif()
endfunction()
endfunction()
7 changes: 3 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
# Settings when 'examples' is the root projet
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.20)
project(zenohcpp_examples LANGUAGES C CXX)
include(../cmake/helpers.cmake)
set_default_build_type(Release)
configure_include_project(ZENOHCPP_ZENOHC zenohc zenohc::lib "../../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c")
configure_include_project(ZENOHCPP zenohcpp zenohcpp ".." zenohcpp "https://github.com/eclipse-zenoh/zenoh-cpp")
configure_include_project(ZENOHCPP_ZENOHC zenohc zenohc::lib "../../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c" "")
configure_include_project(ZENOHCPP zenohcpp zenohcpp ".." zenohcpp "https://github.com/eclipse-zenoh/zenoh-cpp" "")
add_custom_target(examples ALL)
else()
message(STATUS "zenoh-cpp examples")
Expand All @@ -26,7 +26,6 @@ function(add_example file suffix is_cpp)
set_property(TARGET ${target} PROPERTY LANGUAGE C)
set_property(TARGET ${target} PROPERTY C_STANDARD 11)
endif()
set_property(TARGET ${target} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
add_platform_libraries(${target})
endfunction()

Expand Down
8 changes: 4 additions & 4 deletions include/zenohcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#pragma once

#include "zenohcpp_base.h"
#include "zenohcpp_channel.h"
#include "zenohcpp_objects.h"
#include "zenohcpp_structs.h"
#include "zenohcpp/zenohcpp_base.h"
#include "zenohcpp/zenohcpp_channel.h"
#include "zenohcpp/zenohcpp_objects.h"
#include "zenohcpp/zenohcpp_structs.h"
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <iostream>
#include <variant>

#include "zenoh.h"
#include "zenohcpp_base.h"
#include "zenohcpp_structs.h"

Expand Down
File renamed without changes.
Loading

0 comments on commit cb1555d

Please sign in to comment.