From ad8b9b62aca7de79d6052fa22ab89963ac48dcf7 Mon Sep 17 00:00:00 2001 From: "Murilo M. Marinho" Date: Wed, 30 Sep 2026 21:00:38 +0100 Subject: [PATCH 1/2] Use a dqrobotics::dqrobotics target that a parent project provides A parent project that builds dqrobotics in-tree (e.g. the marinholab-sas-core Python package, which links it statically into its extension module) can now define dqrobotics::dqrobotics before add_subdirectory(sas_cpp); the find_path/find_library lookup and the imported target are only used when no such target exists. Standalone builds and the Debian packaging are unchanged. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0daad7..c36caa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,30 +21,35 @@ endif() find_package(Eigen3 REQUIRED) -# dqrobotics does not ship a CMake package: locate its header and library manually. -# The public header lives at /include/dqrobotics/DQ.h, so we search for -# the path-qualified name. We fail early and with a clear message if it is absent. -find_path(DQROBOTICS_INCLUDE_DIR - NAMES dqrobotics/DQ.h - PATHS /usr /usr/local /opt/homebrew - HINTS ENV CMAKE_PREFIX_PATH) -find_library(DQROBOTICS_LIBRARY - NAMES dqrobotics - PATHS /usr /usr/local /opt/homebrew - HINTS ENV CMAKE_PREFIX_PATH) -if(NOT DQROBOTICS_INCLUDE_DIR OR NOT DQROBOTICS_LIBRARY) - message(FATAL_ERROR "dqrobotics not found (looked for dqrobotics/DQ.h and libdqrobotics). " - "Install it from https://launchpad.net/~dqrobotics-dev/+archive/ubuntu/development " - "or build it from https://github.com/dqrobotics/cpp") +# A parent project may provide dqrobotics::dqrobotics itself, e.g. as an ALIAS +# of a dqrobotics target it builds in-tree with add_subdirectory (as the +# marinholab-sas-core Python package does). Otherwise, find an installed one. +if(NOT TARGET dqrobotics::dqrobotics) + # dqrobotics does not ship a CMake package: locate its header and library manually. + # The public header lives at /include/dqrobotics/DQ.h, so we search for + # the path-qualified name. We fail early and with a clear message if it is absent. + find_path(DQROBOTICS_INCLUDE_DIR + NAMES dqrobotics/DQ.h + PATHS /usr /usr/local /opt/homebrew + HINTS ENV CMAKE_PREFIX_PATH) + find_library(DQROBOTICS_LIBRARY + NAMES dqrobotics + PATHS /usr /usr/local /opt/homebrew + HINTS ENV CMAKE_PREFIX_PATH) + if(NOT DQROBOTICS_INCLUDE_DIR OR NOT DQROBOTICS_LIBRARY) + message(FATAL_ERROR "dqrobotics not found (looked for dqrobotics/DQ.h and libdqrobotics). " + "Install it from https://launchpad.net/~dqrobotics-dev/+archive/ubuntu/development " + "or build it from https://github.com/dqrobotics/cpp") + endif() + + # Model dqrobotics as an imported target so its include dir and library location + # are carried through the build/install regardless of the install prefix. + add_library(dqrobotics::dqrobotics UNKNOWN IMPORTED GLOBAL) + set_target_properties(dqrobotics::dqrobotics PROPERTIES + IMPORTED_LOCATION "${DQROBOTICS_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${DQROBOTICS_INCLUDE_DIR}") endif() -# Model dqrobotics as an imported target so its include dir and library location -# are carried through the build/install regardless of the install prefix. -add_library(dqrobotics::dqrobotics UNKNOWN IMPORTED GLOBAL) -set_target_properties(dqrobotics::dqrobotics PROPERTIES - IMPORTED_LOCATION "${DQROBOTICS_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${DQROBOTICS_INCLUDE_DIR}") - # ---- Library (shared by default; build static with -DBUILD_SHARED_LIBS=OFF) ---- add_library(marinholab_sas_core src/sas_clock.cpp From b25253b60d130914aa1328d0e28d4ebc814caa16 Mon Sep 17 00:00:00 2001 From: "Murilo M. Marinho" Date: Thu, 1 Oct 2026 08:01:27 +0100 Subject: [PATCH 2/2] Add MARINHO_LAB_SAS_CORE_INSTALL to turn the install rules off A parent project that provides dqrobotics::dqrobotics from a non-installed target cannot export marinholab_sas_core (install(EXPORT) requires every linked target to be in an export set), and does not need to install it. The option defaults to ON, so standalone builds and the Debian packaging install exactly as before. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 68 +++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c36caa8..6df170d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) option(MARINHO_LAB_SAS_CORE_BUILD_EXAMPLES "Build the example executables" ON) +# A parent project that links the library into its own binary (and provides +# dqrobotics::dqrobotics from a non-installed target) can turn the install rules +# and the exported CMake package off. +option(MARINHO_LAB_SAS_CORE_INSTALL "Install the library, headers and CMake package" ON) # Shared library by default; build a static library with -DBUILD_SHARED_LIBS=OFF. # The option() establishes the default while still honouring a value that a @@ -94,37 +98,39 @@ if(MARINHO_LAB_SAS_CORE_BUILD_EXAMPLES) endif() # ---- Install ---- -include(GNUInstallDirs) -install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) -install(TARGETS marinholab_sas_core - EXPORT marinholab_sas_core-targets - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) - -if(MARINHO_LAB_SAS_CORE_BUILD_EXAMPLES) - install(TARGETS sas_core_example sas_clock_example sas_clock_sched_fifo_example - thread_manager_example sas_robot_driver_example +if(MARINHO_LAB_SAS_CORE_INSTALL) + include(GNUInstallDirs) + install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + install(TARGETS marinholab_sas_core + EXPORT marinholab_sas_core-targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -endif() - -# ---- CMake package ---- -include(CMakePackageConfigHelpers) -install(EXPORT marinholab_sas_core-targets - FILE marinholab_sas_core-targets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) -configure_package_config_file( - ${CMAKE_CURRENT_SOURCE_DIR}/cmake/marinholab_sas_core-config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) - -write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config-version.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY SameMajorVersion) + if(MARINHO_LAB_SAS_CORE_BUILD_EXAMPLES) + install(TARGETS sas_core_example sas_clock_example sas_clock_sched_fifo_example + thread_manager_example sas_robot_driver_example + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + endif() -install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config-version.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) + # ---- CMake package ---- + include(CMakePackageConfigHelpers) + install(EXPORT marinholab_sas_core-targets + FILE marinholab_sas_core-targets.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) + + configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/marinholab_sas_core-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) + + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config-version.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) + + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/marinholab_sas_core-config-version.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/marinholab_sas_core) +endif()