diff --git a/cmake/EkatConfig.cmake.in b/cmake/EkatConfig.cmake.in index 62c00aff..63cec160 100644 --- a/cmake/EkatConfig.cmake.in +++ b/cmake/EkatConfig.cmake.in @@ -26,21 +26,35 @@ endif() # yaml-cpp if (@EKAT_ENABLE_YAML_PARSER@) - if (@EKAT_BUILDS_YAMLCPP@) - # We're installing yaml-cpp alongside ekat - find_dependency(yaml-cpp REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) - else() - find_dependency(yaml-cpp REQUIRED QUIET HINTS @yaml-cpp_DIR@) + if (NOT TARGET yaml-cpp::yaml-cpp AND NOT TARGET yaml-cpp) + if (@EKAT_BUILDS_YAMLCPP@) + # We're installing yaml-cpp alongside ekat + find_dependency(yaml-cpp REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) + else() + find_dependency(yaml-cpp REQUIRED QUIET HINTS @yaml-cpp_DIR@) + endif() + endif() + + # Ensure the namespaced target exists for downstream consumers even if we created it on the fly + if (TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp) + add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp) endif() endif() if (@EKAT_ENABLE_LOGGING@) # spdlog - if (@EKAT_BUILDS_SPDLOG@) - # We're installing yaml-cpp alongside ekat - find_dependency(spdlog REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) - else() - find_dependency(spdlog REQUIRED QUIET HINTS @spdlog_DIR@) + if (NOT TARGET spdlog::spdlog AND NOT TARGET spdlog) + if (@EKAT_BUILDS_SPDLOG@) + # We're installing spdlog alongside ekat + find_dependency(spdlog REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) + else() + find_dependency(spdlog REQUIRED QUIET HINTS @spdlog_DIR@) + endif() + endif() + + # Ensure the namespaced target exists for downstream consumers even if we created it on the fly + if (TARGET spdlog AND NOT TARGET spdlog::spdlog) + add_library(spdlog::spdlog ALIAS spdlog) endif() endif() @@ -51,10 +65,16 @@ if (@EKAT_ENABLE_BOOST_STACKTRACE@) endif() # Catch2 testing system -if (@EKAT_BUILDS_CATCH2@) - find_dependency (Catch2 REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) -else() - find_dependency (Catch2 REQUIRED QUIET HINTS @Catch2_DIR@) +if (NOT TARGET Catch2::Catch2 AND NOT TARGET Catch2) + if (@EKAT_BUILDS_CATCH2@) + find_dependency (Catch2 REQUIRED QUIET HINTS @CMAKE_INSTALL_PREFIX@) + else() + find_dependency (Catch2 REQUIRED QUIET HINTS @Catch2_DIR@) + endif() +endif() +# Ensure the namespaced target exists for downstream consumers even if we created it on the fly +if (TARGET Catch2 AND NOT TARGET Catch2::Catch2) + add_library(Catch2::Catch2 ALIAS Catch2) endif() include (${CMAKE_CURRENT_LIST_DIR}/EkatTargets.cmake) diff --git a/src/logging/CMakeLists.txt b/src/logging/CMakeLists.txt index 9211a94b..d2a594d0 100644 --- a/src/logging/CMakeLists.txt +++ b/src/logging/CMakeLists.txt @@ -1,20 +1,39 @@ -include(GNUInstallDirs) - # spdlog is a REQUIRED dep of this package +set (spdlog_FOUND FALSE) +if (TARGET spdlog::spdlog OR TARGET spdlog) + set(spdlog_FOUND TRUE) + message(STATUS "EKAT: Using spdlog provided by parent project") +endif() + option (EKAT_SKIP_FIND_SPDLOG "Skip find_package for spdlog, and fetch via FetchContent" OFF) -if (NOT EKAT_SKIP_FIND_SPDLOG) +if (NOT spdlog_FOUND AND NOT EKAT_SKIP_FIND_SPDLOG) message (STATUS "Looking for spdlog ...") find_package(spdlog QUIET NO_DEFAULT_PATH) if (spdlog_FOUND) message (STATUS "Looking for spdlog ... FOUND") message (STATUS " spdlog_DIR: ${spdlog_DIR}") + # It is possible that the installation provides the spdlog target, but not the spdlog::spdlog target. If so, define the alias target + if (NOT TARGET spdlog::spdlog) + if (NOT TARGET spdlog) + message (FATAL_ERROR "Neither spdlog nor spdlog::spdlog targets were found") + endif() + add_library (spdlog::spdlog ALIAS spdlog) + endif() else() message (STATUS "Looking for spdlog ... NOT FOUND") endif() endif() -if (NOT TARGET spdlog) +# If we found spdlog (via find_package or as an existing tgt) +# ensure it provides the scoped target +if (spdlog_FOUND) + if (TARGET spdlog AND NOT TARGET spdlog::spdlog) + add_library(spdlog::spdlog ALIAS spdlog) + endif() +endif() + +if (NOT spdlog_FOUND) include(EkatBuildSpdlog) set (EKAT_BUILDS_SPDLOG ON CACHE BOOL "Whether Ekat builds spdlog" FORCE) else() diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index bda0e0cf..857e45bf 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -1,8 +1,14 @@ # yaml-cpp is a REQUIRED dep of this package +set (yaml-cpp_FOUND FALSE) +if (TARGET yaml-cpp::yaml-cpp OR TARGET yaml-cpp) + set(yaml-cpp_FOUND TRUE) + message(STATUS "EKAT: Using yaml-cpp provided by parent project") +endif() + option (EKAT_SKIP_FIND_YAML_CPP "Skip find_package for yaml-cpp, and fetch via FetchContent" OFF) -if (NOT EKAT_SKIP_FIND_YAML_CPP) +if (NOT yaml-cpp_FOUND AND NOT EKAT_SKIP_FIND_YAML_CPP) # I am having issues getting the env var YAML_CPP_ROOT being picked up # by cmake. I suspect this has to do with the presence of the hyphen # CMake *should* convert '-' to '_' when forming the cmake/env var name, @@ -17,19 +23,22 @@ if (NOT EKAT_SKIP_FIND_YAML_CPP) if (yaml-cpp_FOUND) message (STATUS "Looking for yaml-cpp ... FOUND") message (STATUS " yaml-cpp_DIR: ${yaml-cpp_DIR}") - # It is possible that the installation provides the yaml-cpp target, but not the yaml-cpp::yaml-cpp target. If so, define the alias target - if (NOT TARGET yaml-cpp::yaml-cpp) - if (NOT TARGET yaml-cpp) - message (FATAL_ERROR "Neither yaml-cpp nor yaml-cpp::yaml-cpp targets were found") - endif() - add_library (yaml-cpp::yaml-cpp ALIAS yaml-cpp) + if (NOT TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp) endif() else() message (STATUS "Looking for yaml-cpp ... NOT FOUND") endif() endif() -if (NOT TARGET yaml-cpp) +# If we found yaml-cpp (via find_package or as an existing tgt) +# ensure it provides the scoped target +if (yaml-cpp_FOUND) + if (TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp) + add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp) + endif() +endif() + +if (NOT yaml-cpp_FOUND) include(EkatBuildYamlCpp) set (EKAT_BUILDS_YAMLCPP ON CACHE BOOL "Whether Ekat builds yaml-cpp" FORCE) else() diff --git a/src/testing-support/CMakeLists.txt b/src/testing-support/CMakeLists.txt index cab9589d..d09cd246 100644 --- a/src/testing-support/CMakeLists.txt +++ b/src/testing-support/CMakeLists.txt @@ -1,10 +1,14 @@ -include(GNUInstallDirs) - ### CATCH MAIN ### +set (Catch2_FOUND FALSE) +if (TARGET Catch2::Catch2 OR TARGET Catch2) + set(Catch2_FOUND TRUE) + message(STATUS "EKAT: Using Catch2 provided by parent project") +endif() + # Catch2 is a REQUIRED dep of this package option (EKAT_SKIP_FIND_CATCH2 "Skip find_package for Catch2, and fetch via FetchContent" OFF) -if (NOT EKAT_SKIP_FIND_CATCH2) +if (NOT Catch2_FOUND AND NOT EKAT_SKIP_FIND_CATCH2) message (STATUS "Looking for Catch2 ...") find_package(Catch2 QUIET NO_DEFAULT_PATH) if (Catch2_FOUND) @@ -15,7 +19,15 @@ if (NOT EKAT_SKIP_FIND_CATCH2) endif() endif() -if (NOT TARGET Catch2::Catch2) +# If we found Catch2 (via find_package or as an existing tgt) +# ensure it provides the scoped target +if (Catch2_FOUND) + if (TARGET Catch2 AND NOT TARGET Catch2::Catch2) + add_library(Catch2::Catch2 ALIAS Catch2) + endif() +endif() + +if (NOT Catch2_FOUND) set (EKAT_BUILDS_CATCH2 ON CACHE BOOL "Whether Ekat builds Catch2" FORCE) include (EkatBuildCatch2) else()