jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.91k stars 1.77k forks source link

CMake: handle backward compatibility #1285

Open aberaud opened 3 weeks ago

aberaud commented 3 weeks ago

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

sei-mwd commented 3 weeks ago

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.

aberaud commented 3 weeks ago

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.

md5i commented 3 weeks ago

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.