Skip to content

Commit

Permalink
use find_package for fetching dependencies in cmake (#151)
Browse files Browse the repository at this point in the history
* modified cmake only to rely on find_package

* move enable_testing() to right place; output diagnostic messages in find_package

* fix typo

* added script for local build of cmake projects

* fix zenoh-c and zneoh-pico branches through git submodules;
fix find_package;
add scripts for installing from git and for building examples;

* ci.yml update

* ci.yml update

* ci.yml update

* submodules update

* scripts fix

* ci fix

* add install script for installing zenoh-c/pico from local folders

* pico commit hash update

* readme update

* readme update

* readme update

* assume zenoh-pico is not installed by default

* readme update
  • Loading branch information
DenisBiryukov91 authored Jul 22, 2024
1 parent 3cce480 commit 0d77925
Show file tree
Hide file tree
Showing 28 changed files with 345 additions and 336 deletions.
21 changes: 5 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,19 @@ jobs:
- name: install zenoh-cpp
shell: bash
run: |
mkdir -p build_install && cd build_install
cmake ../install -DCMAKE_INSTALL_PREFIX=~/local
cmake --install .
./scripts/install_from_git.sh ~/local
- name: make examples
shell: bash
run: |
mkdir -p build && cd build
cmake ..
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=~/local
cmake --build . --target examples
- name: make examples with zenoh-cpp as subbroject
shell: bash
run: |
mkdir -p build_examples_subproj && cd build_examples_subproj
cmake ../examples -DCMAKE_BUILD_TYPE=Debug
cmake --build . --config Debug
- name: make examples with zenoh-cpp as installed package
- name: make stand-alone examples
shell: bash
run: |
mkdir -p build_examples_findproj && cd build_examples_findproj
cmake ../examples -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/local -DZENOHCXX_SOURCE=PACKAGE
cmake --build . --config Release
./scripts/build_standalone_examples.sh ~/local
- name: make tests
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*.app

build/*
examples/simple/*/build
.vscode/*
target/*
Testing/*
Expand Down
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "zenoh-c"]
path = zenoh-c
url = https://github.com/eclipse-zenoh/zenoh-c.git
[submodule "zenoh-pico"]
path = zenoh-pico
url = https://github.com/eclipse-zenoh/zenoh-pico.git
96 changes: 34 additions & 62 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,48 @@ elseif(PROJECT_VERSION_TWEAK GREATER 1)
endif()
status_print(project_version)

if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
# Temporary measure. Ideally zenoh-cpp should work with any variant
# of zenoh-c build
set(ZENOHC_BUILD_WITH_SHARED_MEMORY TRUE CACHE BOOL "" FORCE )
set(ZENOHC_BUILD_WITH_UNSTABLE_API TRUE CACHE BOOL "" FORCE )
#
enable_testing()
set_default_build_type(Release)
configure_include_project(ZENOHCXX_ZENOHPICO zenohpico zenohpico "../zenoh-pico" zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" ${zenoh_pico_branch})
configure_include_project(ZENOHCXX_ZENOHC zenohc zenohc::lib "../zenoh-c" zenohc "https://github.com/eclipse-zenoh/zenoh-c" ${zenoh_c_branch})
endif()
option(ZENOHCXX_ZENOHC "Build for Zenoh-c target" ON)
option(ZENOHCXX_ZENOHPICO "Build for Zenoh-pico target" OFF)

set_default_build_type(Release)


#
# When changing code below change also corresponding code in install/PackageConfig.cmake.in
#

# zenohcxx without dependencies
add_library(zenohcxx INTERFACE)
target_include_directories(zenohcxx INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")

# zenohcxx for zenohpico
if(TARGET zenohpico)
message(STATUS "defined lib target zenohcxx::zenohpico for zenohpico")
add_library(zenohcxx_zenohpico INTERFACE)
target_compile_definitions(zenohcxx_zenohpico INTERFACE ZENOHCXX_ZENOHPICO)
target_include_directories(zenohcxx_zenohpico INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohpico zenohpico)
target_link_libraries(zenohcxx_zenohpico INTERFACE zenohpico)
add_library(zenohcxx::zenohpico ALIAS zenohcxx_zenohpico)
if(ZENOHCXX_ZENOHPICO)
# zenohcxx for zenohpico
find_package(zenohpico REQUIRED)
if(TARGET zenohpico::lib)
message(STATUS "defined lib target zenohcxx::zenohpico for zenohpico::lib")
add_library(zenohcxx_zenohpico INTERFACE)
target_compile_definitions(zenohcxx_zenohpico INTERFACE ZENOHCXX_ZENOHPICO)
target_include_directories(zenohcxx_zenohpico INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohpico zenohpico::lib)
target_link_libraries(zenohcxx_zenohpico INTERFACE zenohpico::lib)
add_library(zenohcxx::zenohpico ALIAS zenohcxx_zenohpico)
endif()
endif()

# zenohcxx for zenohc static/dynamic depending on ZENOHC_LIB_STATIC
if(TARGET zenohc::lib)
message(STATUS "defined lib target zenohcxx::zenohc::lib for zenohc::lib")
add_library(zenohcxx_zenohc_lib INTERFACE)
target_compile_definitions(zenohcxx_zenohc_lib INTERFACE ZENOHCXX_ZENOHC)
target_include_directories(zenohcxx_zenohc_lib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohc_lib zenohc::lib)
target_link_libraries(zenohcxx_zenohc_lib INTERFACE zenohc::lib)
add_library(zenohcxx::zenohc::lib ALIAS zenohcxx_zenohc_lib)
if(ZENOHCXX_ZENOHC)
# zenohcxx for zenohc
find_package(zenohc REQUIRED)
if(TARGET zenohc::lib)
message(STATUS "defined lib target zenohcxx::zenohc::lib for zenohc::lib")
add_library(zenohcxx_zenohc INTERFACE)
target_compile_definitions(zenohcxx_zenohc INTERFACE ZENOHCXX_ZENOHC)
target_include_directories(zenohcxx_zenohc INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohc zenohc::lib)
target_link_libraries(zenohcxx_zenohc INTERFACE zenohc::lib)
add_library(zenohcxx::zenohc ALIAS zenohcxx_zenohc)
endif()
endif()

# zenohcxx for zenohc dynamic
if(TARGET zenohc::shared)
message(STATUS "defined lib target zenohcxx::zenohc::shared for zenohc::shared")
add_library(zenohcxx_zenohc_shared INTERFACE)
target_compile_definitions(zenohcxx_zenohc_shared INTERFACE ZENOHCXX_ZENOHC)
target_include_directories(zenohcxx_zenohc_shared INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohc_lib zenohc::shared)
target_link_libraries(zenohcxx_zenohc_shared INTERFACE zenohc::shared)
add_library(zenohcxx::zenohc::shared ALIAS zenohcxx_zenohc_shared)
endif()

# zenohcxx for zenohc static
if(TARGET zenohc::static)
message(STATUS "defined lib target zenohcxx::zenohc::static for zenohc::static")
add_library(zenohcxx_zenohc_static INTERFACE)
target_compile_definitions(zenohcxx_zenohc_static INTERFACE ZENOHCXX_ZENOHC)
target_include_directories(zenohcxx_zenohc_static INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_dependencies(zenohcxx_zenohc_static zenohc::static)
target_link_libraries(zenohcxx_zenohc_static INTERFACE zenohc::static)
add_library(zenohcxx::zenohc::static ALIAS zenohcxx_zenohc_static)
endif()

#
# Components included only if project is the root project
#
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(install)
add_subdirectory(tests)
add_subdirectory(examples)
add_subdirectory(docs)
endif()
add_subdirectory(install)
enable_testing()
add_subdirectory(tests)
add_subdirectory(examples)
add_subdirectory(docs)
87 changes: 31 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The Zenoh C++ API is a C++ bindings for [zenoh-c] and [zenoh-pico] libraries. Th
but building [zenoh-c] requires [Rust](rust-lang) installed. Please check [here](https://www.rust-lang.org/tools/install) to learn how to install it.


C++ bindings are still so the Zenoh team will highly appreciate any help in testing them on various platforms, system architecture, etc. and to report any issue you might encounter. This will help in greatly improving its maturity and robustness.
C++ bindings are still under active development so the Zenoh team will highly appreciate any help in testing them on various platforms, system architecture, etc. and to report any issue you might encounter. This will help in greatly improving its maturity and robustness.

-------------------------------
## How to build and install it
Expand All @@ -29,69 +29,61 @@ C++ bindings are still so the Zenoh team will highly appreciate any help in test
To install [zenoh-cpp] do the following steps:

1. Clone the sources
1. Clone the sources.

```bash
git clone https://github.com/eclipse-zenoh/zenoh-cpp.git
```

2. Do install.
Neither [zenoh-c] nor [zenoh-pico] are required for the installation, but both are neccessary for building tests and examples. So, instead of the main project, it's faster to do install from "install" subproject.
2. Build and install.

Neither [zenoh-c] nor [zenoh-pico] backends are required for the installation. But at least one of them is required for usage.

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.

```bash
mkdir build && cd build
cmake ../zenoh-cpp/install -DCMAKE_INSTALL_PREFIX=~/.local
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local
cmake --install .
```

## Building and running tests

The [zenoh-cpp] is header-only C++ library that wraps [zenoh-c] and [zenoh-pico] libraries. To make tests and examples it tries to find `zenoh-c` and `zenoh-pico` libraries in the following places:

1. Directories `zenoh-c` and `zenoh-pico` located on the same level as `zenoh-cpp` itself. **WARNING**: If you see building errors, make sure that you don't have obsolete `zenoh-c` or `zenoh-pico` directories nearby
2. Libraries `zenoh-c` and `zenoh-pico` installed to system. **WARNING**: If you see building errors, make sure that no old version of libraries installed to `/usr/local/lib/cmake`
By default it is expected that you have [zenoh-c] installed. If you want to build and run tests for [zenoh-pico] backend or for both, please set `ZENOHCXX_ZENOHC` or `ZENOHCXX_ZENOHPICO` Cmake variables to`ON` or `OFF` accordingly.

3. Download [zenoh-c] and [zenoh-pico] from GitHub
To build tests run:

```bash
mkdir -p build && cd build
cmake ../zenoh-cpp
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local # to build tests only for zenoh-c backend
cmake .. -DZENOHCXX_ZENOHC=OFF -DZENOHCXX_ZENOHPICO=ON -DCMAKE_INSTALL_PREFIX=~/.local # to build tests only for zenoh-pico backend
cmake .. -DZENOHCXX_ZENOHPICO=ON -DCMAKE_INSTALL_PREFIX=~/.local # to build tests for both backends
cmake --build . --target tests
ctest
```

Notice that the output of `cmake ../zenoh-cpp` shows where the dependencies were found
Notice that the output of `cmake ../zenoh-cpp` shows where [zenoh-c] and/or [zenoh-pico] the dependencies were found.

## Building the Examples

Examples are splitted into two subdirectories. Subdirectory `universal` contains [zenoh-cpp] examples buildable with both [zenoh-c] and [zenoh-pico] backends. The `zenohc` subdirectory contains examples with zenoh-c specific functionality.

The examples can be built in two ways. One is to select `examples` as a build target of the main project:
By default it is expected that you have [zenoh-c] installed. If you want to build examples for [zenoh-pico] backend or for both, please set `ZENOHCXX_ZENOHC` or `ZENOHCXX_ZENOHPICO` Cmake variables to`ON` or `OFF` accordingly.

To build examples run:

```bash
mkdir -p build && cd build
cmake ../zenoh-cpp
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local # to build examples only for zenoh-c backend
cmake .. -DZENOHCXX_ZENOHC=OFF -DZENOHCXX_ZENOHPICO=ON -DCMAKE_INSTALL_PREFIX=~/.local # to build examples only for zenoh-pico backend
cmake .. -DZENOHCXX_ZENOHPICO=ON -DCMAKE_INSTALL_PREFIX=~/.local # to build examples for both backends
cmake --build . --target examples
```

Examples are placed into `build/examples/zenohc` and `build/examples/zenohpico` directories.

Second way is to build `examples` as a root project, which includes [zenoh-cpp] as subproject
```bash
mkdir -p build && cd build
cmake ../zenoh-cpp/examples
cmake --build .
```
Examples are placed into `build/zenohc` and `build/zenohpico` directories.
## Running the examples

Change current directory to the variant you want (`examples/zenohc` or `examples/zenohpico` in the build directory)
Change current directory to the variant you want (`examples/zenohc` or `examples/zenohpico` in the build directory).

See example sources for command line arguments (key expression, value, router address).

Expand All @@ -112,7 +104,7 @@ cargo run
./z_pub
```

The `z_pub` should receive message sent by `z_sub`
The `z_pub` should receive message sent by `z_sub`.

### Queryable and Query Example
```bash
Expand All @@ -123,7 +115,7 @@ The `z_pub` should receive message sent by `z_sub`
./z_get
```

The `z_get` should receive the data from `z_queryable`
The `z_get` should receive the data from `z_queryable`.

### Throughput Examples
```bash
Expand All @@ -134,50 +126,33 @@ The `z_get` should receive the data from `z_queryable`
./z_pub_thr_cpp 1024
```

After 30-40 seconds delay the `z_sub_thr` will start to show the throughput measure results
After 30-40 seconds delay the `z_sub_thr` will start to show the throughput measure results.

## Library usage

Below are the steps to include [zenoh-cpp] into CMake project. See also [examples/simple](examples/simple) directory for short examples of CMakeLists.txt.

- include [zenoh-c] or [zenoh-pico] into your CMake project **before** dependency on [zenoh-cpp] itself.
This is important as the library targets you need (`zenohcxx::zenohpico`, `zenohcxx::zenohc::lib`, `zenohcxx::zenohc::shared` and `zenohcxx::zenohc::static)`
are defined only if their backend library
targets (`zenohpico`, `zenohc::lib`, `zenohc::shared` and `zenohc::static` are defined)
This is important as the library targets you need (`zenohcxx::zenohpico`, `zenohcxx::zenohc::lib`) are defined only if their backend library targets (`zenohpico::lib` and/or `zenohc::lib` are defined)

- include [zenoh-cpp] itself. This can be done with [add_subdirectory], [find_package] or [FetchContent] CMake commands.
```
add_subdirectory(../zenoh-cpp)
```
- include [zenoh-cpp] using [find_package] CMake function:
```
find_package(zenohc) #if using zenoh-c backend
find_package(zenohpico) #if using zenoh-pico backend
find_package(zenohcxx)
```
```
FetchContent_Declare(zenohcxx GIT_REPOSITORY https://github.com/eclipse-zenoh/zenoh-cpp GIT_TAG main)
FetchContent_MakeAvailable(zenohcxx)
```
- add dependency on zenoh-cpp to your project in one of these ways:
```
target_link_libraries(yourproject PUBLIC zenohcxx::zenohpico)
```
- add dependency on zenoh-cpp to your project:
```
target_link_libraries(yourproject PUBLIC zenohcxx::zenohc::lib)
```
```
target_link_libraries(yourproject PUBLIC zenohcxx::zenohc::shared)
```
```
target_link_libraries(yourproject PUBLIC zenohcxx::zenohc::static)
target_link_libraries(yourproject PUBLIC zenohcxx::zenohc) #if using zenoh-c backend
target_link_libraries(yourproject PUBLIC zenohcxx::zenohpico) #if using zenoh-pico backend
```

- include the [zenoh.hxx] header and use namespace `zenoh`.
- include the [zenoh.hxx] header. All zenoh functionality is available under the namespace `zenoh`:
```C++
#include "zenoh.hxx"
using namespace zenoh;
```

## Library API
### Documentation

The documentation is on [zenoh-cpp.readthedocs.io].
Expand Down
2 changes: 1 addition & 1 deletion docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,7 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.

PREDEFINED = ZENOHCXX_ZENOHPICO ZENOHCXX_ZENOHC __DOXYGEN__
PREDEFINED = ZENOHCXX_ZENOHPICO ZENOHCXX_ZENOHC SHARED_MEMORY UNSTABLE __DOXYGEN__

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The
Expand Down
Loading

0 comments on commit 0d77925

Please sign in to comment.