2025-10-20 12:08:01 -06:00
2026-07-18 06:37:36 +01:00
project ( 'json-c' , 'c' , version : '0.19.99' ,
2025-11-23 14:30:36 -08:00
meson_version : '>=0.54.0' ,
2025-11-23 14:56:56 -08:00
license : 'MIT' ,
2025-10-20 12:08:01 -06:00
default_options : [ 'buildtype=release' , 'warning_level=2' ])
cc = meson . get_compiler ( 'c' )
# Configuration header generation
conf_data = configuration_data ()
jconf_data = configuration_data ()
conf_data . set ( 'VERSION' , meson . project_version ())
2026-07-18 06:29:19 +01:00
libjson_deps = []
2025-10-20 12:08:01 -06:00
has_std_lib = cc . has_header ( 'stdlib.h' )
has_std_arg = cc . has_header ( 'stdarg.h' )
has_string = cc . has_header ( 'string.h' )
has_float = cc . has_header ( 'float.h' )
if has_std_lib and has_std_arg and has_string and has_float
conf_data . set ( 'STDC_HEADERS' , 1 , description : 'Define to 1 if you have the ANSI C header files.' )
endif
2025-11-04 09:30:30 -08:00
headers = {
'dlfcn.h' : [],
'endian.h' : [],
'fcntl.h' : [],
'inttypes.h' : [],
'limits.h' : [],
'locale.h' : [],
'memory.h' : [],
'stdarg.h' : [],
'stdint.h' : [],
'stdlib.h' : [],
'string.h' : [],
'strings.h' : [],
'syslog.h' : [],
'sys/cdefs.h' : [],
'sys/param.h' : [],
'sys/random.h' : [],
'sys/resource.h' : [],
'sys/stat.h' : [],
'sys/types.h' : [],
'unistd.h' : [],
'xlocale.h' : [],
}
2026-07-18 06:29:19 +01:00
may_need_libbsd = true
if cc . has_function ( 'arc4random' )
may_need_libbsd = false
conf_data . set ( 'HAVE_@0@' . format ( 'arc4random' . underscorify (). to_upper ()), 1 , description : 'Define to 1 if you have the <@0@> header file' . format ( 'arc4random' ))
else
bsd_dep = dependency ( 'libbsd' , required : get_option ( 'extra_libs' ))
headers += { 'bsd/stdlib.h' : [ bsd_dep ] }
endif
2025-11-04 09:30:30 -08:00
foreach h , d : headers
if cc . has_header ( h , dependencies : d )
conf_data . set ( 'HAVE_@0@' . format ( h . underscorify (). to_upper ()), 1 , description : 'Define to 1 if you have the <@0@> header file' . format ( h ))
endif
endforeach
2025-10-20 12:08:01 -06:00
if cc . has_header ( 'inttypes.h' )
conf_data . set ( 'JSON_C_HAVE_INTTYPES_H' , 1 , description : 'Define to 1 if you have the <inttypes.h> header file.' )
jconf_data . set ( 'JSON_C_HAVE_INTTYPES_H' , 1 , description : 'Define to 1 if you have the <inttypes.h> header file.' )
endif
if cc . has_header ( 'stdint.h' )
conf_data . set ( 'JSON_C_HAVE_STDINT_H' , 1 , description : 'Define to 1 if you have the <stdint.h> header file.' )
jconf_data . set ( 'JSON_C_HAVE_STDINT_H' , 1 , description : 'Define to 1 if you have the <stdint.h> header file.' )
endif
2025-11-04 09:30:30 -08:00
2026-07-18 06:29:19 +01:00
funcs = {
'open' : [],
'realloc' : [],
'setlocale' : [],
'strdup' : [],
'strerror' : [],
'uselocale' : [],
'duplocale' : [],
'vsyslog' : [],
'getrandom' : [],
'getrusage' : [],
'strtoll' : [],
'strtoull' : [],
'vasprintf' : [],
}
if may_need_libbsd
funcs += { 'arc4random' : [ bsd_dep ] }
libjson_deps += [ bsd_dep ]
else
funcs += { 'arc4random' : [] }
endif
2025-11-04 09:30:30 -08:00
2025-11-24 16:46:45 -08:00
if conf_data . has ( 'HAVE_STRINGS_H' )
2026-07-18 06:29:19 +01:00
funcs += { 'strcasecmp' : [], 'strncasecmp' : []}
2025-11-24 16:46:45 -08:00
endif
2026-07-18 06:29:19 +01:00
foreach f , d : funcs
if cc . has_function ( f , dependencies : d )
2025-11-04 09:30:30 -08:00
conf_data . set ( 'HAVE_@0@' . format ( f . to_upper ()), 1 , description : 'Define to 1 if you have the `@0@` function.' . format ( f ))
2025-10-20 12:08:01 -06:00
endif
2025-11-04 09:30:30 -08:00
endforeach
2025-10-20 12:08:01 -06:00
2025-11-04 09:30:30 -08:00
foreach f : [ 'snprintf' , 'vsnprintf' , 'vprintf' ]
if cc . has_header_symbol ( 'stdio.h' , f )
conf_data . set ( 'HAVE_@0@' . format ( f . to_upper ()), 1 , description : 'Define to 1 if you have the `@0@` function.' . format ( f ))
endif
endforeach
2025-10-20 12:08:01 -06:00
2025-11-04 09:30:30 -08:00
if not conf_data . has ( 'HAVE_VPRINTF' ) and cc . has_function ( '_doprnt' )
conf_data . set ( 'HAVE_DOPRNT' , 1 , description : 'Define to 1 if you have _doprnt but not vprintf.' )
2025-10-20 12:08:01 -06:00
endif
2025-11-04 09:30:30 -08:00
if conf_data . has ( 'HAVE_STRTOLL' )
2025-10-20 12:08:01 -06:00
conf_data . set ( 'json_c_strtoll' , 'strtoll' )
elif cc . has_function ( '_strtoi64' , prefix : '#include <stdlib.h>' )
conf_data . set ( 'json_c_strtoll' , '_strtoi64' )
endif
2025-11-04 09:30:30 -08:00
if conf_data . has ( 'HAVE_STRTOULL' )
2025-10-20 12:08:01 -06:00
conf_data . set ( 'json_c_strtoull' , 'strtoull' )
elif cc . has_function ( '_strtoui64' , prefix : '#include <stdlib.h>' )
conf_data . set ( 'json_c_strtoull' , '_strtoui64' )
endif
2025-11-04 09:30:30 -08:00
decls = {
'INFINITY' : 'math.h' ,
'isinf' : 'math.h' ,
'isnan' : 'math.h' ,
'NAN' : 'math.h' ,
'_finite' : 'float.h' ,
'_isnan' : 'float.h' ,
}
foreach d , h : decls
if cc . has_header_symbol ( h , d )
conf_data . set ( 'HAVE_DECL_@0@' . format ( d . to_upper ()), 1 , description : 'Define to 1 if you have the declaration of `@0@`' . format ( d ))
endif
endforeach
2025-11-23 14:50:55 -08:00
check_thread = cc . compiles ( '__thread int x;' , name : 'Check for __thread support' )
2025-10-20 12:08:01 -06:00
if check_thread
conf_data . set ( 'HAVE___THREAD' , 1 )
conf_data . set ( 'SPEC___THREAD' , '__thread' )
2025-11-23 14:50:55 -08:00
elif cc . get_argument_syntax () == 'msvc'
2025-10-20 12:08:01 -06:00
conf_data . set ( 'SPEC___THREAD' , '__declspec(thread)' )
endif
gnu_warning_section_support = cc . compiles ( '''
extern void json_object_get();
__asm__(".section .gnu.json_object_get\n\t.ascii \"Please link against libjson-c instead of libjson\"\n\t.text");
int main(int c, char *v) { return 0; }
''' , name : 'Check for GNU warning section support' )
if gnu_warning_section_support
conf_data . set ( 'HAS_GNU_WARNING_LONG' , 1 , description : 'Define to 1 if the compiler supports .gnu.warning sections.' )
endif
atomic_builtin_support = cc . compiles ( '''
int main() {
int x = 0;
int i = __sync_add_and_fetch(&x, 1);
return x;
}
''' ,
name : 'Check for atomic builtins' )
if atomic_builtin_support
conf_data . set ( 'HAVE_ATOMIC_BUILTINS' , 1 , description : 'Define to 1 if the compiler supports atomic builtins.' )
endif
if get_option ( 'enable_rdrand' )
2025-11-04 09:30:30 -08:00
conf_data . set ( 'ENABLE_RDRAND' , 1 )
2025-10-20 12:08:01 -06:00
endif
if get_option ( 'override_get_random_seed' )
conf_data . set ( 'OVERRIDE_GET_RANDOM_SEED' , get_option ( 'override_get_random_seed' ))
endif
if get_option ( 'enable_threading' )
conf_data . set ( 'ENABLE_THREADING' , 1 )
endif
if get_option ( 'newlocale_needs_freelocale' )
conf_data . set ( 'NEWLOCALE_NEEDS_FREELOCALE' , 1 )
endif
conf_data . set ( 'SIZEOF_INT' , cc . sizeof ( 'int' ))
conf_data . set ( 'SIZEOF_INT64_T' , cc . sizeof ( 'int64_t' , prefix : '#include <stdint.h>' ))
conf_data . set ( 'SIZEOF_LONG' , cc . sizeof ( 'long' ))
conf_data . set ( 'SIZEOF_LONG_LONG' , cc . sizeof ( 'long long' ))
2025-12-01 11:37:59 +01:00
conf_data . set ( 'SIZEOF_SIZE_T' , cc . sizeof ( 'size_t' , prefix : '#include <stddef.h>' ))
2025-11-04 09:30:30 -08:00
if cc . get_argument_syntax () == 'msvc'
2025-10-20 12:08:01 -06:00
conf_data . set ( 'SIZEOF_SSIZE_T' , cc . sizeof ( 'SSIZE_T' , prefix : '#include <BaseTsd.h>\n#include <stddef.h>' ))
else
conf_data . set ( 'SIZEOF_SSIZE_T' , cc . sizeof ( 'ssize_t' , prefix : '#include <sys/types.h>' ))
endif
conf_data . set ( 'PACKAGE_VERSION' , meson . project_version ())
conf_data . set ( 'PROJECT_NAME' , meson . project_name ())
configure_header = configure_file (
output : 'config.h' ,
configuration : conf_data
)
json_configure_header = configure_file (
output : 'json_config.h' ,
configuration : jconf_data
)
jhconf_data = configuration_data ()
jhconf_data . set ( 'JSON_H_JSON_PATCH' ,
get_option ( 'disable_json_patch' ) ? '' : '#include "json_patch.h"'
)
jhconf_data . set ( 'JSON_H_JSON_POINTER' ,
get_option ( 'disable_json_pointer' ) ? '' : '#include "json_pointer.h"'
)
json_header = configure_file (
input : 'json.h.cmakein' ,
output : 'json.h' ,
configuration : jhconf_data
)
# Platform-specific flags
add_project_arguments ( '-D_GNU_SOURCE' , language : 'c' )
if host_machine . system () == 'windows'
add_project_arguments ( '-DWIN32' , language : 'c' )
endif
# Compiler flags
2025-11-04 10:12:42 -08:00
message ( 'target is ' + host_machine . system ())
if host_machine . system () == 'windows'
2025-10-20 12:08:01 -06:00
# Cover any compiler on Windows attempting to use MSVC's standard library
2025-11-04 10:41:08 -08:00
add_project_arguments ( '-D_CRT_NONSTDC_NO_DEPRECATE' , '-D_CRT_SECURE_NO_WARNINGS' , language : 'c' )
2025-11-04 09:30:30 -08:00
endif
2025-10-20 12:08:01 -06:00
2025-11-04 10:41:08 -08:00
add_project_arguments ( cc . get_supported_arguments ( '-Wno-unused-parameter' ), language : 'c' )
2025-10-20 12:08:01 -06:00
2026-02-05 13:30:45 -08:00
sym = cc . get_supported_link_arguments (
'-Wl,--version-script,@0@/json-c.sym' . format (
meson . current_source_dir (),
),
)
2025-10-20 12:08:01 -06:00
# Source files
sources = files (
'arraylist.c' , 'debug.c' , 'json_c_version.c' , 'json_object.c' ,
'json_object_iterator.c' , 'json_tokener.c' , 'json_util.c' ,
'json_visit.c' , 'linkhash.c' , 'printbuf.c' , 'random_seed.c' ,
'strerror_override.c'
)
if not get_option ( 'disable_json_pointer' )
sources += files ( 'json_pointer.c' )
if not get_option ( 'disable_json_patch' )
sources += files ( 'json_patch.c' )
endif
endif
# Include directories
inc = include_directories ( '.' )
# Build library
libjson = library ( 'json-c' ,
sources ,
include_directories : inc ,
2026-07-18 06:29:19 +01:00
dependencies : libjson_deps ,
2025-10-20 12:08:01 -06:00
install : true ,
2026-02-05 13:30:45 -08:00
link_args : sym ,
2026-07-18 06:37:36 +01:00
version : '5.5.0' ,
2025-10-20 12:08:01 -06:00
soversion : '5' ,
)
jsonc_dep = declare_dependency ( link_with : libjson , include_directories : inc )
2025-11-23 14:30:36 -08:00
meson . override_dependency ( 'json-c' , jsonc_dep )
2025-10-20 12:08:01 -06:00
2026-07-13 15:48:20 -06:00
# Install headers into json-c/ subdirectory (matches CMake layout)
installed_headers = [
2025-10-20 12:08:01 -06:00
'arraylist.h' , 'debug.h' , 'json_c_version.h' , 'json_inttypes.h' ,
'json_object.h' , 'json_object_iterator.h' , 'json_tokener.h' ,
'json_types.h' , 'json_util.h' , 'json_visit.h' , 'linkhash.h' ,
'printbuf.h' , json_configure_header , json_header
2026-07-13 15:48:20 -06:00
]
if not get_option ( 'disable_json_pointer' )
installed_headers += [ 'json_pointer.h' ]
if not get_option ( 'disable_json_patch' )
installed_headers += [ 'json_patch.h' ]
endif
endif
install_headers ( installed_headers , subdir : 'json-c' )
# pkg-config file
configure_file (
input : 'json-c.pc.in' ,
output : 'json-c.pc' ,
install : true ,
install_dir : get_option ( 'libdir' ) / 'pkgconfig' ,
configuration : {
'prefix' : get_option ( 'prefix' ),
'exec_prefix' : get_option ( 'prefix' ),
'libdir' : get_option ( 'libdir' ),
'includedir' : get_option ( 'includedir' ),
'VERSION' : meson . project_version (),
'LIBS' : '' ,
}
2025-10-20 12:08:01 -06:00
)
# Optional apps
2025-11-04 10:12:42 -08:00
if get_option ( 'build_apps' ) and host_machine . system () != 'windows'
2025-10-20 12:08:01 -06:00
subdir ( 'apps' )
endif
2026-07-18 06:32:31 +01:00
subdir ( 'tests' )