Skip to content

Commit

Permalink
build both libraries:shared and static when packaging; (#766)
Browse files Browse the repository at this point in the history
add static library to dev package;
  • Loading branch information
DenisBiryukov91 authored Oct 28, 2024
1 parent df70c74 commit 9cc5d70
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 62 deletions.
171 changes: 114 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -257,10 +295,19 @@ file(GLOB_RECURSE PublicHeaders
"include/zenoh-pico/utils/*.h"
"include/zenoh-pico/config.h"
)
target_include_directories(${Libname}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
if(PICO_STATIC)
target_include_directories(${Libname}_static
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif()
if(PICO_SHARED)
target_include_directories(${Libname}_shared
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif()


file(GLOB_RECURSE Sources
"src/api/*.c"
Expand Down Expand Up @@ -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()

#
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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}")
Expand Down
13 changes: 12 additions & 1 deletion PackageConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions examples/freertos_plus_tcp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -74,7 +74,7 @@ function(add_example name)
main
freertos_kernel
freertos_plus_tcp
zenohpico
zenohpico::lib
)
endfunction()

Expand Down

0 comments on commit 9cc5d70

Please sign in to comment.