Skip to content

Commit d9981f6

Browse files
committed
Extend the CMakeLists.txt in the apps directory to be usable as a standalone build, to link against other versions of json-c.
Tweak json_parse.c slightly to allow it to build against older json-c versions.
1 parent 0027229 commit d9981f6

File tree

4 files changed

+139
-9
lines changed

4 files changed

+139
-9
lines changed

apps/CMakeLists.txt

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,122 @@
11

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
53

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+
# ---------------------------------
9107

10108
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.
11122

apps/cmake/apps_config.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/* Define to 1 if you have the <sys/resource.h> header file. */
3+
#cmakedefine HAVE_SYS_RESOURCE_H
4+
5+
/* Define if you have the `getrusage' function. */
6+
#cmakedefine HAVE_GETRUSAGE
7+
8+
#cmakedefine HAVE_JSON_TOKENER_GET_PARSE_END

apps/json_parse.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include <string.h>
99
#include <unistd.h>
1010

11-
#include "config.h"
11+
#include "apps_config.h"
12+
13+
/* XXX for a regular program, these should be <json-c/foo.h>
14+
* but that's inconvenient when building in the json-c source tree.
15+
*/
1216
#include "json_object.h"
1317
#include "json_tokener.h"
1418
#include "json_util.h"
@@ -23,6 +27,10 @@ static int show_output = 1;
2327
static int strict_mode = 0;
2428
static const char *fname = NULL;
2529

30+
#ifndef HAVE_JSON_TOKENER_GET_PARSE_END
31+
#define json_tokener_get_parse_end(tok) ((tok)->char_offset)
32+
#endif
33+
2634
static void usage(int exitval, const char *errmsg);
2735
static void showmem(void);
2836
static int parseit(int fd, int (*callback)(struct json_object *));
@@ -52,7 +60,11 @@ static int parseit(int fd, int (*callback)(struct json_object *))
5260
fprintf(stderr, "unable to allocate json_tokener: %s\n", strerror(errno));
5361
return 1;
5462
}
55-
json_tokener_set_flags(tok, JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS);
63+
json_tokener_set_flags(tok, JSON_TOKENER_STRICT
64+
#ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
65+
| JSON_TOKENER_ALLOW_TRAILING_CHARS
66+
#endif
67+
);
5668

5769
// XXX push this into some kind of json_tokener_parse_fd API?
5870
// json_object_from_fd isn't flexible enough, and mirroring

cmake/config.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* config.h.in. Generated from configure.ac by autoheader. */
21

32
/* Enable RDRAND Hardware RNG Hash Seed */
43
#cmakedefine ENABLE_RDRAND "@ENABLE_RDRAND@"

0 commit comments

Comments
 (0)