tizen: kdbus: make i explicitly positive in make_single_header_vector
[platform/upstream/glib.git] / meson.build
1 project('glib', 'c',
2   version : '2.80.5',
3   # NOTE: See the policy in docs/meson-version.md before changing the Meson dependency
4   meson_version : '>= 1.2.0',
5   default_options : [
6     'buildtype=debugoptimized',
7     'warning_level=3',
8     'c_std=gnu99'
9   ]
10 )
11
12 fs = import('fs')
13
14 cc = meson.get_compiler('c')
15 c_standards = {}
16
17 foreach std : ['90', '99', '11', '17']
18   arg = (cc.get_id() == 'msvc' ? '/std:' : '-std=') + 'c' + std
19   if cc.has_argument(arg)
20     c_standards += { std: arg }
21   endif
22 endforeach
23
24 have_cxx = add_languages('cpp', native: false, required: get_option('oss_fuzz').enabled())
25 if have_cxx
26   cxx = meson.get_compiler('cpp')
27   cxx_standards = {}
28
29   foreach std : ['98', '03', '11', '14', '17', '20', '2b', 'latest']
30     arg = (cxx.get_id() == 'msvc' ? '/std:' : '-std=') + 'c++' + std
31     if cxx.has_argument(arg)
32       cxx_standards += { std: arg }
33     endif
34   endforeach
35 endif
36
37 cc_can_run = meson.can_run_host_binaries()
38
39 if cc.get_argument_syntax() == 'msvc'
40   # Ignore several spurious warnings for things glib does very commonly
41   # (also for clang-cl)
42   add_project_arguments('/FIglib/msvc_recommended_pragmas.h', language : 'c')
43 endif
44
45 if cc.get_id() == 'msvc'
46   # Set the input and exec encoding to utf-8, like is the default with GCC
47   add_project_arguments(cc.get_supported_arguments(['/utf-8']), language: 'c')
48   # Disable SAFESEH with MSVC for plugins and libs that use external deps that
49   # are built with MinGW
50   noseh_link_args = ['/SAFESEH:NO']
51 else
52   noseh_link_args = []
53   # -mms-bitfields vs -fnative-struct ?
54 endif
55
56 host_system = host_machine.system()
57
58 if host_system == 'darwin'
59   ios_test_code = '''#include <TargetConditionals.h>
60   #if ! TARGET_OS_IPHONE
61   #error "Not iOS/tvOS/watchOS/iPhoneSimulator"
62   #endif'''
63   if cc.compiles(ios_test_code, name : 'building for iOS')
64     host_system = 'ios'
65   endif
66 endif
67
68 linux_libc = ''
69 if host_system == 'linux'
70   musl_test_code = '''#include <stdlib.h>
71   #if defined __GLIBC__ || defined __UCLIBC__
72   #error "Not in glibc or uclibc"
73   #endif'''
74   if cc.compiles(musl_test_code, name : 'building for musl libc')
75     linux_libc = 'musl'
76   endif
77 endif
78
79 glib_version = meson.project_version()
80 glib_api_version = '2.0'
81 version_arr = glib_version.split('.')
82 major_version = version_arr[0].to_int()
83 minor_version = version_arr[1].to_int()
84 micro_version = version_arr[2].to_int()
85
86 interface_age = minor_version.is_odd() ? 0 : micro_version
87 binary_age = 100 * minor_version + micro_version
88
89 soversion = 0
90 # Maintain compatibility with previous libtool versioning
91 # current = minor * 100 + micro
92 current = binary_age - interface_age
93 library_version = '@0@.@1@.@2@'.format(soversion, current, interface_age)
94 darwin_versions = [current + 1, '@0@.@1@'.format(current + 1, interface_age)]
95
96 configinc = include_directories('.')
97 glibinc = include_directories('glib')
98 gobjectinc = include_directories('gobject')
99 gmoduleinc = include_directories('gmodule')
100 gioinc = include_directories('gio')
101 girepoinc = include_directories('girepository')
102
103 glib_prefix = get_option('prefix')
104 glib_bindir = join_paths(glib_prefix, get_option('bindir'))
105 glib_libdir = join_paths(glib_prefix, get_option('libdir'))
106 glib_libexecdir = join_paths(glib_prefix, get_option('libexecdir'))
107 glib_datadir = join_paths(glib_prefix, get_option('datadir'))
108 glib_pkgdatadir = join_paths(glib_datadir, 'glib-2.0')
109 glib_includedir = join_paths(glib_prefix, get_option('includedir'), 'glib-2.0')
110 if get_option('gio_module_dir') != ''
111   glib_giomodulesdir = join_paths(glib_prefix, get_option('gio_module_dir'))
112 else
113   glib_giomodulesdir = join_paths(glib_libdir, 'gio', 'modules')
114 endif
115
116 if get_option('multiarch')
117   # For multiarch/multilib distributions, install each architecture's
118   # build of executables used in packaging triggers (like gio-querymodules)
119   # to an architecture-dependent location, with a compatibility symlink
120   # in the PATH.
121   multiarch_bindir = get_option('libdir') / 'glib-2.0'
122   multiarch_libexecdir = multiarch_bindir
123   pkgconfig_multiarch_bindir = '${libdir}/glib-2.0'
124 else
125   # For single-architecture distributions, just install them into the PATH
126   # as was traditionally done.
127   multiarch_bindir = get_option('bindir')
128   multiarch_libexecdir = get_option('libexecdir')
129   pkgconfig_multiarch_bindir = '${bindir}'
130 endif
131
132 glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
133
134 if get_option('charsetalias_dir') != ''
135   glib_charsetaliasdir = join_paths(glib_prefix, get_option('charsetalias_dir'))
136 else
137   glib_charsetaliasdir = glib_libdir
138 endif
139
140 glib_localstatedir = glib_prefix / get_option('localstatedir')
141
142 if get_option('runtime_dir') != ''
143   glib_runstatedir = glib_prefix / get_option('runtime_dir')
144 else
145   # While we’d normally prefix directories like this with, for example,
146   # glib_localstatedir, `/run` is a bit different in that it’s for runtime state
147   # rather than data files, so it’s typically functionally useless to use a
148   # prefixed version. No other processes will be using it. So we default to the
149   # unprefixed system `/run` directory.
150   glib_runstatedir = '/run'
151 endif
152
153 # When building glib and gobject-introspection with subprojects, gobject-introspection
154 # requires to know the path of the sources and the build directory for the subproject.
155 # We provide it here with a variable.
156 glib_source_dir = meson.current_source_dir()
157 glib_build_dir = meson.current_build_dir()
158
159 installed_tests_metadir = join_paths(glib_datadir, 'installed-tests', meson.project_name())
160 installed_tests_execdir = join_paths(glib_libexecdir, 'installed-tests', meson.project_name())
161 installed_tests_enabled = get_option('installed_tests')
162 installed_tests_template = files('tests/template.test.in')
163 installed_tests_template_tap = files('tests/template-tap.test.in')
164
165 # Don’t build the tests unless we can run them (either natively, in an exe wrapper, or by installing them for later use)
166 build_tests = get_option('tests') and (meson.can_run_host_binaries() or installed_tests_enabled)
167
168 common_test_env = [
169   'G_DEBUG=gc-friendly',
170   'G_ENABLE_DIAGNOSTIC=1',
171   'MALLOC_CHECK_=2',
172 ]
173
174 if get_option('werror')
175   common_test_env += 'LINT_WARNINGS_ARE_ERRORS=1'
176 endif
177
178 # Note: this may cause the tests output not to be printed when running in
179 # verbose mode, see https://github.com/mesonbuild/meson/issues/11185
180 # Can be changed it to 'exitcode' if required during development.
181 test_protocol = 'tap'
182 test_timeout = 30
183 test_timeout_slow = 90
184
185 add_test_setup('default',
186   is_default: true,
187   exclude_suites: ['flaky', 'failing'],
188   env: common_test_env,
189   timeout_multiplier: 2,
190 )
191
192 add_test_setup('unstable_tests',
193   env: common_test_env,
194   timeout_multiplier: 2,
195   # Empty test setup, used for having different results set for flaky tests
196   # Sadly we can't use (https://github.com/mesonbuild/meson/issues/10934):
197   #suites: ['flaky', 'unstable']
198 )
199
200 add_test_setup('thorough',
201   exclude_suites: ['flaky', 'failing', 'performance'],
202   env: common_test_env,
203   timeout_multiplier: 20,
204   exe_wrapper: [find_program('./.gitlab-ci/thorough-test-wrapper.sh', required: true)],
205 )
206
207 # Allow the tests to be easily run under valgrind using --setup=valgrind
208 valgrind = find_program('valgrind', required: false)
209 valgrind_suppression_file = files('tools' / 'glib.supp')[0]
210 valgrind_suppression_file_install_subdir = 'glib-2.0' / 'valgrind'
211
212 if valgrind.found()
213   add_test_setup('valgrind',
214     exclude_suites: [ 'no-valgrind', 'flaky' ],
215     exe_wrapper: [
216       valgrind,
217       '--tool=memcheck',
218       '--error-exitcode=1',
219       '--track-origins=yes',
220       '--leak-check=full',
221       '--leak-resolution=high',
222       '--num-callers=50',
223       '--show-leak-kinds=definite,possible',
224       '--show-error-list=yes',
225       '--suppressions=@0@'.format(meson.project_source_root() /
226         '@0@'.format(valgrind_suppression_file)),
227     ],
228     env: common_test_env,
229     timeout_multiplier: 20,
230   )
231 endif
232
233 add_project_arguments('-D_GNU_SOURCE', language: 'c')
234
235 if host_system == 'qnx'
236   add_project_arguments('-D_QNX_SOURCE', language: 'c')
237 endif
238
239 if host_system == 'windows'
240   add_project_arguments(['-DUNICODE', '-D_UNICODE'], language: 'c')
241 endif
242
243 # Disable strict aliasing;
244 # see https://bugzilla.gnome.org/show_bug.cgi?id=791622
245 if cc.has_argument('-fno-strict-aliasing')
246   add_project_arguments('-fno-strict-aliasing', language: 'c')
247 endif
248
249 # dummy/empty dependency() object to declare fallbacks and simpler dependencies
250 not_found = dependency('', required: false)
251
252 ########################
253 # Configuration begins #
254 ########################
255 glib_conf = configuration_data()
256 glibconfig_conf = configuration_data()
257
258 # accumulated list of defines as we check for them, so we can easily
259 # use them later in test programs (autoconf does this automatically)
260 glib_conf_prefix = ''
261
262 glib_conf.set('GLIB_MAJOR_VERSION', major_version)
263 glib_conf.set('GLIB_MINOR_VERSION', minor_version)
264 glib_conf.set('GLIB_MICRO_VERSION', micro_version)
265 glib_conf.set('GLIB_INTERFACE_AGE', interface_age)
266 glib_conf.set('GLIB_BINARY_AGE', binary_age)
267 glib_conf.set_quoted('GETTEXT_PACKAGE', 'glib20')
268 glib_conf.set_quoted('PACKAGE_BUGREPORT', 'https://gitlab.gnome.org/GNOME/glib/issues/new')
269 glib_conf.set_quoted('PACKAGE_NAME', 'glib')
270 glib_conf.set_quoted('PACKAGE_STRING', 'glib @0@'.format(meson.project_version()))
271 glib_conf.set_quoted('PACKAGE_TARNAME', 'glib')
272 glib_conf.set_quoted('PACKAGE_URL', '')
273 glib_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
274 glib_conf.set('ENABLE_NLS', 1)
275
276 # used by the .rc.in files
277 glibconfig_conf.set('LT_CURRENT_MINUS_AGE', soversion)
278
279 glib_conf.set('_GNU_SOURCE', 1)
280
281 if host_system in ['windows', 'darwin']
282   # Poll doesn't work on devices on Windows, and macOS's poll() implementation is known to be broken
283   glib_conf.set('BROKEN_POLL', true)
284 endif
285
286 if host_system == 'windows' and cc.get_id() != 'msvc' and cc.get_id() != 'clang-cl'
287   # FIXME: Ideally we shouldn't depend on this on Windows and should use
288   # 64 bit capable Windows API that also works with MSVC.
289   # The autotools build did set this for mingw and while meson sets it
290   # for gcc/clang by default, it doesn't do so on Windows.
291   glib_conf.set('_FILE_OFFSET_BITS', 64)
292 endif
293
294 glib_build_shared = false
295 glib_build_static = false
296 if get_option('default_library') == 'both'
297   glib_build_static = true
298   glib_build_shared = true
299 elif get_option('default_library') == 'static'
300   glib_build_static = true
301 elif get_option('default_library') == 'shared'
302   glib_build_shared = true
303 endif
304
305 glib_build_both = glib_build_static and glib_build_shared
306 glib_build_static_only = glib_build_static and not glib_build_shared
307 glib_build_shared_only = glib_build_shared and not glib_build_static
308
309 if glib_build_shared and glib_build_static and (
310   host_system == 'windows' or host_system == 'cygwin')
311   error('On Windows default_library must be "shared" or "static" but not "both"')
312 endif
313
314 if glib_build_static_only
315   glibconfig_conf.set('GLIB_STATIC_COMPILATION', '1')
316   glibconfig_conf.set('GOBJECT_STATIC_COMPILATION', '1')
317   glibconfig_conf.set('GIO_STATIC_COMPILATION', '1')
318   glibconfig_conf.set('GMODULE_STATIC_COMPILATION', '1')
319   glibconfig_conf.set('GI_STATIC_COMPILATION', '1')
320   glibconfig_conf.set('G_INTL_STATIC_COMPILATION', '1')
321   glibconfig_conf.set('FFI_STATIC_BUILD', '1')
322 endif
323
324 # Cygwin glib port maintainers made it clear
325 # (via the patches they apply) that they want no
326 # part of glib W32 code, therefore we do not define
327 # G_PLATFORM_WIN32 for host_system == 'cygwin'.
328 # This makes G_PLATFORM_WIN32 a synonym for
329 # G_OS_WIN32.
330 if host_system == 'windows'
331   glib_os = '''#define G_OS_WIN32
332 #define G_PLATFORM_WIN32'''
333 elif host_system == 'cygwin'
334   glib_os = '''#define G_OS_UNIX
335 #define G_WITH_CYGWIN'''
336 else
337   glib_os = '#define G_OS_UNIX'
338 endif
339 glibconfig_conf.set('glib_os', glib_os)
340
341 # We need to know the CRT being used to determine what .lib files we need on
342 # Visual Studio for dependencies that don't normally come with pkg-config files
343 vs_crt = 'release'
344 vs_crt_opt = get_option('b_vscrt')
345 if vs_crt_opt in ['mdd', 'mtd']
346   vs_crt = 'debug'
347 elif vs_crt_opt == 'from_buildtype'
348   if get_option('buildtype') == 'debug'
349     vs_crt = 'debug'
350   endif
351 endif
352
353 # Use debug/optimization flags to determine whether to enable debug or disable
354 # cast checks. We have a non-production (debug) build if debug is true and if
355 # optimization is 0 or g; otherwise, we have a production build.
356 glib_debug_cflags = []
357 glib_debug = get_option('glib_debug')
358 if (glib_debug.enabled() or (
359     glib_debug.auto() and get_option('debug') and get_option('optimization') in [ '0', 'g' ]))
360   glib_debug_cflags += ['-DG_ENABLE_DEBUG']
361   message('Enabling various debug infrastructure')
362 else
363   glib_debug_cflags += ['-DG_DISABLE_CAST_CHECKS']
364   message('Disabling cast checks')
365 endif
366
367 if not get_option('glib_assert')
368   glib_debug_cflags += ['-DG_DISABLE_ASSERT']
369   message('Disabling GLib asserts')
370 endif
371
372 if not get_option('glib_checks')
373   glib_debug_cflags += ['-DG_DISABLE_CHECKS']
374   message('Disabling GLib checks')
375 endif
376
377 add_project_arguments(glib_debug_cflags, language: 'c')
378
379 # check for header files
380
381 headers = [
382   'alloca.h',
383   'afunix.h',
384   'crt_externs.h',
385   'dirent.h', # MSC does not come with this by default
386   'float.h',
387   'fstab.h',
388   'grp.h',
389   'inttypes.h',
390   'libproc.h',
391   'limits.h',
392   'locale.h',
393   'mach/mach_time.h',
394   'memory.h',
395   'mntent.h',
396   'poll.h',
397   'pwd.h',
398   'sched.h',
399   'spawn.h',
400   'stdatomic.h',
401   'stdint.h',
402   'stdlib.h',
403   'string.h',
404   'strings.h',
405   'sys/auxv.h',
406   'sys/event.h',
407   'sys/filio.h',
408   'sys/inotify.h',
409   'sys/mkdev.h',
410   'sys/mntctl.h',
411   'sys/mnttab.h',
412   'sys/mount.h',
413   'sys/param.h',
414   'sys/prctl.h',
415   'sys/resource.h',
416   'sys/select.h',
417   'sys/statfs.h',
418   'sys/stat.h',
419   'sys/statvfs.h',
420   'sys/sysctl.h',
421   'sys/time.h', # MSC does not come with this by default
422   'sys/times.h',
423   'sys/types.h',
424   'sys/uio.h',
425   'sys/vfs.h',
426   'sys/vfstab.h',
427   'sys/vmount.h',
428   'sys/wait.h',
429   'syslog.h',
430   'termios.h',
431   'unistd.h',
432   'values.h',
433   'wchar.h',
434   'xlocale.h',
435 ]
436
437 foreach h : headers
438   if cc.has_header(h)
439     define = 'HAVE_' + h.underscorify().to_upper()
440     glib_conf.set(define, 1)
441     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
442   endif
443 endforeach
444
445 # FreeBSD includes a malloc.h which always throws compilation error, so we have
446 # to use check_header() rather than has_header().
447 if cc.check_header('malloc.h')
448   glib_conf.set('HAVE_MALLOC_H', 1)
449   glib_conf_prefix = glib_conf_prefix + '#define HAVE_MALLOC_H 1\n'
450 endif
451
452 if cc.check_header('linux/netlink.h')
453   glib_conf.set('HAVE_NETLINK', 1)
454 endif
455
456 # Is statx() supported? Android systems don’t reliably support it as of August 2020.
457 statx_code = '''
458   #ifndef _GNU_SOURCE
459   #define _GNU_SOURCE
460   #endif
461   #include <sys/stat.h>
462   #include <fcntl.h>
463   int main (void)
464   {
465     struct statx stat_buf;
466     return statx (AT_FDCWD, "/", AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS | STATX_BTIME, &stat_buf);
467   }
468   '''
469 if host_system != 'android' and cc.compiles(statx_code, name : 'statx() test')
470   glib_conf.set('HAVE_STATX', 1)
471 endif
472
473 if glib_conf.has('HAVE_LOCALE_H')
474   if cc.has_header_symbol('locale.h', 'LC_MESSAGES')
475     glib_conf.set('HAVE_LC_MESSAGES', 1)
476   endif
477 endif
478
479 struct_stat_blkprefix = '''
480 #include <sys/types.h>
481 #include <sys/stat.h>
482 #ifdef HAVE_UNISTD_H
483 #include <unistd.h>
484 #endif
485 #ifdef HAVE_SYS_STATFS_H
486 #include <sys/statfs.h>
487 #endif
488 #ifdef HAVE_SYS_PARAM_H
489 #include <sys/param.h>
490 #endif
491 #ifdef HAVE_SYS_MOUNT_H
492 #include <sys/mount.h>
493 #endif
494 '''
495
496 struct_members = [
497   [ 'stat', 'st_mtimensec' ],
498   [ 'stat', 'st_mtim.tv_nsec' ],
499   [ 'stat', 'st_atimensec' ],
500   [ 'stat', 'st_atim.tv_nsec' ],
501   [ 'stat', 'st_ctimensec' ],
502   [ 'stat', 'st_ctim.tv_nsec' ],
503   [ 'stat', 'st_birthtime' ],
504   [ 'stat', 'st_birthtimensec' ],
505   [ 'stat', 'st_birthtim' ],
506   [ 'stat', 'st_birthtim.tv_nsec' ],
507   [ 'stat', 'st_blksize', struct_stat_blkprefix ],
508   [ 'stat', 'st_blocks', struct_stat_blkprefix ],
509   [ 'statfs', 'f_fstypename', struct_stat_blkprefix ],
510   [ 'statfs', 'f_bavail', struct_stat_blkprefix ],
511   [ 'dirent', 'd_type', '''#include <sys/types.h>
512                            #include <dirent.h>''' ],
513   [ 'statvfs', 'f_basetype', '#include <sys/statvfs.h>' ],
514   [ 'statvfs', 'f_fstypename', '#include <sys/statvfs.h>' ],
515   [ 'statvfs', 'f_type', '#include <sys/statvfs.h>' ],
516   [ 'tm', 'tm_gmtoff', '#include <time.h>' ],
517   [ 'tm', '__tm_gmtoff', '#include <time.h>' ],
518 ]
519
520 foreach m : struct_members
521   header_check_prefix = glib_conf_prefix
522   if m.length() == 3
523     header_check_prefix = header_check_prefix + m[2]
524   else
525     header_check_prefix = header_check_prefix + '#include <sys/stat.h>'
526   endif
527   # Reimplement cc.has_member() to workaround compiler warning
528   # FIXME: https://github.com/mesonbuild/meson/pull/12818
529   code = header_check_prefix + '''
530     void bar(void) {
531         struct ''' + m[0] + ''' foo;
532         (void) ( foo.''' + m[1] + ''' );
533         (void) foo;
534     }
535     '''
536   if cc.compiles(code, name : 'type "struct ' + m[0] + '" has member "' + m[1] + '"')
537     define = 'HAVE_STRUCT_@0@_@1@'.format(m[0].to_upper(), m[1].underscorify().to_upper())
538     glib_conf.set(define, 1)
539     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
540   else
541   endif
542 endforeach
543
544 # Compiler flags
545 if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
546   warning_common_args = [
547     '-Wduplicated-branches',
548     '-Wimplicit-fallthrough',
549     '-Wmisleading-indentation',
550     '-Wmissing-field-initializers',
551     '-Wnonnull',
552     '-Wunused',
553     # Due to maintained deprecated code, we do not want to see unused parameters
554     '-Wno-unused-parameter',
555     # Due to pervasive use of things like GPOINTER_TO_UINT(), we do not support
556     # building with -Wbad-function-cast.
557     '-Wno-cast-function-type',
558     # Due to function casts through (void*) we cannot support -Wpedantic:
559     # ./docs/toolchain-requirements.md#Function_pointer_conversions.
560     '-Wno-pedantic',
561     # A zero-length format string shouldn't be considered an issue.
562     '-Wno-format-zero-length',
563     # We explicitly require variadic macros
564     '-Wno-variadic-macros',
565     '-Werror=format=2',
566     '-Werror=init-self',
567     '-Werror=missing-include-dirs',
568     '-Werror=pointer-arith',
569     '-Werror=unused-result',
570   ]
571
572   warning_c_args = warning_common_args + [
573     '-Wstrict-prototypes',
574     # Due to pervasive use of things like GPOINTER_TO_UINT(), we do not support
575     # building with -Wbad-function-cast.
576     '-Wno-bad-function-cast',
577     '-Werror=implicit-function-declaration',
578     '-Werror=missing-prototypes',
579     '-Werror=pointer-sign',
580     '-Wno-string-plus-int',
581   ]
582   warning_cxx_args = warning_common_args
583   warning_objc_args = warning_c_args
584   warning_c_link_args = [
585     '-Wl,-z,nodelete',
586   ]
587   if get_option('bsymbolic_functions')
588     warning_c_link_args += ['-Wl,-Bsymbolic-functions']
589   endif
590 elif cc.get_id() == 'msvc'
591   warning_c_args = [
592     # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it
593     # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once
594     # NOTE: Only add warnings here if you are sure they're spurious
595     '/wo4057', # 'operator': 'identifier1' differs in indirection to slightly different base types from 'identifier2'
596     '/wd4068', # unknown pragma
597     '/wo4090', # 'operation': different 'modifier' qualifiers
598     '/wd4100', # 'identifier': unreferenced formal parameter
599     '/wd4116', # unnamed type definition in parentheses
600     '/wo4125', # decimal digit terminates octal escape sequence
601     '/wd4127', # conditional expression is constant
602     '/wd4146', # unary minus operator applied to unsigned type, result still unsigned
603     '/wd4152', # nonstandard extension, function/data pointer conversion in expression
604     '/wd4201', # nonstandard extension used: nameless struct/union
605     '/wd4232', # nonstandard extension used: 'identifier': address of dllimport 'dllimport' is not static, identity not guaranteed
606     '/wo4245', # 'conversion_type': conversion from 'type1' to 'type2', signed/unsigned mismatch
607     '/wo4267', # 'variable': conversion from 'size_t' to 'type', possible loss of data
608     '/wd4334', # 'shift_operator': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
609     '/wo4389', # 'operator': signed/unsigned mismatch
610     '/wo4702', # unreachable code
611     '/wd4706', # assignment within conditional expression
612   ]
613
614   warning_cxx_args = []
615   warning_objc_args = []
616   warning_c_link_args = []
617 else
618   warning_c_args = []
619   warning_cxx_args = []
620   warning_objc_args = []
621   warning_c_link_args = []
622 endif
623
624 add_project_arguments(cc.get_supported_arguments(warning_c_args), language: 'c')
625 if have_cxx
626   add_project_arguments(cxx.get_supported_arguments(warning_cxx_args), language: 'cpp')
627 endif
628
629 # FIXME: We cannot build some of the GResource tests with -z nodelete, which
630 # means we cannot use that flag in add_project_link_arguments(), and must add
631 # it to the relevant targets manually. We do the same with -Bsymbolic-functions
632 # because that is what the autotools build did.
633 # See https://github.com/mesonbuild/meson/pull/3520 for a way to eventually
634 # improve this.
635 glib_link_flags = cc.get_supported_link_arguments(warning_c_link_args)
636
637 # Windows SDK requirements and checks
638 if host_system == 'windows'
639   # Check whether we're building for UWP apps
640   code = '''
641   #include <windows.h>
642   #if !(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
643   #error "Not building for UWP"
644   #endif'''
645   if cc.compiles(code, name : 'building for UWP')
646     glib_conf.set('G_WINAPI_ONLY_APP', true)
647     # We require Windows 10+ on WinRT
648     glib_conf.set('_WIN32_WINNT', '0x0A00')
649     uwp_gio_deps = [cc.find_library('shcore'),
650                     cc.find_library('runtimeobject')]
651   else
652     # We require Windows 7+ on Win32
653     glib_conf.set('_WIN32_WINNT', '0x0601')
654     uwp_gio_deps = []
655   endif
656 endif
657
658 functions = [
659   'accept4',
660   'close_range',
661   'copy_file_range',
662   'endmntent',
663   'endservent',
664   'epoll_create',
665   'fallocate',
666   'fchmod',
667   'fchown',
668   'fdwalk',
669   'free_aligned_sized',
670   'free_sized',
671   'fsync',
672   'ftruncate64',
673   'getauxval',
674   'getc_unlocked',
675   'getfsstat',
676   'getgrgid_r',
677   'getmntent_r',
678   'getpwuid_r',
679   'getresuid',
680   'getvfsstat',
681   'gmtime_r',
682   'hasmntopt',
683   'inotify_init1',
684   'issetugid',
685   'kevent',
686   'kqueue',
687   'lchmod',
688   'lchown',
689   'link',
690   'localtime_r',
691   'lstat',
692   'mbrtowc',
693   'memalign',
694   'mmap',
695   'newlocale',
696   'pipe2',
697   'poll',
698   'prlimit',
699   'readlink',
700   'recvmmsg',
701   'sendmmsg',
702   'setenv',
703   'setmntent',
704   'strerror_r',
705   'strnlen',
706   'strsignal',
707   'strtod_l',
708   'strtoll_l',
709   'strtoull_l',
710   'symlink',
711   'timegm',
712   'unsetenv',
713   'uselocale',
714   'utimes',
715   'utimensat',
716   'valloc',
717   'vasprintf',
718   'vsnprintf',
719   'wcrtomb',
720   'wcslen',
721   'wcsnlen',
722   'sysctlbyname',
723 ]
724
725 # _NSGetEnviron is available on iOS too, but its usage gets apps rejected from
726 # the app store since it's considered 'private API'
727 if host_system == 'darwin'
728   functions += ['_NSGetEnviron']
729 endif
730
731 if glib_conf.has('HAVE_SYS_STATVFS_H')
732   functions += ['statvfs']
733 else
734   have_func_statvfs = false
735 endif
736 if glib_conf.has('HAVE_SYS_STATFS_H') or glib_conf.has('HAVE_SYS_MOUNT_H')
737   functions += ['statfs']
738 else
739   have_func_statfs = false
740 endif
741 if glib_conf.has('HAVE_SYS_PRCTL_H')
742   functions += ['prctl']
743 else
744   have_func_prctl = false
745 endif
746
747 if host_system == 'windows'
748   iphlpapi_dep = cc.find_library('iphlpapi')
749   iphlpapi_funcs = ['if_nametoindex', 'if_indextoname']
750   foreach ifunc : iphlpapi_funcs
751     iphl_prefix =  '''#define _WIN32_WINNT @0@
752       #include <winsock2.h>
753       #include <iphlpapi.h>'''.format(glib_conf.get('_WIN32_WINNT'))
754     if cc.has_function(ifunc,
755                        prefix : iphl_prefix,
756                        dependencies : iphlpapi_dep)
757       idefine = 'HAVE_' + ifunc.underscorify().to_upper()
758       glib_conf.set(idefine, 1)
759       glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(idefine)
760       set_variable('have_func_' + ifunc, true)
761     else
762       set_variable('have_func_' + ifunc, false)
763     endif
764   endforeach
765 else
766   functions += ['if_indextoname', 'if_nametoindex']
767 endif
768
769 # AIX splice is something else
770 if host_system != 'aix'
771   functions += ['splice']
772 endif
773
774 foreach f : functions
775   if cc.has_function(f)
776     define = 'HAVE_' + f.underscorify().to_upper()
777     glib_conf.set(define, 1)
778     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
779     set_variable('have_func_' + f, true)
780   else
781     set_variable('have_func_' + f, false)
782   endif
783 endforeach
784
785 # Export the information about free_sized() so we can correctly define a macro
786 # wrapper around g_free()/g_free_sized() depending on whether it’s available
787 glibconfig_conf.set('G_HAVE_FREE_SIZED', have_func_free_sized)
788
789 # Check that stpcpy() is usable; must use header.
790 # See:
791 # https://github.com/mesonbuild/meson/issues/5628.
792 if cc.has_function('stpcpy', prefix : '#include <string.h>')
793   glib_conf.set('HAVE_STPCPY', 1)
794 endif
795
796 if cc.has_function('memalign', prefix: '#include <stdlib.h>\n#include <malloc.h>')
797   glib_conf.set('HAVE_MEMALIGN', 1)
798 endif
799
800 # For example on Openbsd, getservbyname_r() has a different signature.
801 # https://man.openbsd.org/getservbyname.3
802 if cc.compiles('''#include <netdb.h>
803                int main (int argc, char ** argv) {
804                  int (*fcn)(const char *,
805                             const char *,
806                             struct servent *,
807                             char *,
808                             size_t,
809                             struct servent **) = getservbyname_r;
810                  (void) fcn;
811                  return 0;
812                }''',
813                name : 'getservbyname_r()',
814                args: '-Werror=incompatible-pointer-types')
815   glib_conf.set('HAVE_GETSERVBYNAME_R', 1)
816 endif
817
818 if cc.has_function('_aligned_malloc', prefix: '#include <malloc.h>')
819   glib_conf.set('HAVE__ALIGNED_MALLOC', 1)
820 endif
821
822 if host_system != 'windows' and cc.has_function('aligned_alloc', prefix: '#include <stdlib.h>')
823   glib_conf.set('HAVE_ALIGNED_ALLOC', 1)
824 endif
825
826 if host_system != 'windows' and cc.has_function('posix_memalign', prefix: '#include <stdlib.h>')
827   glib_conf.set('HAVE_POSIX_MEMALIGN', 1)
828 endif
829
830 # Check that posix_spawn() is usable; must use header
831 if cc.has_function('posix_spawn', prefix : '#include <spawn.h>')
832   glib_conf.set('HAVE_POSIX_SPAWN', 1)
833 endif
834
835 # Check whether strerror_r returns char *
836 if have_func_strerror_r
837   if cc.compiles('''#define _GNU_SOURCE
838                     #include <string.h>
839                     int func (void) {
840                       char error_string[256];
841                       char *ptr = strerror_r (-2, error_string, 256);
842                       char c = *strerror_r (-2, error_string, 256);
843                       return c != 0 && ptr != (void*) 0L;
844                     }
845                  ''',
846                  name : 'strerror_r() returns char *')
847     glib_conf.set('STRERROR_R_CHAR_P', 1,
848                   description: 'Defined if strerror_r returns char *')
849   endif
850 endif
851
852 # Special-case these functions that have alternative names on Windows/MSVC
853 if cc.has_function('snprintf') or cc.has_header_symbol('stdio.h', 'snprintf')
854   glib_conf.set('HAVE_SNPRINTF', 1)
855   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF 1\n'
856 elif cc.has_function('_snprintf') or cc.has_header_symbol('stdio.h', '_snprintf')
857   hack_define = '1\n#define snprintf _snprintf'
858   glib_conf.set('HAVE_SNPRINTF', hack_define)
859   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF ' + hack_define
860 endif
861
862 if cc.has_function('strcasecmp', prefix: '#include <strings.h>')
863   glib_conf.set('HAVE_STRCASECMP', 1)
864   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP 1\n'
865 elif cc.has_function('_stricmp')
866   hack_define = '1\n#define strcasecmp _stricmp'
867   glib_conf.set('HAVE_STRCASECMP', hack_define)
868   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP ' + hack_define
869 endif
870
871 if cc.has_function('strncasecmp', prefix: '#include <strings.h>')
872   glib_conf.set('HAVE_STRNCASECMP', 1)
873   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP 1\n'
874 elif cc.has_function('_strnicmp')
875   hack_define = '1\n#define strncasecmp _strnicmp'
876   glib_conf.set('HAVE_STRNCASECMP', hack_define)
877   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP ' + hack_define
878 endif
879
880 if cc.has_header_symbol('sys/sysmacros.h', 'major')
881   glib_conf.set('MAJOR_IN_SYSMACROS', 1)
882 elif cc.has_header_symbol('sys/mkdev.h', 'major')
883   glib_conf.set('MAJOR_IN_MKDEV', 1)
884 elif cc.has_header_symbol('sys/types.h', 'major')
885   glib_conf.set('MAJOR_IN_TYPES', 1)
886 endif
887
888 if cc.has_header_symbol('dlfcn.h', 'RTLD_LAZY')
889   glib_conf.set('HAVE_RTLD_LAZY', 1)
890 endif
891
892 if cc.has_header_symbol('dlfcn.h', 'RTLD_NOW')
893   glib_conf.set('HAVE_RTLD_NOW', 1)
894 endif
895
896 if cc.has_header_symbol('dlfcn.h', 'RTLD_GLOBAL')
897   glib_conf.set('HAVE_RTLD_GLOBAL', 1)
898 endif
899
900 have_rtld_next = false
901 if cc.has_header_symbol('dlfcn.h', 'RTLD_NEXT', args: '-D_GNU_SOURCE')
902   have_rtld_next = true
903   glib_conf.set('HAVE_RTLD_NEXT', 1)
904 endif
905
906 if cc.has_type('loff_t', prefix: '#include <sys/types.h>')
907   glib_conf.set('HAVE_LOFF_T', 1)
908 endif
909
910 # Check whether to use statfs or statvfs
911 # Some systems have both statfs and statvfs, pick the most "native" for these
912 if have_func_statfs and have_func_statvfs
913   # on solaris and irix, statfs doesn't even have the f_bavail field
914   if not glib_conf.has('HAVE_STRUCT_STATFS_F_BAVAIL')
915     have_func_statfs = false
916   else
917     # at least on linux, statfs is the actual syscall
918     have_func_statvfs = false
919   endif
920 endif
921 if have_func_statfs
922   glib_conf.set('USE_STATFS', 1)
923   stat_func_to_use = 'statfs'
924 elif have_func_statvfs
925   glib_conf.set('USE_STATVFS', 1)
926   stat_func_to_use = 'statvfs'
927 else
928   stat_func_to_use = 'neither'
929 endif
930 message('Checking whether to use statfs or statvfs .. ' + stat_func_to_use)
931
932 if host_system == 'linux'
933   if cc.has_function('mkostemp',
934                      prefix: '''#define _GNU_SOURCE
935                                 #include <stdlib.h>''')
936     glib_conf.set('HAVE_MKOSTEMP', 1)
937   endif
938 endif
939
940 osx_ldflags = []
941 glib_have_os_x_9_or_later = false
942 glib_have_carbon = false
943 glib_have_cocoa = false
944 if host_system == 'darwin'
945   add_languages('objc', native: false, required: true)
946   objcc = meson.get_compiler('objc')
947
948   add_project_arguments(objcc.get_supported_arguments(warning_objc_args), language: 'objc')
949
950   # Mac OS X Carbon support
951   glib_have_carbon = objcc.compiles('''#include <Carbon/Carbon.h>
952                                        #include <CoreServices/CoreServices.h>''',
953                                     name : 'Mac OS X Carbon support')
954
955   if glib_have_carbon
956     glib_conf.set('HAVE_CARBON', true)
957     glib_have_os_x_9_or_later = objcc.compiles('''#include <AvailabilityMacros.h>
958                                                   #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
959                                                   #error Compiling for minimum OS X version before 10.9
960                                                   #endif''',
961                                                name : 'OS X 9 or later')
962   endif
963
964   # Mac OS X Cocoa support
965   glib_have_cocoa = objcc.compiles('''#include <Cocoa/Cocoa.h>
966                                       #ifdef GNUSTEP_BASE_VERSION
967                                       #error "Detected GNUstep, not Cocoa"
968                                       #endif''',
969                                    name : 'Mac OS X Cocoa support')
970
971   if glib_have_cocoa
972     glib_conf.set('HAVE_COCOA', true)
973     osx_ldflags += ['-Wl,-framework,Foundation', '-Wl,-framework,AppKit']
974   endif
975 endif
976
977 if host_system == 'qnx'
978   glib_conf.set('HAVE_QNX', 1)
979 endif
980
981 # Check for futex(2)
982 if cc.compiles('''#include <linux/futex.h>
983                #include <sys/syscall.h>
984                #include <unistd.h>
985                int main (int argc, char ** argv) {
986                  syscall (__NR_futex, NULL, FUTEX_WAKE, FUTEX_WAIT);
987                  return 0;
988                }''', name : 'futex(2) system call')
989   glib_conf.set('HAVE_FUTEX', 1)
990 endif
991 if cc.compiles('''#include <linux/futex.h>
992                #include <sys/syscall.h>
993                #include <unistd.h>
994                int main (int argc, char ** argv) {
995                  syscall (__NR_futex_time64, NULL, FUTEX_WAKE, FUTEX_WAIT);
996                  return 0;
997                }''', name : 'futex(2) system call')
998   glib_conf.set('HAVE_FUTEX_TIME64', 1)
999 endif
1000
1001 # Check for eventfd(2)
1002 if cc.links('''#include <sys/eventfd.h>
1003                #include <unistd.h>
1004                int main (int argc, char ** argv) {
1005                  eventfd (0, EFD_CLOEXEC);
1006                  return 0;
1007                }''', name : 'eventfd(2) system call')
1008   glib_conf.set('HAVE_EVENTFD', 1)
1009 endif
1010
1011 # Check for pidfd_open(2)
1012 if cc.links('''#include <sys/syscall.h>
1013                #include <sys/wait.h>
1014                #include <linux/wait.h>
1015                #include <unistd.h>
1016                int main (int argc, char ** argv) {
1017                  siginfo_t child_info = { 0, };
1018                  syscall (SYS_pidfd_open, 0, 0);
1019                  waitid (P_PIDFD, 0, &child_info, WEXITED | WNOHANG);
1020                  return 0;
1021                }''', name : 'pidfd_open(2) system call')
1022   glib_conf.set('HAVE_PIDFD', 1)
1023 endif
1024
1025 # Check for __uint128_t (gcc) by checking for 128-bit division
1026 uint128_t_src = '''int main() {
1027 static __uint128_t v1 = 100;
1028 static __uint128_t v2 = 10;
1029 static __uint128_t u;
1030 u = v1 / v2;
1031 (void) u;
1032 }'''
1033 if cc.compiles(uint128_t_src, name : '__uint128_t available')
1034   glib_conf.set('HAVE_UINT128_T', 1)
1035 endif
1036
1037 clock_gettime_test_code = '''
1038   #include <time.h>
1039   struct timespec t;
1040   int main (int argc, char ** argv) {
1041     return clock_gettime(CLOCK_REALTIME, &t);
1042   }'''
1043 librt = []
1044 if cc.links(clock_gettime_test_code, name : 'clock_gettime')
1045   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
1046 elif cc.links(clock_gettime_test_code, args : '-lrt', name : 'clock_gettime in librt')
1047   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
1048   librt = cc.find_library('rt')
1049 endif
1050
1051 dlopen_dlsym_test_code = '''
1052 #include <dlfcn.h>
1053 #include <stdio.h>
1054 int r;
1055 int glib_underscore_test (void) { return 42; }
1056 int main (int argc, char ** argv) {
1057   void *f1 = (void*)0, *f2 = (void*)0, *handle;
1058   handle = dlopen ((void*)0, 0);
1059   if (handle) {
1060     f1 = dlsym (handle, "glib_underscore_test");
1061     f2 = dlsym (handle, "_glib_underscore_test");
1062   }
1063   r = (!f2 || f1) ? puts ("1") : puts ("0");
1064   return r > 0 ? 0 : r;
1065 }'''
1066 libdl_dep = []
1067 if cc.links(dlopen_dlsym_test_code, name : 'dlopen() and dlsym() in system libraries')
1068   have_dlopen_dlsym = true
1069 elif cc.links(dlopen_dlsym_test_code, args : '-ldl', name : 'dlopen() and dlsym() in libdl')
1070   have_dlopen_dlsym = true
1071   libdl_dep = cc.find_library('dl')
1072 else
1073   have_dlopen_dlsym = false
1074 endif
1075
1076 # if statfs() takes 2 arguments (Posix) or 4 (Solaris)
1077 if have_func_statfs
1078   if cc.compiles(glib_conf_prefix + '''
1079                  #include <unistd.h>
1080                         #ifdef HAVE_SYS_PARAM_H
1081                         #include <sys/param.h>
1082                         #endif
1083                         #ifdef HAVE_SYS_VFS_H
1084                         #include <sys/vfs.h>
1085                         #endif
1086                         #ifdef HAVE_SYS_MOUNT_H
1087                         #include <sys/mount.h>
1088                         #endif
1089                         #ifdef HAVE_SYS_STATFS_H
1090                         #include <sys/statfs.h>
1091                         #endif
1092                         void some_func (void) {
1093                           struct statfs st;
1094                           statfs("/", &st);
1095                         }''', name : 'number of arguments to statfs() (n=2)')
1096     glib_conf.set('STATFS_ARGS', 2)
1097   elif cc.compiles(glib_conf_prefix + '''
1098                    #include <unistd.h>
1099                           #ifdef HAVE_SYS_PARAM_H
1100                           #include <sys/param.h>
1101                           #endif
1102                           #ifdef HAVE_SYS_VFS_H
1103                           #include <sys/vfs.h>
1104                           #endif
1105                           #ifdef HAVE_SYS_MOUNT_H
1106                           #include <sys/mount.h>
1107                           #endif
1108                           #ifdef HAVE_SYS_STATFS_H
1109                           #include <sys/statfs.h>
1110                           #endif
1111                           void some_func (void) {
1112                             struct statfs st;
1113                             statfs("/", &st, sizeof (st), 0);
1114                           }''', name : 'number of arguments to statfs() (n=4)')
1115     glib_conf.set('STATFS_ARGS', 4)
1116   else
1117     error('Unable to determine number of arguments to statfs()')
1118   endif
1119 endif
1120
1121 # open takes O_DIRECTORY as an option
1122 #AC_MSG_CHECKING([])
1123 if cc.compiles('''#include <fcntl.h>
1124                   #include <sys/types.h>
1125                   #include <sys/stat.h>
1126                   void some_func (void) {
1127                     open(".", O_DIRECTORY, 0);
1128                   }''', name : 'open() option O_DIRECTORY')
1129   glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
1130 endif
1131
1132 # fcntl takes F_FULLFSYNC as an option
1133 # See https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
1134 if cc.compiles('''#include <fcntl.h>
1135                   #include <sys/types.h>
1136                   #include <sys/stat.h>
1137                   void some_func (void) {
1138                     fcntl(0, F_FULLFSYNC, 0);
1139                   }''', name : 'fcntl() option F_FULLFSYNC')
1140   glib_conf.set('HAVE_FCNTL_F_FULLFSYNC', 1)
1141 endif
1142
1143 # Check whether there is a vsnprintf() function with C99 semantics installed.
1144 # (similar tests to AC_FUNC_VSNPRINTF_C99)
1145 # Check whether there is a snprintf() function with C99 semantics installed.
1146 # (similar tests to AC_FUNC_SNPRINTF_C99)
1147 # Check whether there is a printf() function with Unix98 semantics installed.
1148 # (similar tests to AC_FUNC_PRINTF_UNIX98)
1149 have_good_vsnprintf = false
1150 have_good_snprintf = false
1151 have_good_printf = false
1152
1153 if host_system == 'windows' and (cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl')
1154   # Unfortunately the Visual Studio 2015+ implementations of C99-style
1155   # snprintf and vsnprintf don't seem to be quite good enough.
1156   # (Sorry, I don't know exactly what is the problem,
1157   # but it is related to floating point formatting and decimal point vs. comma.)
1158   # The simple tests in AC_FUNC_VSNPRINTF_C99 and AC_FUNC_SNPRINTF_C99 aren't
1159   # rigorous enough to notice, though.
1160   glib_conf.set('HAVE_C99_SNPRINTF', false)
1161   glib_conf.set('HAVE_C99_VSNPRINTF', false)
1162   glib_conf.set('HAVE_UNIX98_PRINTF', false)
1163 elif not cc_can_run and host_system in ['ios', 'darwin']
1164   # All these are true when compiling natively on macOS, so we should use good
1165   # defaults when building for iOS and tvOS.
1166   glib_conf.set('HAVE_C99_SNPRINTF', true)
1167   glib_conf.set('HAVE_C99_VSNPRINTF', true)
1168   glib_conf.set('HAVE_UNIX98_PRINTF', true)
1169   have_good_vsnprintf = true
1170   have_good_snprintf = true
1171   have_good_printf = true
1172 else
1173   vsnprintf_c99_test_code = '''
1174 #include <stdio.h>
1175 #include <stdarg.h>
1176 #include <stdlib.h>
1177
1178 int
1179 doit(char * s, ...)
1180 {
1181   char buffer[32];
1182   va_list args;
1183   int r;
1184
1185   va_start(args, s);
1186   r = vsnprintf(buffer, 5, s, args);
1187   va_end(args);
1188
1189   if (r != 7)
1190     exit(1);
1191
1192   /* AIX 5.1 and Solaris seems to have a half-baked vsnprintf()
1193      implementation. The above will return 7 but if you replace
1194      the size of the buffer with 0, it borks! */
1195   va_start(args, s);
1196   r = vsnprintf(buffer, 0, s, args);
1197   va_end(args);
1198
1199   if (r != 7)
1200     exit(1);
1201
1202   exit(0);
1203 }
1204
1205 int
1206 main(void)
1207 {
1208   doit("1234567");
1209   exit(1);
1210 }'''
1211
1212   if cc_can_run
1213     rres = cc.run(vsnprintf_c99_test_code, name : 'C99 vsnprintf')
1214     if rres.compiled() and rres.returncode() == 0
1215       glib_conf.set('HAVE_C99_VSNPRINTF', 1)
1216       have_good_vsnprintf = true
1217     endif
1218   else
1219       have_good_vsnprintf = meson.get_external_property('have_c99_vsnprintf', false)
1220       glib_conf.set('HAVE_C99_VSNPRINTF', have_good_vsnprintf)
1221   endif
1222
1223   snprintf_c99_test_code = '''
1224 #include <stdio.h>
1225 #include <stdarg.h>
1226 #include <stdlib.h>
1227
1228 int
1229 doit()
1230 {
1231   char buffer[32];
1232   int r;
1233
1234   r = snprintf(buffer, 5, "1234567");
1235
1236   if (r != 7)
1237     exit(1);
1238
1239   r = snprintf(buffer, 0, "1234567");
1240
1241   if (r != 7)
1242     exit(1);
1243
1244   r = snprintf(NULL, 0, "1234567");
1245
1246   if (r != 7)
1247     exit(1);
1248
1249   exit(0);
1250 }
1251
1252 int
1253 main(void)
1254 {
1255   doit();
1256   exit(1);
1257 }'''
1258
1259   if cc_can_run
1260     rres = cc.run(snprintf_c99_test_code, name : 'C99 snprintf')
1261     if rres.compiled() and rres.returncode() == 0
1262       glib_conf.set('HAVE_C99_SNPRINTF', 1)
1263       have_good_snprintf = true
1264     endif
1265   else
1266       have_good_snprintf = meson.get_external_property('have_c99_snprintf', false)
1267       glib_conf.set('HAVE_C99_SNPRINTF', have_good_snprintf)
1268   endif
1269
1270   printf_unix98_test_code = '''
1271 #include <stdio.h>
1272 #include <stdlib.h>
1273 #include <string.h>
1274
1275 int
1276 main (void)
1277 {
1278   char buffer[128];
1279
1280   sprintf (buffer, "%2\$d %3\$d %1\$d", 1, 2, 3);
1281   if (strcmp ("2 3 1", buffer) == 0)
1282     exit (0);
1283   exit (1);
1284 }'''
1285
1286   if cc_can_run
1287     rres = cc.run(printf_unix98_test_code, name : 'Unix98 printf positional parameters')
1288     if rres.compiled() and rres.returncode() == 0
1289       glib_conf.set('HAVE_UNIX98_PRINTF', 1)
1290       have_good_printf = true
1291     endif
1292   else
1293       have_good_printf = meson.get_external_property('have_unix98_printf', false)
1294       glib_conf.set('HAVE_UNIX98_PRINTF', have_good_printf)
1295   endif
1296 endif
1297
1298 if host_system == 'windows'
1299   glib_conf.set_quoted('EXEEXT', '.exe')
1300 else
1301   glib_conf.set('EXEEXT', '')
1302 endif
1303
1304 # Our printf is 'good' only if vsnpintf()/snprintf()/printf() supports C99 well enough
1305 use_system_printf = have_good_vsnprintf and have_good_snprintf and have_good_printf
1306 glib_conf.set('USE_SYSTEM_PRINTF', use_system_printf)
1307 glibconfig_conf.set('GLIB_USING_SYSTEM_PRINTF', use_system_printf)
1308
1309 if not use_system_printf
1310   # gnulib has vasprintf so override the previous check
1311   glib_conf.set('HAVE_VASPRINTF', 1)
1312 endif
1313
1314 # Check for nl_langinfo and CODESET
1315 if cc.links('''#include <langinfo.h>
1316                int main (int argc, char ** argv) {
1317                  char *codeset = nl_langinfo (CODESET);
1318                  (void) codeset;
1319                  return 0;
1320                }''', name : 'nl_langinfo and CODESET')
1321   glib_conf.set('HAVE_LANGINFO_CODESET', 1)
1322   glib_conf.set('HAVE_CODESET', 1)
1323 endif
1324
1325 # Check for nl_langinfo and LC_TIME parts that are needed in gdatetime.c
1326 have_langinfo_time = false
1327 if cc.links('''#include <langinfo.h>
1328                int main (int argc, char ** argv) {
1329                  char *str;
1330                  str = nl_langinfo (PM_STR);
1331                  str = nl_langinfo (D_T_FMT);
1332                  str = nl_langinfo (D_FMT);
1333                  str = nl_langinfo (T_FMT);
1334                  str = nl_langinfo (T_FMT_AMPM);
1335                  str = nl_langinfo (MON_1);
1336                  str = nl_langinfo (ABMON_12);
1337                  str = nl_langinfo (DAY_1);
1338                  str = nl_langinfo (ABDAY_7);
1339                  (void) str;
1340                  return 0;
1341                }''', name : 'nl_langinfo (PM_STR)')
1342   have_langinfo_time = true
1343   glib_conf.set('HAVE_LANGINFO_TIME', 1)
1344 endif
1345
1346 # Linux glibc supports ERA, but FreeBSD and macOS don’t
1347 if cc.links('''#include <langinfo.h>
1348                int main (int argc, char **argv) {
1349                  char *str;
1350                  str = nl_langinfo (ERA);
1351                  str = nl_langinfo (ERA_D_T_FMT);
1352                  str = nl_langinfo (ERA_D_FMT);
1353                  str = nl_langinfo (ERA_T_FMT);
1354                  str = nl_langinfo (_NL_TIME_ERA_NUM_ENTRIES);
1355                  (void) str;
1356                  return 0;
1357                }''', name : 'nl_langinfo (ERA)')
1358   glib_conf.set('HAVE_LANGINFO_ERA', 1)
1359   if not have_langinfo_time
1360     error('nl_langinfo(ERA) is supported but more basic nl_langinfo() functionality like PM_STR is not')
1361   endif
1362 endif
1363
1364 if cc.links('''#include <langinfo.h>
1365                int main (int argc, char ** argv) {
1366                  char *str;
1367                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB);
1368                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT1_MB);
1369                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT2_MB);
1370                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT3_MB);
1371                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT4_MB);
1372                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT5_MB);
1373                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT6_MB);
1374                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT7_MB);
1375                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT8_MB);
1376                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT9_MB);
1377                  (void) str;
1378                  return 0;
1379                }''', name : 'nl_langinfo (_NL_CTYPE_OUTDIGITn_MB)')
1380   glib_conf.set('HAVE_LANGINFO_OUTDIGIT', 1)
1381   if not have_langinfo_time
1382     error('nl_langinfo(_NL_CTYPE_OUTDIGITn_MB) is supported but more basic nl_langinfo() functionality like PM_STR is not')
1383   endif
1384 endif
1385
1386 # Check for nl_langinfo and alternative month names
1387 if cc.links('''#ifndef _GNU_SOURCE
1388               # define _GNU_SOURCE
1389               #endif
1390               #include <langinfo.h>
1391                int main (int argc, char ** argv) {
1392                  char *str;
1393                  str = nl_langinfo (ALTMON_1);
1394                  str = nl_langinfo (ALTMON_2);
1395                  str = nl_langinfo (ALTMON_3);
1396                  str = nl_langinfo (ALTMON_4);
1397                  str = nl_langinfo (ALTMON_5);
1398                  str = nl_langinfo (ALTMON_6);
1399                  str = nl_langinfo (ALTMON_7);
1400                  str = nl_langinfo (ALTMON_8);
1401                  str = nl_langinfo (ALTMON_9);
1402                  str = nl_langinfo (ALTMON_10);
1403                  str = nl_langinfo (ALTMON_11);
1404                  str = nl_langinfo (ALTMON_12);
1405                  (void) str;
1406                  return 0;
1407                }''', name : 'nl_langinfo (ALTMON_n)')
1408   glib_conf.set('HAVE_LANGINFO_ALTMON', 1)
1409   if not have_langinfo_time
1410     error('nl_langinfo(ALTMON_n) is supported but more basic nl_langinfo() functionality like PM_STR is not')
1411   endif
1412 endif
1413
1414 # Check for nl_langinfo and abbreviated alternative month names
1415 if cc.links('''#ifndef _GNU_SOURCE
1416               # define _GNU_SOURCE
1417               #endif
1418               #include <langinfo.h>
1419                int main (int argc, char ** argv) {
1420                  char *str;
1421                  str = nl_langinfo (_NL_ABALTMON_1);
1422                  str = nl_langinfo (_NL_ABALTMON_2);
1423                  str = nl_langinfo (_NL_ABALTMON_3);
1424                  str = nl_langinfo (_NL_ABALTMON_4);
1425                  str = nl_langinfo (_NL_ABALTMON_5);
1426                  str = nl_langinfo (_NL_ABALTMON_6);
1427                  str = nl_langinfo (_NL_ABALTMON_7);
1428                  str = nl_langinfo (_NL_ABALTMON_8);
1429                  str = nl_langinfo (_NL_ABALTMON_9);
1430                  str = nl_langinfo (_NL_ABALTMON_10);
1431                  str = nl_langinfo (_NL_ABALTMON_11);
1432                  str = nl_langinfo (_NL_ABALTMON_12);
1433                  (void) str;
1434                  return 0;
1435                }''', name : 'nl_langinfo (_NL_ABALTMON_n)')
1436   glib_conf.set('HAVE_LANGINFO_ABALTMON', 1)
1437   if not have_langinfo_time
1438     error('nl_langinfo(_NL_ABALTMON_n) is supported but more basic nl_langinfo() functionality like PM_STR is not')
1439   endif
1440 endif
1441
1442 # Check for nl_langinfo and _NL_TIME_CODESET
1443 if cc.links('''#include <langinfo.h>
1444                int main (int argc, char ** argv) {
1445                  char *codeset = nl_langinfo (_NL_TIME_CODESET);
1446                  (void) codeset;
1447                  return 0;
1448                }''', name : 'nl_langinfo and _NL_TIME_CODESET')
1449   glib_conf.set('HAVE_LANGINFO_TIME_CODESET', 1)
1450   if not have_langinfo_time
1451     error('nl_langinfo(_NL_TIME_CODESET) is supported but more basic nl_langinfo() functionality like PM_STR is not')
1452   endif
1453 endif
1454
1455 # Check if C compiler supports the 'signed' keyword
1456 if not cc.compiles('''signed char x;''', name : 'signed')
1457   glib_conf.set('signed', '/* NOOP */')
1458 endif
1459
1460 # Check if the ptrdiff_t type exists
1461 if cc.has_header_symbol('stddef.h', 'ptrdiff_t')
1462   glib_conf.set('HAVE_PTRDIFF_T', 1)
1463 endif
1464
1465 # Check for sig_atomic_t type
1466 if cc.links('''#include <signal.h>
1467                #include <sys/types.h>
1468                sig_atomic_t val = 42;
1469                int main (int argc, char ** argv) {
1470                  return val == 42 ? 0 : 1;
1471                }''', name : 'sig_atomic_t')
1472   glib_conf.set('HAVE_SIG_ATOMIC_T', 1)
1473 endif
1474
1475 # Check if 'long long' works
1476 # jm_AC_TYPE_LONG_LONG
1477 if cc.compiles('''long long ll = 1LL;
1478                   int i = 63;
1479                   int some_func (void) {
1480                     long long llmax = (long long) -1;
1481                     return ll << i | ll >> i | llmax / ll | llmax % ll;
1482                   }''', name : 'long long')
1483   glib_conf.set('HAVE_LONG_LONG', 1)
1484   have_long_long = true
1485 else
1486   have_long_long = false
1487 endif
1488
1489 # Test whether the compiler supports the 'long double' type.
1490 if cc.compiles('''/* The Stardent Vistra knows sizeof(long double), but does not support it.  */
1491                   long double foo = 0.0;
1492                   /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
1493                   int array [2*(sizeof(long double) >= sizeof(double)) - 1];''',
1494                name : 'long double')
1495   glib_conf.set('HAVE_LONG_DOUBLE', 1)
1496 endif
1497
1498 # Test whether <stddef.h> has the 'wchar_t' type.
1499 if cc.has_header_symbol('stddef.h', 'wchar_t')
1500   glib_conf.set('HAVE_WCHAR_T', 1)
1501 endif
1502
1503 # Test whether <wchar.h> has the 'wint_t' type.
1504 if cc.has_header_symbol('wchar.h', 'wint_t')
1505   glib_conf.set('HAVE_WINT_T', 1)
1506 endif
1507
1508 found_uintmax_t = false
1509
1510 # Define HAVE_INTTYPES_H_WITH_UINTMAX if <inttypes.h> exists,
1511 # doesn't clash with <sys/types.h>, and declares uintmax_t.
1512 # jm_AC_HEADER_INTTYPES_H
1513 if cc.compiles('''#include <sys/types.h>
1514                   #include <inttypes.h>
1515                   void some_func (void) {
1516                     uintmax_t i = (uintmax_t) -1;
1517                     (void) i;
1518                   }''', name : 'uintmax_t in inttypes.h')
1519   glib_conf.set('HAVE_INTTYPES_H_WITH_UINTMAX', 1)
1520   found_uintmax_t = true
1521 endif
1522
1523 # Define HAVE_STDINT_H_WITH_UINTMAX if <stdint.h> exists,
1524 # doesn't clash with <sys/types.h>, and declares uintmax_t.
1525 # jm_AC_HEADER_STDINT_H
1526 if cc.compiles('''#include <sys/types.h>
1527                   #include <stdint.h>
1528                   void some_func (void) {
1529                     uintmax_t i = (uintmax_t) -1;
1530                     (void) i;
1531                   }''', name : 'uintmax_t in stdint.h')
1532   glib_conf.set('HAVE_STDINT_H_WITH_UINTMAX', 1)
1533   found_uintmax_t = true
1534 endif
1535
1536 # Define intmax_t to 'long' or 'long long'
1537 # if it is not already defined in <stdint.h> or <inttypes.h>.
1538 # For simplicity, we assume that a header file defines 'intmax_t' if and
1539 # only if it defines 'uintmax_t'.
1540 if found_uintmax_t
1541   glib_conf.set('HAVE_INTMAX_T', 1)
1542 elif have_long_long
1543   glib_conf.set('intmax_t', 'long long')
1544 else
1545   glib_conf.set('intmax_t', 'long')
1546 endif
1547
1548 char_size = cc.sizeof('char')
1549 short_size = cc.sizeof('short')
1550 int_size = cc.sizeof('int')
1551 voidp_size = cc.sizeof('void*')
1552 long_size = cc.sizeof('long')
1553 if have_long_long
1554   long_long_size = cc.sizeof('long long')
1555 else
1556   long_long_size = 0
1557 endif
1558 sizet_size = cc.sizeof('size_t')
1559 if cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl'
1560   ssizet_size = cc.sizeof('SSIZE_T', prefix : '#include <BaseTsd.h>')
1561 else
1562   ssizet_size = cc.sizeof('ssize_t', prefix : '#include <unistd.h>')
1563 endif
1564
1565 # Some platforms (Apple) hard-code int64_t to long long instead of
1566 # using long on 64-bit architectures. This can cause type mismatch
1567 # warnings when trying to interface with code using the standard
1568 # library type. Test for the warnings and set gint64 to whichever
1569 # works.
1570 if long_long_size == long_size
1571   if cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
1572                     #pragma options langlvl=stdc99
1573                     #endif
1574                     #pragma GCC diagnostic error "-Wincompatible-pointer-types"
1575                     #include <stdint.h>
1576                     #include <stdio.h>
1577                     int main () {
1578                       int64_t i1 = 1;
1579                       long *i2 = &i1;
1580                       (void) i2;
1581                       return 1;
1582                     }''', name : 'int64_t is long')
1583     int64_t_typedef = 'long'
1584   elif cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
1585                       #pragma options langlvl=stdc99
1586                       #endif
1587                       #pragma GCC diagnostic error "-Wincompatible-pointer-types"
1588                       #include <stdint.h>
1589                       #include <stdio.h>
1590                       int main () {
1591                         int64_t i1 = 1;
1592                         long long *i2 = &i1;
1593                         (void) i2;
1594                         return 1;
1595                       }''', name : 'int64_t is long long')
1596     int64_t_typedef = 'long long'
1597   else
1598     error('Cannot detect int64_t typedef')
1599   endif
1600 endif
1601
1602 int64_m = 'll'
1603 char_align = cc.alignment('char')
1604 short_align = cc.alignment('short')
1605 int_align = cc.alignment('int')
1606 voidp_align = cc.alignment('void*')
1607 long_align = cc.alignment('long')
1608 long_long_align = cc.alignment('long long')
1609 # NOTE: We don't check for size of __int64 because long long is guaranteed to
1610 # be 64-bit in C99, and it is available on all supported compilers
1611 sizet_align = cc.alignment('size_t')
1612
1613 glib_conf.set('SIZEOF_CHAR', char_size)
1614 glib_conf.set('SIZEOF_INT', int_size)
1615 glib_conf.set('SIZEOF_SHORT', short_size)
1616 glib_conf.set('SIZEOF_LONG', long_size)
1617 glib_conf.set('SIZEOF_LONG_LONG', long_long_size)
1618 glib_conf.set('SIZEOF_SIZE_T', sizet_size)
1619 glib_conf.set('SIZEOF_SSIZE_T', ssizet_size)
1620 glib_conf.set('SIZEOF_VOID_P', voidp_size)
1621 glib_conf.set('SIZEOF_WCHAR_T', cc.sizeof('wchar_t', prefix: '#include <stddef.h>'))
1622
1623 if short_size == 2
1624   gint16 = 'short'
1625   gint16_modifier='h'
1626   gint16_format='hi'
1627   guint16_format='hu'
1628 elif int_size == 2
1629   gint16 = 'int'
1630   gint16_modifier=''
1631   gint16_format='i'
1632   guint16_format='u'
1633 else
1634   error('Compiler provides no native 16-bit integer type')
1635 endif
1636 glibconfig_conf.set('gint16', gint16)
1637 glibconfig_conf.set_quoted('gint16_modifier', gint16_modifier)
1638 glibconfig_conf.set_quoted('gint16_format', gint16_format)
1639 glibconfig_conf.set_quoted('guint16_format', guint16_format)
1640
1641 if short_size == 4
1642   gint32 = 'short'
1643   gint32_modifier='h'
1644   gint32_format='hi'
1645   guint32_format='hu'
1646   guint32_align = short_align
1647 elif int_size == 4
1648   gint32 = 'int'
1649   gint32_modifier=''
1650   gint32_format='i'
1651   guint32_format='u'
1652   guint32_align = int_align
1653 elif long_size == 4
1654   gint32 = 'long'
1655   gint32_modifier='l'
1656   gint32_format='li'
1657   guint32_format='lu'
1658   guint32_align = long_align
1659 else
1660   error('Compiler provides no native 32-bit integer type')
1661 endif
1662 glibconfig_conf.set('gint32', gint32)
1663 glibconfig_conf.set_quoted('gint32_modifier', gint32_modifier)
1664 glibconfig_conf.set_quoted('gint32_format', gint32_format)
1665 glibconfig_conf.set_quoted('guint32_format', guint32_format)
1666
1667 if int_size == 8
1668   gint64 = 'int'
1669   gint64_modifier=''
1670   gint64_format='i'
1671   guint64_format='u'
1672   glib_extension=''
1673   gint64_constant='(val)'
1674   guint64_constant='(val)'
1675   guint64_align = int_align
1676 elif long_size == 8 and (long_long_size != long_size or int64_t_typedef == 'long')
1677   gint64 = 'long'
1678   glib_extension=''
1679   gint64_modifier='l'
1680   gint64_format='li'
1681   guint64_format='lu'
1682   gint64_constant='(val##L)'
1683   guint64_constant='(val##UL)'
1684   guint64_align = long_align
1685 elif long_long_size == 8 and (long_long_size != long_size or int64_t_typedef == 'long long')
1686   gint64 = 'long long'
1687   glib_extension='G_GNUC_EXTENSION '
1688   gint64_modifier=int64_m
1689   gint64_format=int64_m + 'i'
1690   guint64_format=int64_m + 'u'
1691   gint64_constant='(G_GNUC_EXTENSION (val##LL))'
1692   guint64_constant='(G_GNUC_EXTENSION (val##ULL))'
1693   guint64_align = long_long_align
1694 else
1695   error('Compiler provides no native 64-bit integer type')
1696 endif
1697 glibconfig_conf.set('glib_extension', glib_extension)
1698 glibconfig_conf.set('gint64', gint64)
1699 glibconfig_conf.set_quoted('gint64_modifier', gint64_modifier)
1700 glibconfig_conf.set_quoted('gint64_format', gint64_format)
1701 glibconfig_conf.set_quoted('guint64_format', guint64_format)
1702 glibconfig_conf.set('gint64_constant', gint64_constant)
1703 glibconfig_conf.set('guint64_constant', guint64_constant)
1704
1705 if host_system == 'windows'
1706   glibconfig_conf.set('g_pid_type', 'void*')
1707   glibconfig_conf.set_quoted('g_pid_format', 'p')
1708   if host_machine.cpu_family() == 'x86_64'
1709     glibconfig_conf.set_quoted('g_pollfd_format', '%#' + int64_m + 'x')
1710   else
1711     glibconfig_conf.set_quoted('g_pollfd_format', '%#x')
1712   endif
1713   glibconfig_conf.set('g_dir_separator', '\\\\')
1714   glibconfig_conf.set('g_searchpath_separator', ';')
1715 else
1716   glibconfig_conf.set('g_pid_type', 'int')
1717   glibconfig_conf.set_quoted('g_pid_format', 'i')
1718   glibconfig_conf.set_quoted('g_pollfd_format', '%d')
1719   glibconfig_conf.set('g_dir_separator', '/')
1720   glibconfig_conf.set('g_searchpath_separator', ':')
1721 endif
1722
1723 g_sizet_compatibility = {
1724   'short': sizet_size == short_size,
1725   'int': sizet_size == int_size,
1726   'long': sizet_size == long_size,
1727   'long long': sizet_size == long_long_size,
1728 }
1729
1730 # Do separate checks for gcc/clang (and ignore other compilers for now), since
1731 # we need to explicitly pass -Werror to the compilers.
1732 # FIXME: https://github.com/mesonbuild/meson/issues/5399
1733 if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
1734   foreach type_name, size_compatibility : g_sizet_compatibility
1735     g_sizet_compatibility += { type_name: size_compatibility and
1736       cc.compiles(
1737         '''#include <stddef.h>
1738         static size_t f (size_t *i) { return *i + 1; }
1739         int main (void) {
1740           unsigned ''' + type_name + ''' i = 0;
1741           f (&i);
1742           return 0;
1743         }''',
1744         args: ['-Werror'],
1745         name : 'GCC size_t typedef is ' + type_name), }
1746   endforeach
1747 endif
1748
1749 if g_sizet_compatibility['short']
1750   glibconfig_conf.set('glib_size_type_define', 'short')
1751   glibconfig_conf.set_quoted('gsize_modifier', 'h')
1752   glibconfig_conf.set_quoted('gssize_modifier', 'h')
1753   glibconfig_conf.set_quoted('gsize_format', 'hu')
1754   glibconfig_conf.set_quoted('gssize_format', 'hi')
1755   glibconfig_conf.set('glib_msize_type', 'SHRT')
1756 elif g_sizet_compatibility['int']
1757   glibconfig_conf.set('glib_size_type_define', 'int')
1758   glibconfig_conf.set_quoted('gsize_modifier', '')
1759   glibconfig_conf.set_quoted('gssize_modifier', '')
1760   glibconfig_conf.set_quoted('gsize_format', 'u')
1761   glibconfig_conf.set_quoted('gssize_format', 'i')
1762   glibconfig_conf.set('glib_msize_type', 'INT')
1763 elif g_sizet_compatibility['long']
1764   glibconfig_conf.set('glib_size_type_define', 'long')
1765   glibconfig_conf.set_quoted('gsize_modifier', 'l')
1766   glibconfig_conf.set_quoted('gssize_modifier', 'l')
1767   glibconfig_conf.set_quoted('gsize_format', 'lu')
1768   glibconfig_conf.set_quoted('gssize_format', 'li')
1769   glibconfig_conf.set('glib_msize_type', 'LONG')
1770 elif g_sizet_compatibility['long long']
1771   glibconfig_conf.set('glib_size_type_define', 'long long')
1772   glibconfig_conf.set_quoted('gsize_modifier', int64_m)
1773   glibconfig_conf.set_quoted('gssize_modifier', int64_m)
1774   glibconfig_conf.set_quoted('gsize_format', int64_m + 'u')
1775   glibconfig_conf.set_quoted('gssize_format', int64_m + 'i')
1776   glibconfig_conf.set('glib_msize_type', 'INT64')
1777 else
1778   error('Could not determine size of size_t.')
1779 endif
1780
1781 if voidp_size == int_size
1782   glibconfig_conf.set('glib_intptr_type_define', 'int')
1783   glibconfig_conf.set_quoted('gintptr_modifier', '')
1784   glibconfig_conf.set_quoted('gintptr_format', 'i')
1785   glibconfig_conf.set_quoted('guintptr_format', 'u')
1786   glibconfig_conf.set('glib_gpi_cast', '(gint)')
1787   glibconfig_conf.set('glib_gpui_cast', '(guint)')
1788 elif voidp_size == long_size
1789   glibconfig_conf.set('glib_intptr_type_define', 'long')
1790   glibconfig_conf.set_quoted('gintptr_modifier', 'l')
1791   glibconfig_conf.set_quoted('gintptr_format', 'li')
1792   glibconfig_conf.set_quoted('guintptr_format', 'lu')
1793   glibconfig_conf.set('glib_gpi_cast', '(glong)')
1794   glibconfig_conf.set('glib_gpui_cast', '(gulong)')
1795 elif voidp_size == long_long_size
1796   glibconfig_conf.set('glib_intptr_type_define', 'long long')
1797   glibconfig_conf.set_quoted('gintptr_modifier', int64_m)
1798   glibconfig_conf.set_quoted('gintptr_format', int64_m + 'i')
1799   glibconfig_conf.set_quoted('guintptr_format', int64_m + 'u')
1800   glibconfig_conf.set('glib_gpi_cast', '(gint64)')
1801   glibconfig_conf.set('glib_gpui_cast', '(guint64)')
1802 else
1803   error('Could not determine size of void *')
1804 endif
1805
1806 if long_size != 8 and long_long_size != 8 and int_size != 8
1807   error('GLib requires a 64-bit type. You might want to consider using the GNU C compiler.')
1808 endif
1809
1810 glibconfig_conf.set('gintbits', int_size * 8)
1811 glibconfig_conf.set('glongbits', long_size * 8)
1812 glibconfig_conf.set('gsizebits', sizet_size * 8)
1813 glibconfig_conf.set('gssizebits', ssizet_size * 8)
1814
1815 # XXX: https://gitlab.gnome.org/GNOME/glib/issues/1413
1816 if host_system == 'windows'
1817   g_module_suffix = 'dll'
1818 else
1819   g_module_suffix = 'so'
1820 endif
1821 glibconfig_conf.set('g_module_suffix', g_module_suffix)
1822
1823 glibconfig_conf.set('GLIB_MAJOR_VERSION', major_version)
1824 glibconfig_conf.set('GLIB_MINOR_VERSION', minor_version)
1825 glibconfig_conf.set('GLIB_MICRO_VERSION', micro_version)
1826 glibconfig_conf.set('GLIB_VERSION', glib_version)
1827
1828 glibconfig_conf.set('glib_void_p', voidp_size)
1829 glibconfig_conf.set('glib_long', long_size)
1830 glibconfig_conf.set('glib_size_t', sizet_size)
1831 glibconfig_conf.set('glib_ssize_t', ssizet_size)
1832 if host_machine.endian() == 'big'
1833   glibconfig_conf.set('g_byte_order', 'G_BIG_ENDIAN')
1834   glibconfig_conf.set('g_bs_native', 'BE')
1835   glibconfig_conf.set('g_bs_alien', 'LE')
1836 else
1837   glibconfig_conf.set('g_byte_order', 'G_LITTLE_ENDIAN')
1838   glibconfig_conf.set('g_bs_native', 'LE')
1839   glibconfig_conf.set('g_bs_alien', 'BE')
1840 endif
1841
1842 # === va_copy checks ===
1843
1844 glib_vacopy = ''
1845
1846 # We check for G_VA_COPY_AS_ARRAY for historical reasons, but we no longer
1847 # use it: use Standard C va_copy() instead.
1848
1849 va_list_val_copy_prog = '''
1850   #include <stdarg.h>
1851   #include <stdlib.h>
1852   void f (int i, ...) {
1853     va_list args1, args2;
1854     va_start (args1, i);
1855     args2 = args1;
1856     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1857       exit (1);
1858     va_end (args1); va_end (args2);
1859   }
1860   int main() {
1861     f (0, 42);
1862     return 0;
1863   }'''
1864
1865 if cc_can_run
1866   rres = cc.run(va_list_val_copy_prog, name : 'va_lists can be copied as values')
1867   glib_va_val_copy = rres.compiled() and rres.returncode() == 0
1868 else
1869   glib_va_val_copy = meson.get_external_property('va_val_copy', true)
1870 endif
1871 if not glib_va_val_copy
1872   glib_vacopy = glib_vacopy + '\n#define G_VA_COPY_AS_ARRAY 1'
1873   glib_conf.set('G_VA_COPY_AS_ARRAY', 1)
1874 endif
1875 glibconfig_conf.set('glib_vacopy', glib_vacopy)
1876
1877 # check for flavours of varargs macros
1878 g_have_iso_c_varargs = cc.compiles('''
1879   void some_func (void) {
1880     int a(int p1, int p2, int p3);
1881     #define call_a(...) a(1,__VA_ARGS__)
1882     call_a(2,3);
1883   }''', name : 'ISO C99 varargs macros in C')
1884
1885 if not g_have_iso_c_varargs
1886   error('GLib requires a C compiler with support for C99 __VA_ARG__ in macros.')
1887 endif
1888
1889 if have_cxx
1890   g_have_iso_cxx_varargs = cxx.compiles('''
1891     void some_func (void) {
1892       int a(int p1, int p2, int p3);
1893       #define call_a(...) a(1,__VA_ARGS__)
1894       call_a(2,3);
1895     }''', name : 'ISO C99 varargs macros in C++')
1896
1897   if not g_have_iso_cxx_varargs
1898     error('GLib requires a C++ compiler with support for C99 __VA_ARG__ in macros.')
1899   endif
1900 endif
1901
1902 g_have_gnuc_varargs = cc.compiles('''
1903   void some_func (void) {
1904     int a(int p1, int p2, int p3);
1905     #define call_a(params...) a(1,params)
1906     call_a(2,3);
1907   }''', name : 'GNUC varargs macros')
1908
1909 if cc.has_header('alloca.h')
1910   glibconfig_conf.set('GLIB_HAVE_ALLOCA_H', true)
1911 endif
1912 has_syspoll = cc.has_header('sys/poll.h')
1913 has_systypes = cc.has_header('sys/types.h')
1914 if has_syspoll
1915   glibconfig_conf.set('GLIB_HAVE_SYS_POLL_H', true)
1916 endif
1917 has_winsock2 = cc.has_header('winsock2.h')
1918
1919 if has_syspoll and has_systypes
1920   poll_includes = '''
1921       #include<sys/poll.h>
1922       #include<sys/types.h>'''
1923 elif has_winsock2
1924   poll_includes = '''
1925       #define _WIN32_WINNT @0@
1926       #include <winsock2.h>'''.format(glib_conf.get('_WIN32_WINNT'))
1927 else
1928   # FIXME?
1929   error('FIX POLL* defines')
1930 endif
1931
1932 poll_defines = [
1933   [ 'POLLIN', 'g_pollin', 1 ],
1934   [ 'POLLOUT', 'g_pollout', 4 ],
1935   [ 'POLLPRI', 'g_pollpri', 2 ],
1936   [ 'POLLERR', 'g_pollerr', 8 ],
1937   [ 'POLLHUP', 'g_pollhup', 16 ],
1938   [ 'POLLNVAL', 'g_pollnval', 32 ],
1939 ]
1940
1941 if has_syspoll and has_systypes
1942   foreach d : poll_defines
1943     val = cc.compute_int(d[0], prefix: poll_includes)
1944     glibconfig_conf.set(d[1], val)
1945   endforeach
1946 elif has_winsock2
1947   # Due to a missed bug in configure.ac the poll test
1948   # never succeeded on Windows and used some pre-defined
1949   # values as a fallback. Keep using them to maintain
1950   # ABI compatibility with autotools builds of glibs
1951   # and with *any* glib-using code compiled against them,
1952   # since these values end up in a public header glibconfig.h.
1953   foreach d : poll_defines
1954     glibconfig_conf.set(d[1], d[2])
1955   endforeach
1956 endif
1957
1958 # Internet address families
1959 # FIXME: what about Cygwin (G_WITH_CYGWIN)
1960 if host_system == 'windows'
1961   inet_includes = '''
1962       #include <winsock2.h>'''
1963 else
1964   inet_includes = '''
1965       #include <sys/types.h>
1966       #include <sys/socket.h>'''
1967 endif
1968
1969 inet_defines = [
1970   [ 'AF_UNIX', 'g_af_unix' ],
1971   [ 'AF_INET', 'g_af_inet' ],
1972   [ 'AF_INET6', 'g_af_inet6' ],
1973   [ 'MSG_OOB', 'g_msg_oob' ],
1974   [ 'MSG_PEEK', 'g_msg_peek' ],
1975   [ 'MSG_DONTROUTE', 'g_msg_dontroute' ],
1976 ]
1977 foreach d : inet_defines
1978   val = cc.compute_int(d[0], prefix: inet_includes)
1979   glibconfig_conf.set(d[1], val)
1980 endforeach
1981
1982 if host_system == 'windows'
1983   have_ipv6 = true
1984 else
1985   have_ipv6 = cc.has_type('struct in6_addr', prefix: '#include <netinet/in.h>')
1986 endif
1987 glib_conf.set('HAVE_IPV6', have_ipv6)
1988
1989 # We need to decide at configure time if GLib will use real atomic
1990 # operations ("lock free") or emulated ones with a mutex.  This is
1991 # because we must put this information in glibconfig.h so we know if
1992 # it is safe or not to inline using compiler intrinsics directly from
1993 # the header.
1994 #
1995 # We also publish the information via G_ATOMIC_LOCK_FREE in case the
1996 # user is interested in knowing if they can use the atomic ops across
1997 # processes.
1998 #
1999 # We can currently support the atomic ops natively when building GLib
2000 # with recent versions of GCC or MSVC.
2001 #
2002 # Note that the atomic ops are only available with GCC on x86 when
2003 # using -march=i486 or higher.  If we detect that the atomic ops are
2004 # not available but would be available given the right flags, we want
2005 # to abort and advise the user to fix their CFLAGS.  It's better to do
2006 # that then to silently fall back on emulated atomic ops just because
2007 # the user had the wrong build environment.
2008 atomictest = '''int main() {
2009   int atomic = 2;
2010   __sync_bool_compare_and_swap (&atomic, 2, 3);
2011   return 0;
2012 }
2013 '''
2014
2015 atomicdefine = '''
2016 #ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
2017 #error "compiler has atomic ops, but doesn't define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"
2018 #endif
2019 '''
2020
2021 # We know that we can always use real ("lock free") atomic operations with MSVC
2022 if cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl' or cc.links(atomictest, name : 'atomic ops')
2023   have_atomic_lock_free = true
2024   if cc.get_id() == 'gcc' and not cc.compiles(atomicdefine, name : 'atomic ops define')
2025     # Old gcc release may provide
2026     # __sync_bool_compare_and_swap but doesn't define
2027     # __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
2028     glib_conf.set('__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4', true)
2029   endif
2030
2031   if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
2032     sync_swap_test = '''
2033       int main() {
2034         int atomic = 2;
2035         __sync_swap (&atomic, 2);
2036         return 0;
2037       }
2038     '''
2039
2040     glib_conf.set('_GLIB_GCC_HAVE_SYNC_SWAP', cc.links(sync_swap_test, name : 'sync swap'))
2041   endif
2042 else
2043   have_atomic_lock_free = false
2044   if host_machine.cpu_family() == 'x86' and cc.links(atomictest, args : '-march=i486')
2045     error('GLib must be built with -march=i486 or later.')
2046   endif
2047 endif
2048 glibconfig_conf.set('G_ATOMIC_LOCK_FREE', have_atomic_lock_free)
2049
2050 # === Threads ===
2051
2052 if get_option('force_posix_threads')
2053   warning('DEPRECATION: Option \'force_posix_threads\' is deprecated and will be removed after GLib 2.72; please file an issue with your use case if you still require it')
2054 endif
2055
2056 # Determination of thread implementation
2057 if host_system == 'windows' and not get_option('force_posix_threads')
2058   thread_dep = []
2059   threads_implementation = 'win32'
2060   glibconfig_conf.set('g_threads_impl_def', 'WIN32')
2061   glib_conf.set('THREADS_WIN32', 1)
2062 else
2063   thread_dep = dependency('threads')
2064   threads_implementation = 'posix'
2065   pthread_prefix = '''
2066       #ifndef _GNU_SOURCE
2067       # define _GNU_SOURCE
2068       #endif
2069       #include <pthread.h>'''
2070   glibconfig_conf.set('g_threads_impl_def', 'POSIX')
2071   glib_conf.set('THREADS_POSIX', 1)
2072   if cc.has_header_symbol('pthread.h', 'pthread_attr_setstacksize')
2073     glib_conf.set('HAVE_PTHREAD_ATTR_SETSTACKSIZE', 1)
2074   endif
2075   if cc.has_header_symbol('pthread.h', 'pthread_attr_setinheritsched')
2076     glib_conf.set('HAVE_PTHREAD_ATTR_SETINHERITSCHED', 1)
2077   endif
2078   if cc.has_header_symbol('pthread.h', 'pthread_condattr_setclock')
2079     glib_conf.set('HAVE_PTHREAD_CONDATTR_SETCLOCK', 1)
2080   endif
2081   if cc.has_header_symbol('pthread.h', 'pthread_cond_timedwait_relative_np')
2082     glib_conf.set('HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP', 1)
2083   endif
2084   if cc.has_header_symbol('pthread.h', 'pthread_getname_np', prefix : pthread_prefix)
2085     glib_conf.set('HAVE_PTHREAD_GETNAME_NP', 1)
2086   endif
2087   if cc.has_header_symbol('pthread.h', 'pthread_getaffinity_np', prefix : pthread_prefix)
2088     glib_conf.set('HAVE_PTHREAD_GETAFFINITY_NP', 1)
2089   endif
2090
2091   # Assume that pthread_setname_np is available in some form; same as configure
2092   if cc.links(pthread_prefix + '''
2093               int main() {
2094                 pthread_setname_np("example");
2095                 return 0;
2096               }''',
2097               name : 'pthread_setname_np(const char*)',
2098               dependencies : thread_dep)
2099     # macOS and iOS
2100     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID', 1)
2101   elif cc.links(pthread_prefix + '''
2102                 int main() {
2103                   pthread_setname_np(pthread_self(), "example");
2104                   return 0;
2105                 }''',
2106                 name : 'pthread_setname_np(pthread_t, const char*)',
2107                 dependencies : thread_dep)
2108     # Linux, Solaris, etc.
2109     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITH_TID', 1)
2110   elif cc.links(pthread_prefix + '''
2111                 int main() {
2112                   pthread_setname_np(pthread_self(), "%s", "example");
2113                   return 0;
2114                 }''',
2115                 name : 'pthread_setname_np(pthread_t, const char*, void*)',
2116                 dependencies : thread_dep)
2117     # NetBSD
2118     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG', 1)
2119   elif cc.links(pthread_prefix + '''
2120                 #include <pthread_np.h>
2121                 int main() {
2122                   pthread_set_name_np(pthread_self(), "example");
2123                   return 0;
2124                 }''',
2125                 name : 'pthread_set_name_np(pthread_t, const char*)',
2126                 dependencies : thread_dep)
2127     # FreeBSD, DragonFlyBSD, OpenBSD, etc.
2128     glib_conf.set('HAVE_PTHREAD_SET_NAME_NP', 1)
2129   endif
2130 endif
2131
2132 # FIXME: we should make it print the result and always return 0, so that
2133 # the output in meson shows up as green
2134 # volatile is needed here to avoid optimisations in the test
2135 stack_grows_check_prog = '''
2136   volatile int *a = 0, *b = 0;
2137   void f (int i) {
2138     volatile int x = 5;
2139     if (i == 0)
2140       b = &x;
2141     else
2142       f (i - 1);
2143   }
2144   int main () {
2145     volatile int y = 7;
2146     a = &y;
2147     f (100);
2148     return b > a ? 0 : 1;
2149   }'''
2150
2151 if cc_can_run
2152   rres = cc.run(stack_grows_check_prog, name : 'stack grows check')
2153   growing_stack = rres.compiled() and rres.returncode() == 0
2154 else
2155   growing_stack = meson.get_external_property('growing_stack', false)
2156 endif
2157
2158 glibconfig_conf.set10('G_HAVE_GROWING_STACK', growing_stack)
2159
2160 # Tests for iconv
2161 #
2162 # We should never use the MinGW C library's iconv because it may not be
2163 # available in the actual runtime environment. On Windows, we always use
2164 # the built-in implementation
2165 if host_system == 'windows'
2166   # We have a #include "win_iconv.c" in gconvert.c on Windows, so we don't need
2167   # any external library for it
2168   libiconv = []
2169 else
2170   libiconv = dependency('iconv')
2171 endif
2172
2173 pcre2_req = '>=10.32'
2174
2175 # Pick up pcre from the system, or if "--force-fallback-for libpcre2-8" was specified
2176 pcre2 = dependency('libpcre2-8', version: pcre2_req, required: false, default_options: ['default_library=static'])
2177 if not pcre2.found()
2178   if cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl'
2179   # MSVC: Search for the PCRE2 library by the configuration, which corresponds
2180   # to the output of CMake builds of PCRE2.  Note that debugoptimized
2181   # is really a Release build with .PDB files.
2182     if vs_crt == 'debug'
2183       pcre2 = cc.find_library('pcre2d-8', required : false)
2184     else
2185       pcre2 = cc.find_library('pcre2-8', required : false)
2186     endif
2187   endif
2188 endif
2189
2190 # Try again with the fallback
2191 if not pcre2.found()
2192   pcre2 = dependency('libpcre2-8', version: pcre2_req, allow_fallback: true, default_options: ['default_library=static'])
2193   assert(pcre2.type_name() == 'internal')
2194   # static flags are automatically enabled by the subproject if it's built
2195   # with default_library=static
2196   use_pcre2_static_flag = false
2197 elif host_system == 'windows' and pcre2.type_name() != 'internal'
2198   pcre2_static = cc.links('''#define PCRE2_STATIC
2199                              #define PCRE2_CODE_UNIT_WIDTH 8
2200                              #include <pcre2.h>
2201                              int main() {
2202                                void *p = NULL;
2203                                pcre2_code_free(p);
2204                                return 0;
2205                              }''',
2206                          dependencies: pcre2,
2207                          name : 'Windows system PCRE2 is a static build')
2208   use_pcre2_static_flag = pcre2_static
2209 else
2210   use_pcre2_static_flag = false
2211 endif
2212
2213 # Import the gvdb sources as a subproject to avoid having the copylib in-tree
2214 subproject('gvdb')
2215 gvdb_dep = dependency('gvdb')
2216
2217 libm = cc.find_library('m', required : false)
2218 libffi_dep = dependency('libffi', version : '>= 3.0.0')
2219
2220 libz_dep = dependency('zlib')
2221
2222 # First check in libc, fallback to libintl, and as last chance build
2223 # proxy-libintl subproject.
2224 # FIXME: glib-gettext.m4 has much more checks to detect broken/uncompatible
2225 # implementations. This could be extended if issues are found in some platforms.
2226 libintl_deps = []
2227 libintl_prefix = '#include <libintl.h>'
2228 libintl = dependency('intl', required: false)
2229 if libintl.found() and libintl.type_name() != 'internal'
2230   # libintl supports different threading APIs, which may not
2231   # require additional flags, but it defaults to using pthreads if
2232   # found. Meson's "threads" dependency does not allow you to
2233   # prefer pthreads. We may not be using pthreads for glib itself
2234   # either so just link the library to satisfy libintl rather than
2235   # also defining the macros with the -pthread flag.
2236   #
2237   # Meson's builtin dependency lookup as of 0.60.0 doesn't check for
2238   # pthread, so we do this manually here.
2239   if cc.has_function('ngettext', dependencies : libintl, prefix: libintl_prefix)
2240     libintl_deps += [libintl]
2241   else
2242     libintl_iconv = cc.find_library('iconv', required : false)
2243     if libintl_iconv.found() and cc.has_function('ngettext', args : osx_ldflags, dependencies : [libintl, libintl_iconv])
2244       libintl_deps += [libintl, libintl_iconv]
2245     else
2246       libintl_pthread = cc.find_library('pthread', required : false)
2247       if libintl_pthread.found() and cc.has_function('ngettext', dependencies : [libintl, libintl_pthread], prefix: libintl_prefix)
2248         libintl_deps += [libintl, libintl_pthread]
2249       else
2250         libintl = disabler()
2251       endif
2252     endif
2253   endif
2254 endif
2255
2256 if libintl.found() and libintl.type_name() != 'internal'
2257   have_bind_textdomain_codeset = cc.has_function('bind_textdomain_codeset', dependencies: libintl_deps, prefix: libintl_prefix)
2258 else
2259   # using proxy-libintl fallback
2260   libintl = dependency('intl', allow_fallback: true)
2261   assert(libintl.type_name() == 'internal')
2262   libintl_deps = [libintl]
2263   have_bind_textdomain_codeset = true  # proxy-libintl supports it
2264 endif
2265
2266 glib_conf.set('HAVE_BIND_TEXTDOMAIN_CODESET', have_bind_textdomain_codeset)
2267
2268 # We require gettext to always be present
2269 glib_conf.set('HAVE_DCGETTEXT', 1)
2270 glib_conf.set('HAVE_GETTEXT', 1)
2271
2272 glib_conf.set_quoted('GLIB_LOCALE_DIR', join_paths(glib_datadir, 'locale'))
2273
2274 glib_conf.set_quoted('GLIB_LOCALSTATEDIR', glib_localstatedir)
2275 glib_conf.set_quoted('GLIB_RUNSTATEDIR', glib_runstatedir)
2276
2277 # libmount is only used by gio, but we need to fetch the libs to generate the
2278 # pkg-config file below
2279 libmount_dep = []
2280 if host_system == 'linux'
2281   libmount_dep = dependency('mount', version : '>=2.23', required : get_option('libmount'))
2282   glib_conf.set('HAVE_LIBMOUNT', libmount_dep.found())
2283
2284   if libmount_dep.found() and cc.has_function('mnt_monitor_veil_kernel', dependencies: libmount_dep)
2285     glib_conf.set('HAVE_MNT_MONITOR_VEIL_KERNEL', 1)
2286   endif
2287 endif
2288
2289 if host_system == 'windows'
2290   winsock2 = cc.find_library('ws2_32')
2291 else
2292   winsock2 = not_found
2293 endif
2294
2295 selinux_dep = []
2296 if host_system == 'linux'
2297   selinux_dep = dependency('libselinux', version: '>=2.2', required: get_option('selinux'))
2298
2299   glib_conf.set('HAVE_SELINUX', selinux_dep.found())
2300 endif
2301
2302 xattr_dep = []
2303 if host_system != 'windows' and get_option('xattr')
2304   # either glibc or libattr can provide xattr support
2305   # for both of them, we check for getxattr being in
2306   # the library and a valid xattr header.
2307
2308   # try glibc
2309   if cc.has_function('getxattr') and cc.has_header('sys/xattr.h')
2310     glib_conf.set('HAVE_SYS_XATTR_H', 1)
2311     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format('HAVE_SYS_XATTR_H')
2312   #failure. try libattr
2313   elif cc.has_header_symbol('attr/xattr.h', 'getxattr')
2314     glib_conf.set('HAVE_ATTR_XATTR_H', 1)
2315     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format('HAVE_ATTR_XATTR_H')
2316     xattr_dep = [cc.find_library('xattr')]
2317   else
2318     error('No getxattr implementation found in C library or libxattr')
2319   endif
2320
2321   glib_conf.set('HAVE_XATTR', 1)
2322   if cc.compiles(glib_conf_prefix + '''
2323                  #include <stdio.h>
2324                  #ifdef HAVE_SYS_TYPES_H
2325                  #include <sys/types.h>
2326                  #endif
2327                  #ifdef HAVE_SYS_XATTR_H
2328                  #include <sys/xattr.h>
2329                  #elif HAVE_ATTR_XATTR_H
2330                  #include <attr/xattr.h>
2331                  #endif
2332
2333                  int main (void) {
2334                    ssize_t len = getxattr("", "", NULL, 0, 0, XATTR_NOFOLLOW);
2335                    return len;
2336                  }''',
2337                  name : 'XATTR_NOFOLLOW')
2338     glib_conf.set('HAVE_XATTR_NOFOLLOW', 1)
2339   endif
2340 endif
2341
2342 # If strlcpy is present (BSD and similar), check that it conforms to the BSD
2343 # specification. Specifically Solaris 8's strlcpy() does not, see
2344 # https://bugzilla.gnome.org/show_bug.cgi?id=53933 for further context.
2345 if cc.has_function('strlcpy')
2346   if cc_can_run
2347     rres = cc.run('''#include <stdlib.h>
2348                     #include <string.h>
2349                     int main() {
2350                       char p[10];
2351                       (void) strlcpy (p, "hi", 10);
2352                       if (strlcat (p, "bye", 0) != 3)
2353                         return 1;
2354                       return 0;
2355                     }''',
2356                   name : 'OpenBSD strlcpy/strlcat')
2357     if rres.compiled() and rres.returncode() == 0
2358       glib_conf.set('HAVE_STRLCPY', 1)
2359     endif
2360   elif meson.get_external_property('have_strlcpy', false)
2361     glib_conf.set('HAVE_STRLCPY', 1)
2362   endif
2363 endif
2364
2365 cmdline_test_code = '''
2366 #include <fcntl.h>
2367 #include <sys/stat.h>
2368 #include <stdio.h>
2369 #include <stdlib.h>
2370 #include <unistd.h>
2371
2372 static int
2373 __getcmdline (void)
2374 {
2375 /* This code is a dumbed-down version of g_file_get_contents() */
2376 #ifndef O_BINARY
2377 #define O_BINARY 0
2378 #endif
2379 #define BUFSIZE 1024
2380   char result[BUFSIZE];
2381   struct stat stat_buf;
2382
2383   int fd = open ("/proc/self/cmdline", O_RDONLY|O_BINARY);
2384   if (fd < 0)
2385     exit (1);
2386   if (fstat (fd, &stat_buf))
2387     exit (1);
2388
2389   if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
2390     {
2391       if (read (fd, result, BUFSIZE) <= 0)
2392         exit (1);
2393     }
2394   else
2395     {
2396       FILE *f = fdopen (fd, "r");
2397       if (f == NULL)
2398         exit (1);
2399
2400       if (fread (result, 1, BUFSIZE, f) <= 0)
2401         exit (1);
2402     }
2403
2404   return 0;
2405 }
2406
2407 int
2408 main (void)
2409 {
2410   exit (__getcmdline ());
2411 }'''
2412
2413 if cc_can_run
2414   rres = cc.run(cmdline_test_code, name : '/proc/self/cmdline')
2415   have_proc_self_cmdline = rres.compiled() and rres.returncode() == 0
2416 else
2417   have_proc_self_cmdline = meson.get_external_property('have_proc_self_cmdline', false)
2418 endif
2419
2420 glib_conf.set('HAVE_PROC_SELF_CMDLINE', have_proc_self_cmdline)
2421
2422 python = import('python').find_installation(modules: ['packaging'])
2423 # used for '#!/usr/bin/env <name>'
2424 python_name = 'python3'
2425
2426 python_version = python.language_version()
2427 python_version_req = '>=3.7'
2428 if not python_version.version_compare(python_version_req)
2429   error('Requires Python @0@, @1@ found.'.format(python_version_req, python_version))
2430 endif
2431
2432 # Determine which user environment-dependent files that we want to install
2433 bash = find_program('bash', required : false)
2434 have_bash = bash.found() # For completion scripts
2435 bash_comp_dep = dependency('bash-completion', version: '>=2.0', required: false)
2436 have_sh = find_program('sh', required : false).found() # For glib-gettextize
2437 have_pkg_config = find_program('pkg-config', required: false).found()
2438
2439 # Some installed tests require a custom environment
2440 env_program = find_program('env', required: installed_tests_enabled)
2441
2442 # FIXME: How to detect Solaris? https://github.com/mesonbuild/meson/issues/1578
2443 if host_system == 'sunos'
2444   glib_conf.set('_XOPEN_SOURCE_EXTENDED', 1)
2445   glib_conf.set('_XOPEN_SOURCE', 2)
2446   glib_conf.set('__EXTENSIONS__',1)
2447 endif
2448
2449 # Sadly Meson does not expose this value:
2450 # https://github.com/mesonbuild/meson/pull/3460
2451 if host_system == 'windows'
2452   # Autotools explicitly removed --Wl,--export-all-symbols from windows builds,
2453   # with no explanation. Do the same here for now but this could be revisited if
2454   # if causes issues.
2455   export_dynamic_ldflags = []
2456 elif host_system == 'cygwin'
2457   export_dynamic_ldflags = ['-Wl,--export-all-symbols']
2458 elif host_system in ['darwin', 'ios']
2459   export_dynamic_ldflags = []
2460 elif host_system == 'sunos'
2461   export_dynamic_ldflags = []
2462 else
2463   export_dynamic_ldflags = ['-Wl,--export-dynamic']
2464 endif
2465
2466 win32_cflags = []
2467 win32_ldflags = []
2468 if host_system == 'windows' and cc.get_id() != 'msvc' and cc.get_id() != 'clang-cl'
2469   # Ensure MSVC-compatible struct packing convention is used when
2470   # compiling for Win32 with gcc. It is used for the whole project and exposed
2471   # in glib-2.0.pc.
2472   if not cc.compiles('''
2473 struct _GTestMSBitfields
2474 {
2475   int a : 1;
2476   short b : 1;
2477 };
2478
2479 typedef char _StaticCheck[sizeof(struct _GTestMSBitfields) != sizeof(int) ? 1 : -1];
2480 ''')
2481     warning('''
2482 Your compiler does not have ms-bitfields packing by default.
2483 Please use gcc >= 4.7 or clang >= 12: GLib will drop -mms-bitfields in the future.
2484 ''')
2485   endif
2486   win32_cflags = ['-mms-bitfields']
2487   add_project_arguments(win32_cflags, language : 'c')
2488
2489   # Win32 API libs, used only by libglib and exposed in glib-2.0.pc
2490   win32_ldflags = ['-lws2_32', '-lole32', '-lwinmm', '-lshlwapi', '-luuid']
2491 elif host_system == 'cygwin'
2492   win32_ldflags = ['-luser32', '-lkernel32']
2493 endif
2494
2495 # Tracing: dtrace
2496 want_dtrace = get_option('dtrace')
2497 enable_dtrace = false
2498
2499 # Since dtrace support is opt-in we just error out if it was requested but
2500 # is not available. We don't bother with autodetection yet.
2501 if want_dtrace
2502   if glib_have_carbon
2503     error('GLib dtrace support not yet compatible with macOS dtrace')
2504   endif
2505   dtrace = find_program('dtrace', required : true) # error out if not found
2506   if not cc.has_header('sys/sdt.h')
2507     error('dtrace support needs sys/sdt.h header')
2508   endif
2509   # FIXME: autotools build also passes -fPIC -DPIC but is it needed in this case?
2510   dtrace_obj_gen = generator(dtrace,
2511     output : '@BASENAME@.o',
2512     arguments : ['-G', '-s', '@INPUT@', '-o', '@OUTPUT@'])
2513   dtrace_hdr_gen = generator(python,
2514     output : '@BASENAME@.h',
2515     arguments : ['-c', '''
2516 import subprocess, sys
2517 subprocess.run(sys.argv[1:], check=True)
2518 output = sys.argv[6]
2519 with open(output) as f:
2520   contents = f.read()
2521 contents = contents.replace("define STAP_HAS_SEMAPHORES 1",
2522   "undef STAP_HAS_SEMAPHORES")
2523 contents = contents.replace("define _SDT_HAS_SEMAPHORES 1",
2524   "undef _SDT_HAS_SEMAPHORES")
2525 with open(output, "w") as f:
2526   f.write(contents)
2527 ''', dtrace.full_path(), '-h', '-s', '@INPUT@', '-o', '@OUTPUT@'])
2528   glib_conf.set('HAVE_DTRACE', 1)
2529   enable_dtrace = true
2530 endif
2531
2532 if cc.has_header_symbol('sys/ptrace.h', 'PTRACE_O_EXITKILL')
2533   glib_conf.set('HAVE_PTRACE_O_EXITKILL', 1)
2534 endif
2535
2536 # systemtap
2537 want_systemtap = get_option('systemtap')
2538 enable_systemtap = false
2539
2540 if want_systemtap and enable_dtrace
2541   tapset_install_dir = get_option('tapset_install_dir')
2542   if tapset_install_dir == ''
2543     tapset_install_dir = join_paths(get_option('datadir'), 'systemtap/tapset', host_machine.cpu_family())
2544   endif
2545   stp_cdata = configuration_data()
2546   stp_cdata.set('ABS_GLIB_RUNTIME_LIBDIR', glib_libdir)
2547   stp_cdata.set('LT_CURRENT', minor_version * 100)
2548   stp_cdata.set('LT_REVISION', micro_version)
2549   enable_systemtap = true
2550 endif
2551
2552 # introspection
2553 gir_scanner = find_program('g-ir-scanner', required: get_option('introspection'))
2554 enable_gir = get_option('introspection').allowed() and gir_scanner.found() and meson.can_run_host_binaries()
2555
2556 if get_option('introspection').enabled() and not meson.can_run_host_binaries()
2557   error('Running binaries on the build host needs to be supported to build with -Dintrospection=enabled')
2558 endif
2559
2560 gir_args = [
2561   '--quiet',
2562 ]
2563
2564 pkg = import('pkgconfig')
2565 windows = import('windows')
2566 gnome = import('gnome')
2567
2568 subdir('tools')
2569 subdir('glib')
2570 subdir('gobject')
2571 subdir('gthread')
2572 subdir('gmodule')
2573 subdir('gio')
2574 subdir('girepository')
2575 subdir('fuzzing')
2576 subdir('tests')
2577
2578 # xgettext is optional (on Windows for instance)
2579 if find_program('xgettext', required : get_option('nls')).found()
2580   subdir('po')
2581 endif
2582
2583 # Install m4 macros that other projects use
2584 install_data('m4macros/glib-2.0.m4', 'm4macros/glib-gettext.m4', 'm4macros/gsettings.m4',
2585   install_dir : get_option('datadir') / 'aclocal',
2586   install_tag : 'devel',
2587 )
2588
2589 # Check whether we support overriding the invalid parameter handler on Windows for _get_osfhandle(),
2590 # g_fsync() (i.e. _commit()), etc
2591 if host_system == 'windows'
2592   if cc.has_function('_set_thread_local_invalid_parameter_handler', prefix: '#include <stdlib.h>')
2593     glib_conf.set('HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER', 1)
2594   endif
2595   if cc.has_function('_set_invalid_parameter_handler', prefix: '#include <stdlib.h>')
2596     glib_conf.set('HAVE__SET_INVALID_PARAMETER_HANDLER', 1)
2597   endif
2598   if cc.has_header_symbol('crtdbg.h', '_CrtSetReportMode')
2599     glib_conf.set('HAVE__CRT_SET_REPORT_MODE', 1)
2600   endif
2601 endif
2602
2603 configure_file(output : 'config.h', configuration : glib_conf)
2604
2605 rst2man = find_program('rst2man', 'rst2man.py', required: get_option('man-pages'))
2606 if rst2man.found()
2607   rst2man_flags = [
2608     '--syntax-highlight=none',
2609   ]
2610   man1_dir = join_paths(glib_prefix, get_option('mandir'), 'man1')
2611 endif
2612
2613 gnome = import('gnome')
2614 subdir('docs/reference')
2615
2616 summary({
2617   'host cpu' : host_machine.cpu_family(),
2618   'host endian' : host_machine.endian(),
2619   'host system' : host_system,
2620   'C Compiler' : cc.get_id(),
2621   'C++ Compiler' : have_cxx ? cxx.get_id() : 'none',
2622   'shared build' : glib_build_shared,
2623   'static build' : glib_build_static,
2624 }, section: 'Build environment')
2625
2626 if build_machine.system() != host_system
2627   summary({
2628     'build cpu' : build_machine.cpu_family(),
2629     'build endian' : build_machine.endian(),
2630     'build system' : build_machine.system(),
2631   }, section: 'Build environment')
2632 endif
2633
2634 if linux_libc != ''
2635   summary({
2636     'linux_libc' : linux_libc
2637   }, section: 'Build environment')
2638 endif
2639
2640 summary({
2641   'prefix' : glib_prefix,
2642   'bindir' : glib_bindir,
2643   'libexecdir' : glib_libexecdir,
2644   'pkgdatadir' : glib_pkgdatadir,
2645   'datadir' : glib_datadir,
2646   'includedir' : glib_includedir,
2647   'giomodulesdir' : glib_giomodulesdir,
2648   'localstatedir' : glib_localstatedir,
2649   'runstatedir' : glib_runstatedir,
2650 }, section: 'Directories')
2651
2652 if get_option('multiarch')
2653   summary({
2654     'multiarch bindir' : glib_bindir,
2655     'multiarch libexecdir' : glib_libexecdir,
2656   }, section: 'Directories')
2657 endif
2658
2659 if enable_systemtap
2660   summary('tapset dir', get_option('tapset_install_dir'), section: 'Directories')
2661 endif
2662
2663 if host_system == 'linux'
2664   summary({
2665     'selinux' : selinux_dep.found(),
2666     'libmount' : libmount_dep.found(),
2667   }, section: 'Options')
2668 endif
2669
2670 summary({
2671   'xattr' : xattr_dep.length() > 0,
2672   'man-pages' : get_option('man-pages'),
2673   'dtrace' : get_option('dtrace'),
2674   'systemtap' : enable_systemtap,
2675   'sysprof' : libsysprof_capture_dep.found(),
2676   'documentation' : get_option('documentation'),
2677   'bsymbolic_functions' : get_option('bsymbolic_functions'),
2678   'force_posix_threads' : get_option('force_posix_threads'),
2679   'tests' : get_option('tests'),
2680   'installed_tests' : get_option('installed_tests'),
2681   'nls' : get_option('nls'),
2682   'oss_fuzz' : get_option('oss_fuzz'),
2683   'glib_debug' : get_option('glib_debug'),
2684   'glib_assert' : get_option('glib_assert'),
2685   'glib_checks' : get_option('glib_checks'),
2686   'libelf' : get_option('libelf'),
2687   'multiarch' : get_option('multiarch'),
2688   'introspection' : enable_gir,
2689 }, section: 'Options')