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