jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
332 stars 83 forks source link

Exported CMake target is incomplete #503

Closed awvwgk closed 2 years ago

awvwgk commented 2 years ago

The exported CMake target is currently incomplete, i.e. the required include directory is not associated with the target. Also, the user has to work with the library target, rather than getting an imported library with namespacing.

This requires users of to fixup the jsonfortran target, e.g. by creating their own interface library. I'm currently using the following boilerplate:

if(NOT TARGET "jsonfortran::jsonfortran" AND WITH_JSON)
  # json-fortran tries to make it hard to get found
  string(TOLOWER "jsonfortran-${CMAKE_Fortran_COMPILER_ID}" jsonfortran)
  find_package("${jsonfortran}" REQUIRED)
  add_library("jsonfortran::jsonfortran" IMPORTED INTERFACE)
  target_link_libraries(
    "jsonfortran::jsonfortran"
    INTERFACE
    "jsonfortran$<$<NOT:$<BOOL:BUILD_SHARED_LIBS>>:-static>"
  )
  target_include_directories(
    "jsonfortran::jsonfortran"
    INTERFACE
    "${jsonfortran_INCLUDE_DIRS}"
  )
endif()

Would it be possible to export a complete target (jsonfortran::jsonfortran) in this projects CMake files? I would be happy to contribute the required changes to the CMake build files.

jacobwilliams commented 2 years ago

I'm not really a CMake guru. So if you think there's some way to improve the CMake support feel free to send a merge request!

jacobwilliams commented 2 years ago

@awvwgk Thank you for the pull request. I can merge it in. Is there anything that should be said about this in the Readme? (again, this cmake stuff is not my strong suit).