You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeLists.txt 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. cmake_minimum_required(VERSION 2.8) # see ../CMakeLists.txt for why 2.8
  2. if(POLICY CMP0075)
  3. cmake_policy(SET CMP0075 NEW)
  4. endif()
  5. include(CheckSymbolExists)
  6. include(CheckIncludeFile)
  7. include(CMakePackageConfigHelpers)
  8. # First, sort out whether we're running inside a json-c build,
  9. # or standalone, such as part of a benchmark build.
  10. if ("${PROJECT_NAME}" STREQUAL "json-c")
  11. # Part of an overall json-c build
  12. set(APPS_LINK_LIBS "${PROJECT_NAME}")
  13. # We know we have this in our current sources:
  14. set(HAVE_JSON_TOKENER_GET_PARSE_END)
  15. else()
  16. # Standalone mode, using an already installed json-c library, somewhere.
  17. # The path to the json-c install must be specified with -DCMAKE_PREFIX_PATH=...
  18. project(apps)
  19. find_package(PkgConfig)
  20. # PkgConfig is supposed to include CMAKE_PREFIX_PATH in the PKG_CONFIG_PATH
  21. # that's used when running pkg-config, but it just doesn't work :(
  22. # https://gitlab.kitware.com/cmake/cmake/issues/18150
  23. #set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH True)
  24. # Instead, we handle it explicitly here and update PKG_CONFIG_PATH ourselves.
  25. if (NOT CMAKE_PREFIX_PATH)
  26. message(FATAL_ERROR "Please specify -DCMAKE_PREFIX_PATH=... when running cmake.")
  27. endif()
  28. # Note: find_file isn't recursive :(
  29. find_file(PC_FILE_PATH "json-c.pc"
  30. PATHS "${CMAKE_PREFIX_PATH}/lib64" "${CMAKE_PREFIX_PATH}/lib"
  31. PATH_SUFFIXES "pkgconfig"
  32. NO_DEFAULT_PATH)
  33. get_filename_component(PC_DIR_PATH "${PC_FILE_PATH}" DIRECTORY)
  34. set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${PC_DIR_PATH}")
  35. message(STATUS "PC_FILE_PATH=${PC_FILE_PATH}")
  36. message(STATUS "PC_DIR_PATH=${PC_DIR_PATH}")
  37. pkg_check_modules(PC_JSONC json-c)
  38. if (PC_JSONC_FOUND)
  39. message(STATUS "Found json-c using pkg-config: ${PC_JSONC_PREFIX}")
  40. message(STATUS " PC_JSONC_INCLUDE_DIRS=${PC_JSONC_INCLUDE_DIRS}")
  41. message(STATUS " PC_JSONC_LIBRARIES=${PC_JSONC_LIBRARIES}")
  42. message(STATUS " PC_JSONC_LIBRARY_DIRS=${PC_JSONC_LIBRARY_DIRS}")
  43. link_directories(${PC_JSONC_LIBRARY_DIRS})
  44. include_directories(${PC_JSONC_INCLUDE_DIRS})
  45. # for target_link_libraries(...)
  46. set(APPS_INCLUDE_DIRS ${PC_JSONC_INCLUDE_DIRS})
  47. set(APPS_LINK_DIRS ${PC_JSONC_LIBRARY_DIRS})
  48. set(APPS_LINK_LIBS ${PC_JSONC_LIBRARIES})
  49. else()
  50. message(STATUS "Using find_package to locate json-c")
  51. # Note: find_package needs CMAKE_PREFIX_PATH set appropriately.
  52. # XXX json-c's installed cmake files don't actually set up what's
  53. # needed to use find_package() by itself, so we're just using it
  54. # to confirm the top of the install location.
  55. find_package(json-c CONFIG) # sets json-c_DIR
  56. # Assume json-c-config.cmake is in lib64/cmake/json-c/
  57. get_filename_component(json-c_TOP "${json-c_DIR}/../../.." ABSOLUTE)
  58. get_filename_component(json-c_LIBDIR "${json-c_DIR}/../.." ABSOLUTE)
  59. message(STATUS " json-c_TOP=${json-c_TOP}")
  60. message(STATUS " json-c_DIR=${json-c_DIR}")
  61. message(STATUS " json-c_LIBDIR=${json-c_LIBDIR}")
  62. link_directories(${json-c_LIBDIR})
  63. include_directories(${json-c_TOP}/include)
  64. include_directories(${json-c_TOP}/include/json-c)
  65. set(APPS_LINK_DIRS "${json-c_LIBDIR}")
  66. set(APPS_INCLUDE_DIRS "${json-c_TOP}/include;${json-c_TOP}/include/json-c")
  67. set(APPS_LINK_LIBS json-c)
  68. endif()
  69. set(CMAKE_REQUIRED_LINK_OPTIONS "-L${APPS_LINK_DIRS}")
  70. set(CMAKE_REQUIRED_LIBRARIES ${APPS_LINK_LIBS})
  71. set(CMAKE_REQUIRED_INCLUDES ${APPS_INCLUDE_DIRS})
  72. check_symbol_exists(json_tokener_get_parse_end "json_tokener.h" HAVE_JSON_TOKENER_GET_PARSE_END)
  73. endif() # end "standalone mode" block
  74. # ---------------------------------
  75. check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H) # for getrusage
  76. if (HAVE_SYS_RESOURCE_H)
  77. check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
  78. endif()
  79. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/apps_config.h.in
  80. ${PROJECT_BINARY_DIR}/apps_config.h)
  81. message(STATUS "Wrote ${PROJECT_BINARY_DIR}/apps_config.h")
  82. # ---------------------------------
  83. include_directories(PUBLIC ${CMAKE_SOURCE_DIR})
  84. include_directories(${PROJECT_SOURCE_DIR})
  85. include_directories(${PROJECT_BINARY_DIR})
  86. # ---------------------------------
  87. # Now, finally, the actual executables we're building:
  88. add_executable(json_parse json_parse.c)
  89. target_link_libraries(json_parse PRIVATE ${APPS_LINK_LIBS})
  90. # Note: it is intentional that there are no install instructions here yet.
  91. # When/if the interface of the app(s) listed here settles down enough to
  92. # publish as part of a regular build that will be added.