From 9cc5d700d26d5d9f0616081ef2f3dba85166b6e0 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:13:40 +0100 Subject: [PATCH] build both libraries:shared and static when packaging; (#766) add static library to dev package; --- CMakeLists.txt | 171 ++++++++++++++-------- PackageConfig.cmake.in | 13 +- examples/CMakeLists.txt | 2 +- examples/freertos_plus_tcp/CMakeLists.txt | 6 +- 4 files changed, 130 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48fceed98..83539b850 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Windows") endif() elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") if(WITH_ZEPHYR) + set(PACKAGING OFF) # no packaging support for zephyr set(BUILD_SHARED_LIBS "OFF") endif() endif() @@ -104,16 +105,53 @@ else() endif() endif() +if (PACKAGING) + set(PICO_STATIC ON) + set(PICO_SHARED ON) +endif() +if(BUILD_SHARED_LIBS) + set(PICO_SHARED ON) +else() + set(PICO_STATIC ON) +endif() + set(Libname "zenohpico") -add_library(${Libname}) -add_library(zenohpico::lib ALIAS ${Libname}) +if(PICO_STATIC) + add_library(${Libname}_static STATIC) + set_target_properties(${Libname}_static PROPERTIES OUTPUT_NAME ${Libname}) + add_library(zenohpico::static ALIAS ${Libname}_static) +endif() +if(PICO_SHARED) + add_library(${Libname}_shared SHARED) + set_target_properties(${Libname}_shared PROPERTIES OUTPUT_NAME ${Libname}) + add_library(zenohpico::shared ALIAS ${Libname}_shared) +endif() +if(BUILD_SHARED_LIBS) + add_library(zenohpico::lib ALIAS ${Libname}_shared) +else() + add_library(zenohpico::lib ALIAS ${Libname}_static) +endif() -function(add_definition value) +function(pico_add_compile_definition value) add_definitions(-D${value}) - target_compile_definitions(${Libname} PUBLIC ${value}) + if(PICO_STATIC) + target_compile_definitions(zenohpico_static PUBLIC ${value}) + endif() + if(PICO_SHARED) + target_compile_definitions(zenohpico_shared PUBLIC ${value}) + endif() endfunction() -add_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD}) +function(pico_target_link_library value) + if(PICO_STATIC) + target_link_libraries(zenohpico_static ${value}) + endif() + if(PICO_SHARED) + target_link_libraries(zenohpico_shared ${value}) + endif() +endfunction() + +pico_add_compile_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD}) if (NOT CMAKE_BUILD_TYPE MATCHES "RELEASE" OR "Release") # while in development, use timestamp for patch version: @@ -125,25 +163,25 @@ set(CHECK_THREADS "ON") # System definition if(CMAKE_SYSTEM_NAME MATCHES "Linux") - add_definition(ZENOH_LINUX) + pico_add_compile_definition(ZENOH_LINUX) elseif(POSIX_COMPATIBLE) - add_definition(ZENOH_LINUX) + pico_add_compile_definition(ZENOH_LINUX) set(CHECK_THREADS "OFF") elseif(CMAKE_SYSTEM_NAME MATCHES "BSD") - add_definition(ZENOH_BSD) + pico_add_compile_definition(ZENOH_BSD) elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") - add_definition(ZENOH_MACOS) + pico_add_compile_definition(ZENOH_MACOS) set(MACOSX_RPATH "ON") elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") - add_definition(ZENOH_EMSCRIPTEN) + pico_add_compile_definition(ZENOH_EMSCRIPTEN) elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") - add_definition(ZENOH_WINDOWS) - add_definition(_CRT_SECURE_NO_WARNINGS) + pico_add_compile_definition(ZENOH_WINDOWS) + pico_add_compile_definition(_CRT_SECURE_NO_WARNINGS) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") if(WITH_ZEPHYR) - add_definition(ZENOH_ZEPHYR) + pico_add_compile_definition(ZENOH_ZEPHYR) elseif(WITH_FREERTOS_PLUS_TCP) - add_definition(ZENOH_FREERTOS_PLUS_TCP) + pico_add_compile_definition(ZENOH_FREERTOS_PLUS_TCP) endif() else() message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform") @@ -153,18 +191,18 @@ endif() # Compiler definition message("Compilers in use: ${CMAKE_C_COMPILER_ID}, ${CMAKE_CXX_COMPILER_ID}") if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") - add_definition(ZENOH_COMPILER_CLANG) + pico_add_compile_definition(ZENOH_COMPILER_CLANG) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") - add_definition(ZENOH_COMPILER_GCC) + pico_add_compile_definition(ZENOH_COMPILER_GCC) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_C_COMPILER_ID STREQUAL "Intel") - add_definition(ZENOH_COMPILER_INTEL) + pico_add_compile_definition(ZENOH_COMPILER_INTEL) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR CMAKE_C_COMPILER_ID STREQUAL "MSVC") - add_definition(ZENOH_COMPILER_MSVC) + pico_add_compile_definition(ZENOH_COMPILER_MSVC) else() - add_definition(ZENOH_COMPILER_OTHER) + pico_add_compile_definition(ZENOH_COMPILER_OTHER) endif() -add_definition(ZENOH_DEBUG=${ZENOH_DEBUG}) +pico_add_compile_definition(ZENOH_DEBUG=${ZENOH_DEBUG}) # Zenoh pico feature configuration options @@ -257,10 +295,19 @@ file(GLOB_RECURSE PublicHeaders "include/zenoh-pico/utils/*.h" "include/zenoh-pico/config.h" ) -target_include_directories(${Libname} - PUBLIC - $ - $) +if(PICO_STATIC) + target_include_directories(${Libname}_static + PUBLIC + $ + $) +endif() +if(PICO_SHARED) + target_include_directories(${Libname}_shared + PUBLIC + $ + $) +endif() + file(GLOB_RECURSE Sources "src/api/*.c" @@ -294,18 +341,24 @@ endif() set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) link_directories(${LIBRARY_OUTPUT_PATH}) -target_sources(${Libname} PRIVATE ${Sources}) +if(PICO_STATIC) + target_sources(zenohpico_static PRIVATE ${Sources}) +endif() +if(PICO_SHARED) + target_sources(zenohpico_shared PRIVATE ${Sources}) +endif() if(CHECK_THREADS) - target_link_libraries(${Libname} Threads::Threads) + pico_target_link_library(Threads::Threads) endif() if(CMAKE_SYSTEM_NAME MATCHES "Linux") - target_link_libraries(${Libname} rt) + pico_target_link_library(rt) endif() if(CMAKE_SYSTEM_NAME MATCHES "Windows") - target_link_libraries(${Libname} Ws2_32 Iphlpapi) + pico_target_link_library(Ws2_32) + pico_target_link_library(Iphlpapi) endif() # @@ -325,13 +378,21 @@ message(STATUS "Build tools: ${BUILD_TOOLS}") message(STATUS "Build tests: ${BUILD_TESTING}") message(STATUS "Build integration: ${BUILD_INTEGRATION}") -install(TARGETS ${Libname} +set(PICO_LIBS "") +if(PICO_STATIC) + list(APPEND PICO_LIBS zenohpico_static) +endif() +if(PICO_SHARED) + list(APPEND PICO_LIBS zenohpico_shared) +endif() + +install(TARGETS ${PICO_LIBS} EXPORT zenohpicoTargets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin LIBRARY COMPONENT Runtime - ARCHIVE COMPONENT Runtime + ARCHIVE COMPONENT Dev RUNTIME COMPONENT Runtime ) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico.h @@ -388,7 +449,7 @@ 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}) + target_link_libraries(z_keyexpr_canonizer zenohpico::lib) endif() if(BUILD_TESTING AND CMAKE_C_STANDARD MATCHES "11") @@ -412,23 +473,23 @@ if(UNIX OR MSVC) add_executable(z_api_encoding_test ${PROJECT_SOURCE_DIR}/tests/z_api_encoding_test.c) add_executable(z_refcount_test ${PROJECT_SOURCE_DIR}/tests/z_refcount_test.c) - target_link_libraries(z_data_struct_test ${Libname}) - target_link_libraries(z_channels_test ${Libname}) - target_link_libraries(z_collections_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}) - target_link_libraries(z_api_null_drop_test ${Libname}) - target_link_libraries(z_api_double_drop_test ${Libname}) - target_link_libraries(z_test_fragment_tx ${Libname}) - target_link_libraries(z_test_fragment_rx ${Libname}) - target_link_libraries(z_perf_tx ${Libname}) - target_link_libraries(z_perf_rx ${Libname}) - target_link_libraries(z_bytes_test ${Libname}) - target_link_libraries(z_api_bytes_test ${Libname}) - target_link_libraries(z_api_encoding_test ${Libname}) - target_link_libraries(z_refcount_test ${Libname}) + target_link_libraries(z_data_struct_test zenohpico::lib) + target_link_libraries(z_channels_test zenohpico::lib) + target_link_libraries(z_collections_test zenohpico::lib) + target_link_libraries(z_endpoint_test zenohpico::lib) + target_link_libraries(z_iobuf_test zenohpico::lib) + target_link_libraries(z_msgcodec_test zenohpico::lib) + target_link_libraries(z_keyexpr_test zenohpico::lib) + target_link_libraries(z_api_null_drop_test zenohpico::lib) + target_link_libraries(z_api_double_drop_test zenohpico::lib) + target_link_libraries(z_test_fragment_tx zenohpico::lib) + target_link_libraries(z_test_fragment_rx zenohpico::lib) + target_link_libraries(z_perf_tx zenohpico::lib) + target_link_libraries(z_perf_rx zenohpico::lib) + target_link_libraries(z_bytes_test zenohpico::lib) + target_link_libraries(z_api_bytes_test zenohpico::lib) + target_link_libraries(z_api_encoding_test zenohpico::lib) + target_link_libraries(z_refcount_test zenohpico::lib) configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY) @@ -459,7 +520,7 @@ if(UNIX OR MSVC) 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}) + target_link_libraries(z_peer_multicast_test zenohpico::lib) configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY) @@ -476,9 +537,9 @@ if(UNIX OR MSVC) add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) add_executable(z_session_test ${PROJECT_SOURCE_DIR}/tests/z_session_test.c) - target_link_libraries(z_client_test ${Libname}) - target_link_libraries(z_api_alignment_test ${Libname}) - target_link_libraries(z_session_test ${Libname}) + target_link_libraries(z_client_test zenohpico::lib) + target_link_libraries(z_api_alignment_test zenohpico::lib) + target_link_libraries(z_session_test zenohpico::lib) 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) @@ -527,11 +588,7 @@ if(PACKAGING) if(PACKAGING MATCHES "DEB") if(NOT DEBARCH) - execute_process( - COMMAND dpkg --print-architecture - OUTPUT_VARIABLE DEBARCH - OUTPUT_STRIP_TRAILING_WHITESPACE - ) + set(DEBARCH ${CMAKE_SYSTEM_PROCESSOR}) endif() message(STATUS "Configure DEB packaging for Linux ${DEBARCH}") diff --git a/PackageConfig.cmake.in b/PackageConfig.cmake.in index 94e4777a4..55339d35e 100644 --- a/PackageConfig.cmake.in +++ b/PackageConfig.cmake.in @@ -15,4 +15,15 @@ endif() include("${CMAKE_CURRENT_LIST_DIR}/zenohpicoTargets.cmake") -add_library(zenohpico::lib ALIAS zenohpico::zenohpico) +if(@PICO_SHARED@) + add_library(zenohpico::shared ALIAS zenohpico::zenohpico_shared) +endif() +if(@PICO_STATIC@) + add_library(zenohpico::static ALIAS zenohpico::zenohpico_static) +endif() + +if(@BUILD_SHARED_LIBS@) + add_library(zenohpico::lib ALIAS zenohpico::zenohpico_shared) +else() + add_library(zenohpico::lib ALIAS zenohpico::zenohpico_static) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a3fc911cd..a001356e8 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -14,7 +14,7 @@ endif() function(add_example name) add_executable(${name} ${ARGN}) set_property(TARGET ${name} PROPERTY C_STANDARD 11) - target_link_libraries(${name} zenohpico) + target_link_libraries(${name} zenohpico::lib) add_dependencies(examples ${name}) endfunction() diff --git a/examples/freertos_plus_tcp/CMakeLists.txt b/examples/freertos_plus_tcp/CMakeLists.txt index 83f890f30..3a0d44b54 100644 --- a/examples/freertos_plus_tcp/CMakeLists.txt +++ b/examples/freertos_plus_tcp/CMakeLists.txt @@ -55,9 +55,9 @@ FetchContent_MakeAvailable(freertos_kernel freertos_plus_tcp) set(BUILD_SHARED_LIBS OFF) set(WITH_FREERTOS_PLUS_TCP ON) set(ZENOH_DEBUG 3) -configure_include_project(ZENOHPICO zenohpico zenohpico "../.." zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "") +configure_include_project(ZENOHPICO zenohpico zenohpico::lib "../.." zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "") -target_link_libraries(zenohpico +target_link_libraries(zenohpico_static freertos_kernel freertos_plus_tcp ) @@ -74,7 +74,7 @@ function(add_example name) main freertos_kernel freertos_plus_tcp - zenohpico + zenohpico::lib ) endfunction()