Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

HighFiveConfig.cmake should not unconditionally add_library(HighFive ALIAS HighFive::HighFive) #1050

@Jacobfaib

Description

@Jacobfaib

Bug Description
If a project foo and its dependency bar have a diamond dependency on HighFive, and both export HighFive cmake files (as is default), then you run into the following error:

CMake Error at /path/to/test/install/lib/cmake/HighFive/HighFiveConfig.cmake:18 (add_library):
  add_library cannot create ALIAS target "HighFive" because another target
  with the same name already exists.
Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.30.5/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package)
  /path/to/test/install/lib/cmake/foo/foo-dependencies.cmake:45 (find_dependency)
  /path/to/test/install/lib/cmake/foo/foo-config.cmake:72 (include)
  ...

Because:

  1. In foo-config.cmake it goes to load bar-config.cmake.
  2. bar-config.cmake successfully loads and loads HighFive.
  3. HighFive loads and creates its targets.
  4. Now foo-config.cmake goes on to also load HighFive.
  5. HighFive now re-loads. HighFiveTargets.cmake is idempotent, and early-returns before re-creating HighFive::HighFive.
  6. HighFiveConfig.cmake is not, and attempts to create the targets again.
  7. Error.

The fix is

diff --git a/cmake/HighFiveConfig.cmake b/cmake/HighFiveConfig.cmake
index 33ce7b7..a270d4c 100644
--- a/cmake/HighFiveConfig.cmake
+++ b/cmake/HighFiveConfig.cmake
@@ -14,6 +14,9 @@ if(HDF5_IS_PARALLEL)
   find_dependency(MPI)
   target_link_libraries(HighFive::HighFive INTERFACE MPI::MPI_C MPI::MPI_CXX)
 endif()
-
-add_library(HighFive ALIAS HighFive::HighFive)
-add_library(HighFiveInclude ALIAS HighFive::Include)
+if(NOT TARGET HighFive)
+  add_library(HighFive ALIAS HighFive::HighFive)
+endif()
+if(NOT TARGET HighFiveInclude)
+  add_library(HighFiveInclude ALIAS HighFive::Include)
+endif()

Version Information

  • HighFive: 2.9.0
  • CMake: 3.30.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions