yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

CMake: handle backward compatibility

Open aberaud opened this issue 1 year ago • 3 comments

Some versions of yaml-cpp require to link cmake targets with yaml-cpp, while more recent version require to link them with yaml-cpp::yaml-cpp.

Is there a way to handle backward compatibility in CMake so it would work in both cases ? Many Linux distros still ship with versions that require yaml-cpp.

Many thanks

aberaud avatar Jun 06 '24 14:06 aberaud

Well, I haven't tried it with yaml-cpp, but I have had to deal with this problem before. Here's a snippet from that:

  find_package(date CONFIG NAMES date howardhinnant-date REQUIRED)
  message(STATUS "Found and using libdate.")
  if(TARGET howardhinnant-date::howardhinnant-date-tz)
    add_library(date::date-tz ALIAS howardhinnant-date::howardhinnant-date-tz)
  endif()

I imagine you can use something similar.

sei-mwd avatar Jun 06 '24 20:06 sei-mwd

Thanks :)

We ended up using a similar approach:

    if (TARGET yaml-cpp)
        target_link_libraries(${PROJECT_NAME} PRIVATE yaml-cpp)
    else()
        target_link_libraries(${PROJECT_NAME} PRIVATE yaml-cpp::yaml-cpp)
    endif()

This should probably documented somewhere.

aberaud avatar Jun 07 '24 18:06 aberaud

The advantage of creating the ALIAS library is if you ever need to use it more than one place in your build system, you only have to have the conditional in one place.

md5i avatar Jun 07 '24 18:06 md5i