]> git.ipfire.org Git - thirdparty/json-c.git/blob - CMakeLists.txt
Update the master branch to version 0.17.99
[thirdparty/json-c.git] / CMakeLists.txt
1 # CMake 3.9 was released in 2017/07
2 # As of 2023, many versions of Linux, NetBSD and FreeBSD provide,
3 # and many OpenWRT packages require, much newer CMake packages.
4 # We're stopping before 3.10 because that version starts requiring
5 # c++11, which isn't available on e.g HPUX.
6 cmake_minimum_required(VERSION 3.9)
7
8 # The project() command manages VERSION variables.
9 cmake_policy(SET CMP0048 NEW)
10
11 # JSON-C library is C only project.
12 # PROJECT_VERSION{,_MAJOR,_MINOR,_PATCH} set by project():
13 project(json-c LANGUAGES C VERSION 0.17.99)
14
15 # Targets may not link directly to themselves.
16 cmake_policy(SET CMP0038 NEW)
17
18 # MACOSX_RPATH is enabled by default.
19 # We set it explicitly to avoid the warning
20 cmake_policy(SET CMP0042 NEW)
21
22 # Only interpret if() arguments as variables or keywords when unquoted.
23 cmake_policy(SET CMP0054 NEW)
24
25 # set default build type if not specified by user
26 if(NOT CMAKE_BUILD_TYPE)
27 set(CMAKE_BUILD_TYPE debug)
28 endif()
29
30 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
31
32 # Include file check macros honor CMAKE_REQUIRED_LIBRARIES
33 # i.e. the check_include_file() calls will include -lm when checking.
34 # New in version 3.12.
35 if(POLICY CMP0075)
36 cmake_policy(SET CMP0075 NEW)
37 endif()
38
39 include(CTest)
40
41 # Set some packaging variables.
42 set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
43 set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
44 set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
45 set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
46 set(JSON_C_BUGREPORT "json-c@googlegroups.com")
47 set(CPACK_SOURCE_IGNORE_FILES
48 ${PROJECT_SOURCE_DIR}/build
49 ${PROJECT_SOURCE_DIR}/cmake-build-debug
50 ${PROJECT_SOURCE_DIR}/pack
51 ${PROJECT_SOURCE_DIR}/.idea
52 ${PROJECT_SOURCE_DIR}/.DS_Store
53 ${PROJECT_SOURCE_DIR}/.git
54 ${PROJECT_SOURCE_DIR}/.vscode)
55
56 include(CheckSymbolExists)
57 include(CheckIncludeFile)
58 include(CheckIncludeFiles)
59 include(CheckCSourceCompiles)
60 include(CheckTypeSize)
61 include(CPack)
62 include(GNUInstallDirs)
63 include(CMakePackageConfigHelpers)
64
65 option(BUILD_SHARED_LIBS "Default to building shared libraries" ON)
66 option(BUILD_STATIC_LIBS "Default to building static libraries" ON)
67
68 if (BUILD_SHARED_LIBS)
69 add_definitions(-D JSON_C_DLL)
70 endif()
71
72 # Generate a release merge and test it to verify the correctness of republishing the package.
73 ADD_CUSTOM_TARGET(distcheck
74 COMMAND make package_source
75 COMMAND tar -xvf "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source.tar.gz"
76 COMMAND mkdir "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
77 COMMAND cmake "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/" -B"./${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build/"
78 COMMAND make -C "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
79 COMMAND make test -C "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source/build"
80 COMMAND rm -rf "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source"
81 )
82
83 # Enable or disable features. By default, all features are turned off.
84 option(DISABLE_BSYMBOLIC "Avoid linking with -Bsymbolic-function." OFF)
85 option(DISABLE_THREAD_LOCAL_STORAGE "Disable using Thread-Local Storage (HAVE___THREAD)." OFF)
86 option(DISABLE_WERROR "Avoid treating compiler warnings as fatal errors." OFF)
87 option(ENABLE_RDRAND "Enable RDRAND Hardware RNG Hash Seed." OFF)
88 option(ENABLE_THREADING "Enable partial threading support." OFF)
89 option(OVERRIDE_GET_RANDOM_SEED "Override json_c_get_random_seed() with custom code." OFF)
90 option(DISABLE_EXTRA_LIBS "Avoid linking against extra libraries, such as libbsd." OFF)
91 option(DISABLE_JSON_POINTER "Disable JSON pointer (RFC6901) and JSON patch support." OFF)
92 option(DISABLE_JSON_PATCH "Disable JSON patch (RFC6902) support." OFF)
93 option(NEWLOCALE_NEEDS_FREELOCALE "Work around newlocale bugs in old FreeBSD by calling freelocale" OFF)
94 option(BUILD_APPS "Default to building apps" ON)
95
96
97 if (UNIX OR MINGW OR CYGWIN)
98 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
99 endif()
100
101 if (UNIX)
102 list(APPEND CMAKE_REQUIRED_LIBRARIES m)
103 endif()
104
105 if (MSVC)
106 list(APPEND CMAKE_REQUIRED_DEFINITIONS /D_CRT_SECURE_NO_DEPRECATE)
107 list(APPEND CMAKE_REQUIRED_FLAGS /wd4996)
108 endif()
109
110 if (NOT DISABLE_STATIC_FPIC)
111 # Use '-fPIC'/'-fPIE' option.
112 # This will allow other libraries to statically link in libjson-c.a
113 # which in turn prevents crashes in downstream apps that may use
114 # a different JSON library with identical symbol names.
115 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
116 endif()
117
118 check_include_file("fcntl.h" HAVE_FCNTL_H)
119 check_include_file("inttypes.h" HAVE_INTTYPES_H)
120 check_include_file(stdarg.h HAVE_STDARG_H)
121 check_include_file(strings.h HAVE_STRINGS_H)
122 check_include_file(string.h HAVE_STRING_H)
123 check_include_file(syslog.h HAVE_SYSLOG_H)
124
125
126 check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
127
128 check_include_file(unistd.h HAVE_UNISTD_H)
129 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
130 check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H) # for getrusage
131
132 check_include_file("dlfcn.h" HAVE_DLFCN_H)
133 check_include_file("endian.h" HAVE_ENDIAN_H)
134 check_include_file("limits.h" HAVE_LIMITS_H)
135 check_include_file("locale.h" HAVE_LOCALE_H)
136 check_include_file("memory.h" HAVE_MEMORY_H)
137
138 check_include_file(stdint.h HAVE_STDINT_H)
139 check_include_file(stdlib.h HAVE_STDLIB_H)
140 check_include_file(sys/cdefs.h HAVE_SYS_CDEFS_H)
141 check_include_file(sys/param.h HAVE_SYS_PARAM_H)
142 check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
143 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
144 check_include_file(xlocale.h HAVE_XLOCALE_H)
145
146 # Set json-c specific vars to stamp into json_config.h
147 # in a way that hopefully won't conflict with other
148 # projects that use json-c.
149 if (HAVE_INTTYPES_H)
150 set(JSON_C_HAVE_INTTYPES_H 1)
151 endif()
152 if (HAVE_STDINT_H)
153 set(JSON_C_HAVE_STDINT_H 1)
154 endif()
155
156 check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
157 check_symbol_exists(_finite "float.h" HAVE_DECL__FINITE)
158
159 if ((MSVC AND NOT (MSVC_VERSION LESS 1800)) OR MINGW OR CYGWIN OR UNIX)
160 check_symbol_exists(INFINITY "math.h" HAVE_DECL_INFINITY)
161 check_symbol_exists(isinf "math.h" HAVE_DECL_ISINF)
162 check_symbol_exists(isnan "math.h" HAVE_DECL_ISNAN)
163 check_symbol_exists(nan "math.h" HAVE_DECL_NAN)
164 endif()
165
166 check_symbol_exists(_doprnt "stdio.h" HAVE_DOPRNT)
167 if (UNIX OR MINGW OR CYGWIN)
168 check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
169 endif()
170 check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
171 check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
172 check_symbol_exists(vprintf "stdio.h" HAVE_VPRINTF)
173
174 check_symbol_exists(arc4random "stdlib.h" HAVE_ARC4RANDOM)
175 if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
176 check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
177 if (HAVE_BSD_STDLIB_H)
178 list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
179 unset(HAVE_ARC4RANDOM CACHE)
180 check_symbol_exists(arc4random "bsd/stdlib.h" HAVE_ARC4RANDOM)
181 if (NOT HAVE_ARC4RANDOM)
182 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
183 endif()
184 endif()
185 endif()
186
187 if (HAVE_FCNTL_H)
188 check_symbol_exists(open "fcntl.h" HAVE_OPEN)
189 endif()
190 if (HAVE_STDLIB_H)
191 check_symbol_exists(realloc "stdlib.h" HAVE_REALLOC)
192 endif()
193 if (HAVE_LOCALE_H)
194 check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
195 check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
196 endif()
197
198 # uClibc *intentionally* crashes in duplocale(), at least as of:
199 # https://github.com/ffainelli/uClibc/blob/266bdc1/libc/misc/locale/locale.c#L1322
200 # So, if it looks like we're compiling for a system like that just disable
201 # locale handling entirely.
202 exec_program(${CMAKE_C_COMPILER} ARGS -dumpmachine OUTPUT_VARIABLE CMAKE_GNU_C_MACHINE)
203 if (CMAKE_GNU_C_MACHINE MATCHES "uclibc")
204 message(STATUS "Detected uClibc compiler, disabling locale handling")
205 set(HAVE_SETLOCALE 0)
206 set(HAVE_USELOCALE 0)
207 endif()
208
209 if (HAVE_STRINGS_H)
210 check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
211 check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
212 endif()
213 if (HAVE_STRING_H)
214 check_symbol_exists(strdup "string.h" HAVE_STRDUP)
215 check_symbol_exists(strerror "string.h" HAVE_STRERROR)
216 endif()
217 if (HAVE_SYSLOG_H)
218 check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
219 endif()
220 if (HAVE_SYS_RANDOM_H)
221 check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
222 endif()
223 if (HAVE_SYS_RESOURCE_H)
224 check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
225 endif()
226
227 check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
228 check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
229
230 set(json_c_strtoll "strtoll")
231 if (NOT HAVE_STRTOLL)
232 # Use _strtoi64 if strtoll is not available.
233 check_symbol_exists(_strtoi64 "stdlib.h" __have_strtoi64)
234 if (__have_strtoi64)
235 #set(HAVE_STRTOLL 1)
236 set(json_c_strtoll "_strtoi64")
237 endif()
238 endif()
239
240 set(json_c_strtoull "strtoull")
241 if (NOT HAVE_STRTOULL)
242 # Use _strtoui64 if strtoull is not available.
243 check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
244 if (__have_strtoui64)
245 #set(HAVE_STRTOULL 1)
246 set(json_c_strtoull "_strtoui64")
247 endif()
248 endif()
249
250
251 check_type_size(int SIZEOF_INT)
252 check_type_size(int64_t SIZEOF_INT64_T)
253 check_type_size(long SIZEOF_LONG)
254 check_type_size("long long" SIZEOF_LONG_LONG)
255 check_type_size("size_t" SIZEOF_SIZE_T)
256 if (MSVC)
257 list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
258 check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
259 else()
260 check_type_size("ssize_t" SIZEOF_SSIZE_T)
261 endif()
262
263 check_c_source_compiles(
264 "
265 extern void json_object_get();
266 __asm__(\".section .gnu.json_object_get\\n\\t.ascii \\\"Please link against libjson-c instead of libjson\\\"\\n\\t.text\");
267 int main(int c, char *v) { return 0;}
268 "
269 HAS_GNU_WARNING_LONG)
270
271 check_c_source_compiles(
272 "int main() { int i, x = 0; i = __sync_add_and_fetch(&x,1); return x; }"
273 HAVE_ATOMIC_BUILTINS)
274
275 if (NOT DISABLE_THREAD_LOCAL_STORAGE)
276 check_c_source_compiles(
277 "__thread int x = 0; int main() { return 0; }"
278 HAVE___THREAD)
279
280 if (HAVE___THREAD)
281 set(SPEC___THREAD __thread)
282 elseif (MSVC)
283 set(SPEC___THREAD __declspec(thread))
284 endif()
285 endif()
286
287 # Hardware random number is not available on Windows? Says, config.h.win32. Best to preserve compatibility.
288 if (WIN32)
289 set(ENABLE_RDRAND 0)
290 endif()
291
292 # Once we've done basic symbol/header searches let's add them in.
293 configure_file(${PROJECT_SOURCE_DIR}/cmake/config.h.in ${PROJECT_BINARY_DIR}/config.h)
294 message(STATUS "Wrote ${PROJECT_BINARY_DIR}/config.h")
295 configure_file(${PROJECT_SOURCE_DIR}/cmake/json_config.h.in ${PROJECT_BINARY_DIR}/json_config.h)
296 message(STATUS "Wrote ${PROJECT_BINARY_DIR}/json_config.h")
297
298 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
299 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
300 if ("${DISABLE_WERROR}" STREQUAL "OFF")
301 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
302 endif()
303 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
304 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual")
305 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
306 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
307 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings")
308 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
309 if (NOT WIN32)
310 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
311 endif()
312
313 add_definitions(-D_GNU_SOURCE)
314
315 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
316 # Remove this for 1.0 when we can bump the ABI and actually fix these warnings.
317 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-shorten-64-to-32")
318 endif()
319 elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
320 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DEBUG")
321 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
322 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
323 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4244")
324 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706")
325 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4702")
326 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
327 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
328 endif()
329
330 if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
331 check_c_source_compiles(
332 "
333 /* uClibc toolchains without threading barf when _REENTRANT is defined */
334 #define _REENTRANT 1
335 #include <sys/types.h>
336 int main (void)
337 {
338 return 0;
339 }
340 "
341 REENTRANT_WORKS
342 )
343 if (REENTRANT_WORKS)
344 add_compile_options("-D_REENTRANT")
345 endif()
346
347 # OSX Mach-O doesn't support linking with '-Bsymbolic-functions'.
348 # Others may not support it, too.
349 list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
350 check_c_source_compiles(
351 "
352 int main (void)
353 {
354 return 0;
355 }
356 "
357 BSYMBOLIC_WORKS
358 )
359 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,-Bsymbolic-functions")
360 if (DISABLE_BSYMBOLIC STREQUAL "OFF" AND BSYMBOLIC_WORKS)
361 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Bsymbolic-functions")
362 # XXX need cmake>=3.13 for this:
363 #add_link_options("-Wl,-Bsymbolic-functions")
364 endif()
365
366 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym" "TEST { global: *; };")
367 list(APPEND CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
368 check_c_source_compiles(
369 "
370 int main (void)
371 {
372 return 0;
373 }
374 "
375 VERSION_SCRIPT_WORKS
376 )
377 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/check-version-script.sym")
378 if (VERSION_SCRIPT_WORKS)
379 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/json-c.sym")
380 endif()
381 endif()
382
383 if ($ENV{VALGRIND})
384 # Build so that valgrind doesn't complain about linkhash.c
385 add_definitions(-DVALGRIND=1)
386 endif()
387
388 set(JSON_C_PUBLIC_HEADERS
389 # Note: config.h is _not_ included here
390 ${PROJECT_BINARY_DIR}/json_config.h
391
392 ${PROJECT_BINARY_DIR}/json.h
393 ${PROJECT_SOURCE_DIR}/arraylist.h
394 ${PROJECT_SOURCE_DIR}/debug.h
395 ${PROJECT_SOURCE_DIR}/json_c_version.h
396 ${PROJECT_SOURCE_DIR}/json_inttypes.h
397 ${PROJECT_SOURCE_DIR}/json_object.h
398 ${PROJECT_SOURCE_DIR}/json_object_iterator.h
399 ${PROJECT_SOURCE_DIR}/json_tokener.h
400 ${PROJECT_SOURCE_DIR}/json_types.h
401 ${PROJECT_SOURCE_DIR}/json_util.h
402 ${PROJECT_SOURCE_DIR}/json_visit.h
403 ${PROJECT_SOURCE_DIR}/linkhash.h
404 ${PROJECT_SOURCE_DIR}/printbuf.h
405 )
406
407 set(JSON_C_HEADERS
408 ${JSON_C_PUBLIC_HEADERS}
409 ${PROJECT_SOURCE_DIR}/json_object_private.h
410 ${PROJECT_SOURCE_DIR}/json_pointer_private.h
411 ${PROJECT_SOURCE_DIR}/random_seed.h
412 ${PROJECT_SOURCE_DIR}/strerror_override.h
413 ${PROJECT_SOURCE_DIR}/math_compat.h
414 ${PROJECT_SOURCE_DIR}/snprintf_compat.h
415 ${PROJECT_SOURCE_DIR}/strdup_compat.h
416 ${PROJECT_SOURCE_DIR}/vasprintf_compat.h
417 )
418
419 set(JSON_C_SOURCES
420 ${PROJECT_SOURCE_DIR}/arraylist.c
421 ${PROJECT_SOURCE_DIR}/debug.c
422 ${PROJECT_SOURCE_DIR}/json_c_version.c
423 ${PROJECT_SOURCE_DIR}/json_object.c
424 ${PROJECT_SOURCE_DIR}/json_object_iterator.c
425 ${PROJECT_SOURCE_DIR}/json_tokener.c
426 ${PROJECT_SOURCE_DIR}/json_util.c
427 ${PROJECT_SOURCE_DIR}/json_visit.c
428 ${PROJECT_SOURCE_DIR}/linkhash.c
429 ${PROJECT_SOURCE_DIR}/printbuf.c
430 ${PROJECT_SOURCE_DIR}/random_seed.c
431 ${PROJECT_SOURCE_DIR}/strerror_override.c
432 )
433
434 if (NOT DISABLE_JSON_POINTER)
435 set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_pointer.h)
436 set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_pointer.c)
437 set(JSON_H_JSON_POINTER "#include \"json_pointer.h\"")
438
439 if (NOT DISABLE_JSON_PATCH)
440 set(JSON_C_PUBLIC_HEADERS ${JSON_C_PUBLIC_HEADERS} ${PROJECT_SOURCE_DIR}/json_patch.h)
441 set(JSON_C_SOURCES ${JSON_C_SOURCES} ${PROJECT_SOURCE_DIR}/json_patch.c)
442 set(JSON_H_JSON_PATCH "#include \"json_patch.h\"")
443 endif()
444 else()
445 set(JSON_H_JSON_POINTER "")
446 set(JSON_H_JSON_PATCH "")
447 endif()
448
449 configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
450
451 include_directories(${PROJECT_SOURCE_DIR})
452 include_directories(${PROJECT_BINARY_DIR})
453
454 add_subdirectory(doc)
455
456 # "uninstall" custom target for make generators in unix like operating systems
457 # and if that target is not present
458 if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
459 if(NOT TARGET uninstall)
460 add_custom_target(uninstall
461 COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm
462 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
463 )
464 endif()
465 endif()
466
467 # XXX for a normal full distribution we'll need to figure out
468 # XXX how to build both shared and static libraries.
469 # Probably leverage that to build a local VALGRIND=1 library for testing too.
470 add_library(${PROJECT_NAME}
471 ${JSON_C_SOURCES}
472 ${JSON_C_HEADERS}
473 )
474 set_target_properties(${PROJECT_NAME} PROPERTIES
475 VERSION 5.3.0
476 SOVERSION 5)
477 list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
478 # If json-c is used as subroject it set to target correct interface -I flags and allow
479 # to build external target without extra include_directories(...)
480 target_include_directories(${PROJECT_NAME}
481 PUBLIC
482 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
483 $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
484 )
485
486 target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
487
488 # Allow to build static and shared libraries at the same time
489 if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
490 set(STATIC_LIB ${PROJECT_NAME}-static)
491 add_library(${STATIC_LIB} STATIC
492 ${JSON_C_SOURCES}
493 ${JSON_C_HEADERS}
494 )
495 target_include_directories(${PROJECT_NAME}-static
496 PUBLIC
497 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
498 $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
499 )
500
501 target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
502
503 # rename the static library
504 if (NOT MSVC)
505 set_target_properties(${STATIC_LIB} PROPERTIES
506 OUTPUT_NAME ${PROJECT_NAME}
507 )
508 endif()
509 list(APPEND CMAKE_TARGETS ${STATIC_LIB})
510 endif ()
511
512 # Always create new install dirs with 0755 permissions, regardless of umask
513 set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
514 OWNER_READ
515 OWNER_WRITE
516 OWNER_EXECUTE
517 GROUP_READ
518 GROUP_EXECUTE
519 WORLD_READ
520 WORLD_EXECUTE
521 )
522
523 install(TARGETS ${CMAKE_TARGETS}
524 EXPORT ${PROJECT_NAME}-targets
525 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
526 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
527 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
528 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/json-c
529 )
530
531 install(EXPORT ${PROJECT_NAME}-targets
532 FILE ${PROJECT_NAME}-targets.cmake
533 NAMESPACE ${PROJECT_NAME}::
534 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
535 )
536
537 configure_package_config_file(
538 "cmake/Config.cmake.in"
539 ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
540 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
541 )
542
543 install(
544 FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
545 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
546 )
547
548 if (UNIX OR MINGW OR CYGWIN)
549 SET(prefix ${CMAKE_INSTALL_PREFIX})
550 # exec_prefix is prefix by default and CMake does not have the
551 # concept.
552 SET(exec_prefix ${CMAKE_INSTALL_PREFIX})
553 SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
554 SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
555 SET(VERSION ${PROJECT_VERSION})
556
557 # Linking against the static json-c requires
558 # dependent packages to include additional libs:
559 SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
560
561 # Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
562 function(list_transform_prepend var prefix)
563 set(temp "")
564 foreach(f ${${var}})
565 list(APPEND temp "${prefix}${f}")
566 endforeach()
567 set(${var} "${temp}" PARENT_SCOPE)
568 endfunction()
569 list_transform_prepend(LIBS_LIST "-l")
570
571 string(REPLACE ";" " " LIBS "${LIBS_LIST}")
572
573 configure_file(json-c.pc.in json-c.pc @ONLY)
574 set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
575 install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")
576 endif ()
577
578 install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
579
580 if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING AND
581 (NOT MSVC OR NOT (MSVC_VERSION LESS 1800)) # Tests need at least VS2013
582 )
583 add_subdirectory(tests)
584 endif()
585
586 if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_APPS)
587 # skip apps when we're included in someone else's build
588 if (NOT MSVC) # cmd line apps don't built on Windows currently.
589 add_subdirectory(apps)
590 endif()
591 endif()
592