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