Findnlohmann_json.cmake 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. find_package(PkgConfig QUIET)
  2. pkg_check_modules(PC_nlohmann_json QUIET nlohmann_json)
  3. find_path(nlohmann_json_INCLUDE_DIR
  4. NAMES json.hpp
  5. PATHS ${PC_nlohmann_json_INCLUDE_DIRS}
  6. PATH_SUFFIXES nlohmann
  7. )
  8. if(nlohmann_json_INCLUDE_DIR)
  9. file(STRINGS "${nlohmann_json_INCLUDE_DIR}/json.hpp" _nlohmann_json_version_lines
  10. REGEX "#define[ \t]+NLOHMANN_JSON_VERSION_(MAJOR|MINOR|PATCH)")
  11. string(REGEX REPLACE ".*NLOHMANN_JSON_VERSION_MAJOR +\([0-9]+\).*" "\\1" _nlohmann_json_version_major "${_nlohmann_json_version_lines}")
  12. string(REGEX REPLACE ".*NLOHMANN_JSON_VERSION_MINOR +\([0-9]+\).*" "\\1" _nlohmann_json_version_minor "${_nlohmann_json_version_lines}")
  13. string(REGEX REPLACE ".*NLOHMANN_JSON_VERSION_PATCH +\([0-9]+\).*" "\\1" _nlohmann_json_version_patch "${_nlohmann_json_version_lines}")
  14. set(nlohmann_json_VERSION "${_nlohmann_json_version_major}.${_nlohmann_json_version_minor}.${_nlohmann_json_version_patch}")
  15. unset(_nlohmann_json_version_major)
  16. unset(_nlohmann_json_version_minor)
  17. unset(_nlohmann_json_version_patch)
  18. unset(_nlohmann_json_version_lines)
  19. endif()
  20. include(FindPackageHandleStandardArgs)
  21. find_package_handle_standard_args(nlohmann_json
  22. FOUND_VAR nlohmann_json_FOUND
  23. REQUIRED_VARS
  24. nlohmann_json_INCLUDE_DIR
  25. nlohmann_json_VERSION
  26. VERSION_VAR nlohmann_json_VERSION
  27. )
  28. if(nlohmann_json_FOUND)
  29. set(nlohmann_json_INCLUDE_DIRS ${nlohmann_json_INCLUDE_DIR})
  30. set(nlohmann_json_DEFINITIONS ${PC_nlohmann_json_CFLAGS_OTHER})
  31. endif()
  32. if(nlohmann_json_FOUND AND NOT TARGET nlohmann_json::nlohmann_json)
  33. add_library(nlohmann_json::nlohmann_json UNKNOWN IMPORTED)
  34. set_target_properties(nlohmann_json::nlohmann_json PROPERTIES
  35. IMPORTED_LOCATION "${nlohmann_json_LIBRARY}"
  36. INTERFACE_COMPILE_OPTIONS "${PC_nlohmann_json_CFLAGS_OTHER}"
  37. INTERFACE_INCLUDE_DIRECTORIES "${nlohmann_json_INCLUDE_DIR}"
  38. )
  39. endif()
  40. mark_as_advanced(
  41. nlohmann_json_INCLUDE_DIR
  42. )