Skip to content

Commit

Permalink
zenohc::lib depends on ZENOHC_LIB_STATIC variable (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin authored Dec 7, 2023
1 parent 10a198a commit 542c6b8
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 51 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ jobs:
shell: bash
run: |
cd build
cmake .. -DZENOHC_BUILD_TESTS_WITH_STATIC_LIB=FALSE -DCMAKE_BUILD_TYPE=Release
cmake .. -DZENOHC_LIB_STATIC=FALSE -DCMAKE_BUILD_TYPE=Release
cmake --build . --target tests --config Release
ctest -C Release --output-on-failure -E z_api_alignment_test
ctest -C Release --output-on-failure -E "(unit_z_api_alignment_test|build_z_build_static)"
- name: run cmake tests with zenoh-c as static library
shell: bash
run: |
cd build
cmake .. -DZENOHC_BUILD_TESTS_WITH_STATIC_LIB=TRUE -DCMAKE_BUILD_TYPE=Release
cmake .. -DZENOHC_LIB_STATIC=TRUE -DCMAKE_BUILD_TYPE=Release
cmake --build . --target tests --config Release
ctest -C Release --output-on-failure -E z_api_alignment_test
ctest -C Release --output-on-failure -E "(unit_z_api_alignment_test|build_z_build_shared)"
- name: make examples with zenoh-c
shell: bash
Expand All @@ -76,7 +76,7 @@ jobs:
shell: bash
run: |
mkdir -p build_examples_subproj && cd build_examples_subproj
cmake ../examples -DCMAKE_BUILD_TYPE=Debug -DZENOHC_BUILD_EXAMPLES_WITH_STATIC_LIB=TRUE
cmake ../examples -DCMAKE_BUILD_TYPE=Debug -DZENOHC_LIB_STATIC=TRUE
cmake --build . --config Debug
- name: make examples with zenoh-c as installed package
Expand Down
29 changes: 18 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ enable_testing()
#
# Build zenohc library from rust sources
#
# target zenohc::lib - for linking zenoh-c as dynamic library
# target zenohc::lib - for linking zenoh-c as static or dynamic library depending on ZENOHC_STATIC boolean cache var
# target zenohc::static - for linking zenoh-c as static library
# target zenohc::shared - for linking zenoh-c as dynamic library
#

#
Expand All @@ -28,6 +29,7 @@ declare_cache_var(ZENOHC_BUILD_WITH_SHARED_MEMORY TRUE BOOL "Enable shared-memor
declare_cache_var(ZENOHC_CUSTOM_TARGET "" STRING "Rust target for cross compilation, 'aarch64-unknown-linux-gnu' for example")
declare_cache_var(ZENOHC_CARGO_CHANNEL "stable" STRING "Cargo channel selected: stable or nightly")
declare_cache_var(ZENOHC_CARGO_FLAGS "" STRING "Additional cargo flags")
declare_cache_var(ZENOHC_LIB_STATIC FALSE BOOL "Alias zenohc::lib target to zenohc::static if TRUE, to zenohc::shared if FALSE")

#
# Setup project version
Expand Down Expand Up @@ -208,11 +210,16 @@ add_custom_target(cargo ALL DEPENDS "${libs}")
# Define libraries built by cargo as targets
#
add_library(zenohc_static STATIC IMPORTED GLOBAL)
add_library(zenohc SHARED IMPORTED GLOBAL)
add_library(zenohc_shared SHARED IMPORTED GLOBAL)
add_library(zenohc::static ALIAS zenohc_static)
add_library(zenohc::lib ALIAS zenohc)
add_library(zenohc::shared ALIAS zenohc_shared)
if (ZENOHC_LIB_STATIC)
add_library(zenohc::lib ALIAS zenohc_static)
else()
add_library(zenohc::lib ALIAS zenohc_shared)
endif()
add_dependencies(zenohc_static cargo)
add_dependencies(zenohc cargo)
add_dependencies(zenohc_shared cargo)

#
# Setup additional properties for library targets
Expand All @@ -221,12 +228,12 @@ add_dependencies(zenohc cargo)
#
get_required_static_libs(NATIVE_STATIC_LIBS)
target_link_libraries(zenohc_static INTERFACE ${NATIVE_STATIC_LIBS})
target_compile_definitions(zenohc INTERFACE ZENOHC_DYN_LIB)
target_compile_definitions(zenohc_shared INTERFACE ZENOHC_DYN_LIB)

# Workaroud for https://github.com/rust-lang/cargo/issues/5045
# mentioned in https://github.com/eclipse-zenoh/zenoh-c/issues/138
# If it's fixed, do not forget to correct PackageConfig.cmake.in also
set_target_properties(zenohc PROPERTIES IMPORTED_NO_SONAME TRUE)
set_target_properties(zenohc_shared PROPERTIES IMPORTED_NO_SONAME TRUE)

function(set_target_imported_locations target libr libd)
set_target_properties(${target}
Expand All @@ -251,24 +258,24 @@ function(set_target_imported_implib target libr libd)
endfunction()

set_target_imported_locations(zenohc_static ${staticlibr} ${staticlibd})
set_target_imported_locations(zenohc ${dylibr} ${dylibd})
set_target_imported_locations(zenohc_shared ${dylibr} ${dylibd})

if(DEFINED implibr)
set_target_imported_implib(zenohc ${implibr} ${implibd})
set_target_imported_implib(zenohc_shared ${implibr} ${implibd})
endif()

# Define include directories for library targets
status_print(source_include_dir)
status_print(cargo_generated_include_dir)
target_include_directories(zenohc_static INTERFACE ${source_include_dir})
target_include_directories(zenohc INTERFACE ${source_include_dir})
target_include_directories(zenohc_shared INTERFACE ${source_include_dir})

if(NOT(cargo_generated_include_dir STREQUAL ${source_include_dir}))
target_include_directories(zenohc_static INTERFACE ${cargo_generated_include_dir})
target_include_directories(zenohc INTERFACE ${cargo_generated_include_dir})
target_include_directories(zenohc_shared INTERFACE ${cargo_generated_include_dir})
endif()

set_target_properties(zenohc zenohc_static PROPERTIES IMPORTED_GLOBAL TRUE)
set_target_properties(zenohc_shared zenohc_static PROPERTIES IMPORTED_GLOBAL TRUE)

#
# Components included only if project is the root project
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ serde_yaml = "0.9.19"

[lib]
path="src/lib.rs"
name = "zenohcd"
name = "zenohc"
crate-type = ["cdylib", "staticlib"]
doctest = false

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,18 @@ This repository provides a C binding based on the main [Zenoh implementation wri
$ cmake --build . --target install
```

By default only dynamic library is installed. Set `ZENOHC_INSTALL_STATIC_LIBRARY` variable to install static library also:
By default only dynamic library is installed. Set `ZENOHC_INSTALL_STATIC_LIBRARY` variable to true to install static library also:

```bash
$ cmake ../zenoh-c -DCMAKE_INSTALL_PREFIX=~/.local -DZENOHC_INSTALL_STATIC_LIBRARY=TRUE
$ cmake --build . --target install
```

The result of installation is the header files in `include` directory, the library files in `lib` directory and cmake package configuration files for package `zenohc` in `lib/cmake` directory. The library later can be loaded with CMake command `find_package(zenohc)`.
Link to targets `zenohc::lib` for dynamic library and `zenohc::static` for static one in your CMakeLists.txt configuration file.
Add dependency in CMakeLists.txt on target
- `zenohc::shared` for linking dynamic library
- `zenohc::static` for linking static library
- `zenohc::lib` for linking static or dynamic library depending on boolean variable `ZENOHC_LIB_STATIC`

For `Debug` configuration the library package `zenohc_debug` is installed side-by-side with release `zenohc` library. Suffix `d` is added to names of library files (libzenohc**d**.so).

Expand Down
2 changes: 1 addition & 1 deletion cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ endfunction()
function(copy_dlls target)
if(WIN32)
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}> $<TARGET_FILE_DIR:${target}>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<IF:$<STREQUAL:$<TARGET_RUNTIME_DLLS:${target}>,>,nul,$<TARGET_RUNTIME_DLLS:${target}>> $<TARGET_FILE_DIR:${target}>
COMMAND_EXPAND_LISTS
)
endif()
Expand Down
13 changes: 3 additions & 10 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ else()
add_custom_target(examples)
endif()

declare_cache_var(ZENOHC_BUILD_EXAMPLES_WITH_STATIC_LIB FALSE BOOL "Use static zenohc lib for examples")

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")

foreach(file ${files})
Expand All @@ -26,14 +24,9 @@ foreach(file ${files})
add_executable(${target} EXCLUDE_FROM_ALL ${file})
add_dependencies(examples ${target})

if(ZENOHC_BUILD_EXAMPLES_WITH_STATIC_LIB)
add_dependencies(${target} zenohc::static)
target_link_libraries(${target} PRIVATE zenohc::static)
else()
add_dependencies(${target} zenohc::lib)
target_link_libraries(${target} PRIVATE zenohc::lib)
copy_dlls(${target})
endif()
add_dependencies(${target} zenohc::lib)
target_link_libraries(${target} PRIVATE zenohc::lib)
copy_dlls(${target})

set_property(TARGET ${target} PROPERTY C_STANDARD 11)
endforeach()
20 changes: 13 additions & 7 deletions install/PackageConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ set_property(TARGET __zenohc_static PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_P
target_link_libraries(__zenohc_static INTERFACE @NATIVE_STATIC_LIBS@)
target_include_directories(__zenohc_static INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")

add_library(__zenohc_lib SHARED IMPORTED GLOBAL)
add_library(zenohc::lib ALIAS __zenohc_lib)
set_target_properties(__zenohc_lib PROPERTIES IMPORTED_NO_SONAME TRUE)
set_property(TARGET __zenohc_lib PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@DYLIB@")
add_library(__zenohc_shared SHARED IMPORTED GLOBAL)
add_library(zenohc::shared ALIAS __zenohc_shared)
set_target_properties(__zenohc_shared PROPERTIES IMPORTED_NO_SONAME TRUE)
set_property(TARGET __zenohc_shared PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@DYLIB@")
if(NOT ("@IMPLIB@" STREQUAL ""))
set_property(TARGET __zenohc_lib PROPERTY IMPORTED_IMPLIB "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@IMPLIB@")
set_property(TARGET __zenohc_shared PROPERTY IMPORTED_IMPLIB "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@IMPLIB@")
endif()
target_include_directories(__zenohc_lib INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
target_compile_definitions(__zenohc_lib INTERFACE ZENOHC_DYN_LIB)
target_include_directories(__zenohc_shared INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
target_compile_definitions(__zenohc_shared INTERFACE ZENOHC_DYN_LIB)

if(ZENOHC_LIB_STATIC)
add_library(zenohc::lib ALIAS __zenohc_static)
else()
add_library(zenohc::lib ALIAS __zenohc_shared)
endif()
20 changes: 6 additions & 14 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
message(STATUS "zenoh-c tests")

declare_cache_var(ZENOHC_BUILD_TESTS_WITH_STATIC_LIB FALSE BOOL "Use static zenohc lib for tests")

add_custom_target(tests)

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
Expand All @@ -13,24 +11,18 @@ foreach(file ${files})
set(test_type "unit")
elseif (${file} MATCHES "^.*z_int_.*$")
set(test_type "integration")
elseif (${file} MATCHES "^.*z_build_.*$")
set(test_type "build")
else()
message(FATAL_ERROR "Test file ${file} does not match any known type (z_api_ or z_int_)")
message(FATAL_ERROR "Test file ${file} does not match any known type (z_api_ or z_int_ or z_build)")
endif()

add_executable(${target} EXCLUDE_FROM_ALL ${file})
add_dependencies(tests ${target})

if(ZENOHC_BUILD_TESTS_WITH_STATIC_LIB)
add_dependencies(${target} zenohc::static)
target_link_libraries(${target} PRIVATE zenohc::static)
else()
add_dependencies(${target} zenohc::lib)
target_link_libraries(${target} PRIVATE zenohc::lib)
copy_dlls(${target})
endif()

add_dependencies(${target} zenohc::lib)
target_link_libraries(${target} PRIVATE zenohc::lib)
copy_dlls(${target})
set_property(TARGET ${target} PROPERTY C_STANDARD 11)
# set_property(TARGET ${target} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${cargo_target_dir}/tests")
add_test(NAME "${test_type}_${target}" COMMAND ${target})
endforeach()

21 changes: 21 additions & 0 deletions tests/z_build_shared.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#undef NDEBUG
#include <assert.h>

// This test should fail if built dependent on static zenoh library
int main(int argc, char **argv) {
#ifndef ZENOHC_DYN_LIB
assert(0);
#endif
}
21 changes: 21 additions & 0 deletions tests/z_build_static.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#undef NDEBUG
#include <assert.h>

// This test should fail if built dependent on dynamic zenoh library
int main(int argc, char **argv) {
#ifdef ZENOHC_DYN_LIB
assert(0);
#endif
}

0 comments on commit 542c6b8

Please sign in to comment.