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)
8 # The project() command manages VERSION variables.
9 cmake_policy(SET CMP0048 NEW)
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)
15 # Targets may not link directly to themselves.
16 cmake_policy(SET CMP0038 NEW)
18 # MACOSX_RPATH is enabled by default.
19 # We set it explicitly to avoid the warning
20 cmake_policy(SET CMP0042 NEW)
22 # Only interpret if() arguments as variables or keywords when unquoted.
23 cmake_policy(SET CMP0054 NEW)
25 # set default build type if not specified by user
26 if(NOT CMAKE_BUILD_TYPE)
27 set(CMAKE_BUILD_TYPE debug)
30 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
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.
36 cmake_policy(SET CMP0075 NEW)
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)
56 include(CheckSymbolExists)
57 include(CheckIncludeFile)
58 include(CheckIncludeFiles)
59 include(CheckCSourceCompiles)
60 include(CheckTypeSize)
62 include(GNUInstallDirs)
63 include(CMakePackageConfigHelpers)
65 option(BUILD_SHARED_LIBS "Default to building shared libraries" ON)
66 option(BUILD_STATIC_LIBS "Default to building static libraries" ON)
68 if (BUILD_SHARED_LIBS)
69 add_definitions(-D JSON_C_DLL)
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"
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)
97 if (UNIX OR MINGW OR CYGWIN)
98 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
102 list(APPEND CMAKE_REQUIRED_LIBRARIES m)
106 list(APPEND CMAKE_REQUIRED_DEFINITIONS /D_CRT_SECURE_NO_DEPRECATE)
107 list(APPEND CMAKE_REQUIRED_FLAGS /wd4996)
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)
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)
126 check_include_files("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
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
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)
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)
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.
150 set(JSON_C_HAVE_INTTYPES_H 1)
153 set(JSON_C_HAVE_STDINT_H 1)
156 check_symbol_exists(_isnan "float.h" HAVE_DECL__ISNAN)
157 check_symbol_exists(_finite "float.h" HAVE_DECL__FINITE)
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)
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)
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)
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")
188 check_symbol_exists(open "fcntl.h" HAVE_OPEN)
191 check_symbol_exists(realloc "stdlib.h" HAVE_REALLOC)
194 check_symbol_exists(setlocale "locale.h" HAVE_SETLOCALE)
195 check_symbol_exists(uselocale "locale.h" HAVE_USELOCALE)
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)
210 check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
211 check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
214 check_symbol_exists(strdup "string.h" HAVE_STRDUP)
215 check_symbol_exists(strerror "string.h" HAVE_STRERROR)
218 check_symbol_exists(vsyslog "syslog.h" HAVE_VSYSLOG)
220 if (HAVE_SYS_RANDOM_H)
221 check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
223 if (HAVE_SYS_RESOURCE_H)
224 check_symbol_exists(getrusage "sys/resource.h" HAVE_GETRUSAGE)
227 check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
228 check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
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)
236 set(json_c_strtoll "_strtoi64")
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")
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)
257 list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
258 check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
260 check_type_size("ssize_t" SIZEOF_SSIZE_T)
263 check_c_source_compiles(
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;}
269 HAS_GNU_WARNING_LONG)
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)
275 if (NOT DISABLE_THREAD_LOCAL_STORAGE)
276 check_c_source_compiles(
277 "__thread int x = 0; int main() { return 0; }"
281 set(SPEC___THREAD __thread)
283 set(SPEC___THREAD __declspec(thread))
287 # Hardware random number is not available on Windows? Says, config.h.win32. Best to preserve compatibility.
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")
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")
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")
310 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
313 add_definitions(-D_GNU_SOURCE)
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")
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")
330 if (NOT ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC"))
331 check_c_source_compiles(
333 /* uClibc toolchains without threading barf when _REENTRANT is defined */
335 #include <sys/types.h>
344 add_compile_options("-D_REENTRANT")
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(
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")
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(
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")
384 # Build so that valgrind doesn't complain about linkhash.c
385 add_definitions(-DVALGRIND=1)
388 set(JSON_C_PUBLIC_HEADERS
389 # Note: config.h is _not_ included here
390 ${PROJECT_BINARY_DIR}/json_config.h
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
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
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
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\"")
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\"")
445 set(JSON_H_JSON_POINTER "")
446 set(JSON_H_JSON_PATCH "")
449 configure_file(json.h.cmakein ${PROJECT_BINARY_DIR}/json.h @ONLY)
451 include_directories(${PROJECT_SOURCE_DIR})
452 include_directories(${PROJECT_BINARY_DIR})
454 add_subdirectory(doc)
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}
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}
474 set_target_properties(${PROJECT_NAME} PROPERTIES
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}
482 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
483 $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
486 target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
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
495 target_include_directories(${PROJECT_NAME}-static
497 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
498 $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
501 target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
503 # rename the static library
505 set_target_properties(${STATIC_LIB} PROPERTIES
506 OUTPUT_NAME ${PROJECT_NAME}
509 list(APPEND CMAKE_TARGETS ${STATIC_LIB})
512 # Always create new install dirs with 0755 permissions, regardless of umask
513 set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
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
531 install(EXPORT ${PROJECT_NAME}-targets
532 FILE ${PROJECT_NAME}-targets.cmake
533 NAMESPACE ${PROJECT_NAME}::
534 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
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}"
544 FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
545 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
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
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})
557 # Linking against the static json-c requires
558 # dependent packages to include additional libs:
559 SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
561 # Note: We would need cmake >= 3.12 in order to use list(TRANSFORM ...)
562 function(list_transform_prepend var prefix)
565 list(APPEND temp "${prefix}${f}")
567 set(${var} "${temp}" PARENT_SCOPE)
569 list_transform_prepend(LIBS_LIST "-l")
571 string(REPLACE ";" " " LIBS "${LIBS_LIST}")
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}")
578 install(FILES ${JSON_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/json-c)
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
583 add_subdirectory(tests)
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)