diff --git a/CMakeLists.txt b/CMakeLists.txt index d0daad7..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 @@ -21,30 +25,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 @@ -89,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 + 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() + + # ---- 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) - -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()