Skip to content

Commit

Permalink
Merge pull request #79 from ami-iit/traversaro-patch-2
Browse files Browse the repository at this point in the history
InstallBasicPackageFiles: Fix bug of OVERRIDE_MODULE_PATH that corrupt CMAKE_MODULE_PATH values set by blf transitive dependencies and fix OVERRIDE_MODULE_PATH  with CMake 3.29.1
  • Loading branch information
traversaro committed Apr 9, 2024
2 parents 2dbf5db + 23ca72a commit a6cfddf
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 104 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

## [0.2.4] - 2024-04-09
- Remove use of brew from CI [#76](https://github.com/ami-iit/matio-cpp/pull/76)
-
- InstallBasicPackageFiles: Fix bug of OVERRIDE_MODULE_PATH that corrupt CMAKE_MODULE_PATH values set by blf transitive dependencies and fix OVERRIDE_MODULE_PATH with CMake 3.29.1 [#79](https://github.com/ami-iit/matio-cpp/pull/79)

## [0.2.3] - 2023-11-15
- Added example. It is tested in CI. [#67](https://github.com/ami-iit/matio-cpp/pull/67)
- Clarify how to install matio with conda-forge [#68](https://github.com/ami-iit/matio-cpp/pull/68)
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).

cmake_minimum_required(VERSION 3.10)
project(matioCpp VERSION 0.2.3 LANGUAGES CXX)
project(matioCpp VERSION 0.2.4 LANGUAGES CXX)

# Defines the CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR and many other useful macros.
# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
Expand Down
210 changes: 108 additions & 102 deletions cmake/InstallBasicPackageFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
# :command:`install` commands, otherwise <Name> is used.
#
# If the ``OVERRIDE_MODULE_PATH`` is set, the autogenerated ``<Name>Config.cmake``
# file temporarily overrides the ``CMAKE_MODULE_PATH`` with the specified paths.
# file temporarily prepends the ``CMAKE_MODULE_PATH`` with the specified paths.

#=============================================================================
# Copyright 2013 Istituto Italiano di Tecnologia (IIT)
Expand Down Expand Up @@ -376,7 +376,107 @@ function(INSTALL_BASIC_PACKAGE_FILES _Name)
list(APPEND configure_package_config_file_extra_args NO_CHECK_REQUIRED_COMPONENTS_MACRO)
endif()

# Prepare PACKAGE_DEPENDENCIES variable
set(_need_private_deps 0)
if(NOT BUILD_SHARED_LIBS)
set(_need_private_deps 1)
else()
foreach(_target ${_targets})
get_property(_type TARGET ${_target} PROPERTY TYPE)
if("${_type}" STREQUAL "STATIC_LIBRARY")
set(_need_private_deps 1)
break()
endif()
endforeach()
endif()

unset(PACKAGE_DEPENDENCIES)
set(_overridden_module_path_list "")
if(DEFINED _IBPF_DEPENDENCIES)
set(PACKAGE_DEPENDENCIES "#### Expanded from PACKAGE_DEPENDENCIES by install_basic_package_files() ####\n\ninclude(CMakeFindDependencyMacro)\n")

if (DEFINED _IBPF_OVERRIDE_MODULE_PATH)
foreach(_path ${_IBPF_OVERRIDE_MODULE_PATH})

if (IS_ABSOLUTE ${_path})
set(_absolute_module_path ${_path})
else()
set(_absolute_module_path "${CMAKE_CURRENT_SOURCE_DIR}/${_path}")
endif()

file(RELATIVE_PATH _relative_path ${CMAKE_INSTALL_PREFIX} ${_absolute_module_path})
string(APPEND _overridden_module_path_list ";@PACKAGE_CMAKE_INSTALL_PREFIX@/${_relative_path}")
endforeach()
string(APPEND PACKAGE_DEPENDENCIES "foreach(overridden_module_path ${_overridden_module_path_list})\n")
string(APPEND PACKAGE_DEPENDENCIES " list(PREPEND CMAKE_MODULE_PATH \${overridden_module_path})\n")
string(APPEND PACKAGE_DEPENDENCIES "endforeach()\n")

# If OVERRIDE_MODULE_PATH is used, then if a dependency is not found find_dependency will
# halt the execution of the <package>config.cmake script, never restoring the original
# value of CMAKE_MODULE_PATH. For this reason, in this case we just use find_package
set(_IBPF_FIND_DEPENDENCY_COMMAND "find_package")
else()
set(_IBPF_FIND_DEPENDENCY_COMMAND "find_dependency")
endif()

# FIXME When CMake 3.9 or greater is required, remove this madness and just
# use find_dependency
if (CMAKE_VERSION VERSION_LESS 3.9)
string(APPEND PACKAGE_DEPENDENCIES "
set(_${_Name}_FIND_PARTS_REQUIRED)
if (${_Name}_FIND_REQUIRED)
set(_${_Name}_FIND_PARTS_REQUIRED REQUIRED)
endif()
set(_${_Name}_FIND_PARTS_QUIET)
if (${_Name}_FIND_QUIETLY)
set(_${_Name}_FIND_PARTS_QUIET QUIET)
endif()
")

foreach(_dep ${_IBPF_DEPENDENCIES})
if("${_dep}" MATCHES ".+ .+")
string(REPLACE " " ";" _dep_list "${_dep}")
list(INSERT _dep_list 1 \${_${_Name}_FIND_PARTS_QUIET} \${_${_Name}_FIND_PARTS_REQUIRED})
string(REPLACE ";" " " _depx "${_dep_list}")
string(APPEND PACKAGE_DEPENDENCIES "find_package(${_depx})\n")
else()
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endif()
endforeach()
if(_need_private_deps)
foreach(_dep ${_IBPF_PRIVATE_DEPENDENCIES})
if("${_dep}" MATCHES ".+ .+")
string(REPLACE " " ";" _dep_list "${_dep}")
list(INSERT _dep_list 1 \${_${_Name}_FIND_PARTS_QUIET} \${_${_Name}_FIND_PARTS_REQUIRED})
string(REPLACE ";" "\n " _depx "${_dep_list}")
string(APPEND PACKAGE_DEPENDENCIES "find_package(${_depx})\n")
else()
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endif()
endforeach()
endif()

else()

foreach(_dep ${_IBPF_DEPENDENCIES})
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endforeach()
if(_need_private_deps)
foreach(_dep ${_IBPF_PRIVATE_DEPENDENCIES})
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endforeach()
endif()

endif()

if(DEFINED _IBPF_OVERRIDE_MODULE_PATH)
string(APPEND PACKAGE_DEPENDENCIES "foreach(overridden_module_path ${_overridden_module_path_list})\n")
string(APPEND PACKAGE_DEPENDENCIES " list(REMOVE_ITEM CMAKE_MODULE_PATH \${overridden_module_path})\n")
string(APPEND PACKAGE_DEPENDENCIES "endforeach()\n")
endif()

set(PACKAGE_DEPENDENCIES "${PACKAGE_DEPENDENCIES}\n###############################################################################\n")
endif()

# Set input file for config, and ensure that _IBPF_UPPERCASE_FILENAMES
# and _IBPF_LOWERCASE_FILENAMES are set correctly
Expand Down Expand Up @@ -460,6 +560,10 @@ function(INSTALL_BASIC_PACKAGE_FILES _Name)
set(_targets_filename ${_name}-targets.cmake)
endif()

# OVERRIDE_MODULE_PATH is not compatible with a custom template config file
if(NOT _generate_file AND DEFINED _IBPF_OVERRIDE_MODULE_PATH)
message(FATAL_ERROR "OVERRIDE_MODULE_PATH option is not compatible with a custom CMake config template.")
endif()

# If the template config file does not exist, write a basic one
if(_generate_file)
Expand Down Expand Up @@ -503,7 +607,7 @@ function(INSTALL_BASIC_PACKAGE_FILES _Name)
\@PACKAGE_INIT\@
\@PACKAGE_DEPENDENCIES\@
${PACKAGE_DEPENDENCIES}
if(NOT TARGET ${_IBPF_NAMESPACE}${_first_target})
include(\"\${CMAKE_CURRENT_LIST_DIR}/${_targets_filename}\")
Expand Down Expand Up @@ -560,106 +664,6 @@ ${_compatibility_vars}
DESTINATION ${_IBPF_INSTALL_DESTINATION}
COMPONENT ${_IBPF_COMPONENT})


# Prepare PACKAGE_DEPENDENCIES variable
set(_need_private_deps 0)
if(NOT BUILD_SHARED_LIBS)
set(_need_private_deps 1)
else()
foreach(_target ${_targets})
get_property(_type TARGET ${_target} PROPERTY TYPE)
if("${_type}" STREQUAL "STATIC_LIBRARY")
set(_need_private_deps 1)
break()
endif()
endforeach()
endif()

unset(PACKAGE_DEPENDENCIES)
if(DEFINED _IBPF_DEPENDENCIES)
set(PACKAGE_DEPENDENCIES "#### Expanded from @PACKAGE_DEPENDENCIES@ by install_basic_package_files() ####\n\ninclude(CMakeFindDependencyMacro)\n")

if (DEFINED _IBPF_OVERRIDE_MODULE_PATH)

string(APPEND PACKAGE_DEPENDENCIES "set(CMAKE_MODULE_PATH_BK_${_IBPF_VARS_PREFIX} \${CMAKE_MODULE_PATH})\n")
set(_overridden_module_path "")
foreach(_path ${_IBPF_OVERRIDE_MODULE_PATH})

if (IS_ABSOLUTE ${_path})
set(_absolute_module_path ${_path})
else()
set(_absolute_module_path "${CMAKE_CURRENT_SOURCE_DIR}/${_path}")
endif()

file(RELATIVE_PATH _relative_path ${CMAKE_INSTALL_PREFIX} ${_absolute_module_path})
string(APPEND _overridden_module_path " \${PACKAGE_PREFIX_DIR}/${_relative_path}")
endforeach()
string(APPEND PACKAGE_DEPENDENCIES "set(CMAKE_MODULE_PATH${_overridden_module_path})\n")
# If OVERRIDE_MODULE_PATH is used, then if a dependency is not found find_dependency will
# halt the execution of the <package>config.cmake script, never restoring the original
# value of CMAKE_MODULE_PATH. For this reason, in this case we just use find_package
set(_IBPF_FIND_DEPENDENCY_COMMAND "find_package")
else()
set(_IBPF_FIND_DEPENDENCY_COMMAND "find_dependency")
endif()

# FIXME When CMake 3.9 or greater is required, remove this madness and just
# use find_dependency
if (CMAKE_VERSION VERSION_LESS 3.9)
string(APPEND PACKAGE_DEPENDENCIES "
set(_${_Name}_FIND_PARTS_REQUIRED)
if (${_Name}_FIND_REQUIRED)
set(_${_Name}_FIND_PARTS_REQUIRED REQUIRED)
endif()
set(_${_Name}_FIND_PARTS_QUIET)
if (${_Name}_FIND_QUIETLY)
set(_${_Name}_FIND_PARTS_QUIET QUIET)
endif()
")

foreach(_dep ${_IBPF_DEPENDENCIES})
if("${_dep}" MATCHES ".+ .+")
string(REPLACE " " ";" _dep_list "${_dep}")
list(INSERT _dep_list 1 \${_${_Name}_FIND_PARTS_QUIET} \${_${_Name}_FIND_PARTS_REQUIRED})
string(REPLACE ";" " " _depx "${_dep_list}")
string(APPEND PACKAGE_DEPENDENCIES "find_package(${_depx})\n")
else()
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endif()
endforeach()
if(_need_private_deps)
foreach(_dep ${_IBPF_PRIVATE_DEPENDENCIES})
if("${_dep}" MATCHES ".+ .+")
string(REPLACE " " ";" _dep_list "${_dep}")
list(INSERT _dep_list 1 \${_${_Name}_FIND_PARTS_QUIET} \${_${_Name}_FIND_PARTS_REQUIRED})
string(REPLACE ";" "\n " _depx "${_dep_list}")
string(APPEND PACKAGE_DEPENDENCIES "find_package(${_depx})\n")
else()
string(APPEND PACKAGE_DEPENDENCIES "$_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endif()
endforeach()
endif()

else()

foreach(_dep ${_IBPF_DEPENDENCIES})
string(APPEND PACKAGE_DEPENDENCIES "${_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endforeach()
if(_need_private_deps)
foreach(_dep ${_IBPF_PRIVATE_DEPENDENCIES})
string(APPEND PACKAGE_DEPENDENCIES "$_IBPF_FIND_DEPENDENCY_COMMAND}(${_dep})\n")
endforeach()
endif()

endif()

if(DEFINED _IBPF_OVERRIDE_MODULE_PATH)
string(APPEND PACKAGE_DEPENDENCIES "set(CMAKE_MODULE_PATH \${CMAKE_MODULE_PATH_BK_${_IBPF_VARS_PREFIX}})\n")
endif()

set(PACKAGE_DEPENDENCIES "${PACKAGE_DEPENDENCIES}\n###############################################################################\n")
endif()

# Prepare PACKAGE_VERSION variable
set(PACKAGE_VERSION ${_IBPF_VERSION})

Expand All @@ -686,6 +690,7 @@ endif()
set(${_IBPF_VARS_PREFIX}_${p} "${INSTALL_${_IBPF_VARS_PREFIX}_${p}}")
endif()
endforeach()
list(APPEND _install_path_vars CMAKE_INSTALL_PREFIX)
configure_package_config_file("${_config_cmake_in}"
"${CMAKE_CURRENT_BINARY_DIR}/${_config_filename}.install"
INSTALL_DESTINATION ${_IBPF_INSTALL_DESTINATION}
Expand All @@ -709,3 +714,4 @@ endif()
FILE "${_targets_filename}"
COMPONENT ${_IBPF_COMPONENT})
endfunction()

0 comments on commit a6cfddf

Please sign in to comment.