Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and simplify cmake package config #596

Merged
merged 10 commits into from
Sep 3, 2024
18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ else()
endif()

set(Libname "zenohpico")
if(BUILD_SHARED_LIBS)
add_library(${Libname} SHARED)
else()
add_library(${Libname} STATIC)
endif()
add_library(${Libname})

function(add_definition value)
add_definitions(-D${value})
Expand Down Expand Up @@ -268,7 +264,10 @@ file(GLOB_RECURSE PublicHeaders
"include/zenoh-pico/utils/*.h"
"include/zenoh-pico/config.h"
)
target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(${Libname}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
Comment on lines +268 to +271
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The included directory is different for installed target


file(GLOB_RECURSE Sources
"src/api/*.c"
Expand Down Expand Up @@ -334,6 +333,7 @@ message(STATUS "Build tests: ${BUILD_TESTING}")
message(STATUS "Build integration: ${BUILD_INTEGRATION}")

install(TARGETS ${Libname}
EXPORT zenohpicoTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
COMPONENT Library
Expand Down Expand Up @@ -372,6 +372,12 @@ install(
CONFIGURATIONS ${configurations}
COMPONENT dev)

# Generate <Package>Targets.cmake
install(
EXPORT zenohpicoTargets
NAMESPACE zenohpico::
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}")

if(UNIX)
configure_file("${CMAKE_SOURCE_DIR}/zenohpico.pc.in" "${CMAKE_SOURCE_DIR}/zenohpico.pc" @ONLY)
install(FILES "${CMAKE_SOURCE_DIR}/zenohpico.pc" CONFIGURATIONS Release RelWithDebInfo DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Expand Down
69 changes: 4 additions & 65 deletions PackageConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,68 +1,7 @@
#
# Copyright (c) 2023 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, <[email protected]>
#
include(CMakeFindDependencyMacro)

@PACKAGE_INIT@

set(ZENOHPICO_BUILD_SHARED_LIBS @BUILD_SHARED_LIBS@)
set(CMAKE_SYSTEM_NAME @CMAKE_SYSTEM_NAME@)

set(ZENOHPICO_FEATURE_UNSTABLE_API @Z_FEATURE_UNSTABLE_API@)
set(ZENOHPICO_FEATURE_MULTI_THREAD @Z_FEATURE_MULTI_THREAD@)
set(ZENOHPICO_FEATURE_PUBLICATION @Z_FEATURE_PUBLICATION@)
set(ZENOHPICO_FEATURE_SUBSCRIPTION @Z_FEATURE_SUBSCRIPTION@)
set(ZENOHPICO_FEATURE_QUERY @Z_FEATURE_QUERY@)
set(ZENOHPICO_FEATURE_QUERYABLE @Z_FEATURE_QUERYABLE@)
set(ZENOHPICO_FEATURE_RAWETH_TRANSPORT @Z_FEATURE_RAWETH_TRANSPORT@)
set(ZENOHPICO_FEATURE_INTEREST @Z_FEATURE_INTEREST@)

if(ZENOHPICO_BUILD_SHARED_LIBS)
if (NOT TARGET __zenohpico_lib)
add_library(__zenohpico_lib SHARED IMPORTED GLOBAL)
add_library(zenohpico::lib ALIAS __zenohpico_lib)
set_target_properties(__zenohpico_lib PROPERTIES IMPORTED_NO_SONAME TRUE)
set_property(TARGET __zenohpico_lib PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@LIBNAME@")
if(NOT ("@IMPLIB@" STREQUAL ""))
set_property(TARGET __zenohpico_lib PROPERTY IMPORTED_IMPLIB "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@IMPLIB@")
endif()
target_include_directories(__zenohpico_lib INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
endif()
else()
if (NOT TARGET __zenohpico_static)
add_library(__zenohpico_static STATIC IMPORTED GLOBAL)
add_library(zenohpico::lib ALIAS __zenohpico_static)
set_property(TARGET __zenohpico_static PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@LIBNAME@")
target_include_directories(__zenohpico_static INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
endif()
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_compile_definitions(ZENOH_LINUX)
elseif(POSIX_COMPATIBLE)
add_compile_definitions(ZENOH_LINUX)
elseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
add_compile_definitions(ZENOH_BSD)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
add_compile_definitions(ZENOH_MACOS)
elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
add_compile_definitions(EMSCRIPTEN)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_compile_definitions(ZENOH_WINDOWS)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
if(WITH_ZEPHYR)
add_compile_definitions(ZENOH_ZEPHYR)
elseif(WITH_FREERTOS_PLUS_TCP)
add_compile_definitions(ZENOH_FREERTOS_PLUS_TCP)
endif()
if(@CHECK_THREADS@)
find_dependency(Threads REQUIRED)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/zenohpicoTargets.cmake")
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
project(zenohpico_examples LANGUAGES C)
include(../cmake/helpers.cmake)
set_default_build_type(Release)
configure_include_project(ZENOHPICO zenohpico zenohpico::static ".." zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "")
configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually fixes the build if building the examples as the root project. When including the project using add_subdirectory or FetchContent the library target will be named zenohpico.
The only small problem is that the target name for a find_packaged library will be zenohpico::zenohpico and this macro does not take the target namespace into account.

add_custom_target(examples ALL)
else()
message(STATUS "zenoh-pico examples")
Expand Down
Loading