Skip to content

Commit

Permalink
Simplify the Findyaml-cpp module
Browse files Browse the repository at this point in the history
This fixes compatibility with yaml-cpp 0.8, which previously failed
because of a `get_property` call with the wrong target name.
I took the liberty to add a few simplifications along the way.

Signed-off-by: Tobias Mayer <[email protected]>
  • Loading branch information
tobim committed Oct 20, 2023
1 parent dddbee0 commit 15f2581
Showing 1 changed file with 31 additions and 47 deletions.
78 changes: 31 additions & 47 deletions share/cmake/modules/Findyaml-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
# yaml-cpp_VERSION - Library's version
#
# Global targets defined by this module:
# yaml-cpp
# yaml-cpp::yaml-cpp
#
# For compatibility with the upstream CMake package, the following variables and targets are defined:
# yaml-cpp::yaml-cpp - Alias of the yaml-cpp target
# YAML_CPP_LIBRARIES - Libraries to link against yaml-cpp
# YAML_CPP_INCLUDE_DIR - Include directory
#
Expand Down Expand Up @@ -42,22 +41,23 @@ if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]")
endif()

if(NOT OCIO_INSTALL_EXT_PACKAGES STREQUAL ALL)
set(_yaml-cpp_REQUIRED_VARS yaml-cpp_LIBRARY)

if(NOT DEFINED yaml-cpp_ROOT)
# Search for yaml-cpp-config.cmake
find_package(yaml-cpp ${yaml-cpp_FIND_VERSION} CONFIG QUIET)
endif()
# Search for yaml-cpp-config.cmake
find_package(yaml-cpp ${yaml-cpp_FIND_VERSION} CONFIG QUIET)

if(yaml-cpp_FOUND)
get_target_property(yaml-cpp_LIBRARY yaml-cpp LOCATION)
### Alias target for yaml-cpp < 0.8 compatibility ###
if(TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp)
add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp)
endif()

set(yaml-cpp_INCLUDE_DIR ${YAML_CPP_INCLUDE_DIR})
get_target_property(yaml-cpp_LIBRARY yaml-cpp::yaml-cpp LOCATION)
else()

# As yaml-cpp-config.cmake search fails, search an installed library
# using yaml-cpp.pc .

list(APPEND _yaml-cpp_REQUIRED_VARS yaml-cpp_INCLUDE_DIR yaml-cpp_VERSION)

# Search for yaml-cpp.pc
find_package(PkgConfig QUIET)
pkg_check_modules(PC_yaml-cpp QUIET "yaml-cpp>=${yaml-cpp_FIND_VERSION}")
Expand Down Expand Up @@ -125,44 +125,28 @@ if(NOT OCIO_INSTALL_EXT_PACKAGES STREQUAL ALL)
set(yaml-cpp_FIND_REQUIRED FALSE)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(yaml-cpp
REQUIRED_VARS
${_yaml-cpp_REQUIRED_VARS}
VERSION_VAR
yaml-cpp_VERSION
)
endif()

###############################################################################
### Create target

if(yaml-cpp_FOUND AND NOT TARGET yaml-cpp)
add_library(yaml-cpp UNKNOWN IMPORTED GLOBAL)
set(_yaml-cpp_TARGET_CREATE TRUE)
endif()

###############################################################################
### Configure target ###

if(_yaml-cpp_TARGET_CREATE)
set_target_properties(yaml-cpp PROPERTIES
IMPORTED_LOCATION ${yaml-cpp_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${yaml-cpp_INCLUDE_DIR}
)

mark_as_advanced(yaml-cpp_INCLUDE_DIR yaml-cpp_LIBRARY yaml-cpp_VERSION)
endif()

###############################################################################
### Set variables for compatibility ###

if(TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp)
add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp)
endif()

if(yaml-cpp_INCLUDE_DIR)
set(YAML_CPP_INCLUDE_DIR "${yaml-cpp_INCLUDE_DIR}")

# Create target
if(yaml-cpp_FOUND AND NOT TARGET yaml-cpp::yaml-cpp)
add_library(yaml-cpp::yaml-cpp UNKNOWN IMPORTED GLOBAL)
set_target_properties(yaml-cpp::yaml-cpp PROPERTIES
IMPORTED_LOCATION ${yaml-cpp_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${yaml-cpp_INCLUDE_DIR}
)
endif()
endif()

set(YAML_CPP_LIBRARIES yaml-cpp::yaml-cpp)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(yaml-cpp
REQUIRED_VARS
yaml-cpp_LIBRARY
yaml-cpp_INCLUDE_DIR
yaml-cpp_VERSION
VERSION_VAR
yaml-cpp_VERSION
)

mark_as_advanced(yaml-cpp_INCLUDE_DIR yaml-cpp_LIBRARY yaml-cpp_VERSION)

0 comments on commit 15f2581

Please sign in to comment.