diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b176e9664..851819859 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index eade09038..daa860df9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 # # @@ -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 @@ -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 @@ -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} @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 332c502dc..1dfcc3c92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ serde_yaml = "0.9.19" [lib] path="src/lib.rs" -name = "zenohcd" +name = "zenohc" crate-type = ["cdylib", "staticlib"] doctest = false diff --git a/README.md b/README.md index bea30f20b..4e311ffbf 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ 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 @@ -88,7 +88,10 @@ This repository provides a C binding based on the main [Zenoh implementation wri ``` 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). diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake index 45a1917c3..1244ad0c1 100644 --- a/cmake/helpers.cmake +++ b/cmake/helpers.cmake @@ -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 $ $ + COMMAND ${CMAKE_COMMAND} -E copy_if_different $,>,nul,$> $ COMMAND_EXPAND_LISTS ) endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 9757d7c95..c2aecbd42 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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}) @@ -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() diff --git a/install/PackageConfig.cmake.in b/install/PackageConfig.cmake.in index a1b29399d..e66109fea 100644 --- a/install/PackageConfig.cmake.in +++ b/install/PackageConfig.cmake.in @@ -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() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2a68c4e3d..21695a439 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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") @@ -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() diff --git a/tests/z_build_shared.c b/tests/z_build_shared.c new file mode 100644 index 000000000..dbec93e02 --- /dev/null +++ b/tests/z_build_shared.c @@ -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, +#undef NDEBUG +#include + +// This test should fail if built dependent on static zenoh library +int main(int argc, char **argv) { +#ifndef ZENOHC_DYN_LIB + assert(0); +#endif +} diff --git a/tests/z_build_static.c b/tests/z_build_static.c new file mode 100644 index 000000000..5455f5846 --- /dev/null +++ b/tests/z_build_static.c @@ -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, +#undef NDEBUG +#include + +// This test should fail if built dependent on dynamic zenoh library +int main(int argc, char **argv) { +#ifdef ZENOHC_DYN_LIB + assert(0); +#endif +}