Imported Upstream version 2.61.1
[platform/upstream/glib.git] / meson.build
1 project('glib', 'c', 'cpp',
2   version : '2.61.1',
3   meson_version : '>= 0.48.0',
4   default_options : [
5     'buildtype=debugoptimized',
6     'warning_level=1',
7     'c_std=gnu89'
8   ]
9 )
10
11 cc = meson.get_compiler('c')
12 cxx = meson.get_compiler('cpp')
13
14 cc_can_run = not meson.is_cross_build() or meson.has_exe_wrapper()
15
16 if cc.get_id() == 'msvc'
17   # Ignore several spurious warnings for things glib does very commonly
18   # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it
19   # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once
20   # NOTE: Only add warnings here if you are sure they're spurious
21   add_project_arguments('/wd4035', '/wd4715', '/wd4116',
22     '/wd4046', '/wd4068', '/wo4090', '/FImsvc_recommended_pragmas.h',language : 'c')
23   # Disable SAFESEH with MSVC for plugins and libs that use external deps that
24   # are built with MinGW
25   noseh_link_args = ['/SAFESEH:NO']
26   # Set the input and exec encoding to utf-8, like is the default with GCC
27   add_project_arguments(cc.get_supported_arguments(['/utf-8']), language: 'c')
28 else
29   noseh_link_args = []
30   # -mms-bitfields vs -fnative-struct ?
31 endif
32
33 host_system = host_machine.system()
34
35 glib_version = meson.project_version()
36 glib_api_version = '2.0'
37 version_arr = glib_version.split('.')
38 major_version = version_arr[0].to_int()
39 minor_version = version_arr[1].to_int()
40 micro_version = version_arr[2].to_int()
41
42 interface_age = minor_version.is_odd() ? 0 : micro_version
43 binary_age = 100 * minor_version + micro_version
44
45 soversion = 0
46 # Maintain compatibility with previous libtool versioning
47 # current = minor * 100 + micro
48 current = binary_age - interface_age
49 library_version = '@0@.@1@.@2@'.format(soversion, current, interface_age)
50 darwin_versions = [current + 1, '@0@.@1@'.format(current + 1, interface_age)]
51
52 configinc = include_directories('.')
53 glibinc = include_directories('glib')
54 gobjectinc = include_directories('gobject')
55 gmoduleinc = include_directories('gmodule')
56 gioinc = include_directories('gio')
57
58 glib_prefix = get_option('prefix')
59 glib_bindir = join_paths(glib_prefix, get_option('bindir'))
60 glib_libdir = join_paths(glib_prefix, get_option('libdir'))
61 glib_libexecdir = join_paths(glib_prefix, get_option('libexecdir'))
62 glib_datadir = join_paths(glib_prefix, get_option('datadir'))
63 glib_pkgdatadir = join_paths(glib_datadir, 'glib-2.0')
64 glib_includedir = join_paths(glib_prefix, get_option('includedir'))
65 glib_giomodulesdir = get_option('gio_module_dir')
66 if glib_giomodulesdir == ''
67   glib_giomodulesdir = join_paths(glib_libdir, 'gio', 'modules')
68 endif
69
70 glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
71
72 installed_tests_metadir = join_paths(glib_datadir, 'installed-tests', meson.project_name())
73 installed_tests_execdir = join_paths(glib_libexecdir, 'installed-tests', meson.project_name())
74 installed_tests_enabled = get_option('installed_tests')
75 installed_tests_template = files('template.test.in')
76 installed_tests_template_tap = files('template-tap.test.in')
77
78 add_project_arguments('-D_GNU_SOURCE', language: 'c')
79
80 # Disable strict aliasing;
81 # see https://bugzilla.gnome.org/show_bug.cgi?id=791622
82 if cc.has_argument('-fno-strict-aliasing')
83   add_project_arguments('-fno-strict-aliasing', language: 'c')
84 endif
85
86 ########################
87 # Configuration begins #
88 ########################
89 glib_conf = configuration_data()
90 glibconfig_conf = configuration_data()
91
92 # accumulated list of defines as we check for them, so we can easily
93 # use them later in test programs (autoconf does this automatically)
94 glib_conf_prefix = ''
95
96 glib_conf.set('GLIB_MAJOR_VERSION', major_version)
97 glib_conf.set('GLIB_MINOR_VERSION', minor_version)
98 glib_conf.set('GLIB_MICRO_VERSION', micro_version)
99 glib_conf.set('GLIB_INTERFACE_AGE', interface_age)
100 glib_conf.set('GLIB_BINARY_AGE', binary_age)
101 glib_conf.set_quoted('GETTEXT_PACKAGE', 'glib20')
102 glib_conf.set_quoted('PACKAGE_BUGREPORT', 'https://gitlab.gnome.org/GNOME/glib/issues/new')
103 glib_conf.set_quoted('PACKAGE_NAME', 'glib')
104 glib_conf.set_quoted('PACKAGE_STRING', 'glib @0@'.format(meson.project_version()))
105 glib_conf.set_quoted('PACKAGE_TARNAME', 'glib')
106 glib_conf.set_quoted('PACKAGE_URL', '')
107 glib_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
108 glib_conf.set('ENABLE_NLS', 1)
109
110 # used by the .rc.in files
111 glibconfig_conf.set('LT_CURRENT_MINUS_AGE', soversion)
112
113 glib_conf.set('_GNU_SOURCE', 1)
114
115 if host_system == 'windows'
116   # Poll doesn't work on devices on Windows
117   glib_conf.set('BROKEN_POLL', true)
118 endif
119
120 if host_system == 'windows' and cc.get_id() != 'msvc'
121   # FIXME: Ideally we shouldn't depend on this on Windows and should use
122   # 64 bit capable Windows API that also works with MSVC.
123   # The autotools build did set this for mingw and while meson sets it
124   # for gcc/clang by default, it doesn't do so on Windows.
125   glib_conf.set('_FILE_OFFSET_BITS', 64)
126 endif
127
128 # Check for GNU visibility attributes
129 g_have_gnuc_visibility = cc.compiles('''
130   void
131   __attribute__ ((visibility ("hidden")))
132        f_hidden (void)
133   {
134   }
135   void
136   __attribute__ ((visibility ("internal")))
137        f_internal (void)
138   {
139   }
140   void
141   __attribute__ ((visibility ("protected")))
142        f_protected (void)
143   {
144   }
145   void
146   __attribute__ ((visibility ("default")))
147        f_default (void)
148   {
149   }
150   int main (void)
151   {
152     f_hidden();
153     f_internal();
154     f_protected();
155     f_default();
156     return 0;
157   }
158   ''',
159   # Not supported by MSVC, but MSVC also won't support visibility,
160   # so it's OK to pass -Werror explicitly. Replace with
161   # override_options : 'werror=true' once that is supported
162   args: ['-Werror'],
163   name : 'GNU C visibility attributes test')
164
165 if g_have_gnuc_visibility
166   glibconfig_conf.set('G_HAVE_GNUC_VISIBILITY', '1')
167 endif
168
169 # Detect and set symbol visibility
170 glib_hidden_visibility_args = []
171 if get_option('default_library') != 'static'
172   if host_system == 'windows' or host_system == 'cygwin'
173     glib_conf.set('DLL_EXPORT', true)
174     if cc.get_id() == 'msvc'
175       glib_conf.set('_GLIB_EXTERN', '__declspec(dllexport) extern')
176     elif cc.has_argument('-fvisibility=hidden')
177       glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) __declspec(dllexport) extern')
178       glib_hidden_visibility_args = ['-fvisibility=hidden']
179     endif
180   elif cc.has_argument('-fvisibility=hidden')
181     glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) extern')
182     glib_hidden_visibility_args = ['-fvisibility=hidden']
183   endif
184 endif
185
186 if host_system == 'windows' and get_option('default_library') == 'static'
187     glibconfig_conf.set('GLIB_STATIC_COMPILATION', '1')
188     glibconfig_conf.set('GOBJECT_STATIC_COMPILATION', '1')
189 endif
190
191 # Cygwin glib port maintainers made it clear
192 # (via the patches they apply) that they want no
193 # part of glib W32 code, therefore we do not define
194 # G_PLATFORM_WIN32 for host_system == 'cygwin'.
195 # This makes G_PLATFORM_WIN32 a synonym for
196 # G_OS_WIN32.
197 if host_system == 'windows'
198   glib_os = '''#define G_OS_WIN32
199 #define G_PLATFORM_WIN32'''
200 elif host_system == 'cygwin'
201   glib_os = '''#define G_OS_UNIX
202 #define G_WITH_CYGWIN'''
203 else
204   glib_os = '#define G_OS_UNIX'
205 endif
206 glibconfig_conf.set('glib_os', glib_os)
207
208 # We need to know the build type to determine what .lib files we need on Visual Studio
209 # for dependencies that don't normally come with pkg-config files for Visual Studio builds
210 buildtype = get_option('buildtype')
211
212 glib_debug_cflags = []
213 if buildtype.startswith('debug')
214   glib_debug_cflags += ['-DG_ENABLE_DEBUG']
215 elif buildtype == 'release'
216   glib_debug_cflags += ['-DG_DISABLE_CAST_CHECKS']
217 endif
218
219 add_project_arguments(glib_debug_cflags, language: 'c')
220
221 # check for header files
222
223 headers = [
224   'alloca.h',
225   'crt_externs.h',
226   'dirent.h', # MSC does not come with this by default
227   'float.h',
228   'fstab.h',
229   'grp.h',
230   'inttypes.h',
231   'limits.h',
232   'linux/magic.h',
233   'locale.h',
234   'mach/mach_time.h',
235   'memory.h',
236   'mntent.h',
237   'poll.h',
238   'pwd.h',
239   'sched.h',
240   'spawn.h',
241   'stdint.h',
242   'stdlib.h',
243   'string.h',
244   'strings.h',
245   'sys/auxv.h',
246   'sys/event.h',
247   'sys/filio.h',
248   'sys/inotify.h',
249   'sys/mkdev.h',
250   'sys/mntctl.h',
251   'sys/mnttab.h',
252   'sys/mount.h',
253   'sys/param.h',
254   'sys/resource.h',
255   'sys/select.h',
256   'sys/statfs.h',
257   'sys/stat.h',
258   'sys/statvfs.h',
259   'sys/sysctl.h',
260   'sys/time.h', # MSC does not come with this by default
261   'sys/times.h',
262   'sys/types.h',
263   'sys/uio.h',
264   'sys/vfs.h',
265   'sys/vfstab.h',
266   'sys/vmount.h',
267   'sys/wait.h',
268   'termios.h',
269   'unistd.h',
270   'values.h',
271   'xlocale.h',
272 ]
273
274 foreach h : headers
275   if cc.has_header(h)
276     define = 'HAVE_' + h.underscorify().to_upper()
277     glib_conf.set(define, 1)
278     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
279   endif
280 endforeach
281
282 # FIXME: Use cc.check_header from Meson 0.47.
283 # FreeBSD includes a malloc.h which always throw compilation error.
284 if cc.compiles('#include <malloc.h>', name : 'malloc.h')
285   glib_conf.set('HAVE_MALLOC_H', 1)
286   glib_conf_prefix = glib_conf_prefix + '#define HAVE_MALLOC_H 1\n'
287 endif
288
289 if cc.has_header('linux/netlink.h')
290   glib_conf.set('HAVE_NETLINK', 1)
291 endif
292
293 if glib_conf.has('HAVE_LOCALE_H')
294   if cc.has_header_symbol('locale.h', 'LC_MESSAGES')
295     glib_conf.set('HAVE_LC_MESSAGES', 1)
296   endif
297 endif
298
299 struct_stat_blkprefix = '''
300 #include <sys/types.h>
301 #include <sys/stat.h>
302 #ifdef HAVE_UNISTD_H
303 #include <unistd.h>
304 #endif
305 #ifdef HAVE_SYS_STATFS_H
306 #include <sys/statfs.h>
307 #endif
308 #ifdef HAVE_SYS_PARAM_H
309 #include <sys/param.h>
310 #endif
311 #ifdef HAVE_SYS_MOUNT_H
312 #include <sys/mount.h>
313 #endif
314 '''
315
316 struct_members = [
317   [ 'stat', 'st_mtimensec' ],
318   [ 'stat', 'st_mtim.tv_nsec' ],
319   [ 'stat', 'st_atimensec' ],
320   [ 'stat', 'st_atim.tv_nsec' ],
321   [ 'stat', 'st_ctimensec' ],
322   [ 'stat', 'st_ctim.tv_nsec' ],
323   [ 'stat', 'st_birthtime' ],
324   [ 'stat', 'st_birthtimensec' ],
325   [ 'stat', 'st_birthtim' ],
326   [ 'stat', 'st_birthtim.tv_nsec' ],
327   [ 'stat', 'st_blksize', struct_stat_blkprefix ],
328   [ 'stat', 'st_blocks', struct_stat_blkprefix ],
329   [ 'statfs', 'f_fstypename', struct_stat_blkprefix ],
330   [ 'statfs', 'f_bavail', struct_stat_blkprefix ],
331   [ 'dirent', 'd_type', '''#include <sys/types.h>
332                            #include <dirent.h>''' ],
333   [ 'statvfs', 'f_basetype', '#include <sys/statvfs.h>' ],
334   [ 'statvfs', 'f_fstypename', '#include <sys/statvfs.h>' ],
335   [ 'tm', 'tm_gmtoff', '#include <time.h>' ],
336   [ 'tm', '__tm_gmtoff', '#include <time.h>' ],
337 ]
338
339 foreach m : struct_members
340   header_check_prefix = glib_conf_prefix
341   if m.length() == 3
342     header_check_prefix = header_check_prefix + m[2]
343   else
344     header_check_prefix = header_check_prefix + '#include <sys/stat.h>'
345   endif
346   if cc.has_member('struct ' + m[0], m[1], prefix : header_check_prefix)
347     define = 'HAVE_STRUCT_@0@_@1@'.format(m[0].to_upper(), m[1].underscorify().to_upper())
348     glib_conf.set(define, 1)
349     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
350   else
351   endif
352 endforeach
353
354 # Compiler flags
355 if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
356   warning_c_args = [
357     '-Wall',
358     '-Wduplicated-branches',
359     '-Wimplicit-fallthrough',
360     '-Wmisleading-indentation',
361     '-Wstrict-prototypes',
362     '-Wunused',
363     # Due to maintained deprecated code, we do not want to see unused parameters
364     '-Wno-unused-parameter',
365     # Due to pervasive use of things like GPOINTER_TO_UINT(), we do not support
366     # building with -Wbad-function-cast.
367     '-Wno-bad-function-cast',
368     '-Wno-cast-function-type',
369     # Due to function casts through (void*) we cannot support -Wpedantic:
370     # https://wiki.gnome.org/Projects/GLib/CompilerRequirements#Function_pointer_conversions.
371     '-Wno-pedantic',
372     '-Werror=declaration-after-statement',
373     '-Werror=format=2',
374     '-Werror=implicit-function-declaration',
375     '-Werror=init-self',
376     '-Werror=missing-include-dirs',
377     '-Werror=missing-prototypes',
378     '-Werror=pointer-arith',
379   ]
380   warning_c_link_args = [
381     '-Wl,-z,nodelete',
382   ]
383   if get_option('bsymbolic_functions')
384     warning_c_link_args += ['-Wl,-Bsymbolic-functions']
385   endif
386 else
387   warning_c_args = []
388   warning_c_link_args = []
389 endif
390
391 add_project_arguments(cc.get_supported_arguments(warning_c_args), language: 'c')
392
393 # FIXME: We cannot build some of the GResource tests with -z nodelete, which
394 # means we cannot use that flag in add_project_link_arguments(), and must add
395 # it to the relevant targets manually. We do the same with -Bsymbolic-functions
396 # because that is what the autotools build did.
397 # See https://github.com/mesonbuild/meson/pull/3520 for a way to eventually
398 # improve this.
399 glib_link_flags = cc.get_supported_link_arguments(warning_c_link_args)
400
401 # Windows Support (7+)
402 if host_system == 'windows'
403   glib_conf.set('_WIN32_WINNT', '0x0601')
404 endif
405
406 functions = [
407   'endmntent',
408   'endservent',
409   'fallocate',
410   'fchmod',
411   'fchown',
412   'fdwalk',
413   'fsync',
414   'getauxval',
415   'getc_unlocked',
416   'getfsstat',
417   'getgrgid_r',
418   'getmntent_r',
419   'getpwuid_r',
420   'getresuid',
421   'getvfsstat',
422   'gmtime_r',
423   'hasmntopt',
424   'inotify_init1',
425   'issetugid',
426   'kevent',
427   'kqueue',
428   'lchmod',
429   'lchown',
430   'link',
431   'localtime_r',
432   'lstat',
433   'mbrtowc',
434   'memalign',
435   'mmap',
436   'newlocale',
437   'pipe2',
438   'poll',
439   'prlimit',
440   'readlink',
441   'recvmmsg',
442   'sendmmsg',
443   'setenv',
444   'setmntent',
445   'strerror_r',
446   'strnlen',
447   'strsignal',
448   'strtod_l',
449   'strtoll_l',
450   'strtoull_l',
451   'symlink',
452   'timegm',
453   'unsetenv',
454   'uselocale',
455   'utimes',
456   'valloc',
457   'vasprintf',
458   'vsnprintf',
459   'wcrtomb',
460   'wcslen',
461   'wcsnlen',
462   'sysctlbyname',
463   '_NSGetEnviron',
464 ]
465
466 if glib_conf.has('HAVE_SYS_STATVFS_H')
467   functions += ['statvfs']
468 else
469   have_func_statvfs = false
470 endif
471 if glib_conf.has('HAVE_SYS_STATFS_H') or glib_conf.has('HAVE_SYS_MOUNT_H')
472   functions += ['statfs']
473 else
474   have_func_statfs = false
475 endif
476
477 if host_system == 'windows'
478   iphlpapi_dep = cc.find_library('iphlpapi')
479   iphlpapi_funcs = ['if_nametoindex', 'if_indextoname']
480   foreach ifunc : iphlpapi_funcs
481     iphl_prefix =  '''#define _WIN32_WINNT @0@
482       #include <winsock2.h>
483       #include <iphlpapi.h>'''.format(glib_conf.get('_WIN32_WINNT'))
484     if cc.has_function(ifunc,
485                        prefix : iphl_prefix,
486                        dependencies : iphlpapi_dep)
487       idefine = 'HAVE_' + ifunc.underscorify().to_upper()
488       glib_conf.set(idefine, 1)
489       glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(idefine)
490       set_variable('have_func_' + ifunc, true)
491     else
492       set_variable('have_func_' + ifunc, false)
493     endif
494   endforeach
495 else
496   functions += ['if_indextoname', 'if_nametoindex']
497 endif
498
499 # AIX splice is something else
500 if host_system != 'aix'
501   functions += ['splice']
502 endif
503
504 foreach f : functions
505   if cc.has_function(f)
506     define = 'HAVE_' + f.underscorify().to_upper()
507     glib_conf.set(define, 1)
508     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
509     set_variable('have_func_' + f, true)
510   else
511     set_variable('have_func_' + f, false)
512   endif
513 endforeach
514
515 # Check that stpcpy() is usable; must use header
516 if cc.has_function('stpcpy', prefix : '#include <string.h>')
517   glib_conf.set('HAVE_STPCPY', 1)
518 endif
519
520 # Check that posix_memalign() is usable; must use header
521 if cc.has_function('posix_memalign', prefix : '#include <stdlib.h>')
522   glib_conf.set('HAVE_POSIX_MEMALIGN', 1)
523 endif
524
525 # Check that posix_spawn() is usable; must use header
526 if cc.has_function('posix_spawn', prefix : '#include <spawn.h>')
527   glib_conf.set('HAVE_POSIX_SPAWN', 1)
528 endif
529
530 # Check whether strerror_r returns char *
531 if have_func_strerror_r
532   if cc.compiles('''#define _GNU_SOURCE
533                     #include <string.h>
534                     int func (void) {
535                       char error_string[256];
536                       char *ptr = strerror_r (-2, error_string, 256);
537                       char c = *strerror_r (-2, error_string, 256);
538                       return c != 0 && ptr != (void*) 0L;
539                     }
540                  ''',
541                  name : 'strerror_r() returns char *')
542     glib_conf.set('STRERROR_R_CHAR_P', 1,
543                   description: 'Defined if strerror_r returns char *')
544   endif
545 endif
546
547 # Special-case these functions that have alternative names on Windows/MSVC
548 if cc.has_function('snprintf') or cc.has_header_symbol('stdio.h', 'snprintf')
549   glib_conf.set('HAVE_SNPRINTF', 1)
550   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF 1\n'
551 elif cc.has_function('_snprintf') or cc.has_header_symbol('stdio.h', '_snprintf')
552   hack_define = '1\n#define snprintf _snprintf'
553   glib_conf.set('HAVE_SNPRINTF', hack_define)
554   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF ' + hack_define
555 endif
556
557 if cc.has_function('strcasecmp')
558   glib_conf.set('HAVE_STRCASECMP', 1)
559   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP 1\n'
560 elif cc.has_function('_stricmp')
561   hack_define = '1\n#define strcasecmp _stricmp'
562   glib_conf.set('HAVE_STRCASECMP', hack_define)
563   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP ' + hack_define
564 endif
565
566 if cc.has_function('strncasecmp')
567   glib_conf.set('HAVE_STRNCASECMP', 1)
568   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP 1\n'
569 elif cc.has_function('_strnicmp')
570   hack_define = '1\n#define strncasecmp _strnicmp'
571   glib_conf.set('HAVE_STRNCASECMP', hack_define)
572   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP ' + hack_define
573 endif
574
575 if cc.has_header_symbol('sys/sysmacros.h', 'major')
576   glib_conf.set('MAJOR_IN_SYSMACROS', 1)
577 elif cc.has_header_symbol('sys/mkdev.h', 'major')
578   glib_conf.set('MAJOR_IN_MKDEV', 1)
579 elif cc.has_header_symbol('sys/types.h', 'major')
580   glib_conf.set('MAJOR_IN_TYPES', 1)
581 endif
582
583 if cc.has_header_symbol('dlfcn.h', 'RTLD_LAZY')
584   glib_conf.set('HAVE_RTLD_LAZY', 1)
585 endif
586
587 if cc.has_header_symbol('dlfcn.h', 'RTLD_NOW')
588   glib_conf.set('HAVE_RTLD_NOW', 1)
589 endif
590
591 if cc.has_header_symbol('dlfcn.h', 'RTLD_GLOBAL')
592   glib_conf.set('HAVE_RTLD_GLOBAL', 1)
593 endif
594
595 have_rtld_next = false
596 if cc.has_header_symbol('dlfcn.h', 'RTLD_NEXT', args: '-D_GNU_SOURCE')
597   have_rtld_next = true
598   glib_conf.set('HAVE_RTLD_NEXT', 1)
599 endif
600
601 # Check whether to use statfs or statvfs
602 # Some systems have both statfs and statvfs, pick the most "native" for these
603 if have_func_statfs and have_func_statvfs
604   # on solaris and irix, statfs doesn't even have the f_bavail field
605   if not glib_conf.has('HAVE_STRUCT_STATFS_F_BAVAIL')
606     have_func_statfs = false
607   else
608     # at least on linux, statfs is the actual syscall
609     have_func_statvfs = false
610   endif
611 endif
612 if have_func_statfs
613   glib_conf.set('USE_STATFS', 1)
614   stat_func_to_use = 'statfs'
615 elif have_func_statvfs
616   glib_conf.set('USE_STATVFS', 1)
617   stat_func_to_use = 'statvfs'
618 else
619   stat_func_to_use = 'neither'
620 endif
621 message('Checking whether to use statfs or statvfs .. ' + stat_func_to_use)
622
623 if host_system == 'linux'
624   if cc.has_function('mkostemp',
625                      prefix: '''#define _GNU_SOURCE
626                                 #include <stdlib.h>''')
627     glib_conf.set('HAVE_MKOSTEMP', 1)
628   endif
629 endif
630
631 osx_ldflags = []
632 glib_have_os_x_9_or_later = false
633 glib_have_carbon = false
634 glib_have_cocoa = false
635 if host_system == 'darwin'
636   add_languages('objc')
637   objcc = meson.get_compiler('objc')
638
639   osx_ldflags += ['-Wl,-framework,CoreFoundation']
640
641   # Mac OS X Carbon support
642   glib_have_carbon = objcc.compiles('''#include <Carbon/Carbon.h>
643                                        #include <CoreServices/CoreServices.h>''',
644                                     name : 'Mac OS X Carbon support')
645
646   if glib_have_carbon
647     glib_conf.set('HAVE_CARBON', true)
648     osx_ldflags += '-Wl,-framework,Carbon'
649     glib_have_os_x_9_or_later = objcc.compiles('''#include <AvailabilityMacros.h>
650                                                   #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
651                                                   #error Compiling for minimum OS X version before 10.9
652                                                   #endif''',
653                                                name : 'OS X 9 or later')
654   endif
655
656   # Mac OS X Cocoa support
657   glib_have_cocoa = objcc.compiles('''#include <Cocoa/Cocoa.h>
658                                       #ifdef GNUSTEP_BASE_VERSION
659                                       #error "Detected GNUstep, not Cocoa"
660                                       #endif''',
661                                    name : 'Mac OS X Cocoa support')
662
663   if glib_have_cocoa
664     glib_conf.set('HAVE_COCOA', true)
665     osx_ldflags += ['-Wl,-framework,Foundation', '-Wl,-framework,AppKit']
666   endif
667
668   # FIXME: libgio mix C and objC source files and there is no way to reliably
669   # know which language flags it's going to use to link. Add to both languages
670   # for now. See https://github.com/mesonbuild/meson/issues/3585.
671   add_project_link_arguments(osx_ldflags, language : ['objc', 'c'])
672 endif
673
674 # Check for futex(2)
675 if cc.links('''#include <linux/futex.h>
676                #include <sys/syscall.h>
677                #include <unistd.h>
678                int main (int argc, char ** argv) {
679                  syscall (__NR_futex, NULL, FUTEX_WAKE, FUTEX_WAIT);
680                  return 0;
681                }''', name : 'futex(2) system call')
682   glib_conf.set('HAVE_FUTEX', 1)
683 endif
684
685 # Check for eventfd(2)
686 if cc.links('''#include <sys/eventfd.h>
687                #include <unistd.h>
688                int main (int argc, char ** argv) {
689                  eventfd (0, EFD_CLOEXEC);
690                  return 0;
691                }''', name : 'eventfd(2) system call')
692   glib_conf.set('HAVE_EVENTFD', 1)
693 endif
694
695 clock_gettime_test_code = '''
696   #include <time.h>
697   struct timespec t;
698   int main (int argc, char ** argv) {
699     return clock_gettime(CLOCK_REALTIME, &t);
700   }'''
701 librt = []
702 if cc.links(clock_gettime_test_code, name : 'clock_gettime')
703   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
704 elif cc.links(clock_gettime_test_code, args : '-lrt', name : 'clock_gettime in librt')
705   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
706   librt = cc.find_library('rt')
707 endif
708
709 # if statfs() takes 2 arguments (Posix) or 4 (Solaris)
710 if have_func_statfs
711   if cc.compiles(glib_conf_prefix + '''
712                  #include <unistd.h>
713                         #ifdef HAVE_SYS_PARAM_H
714                         #include <sys/param.h>
715                         #endif
716                         #ifdef HAVE_SYS_VFS_H
717                         #include <sys/vfs.h>
718                         #endif
719                         #ifdef HAVE_SYS_MOUNT_H
720                         #include <sys/mount.h>
721                         #endif
722                         #ifdef HAVE_SYS_STATFS_H
723                         #include <sys/statfs.h>
724                         #endif
725                         void some_func (void) {
726                           struct statfs st;
727                           statfs("/", &st);
728                         }''', name : 'number of arguments to statfs() (n=2)')
729     glib_conf.set('STATFS_ARGS', 2)
730   elif cc.compiles(glib_conf_prefix + '''
731                    #include <unistd.h>
732                           #ifdef HAVE_SYS_PARAM_H
733                           #include <sys/param.h>
734                           #endif
735                           #ifdef HAVE_SYS_VFS_H
736                           #include <sys/vfs.h>
737                           #endif
738                           #ifdef HAVE_SYS_MOUNT_H
739                           #include <sys/mount.h>
740                           #endif
741                           #ifdef HAVE_SYS_STATFS_H
742                           #include <sys/statfs.h>
743                           #endif
744                           void some_func (void) {
745                             struct statfs st;
746                             statfs("/", &st, sizeof (st), 0);
747                           }''', name : 'number of arguments to statfs() (n=4)')
748     glib_conf.set('STATFS_ARGS', 4)
749   else
750     error('Unable to determine number of arguments to statfs()')
751   endif
752 endif
753
754 # open takes O_DIRECTORY as an option
755 #AC_MSG_CHECKING([])
756 if cc.compiles('''#include <fcntl.h>
757                   #include <sys/types.h>
758                   #include <sys/stat.h>],
759                   void some_func (void) {
760                     open(0, O_DIRECTORY, 0);
761                   }''', name : 'open() option O_DIRECTORY')
762   glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
763 endif
764
765 # Check whether there is a vsnprintf() function with C99 semantics installed.
766 # (similar tests to AC_FUNC_VSNPRINTF_C99)
767 # Check whether there is a snprintf() function with C99 semantics installed.
768 # (similar tests to AC_FUNC_SNPRINTF_C99)
769 # Check whether there is a printf() function with Unix98 semantics installed.
770 # (similar tests to AC_FUNC_PRINTF_UNIX98)
771 have_good_vsnprintf = false
772 have_good_snprintf = false
773 have_good_printf = false
774
775 if host_system == 'windows' and cc.get_id() == 'msvc'
776   # Unfortunately the Visual Studio 2015+ implementations of C99-style
777   # snprintf and vsnprintf don't seem to be quite good enough.
778   # (Sorry, I don't know exactly what is the problem,
779   # but it is related to floating point formatting and decimal point vs. comma.)
780   # The simple tests in AC_FUNC_VSNPRINTF_C99 and AC_FUNC_SNPRINTF_C99 aren't
781   # rigorous enough to notice, though.
782   glib_conf.set('HAVE_C99_SNPRINTF', false)
783   glib_conf.set('HAVE_C99_VSNPRINTF', false)
784   glib_conf.set('HAVE_UNIX98_PRINTF', false)
785 else
786   vsnprintf_c99_test_code = '''
787 #include <stdio.h>
788 #include <stdarg.h>
789
790 int
791 doit(char * s, ...)
792 {
793   char buffer[32];
794   va_list args;
795   int r;
796
797   va_start(args, s);
798   r = vsnprintf(buffer, 5, s, args);
799   va_end(args);
800
801   if (r != 7)
802     exit(1);
803
804   /* AIX 5.1 and Solaris seems to have a half-baked vsnprintf()
805      implementation. The above will return 7 but if you replace
806      the size of the buffer with 0, it borks! */
807   va_start(args, s);
808   r = vsnprintf(buffer, 0, s, args);
809   va_end(args);
810
811   if (r != 7)
812     exit(1);
813
814   exit(0);
815 }
816
817 int
818 main(void)
819 {
820   doit("1234567");
821   exit(1);
822 }'''
823
824   if cc_can_run
825     rres = cc.run(vsnprintf_c99_test_code, name : 'C99 vsnprintf')
826     if rres.compiled() and rres.returncode() == 0
827       glib_conf.set('HAVE_C99_VSNPRINTF', 1)
828       have_good_vsnprintf = true
829     endif
830   else
831       have_good_vsnprintf = meson.get_cross_property('have_c99_vsnprintf', false)
832       glib_conf.set('HAVE_C99_VSNPRINTF', have_good_vsnprintf)
833   endif
834
835   snprintf_c99_test_code = '''
836 #include <stdio.h>
837 #include <stdarg.h>
838
839 int
840 doit()
841 {
842   char buffer[32];
843   va_list args;
844   int r;
845
846   r = snprintf(buffer, 5, "1234567");
847
848   if (r != 7)
849     exit(1);
850
851   r = snprintf(buffer, 0, "1234567");
852
853   if (r != 7)
854     exit(1);
855
856   r = snprintf(NULL, 0, "1234567");
857
858   if (r != 7)
859     exit(1);
860
861   exit(0);
862 }
863
864 int
865 main(void)
866 {
867   doit();
868   exit(1);
869 }'''
870
871   if cc_can_run
872     rres = cc.run(snprintf_c99_test_code, name : 'C99 snprintf')
873     if rres.compiled() and rres.returncode() == 0
874       glib_conf.set('HAVE_C99_SNPRINTF', 1)
875       have_good_snprintf = true
876     endif
877   else
878       have_good_snprintf = meson.get_cross_property('have_c99_snprintf', false)
879       glib_conf.set('HAVE_C99_SNPRINTF', have_good_snprintf)
880   endif
881
882   printf_unix98_test_code = '''
883 #include <stdio.h>
884
885 int
886 main (void)
887 {
888   char buffer[128];
889
890   sprintf (buffer, "%2\$d %3\$d %1\$d", 1, 2, 3);
891   if (strcmp ("2 3 1", buffer) == 0)
892     exit (0);
893   exit (1);
894 }'''
895
896   if cc_can_run
897     rres = cc.run(printf_unix98_test_code, name : 'Unix98 printf positional parameters')
898     if rres.compiled() and rres.returncode() == 0
899       glib_conf.set('HAVE_UNIX98_PRINTF', 1)
900       have_good_printf = true
901     endif
902   else
903       have_good_printf = meson.get_cross_property('have_unix98_printf', false)
904       glib_conf.set('HAVE_UNIX98_PRINTF', have_good_printf)
905   endif
906 endif
907
908 if host_system == 'windows'
909   glib_conf.set_quoted('EXEEXT', '.exe')
910 else
911   glib_conf.set('EXEEXT', '')
912 endif
913
914 if have_good_vsnprintf and have_good_snprintf and have_good_printf
915   # Our printf is 'good' only if vsnpintf()/snprintf()/printf() supports C99 well enough
916   glib_conf.set('HAVE_GOOD_PRINTF', 1)
917 else
918   glib_conf.set('HAVE_VASPRINTF', 1)
919 endif
920
921 glibconfig_conf.set('GLIB_USING_SYSTEM_PRINTF',
922                     have_good_vsnprintf and have_good_snprintf and have_good_printf)
923
924 # Check whether the printf() family supports Unix98 %n$ positional parameters
925 # AC_FUNC_PRINTF_UNIX98
926 # Nothing uses HAVE_UNIX98_PRINTF
927
928
929 # Check for nl_langinfo and CODESET
930 if cc.links('''#include <langinfo.h>
931                int main (int argc, char ** argv) {
932                  char *codeset = nl_langinfo (CODESET);
933                  return 0;
934                }''', name : 'nl_langinfo and CODESET')
935   glib_conf.set('HAVE_LANGINFO_CODESET', 1)
936   glib_conf.set('HAVE_CODESET', 1)
937 endif
938
939 # Check for nl_langinfo and LC_TIME parts that are needed in gdatetime.c
940 if cc.links('''#include <langinfo.h>
941                int main (int argc, char ** argv) {
942                  char *str;
943                  str = nl_langinfo (PM_STR);
944                  str = nl_langinfo (D_T_FMT);
945                  str = nl_langinfo (D_FMT);
946                  str = nl_langinfo (T_FMT);
947                  str = nl_langinfo (T_FMT_AMPM);
948                  str = nl_langinfo (MON_1);
949                  str = nl_langinfo (ABMON_12);
950                  str = nl_langinfo (DAY_1);
951                  str = nl_langinfo (ABDAY_7);
952                  return 0;
953                }''', name : 'nl_langinfo (PM_STR)')
954   glib_conf.set('HAVE_LANGINFO_TIME', 1)
955 endif
956 if cc.links('''#include <langinfo.h>
957                int main (int argc, char ** argv) {
958                  char *str;
959                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB);
960                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT1_MB);
961                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT2_MB);
962                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT3_MB);
963                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT4_MB);
964                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT5_MB);
965                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT6_MB);
966                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT7_MB);
967                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT8_MB);
968                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT9_MB);
969                  return 0;
970                }''', name : 'nl_langinfo (_NL_CTYPE_OUTDIGITn_MB)')
971   glib_conf.set('HAVE_LANGINFO_OUTDIGIT', 1)
972 endif
973
974 # Check for nl_langinfo and alternative month names
975 if cc.links('''#ifndef _GNU_SOURCE
976               # define _GNU_SOURCE
977               #endif
978               #include <langinfo.h>
979                int main (int argc, char ** argv) {
980                  char *str;
981                  str = nl_langinfo (ALTMON_1);
982                  str = nl_langinfo (ALTMON_2);
983                  str = nl_langinfo (ALTMON_3);
984                  str = nl_langinfo (ALTMON_4);
985                  str = nl_langinfo (ALTMON_5);
986                  str = nl_langinfo (ALTMON_6);
987                  str = nl_langinfo (ALTMON_7);
988                  str = nl_langinfo (ALTMON_8);
989                  str = nl_langinfo (ALTMON_9);
990                  str = nl_langinfo (ALTMON_10);
991                  str = nl_langinfo (ALTMON_11);
992                  str = nl_langinfo (ALTMON_12);
993                  return 0;
994                }''', name : 'nl_langinfo (ALTMON_n)')
995   glib_conf.set('HAVE_LANGINFO_ALTMON', 1)
996 endif
997
998 # Check for nl_langinfo and abbreviated alternative month names
999 if cc.links('''#ifndef _GNU_SOURCE
1000               # define _GNU_SOURCE
1001               #endif
1002               #include <langinfo.h>
1003                int main (int argc, char ** argv) {
1004                  char *str;
1005                  str = nl_langinfo (_NL_ABALTMON_1);
1006                  str = nl_langinfo (_NL_ABALTMON_2);
1007                  str = nl_langinfo (_NL_ABALTMON_3);
1008                  str = nl_langinfo (_NL_ABALTMON_4);
1009                  str = nl_langinfo (_NL_ABALTMON_5);
1010                  str = nl_langinfo (_NL_ABALTMON_6);
1011                  str = nl_langinfo (_NL_ABALTMON_7);
1012                  str = nl_langinfo (_NL_ABALTMON_8);
1013                  str = nl_langinfo (_NL_ABALTMON_9);
1014                  str = nl_langinfo (_NL_ABALTMON_10);
1015                  str = nl_langinfo (_NL_ABALTMON_11);
1016                  str = nl_langinfo (_NL_ABALTMON_12);
1017                  return 0;
1018                }''', name : 'nl_langinfo (_NL_ABALTMON_n)')
1019   glib_conf.set('HAVE_LANGINFO_ABALTMON', 1)
1020 endif
1021
1022 # Check if C compiler supports the 'signed' keyword
1023 if not cc.compiles('''signed char x;''', name : 'signed')
1024   glib_conf.set('signed', '/* NOOP */')
1025 endif
1026
1027 # Check if the ptrdiff_t type exists
1028 if cc.has_header_symbol('stddef.h', 'ptrdiff_t')
1029   glib_conf.set('HAVE_PTRDIFF_T', 1)
1030 endif
1031
1032 # Check for sig_atomic_t type
1033 if cc.links('''#include <signal.h>
1034                #include <sys/types.h>
1035                sig_atomic_t val = 42;
1036                int main (int argc, char ** argv) {
1037                  return val == 42 ? 0 : 1;
1038                }''', name : 'sig_atomic_t')
1039   glib_conf.set('HAVE_SIG_ATOMIC_T', 1)
1040 endif
1041
1042 # Check if 'long long' works
1043 # jm_AC_TYPE_LONG_LONG
1044 if cc.compiles('''long long ll = 1LL;
1045                   int i = 63;
1046                   int some_func (void) {
1047                     long long llmax = (long long) -1;
1048                     return ll << i | ll >> i | llmax / ll | llmax % ll;
1049                   }''', name : 'long long')
1050   glib_conf.set('HAVE_LONG_LONG', 1)
1051   have_long_long = true
1052 else
1053   have_long_long = false
1054 endif
1055
1056 # Test whether the compiler supports the 'long double' type.
1057 if cc.compiles('''/* The Stardent Vistra knows sizeof(long double), but does not support it.  */
1058                   long double foo = 0.0;
1059                   /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
1060                   int array [2*(sizeof(long double) >= sizeof(double)) - 1];''',
1061                name : 'long double')
1062   glib_conf.set('HAVE_LONG_DOUBLE', 1)
1063 endif
1064
1065 # Test whether <stddef.h> has the 'wchar_t' type.
1066 if cc.has_header_symbol('stddef.h', 'wchar_t')
1067   glib_conf.set('HAVE_WCHAR_T', 1)
1068 endif
1069
1070 # Test whether <wchar.h> has the 'wint_t' type.
1071 if cc.has_header_symbol('wchar.h', 'wint_t')
1072   glib_conf.set('HAVE_WINT_T', 1)
1073 endif
1074
1075 found_uintmax_t = false
1076
1077 # Define HAVE_INTTYPES_H_WITH_UINTMAX if <inttypes.h> exists,
1078 # doesn't clash with <sys/types.h>, and declares uintmax_t.
1079 # jm_AC_HEADER_INTTYPES_H
1080 if cc.compiles('''#include <sys/types.h>
1081                   #include <inttypes.h>
1082                   void some_func (void) {
1083                     uintmax_t i = (uintmax_t) -1;
1084                   }''', name : 'uintmax_t in inttypes.h')
1085   glib_conf.set('HAVE_INTTYPES_H_WITH_UINTMAX', 1)
1086   found_uintmax_t = true
1087 endif
1088
1089 # Define HAVE_STDINT_H_WITH_UINTMAX if <stdint.h> exists,
1090 # doesn't clash with <sys/types.h>, and declares uintmax_t.
1091 # jm_AC_HEADER_STDINT_H
1092 if cc.compiles('''#include <sys/types.h>
1093                   #include <stdint.h>
1094                   void some_func (void) {
1095                     uintmax_t i = (uintmax_t) -1;
1096                   }''', name : 'uintmax_t in stdint.h')
1097   glib_conf.set('HAVE_STDINT_H_WITH_UINTMAX', 1)
1098   found_uintmax_t = true
1099 endif
1100
1101 # Define intmax_t to 'long' or 'long long'
1102 # if it is not already defined in <stdint.h> or <inttypes.h>.
1103 # For simplicity, we assume that a header file defines 'intmax_t' if and
1104 # only if it defines 'uintmax_t'.
1105 if found_uintmax_t
1106   glib_conf.set('HAVE_INTMAX_T', 1)
1107 elif have_long_long
1108   glib_conf.set('intmax_t', 'long long')
1109 else
1110   glib_conf.set('intmax_t', 'long')
1111 endif
1112
1113 char_size = cc.sizeof('char')
1114 short_size = cc.sizeof('short')
1115 int_size = cc.sizeof('int')
1116 voidp_size = cc.sizeof('void*')
1117 long_size = cc.sizeof('long')
1118 if have_long_long
1119   long_long_size = cc.sizeof('long long')
1120 else
1121   long_long_size = 0
1122 endif
1123 sizet_size = cc.sizeof('size_t')
1124 if cc.get_id() == 'msvc'
1125   ssizet_size = cc.sizeof('SSIZE_T', prefix : '#include <BaseTsd.h>')
1126 else
1127   ssizet_size = cc.sizeof('ssize_t')
1128 endif
1129
1130 # Some platforms (Apple) hard-code int64_t to long long instead of
1131 # using long on 64-bit architectures. This can cause type mismatch
1132 # warnings when trying to interface with code using the standard
1133 # library type. Test for the warnings and set gint64 to whichever
1134 # works.
1135 if long_long_size == long_size
1136   if cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
1137                     #pragma options langlvl=stdc99
1138                     #endif
1139                     #pragma GCC diagnostic error "-Wincompatible-pointer-types"
1140                     #include <stdint.h>
1141                     #include <stdio.h>
1142                     int main () {
1143                       int64_t i1 = 1;
1144                       long *i2 = &i1;
1145                       return 1;
1146                     }''', name : 'int64_t is long')
1147     int64_t_typedef = 'long'
1148   elif cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
1149                       #pragma options langlvl=stdc99
1150                       #endif
1151                       #pragma GCC diagnostic error "-Wincompatible-pointer-types"
1152                       #include <stdint.h>
1153                       #include <stdio.h>
1154                       int main () {
1155                         int64_t i1 = 1;
1156                         long long *i2 = &i1;
1157                         return 1;
1158                       }''', name : 'int64_t is long long')
1159     int64_t_typedef = 'long long'
1160   endif
1161 endif
1162
1163 int64_m = 'll'
1164 char_align = cc.alignment('char')
1165 short_align = cc.alignment('short')
1166 int_align = cc.alignment('int')
1167 voidp_align = cc.alignment('void*')
1168 long_align = cc.alignment('long')
1169 long_long_align = cc.alignment('long long')
1170 # NOTE: We don't check for size of __int64 because long long is guaranteed to
1171 # be 64-bit in C99, and it is available on all supported compilers
1172 sizet_align = cc.alignment('size_t')
1173
1174 glib_conf.set('ALIGNOF_UNSIGNED_LONG', long_align)
1175
1176 glib_conf.set('SIZEOF_CHAR', char_size)
1177 glib_conf.set('SIZEOF_INT', int_size)
1178 glib_conf.set('SIZEOF_SHORT', short_size)
1179 glib_conf.set('SIZEOF_LONG', long_size)
1180 glib_conf.set('SIZEOF_LONG_LONG', long_long_size)
1181 glib_conf.set('SIZEOF_SIZE_T', sizet_size)
1182 glib_conf.set('SIZEOF_SSIZE_T', ssizet_size)
1183 glib_conf.set('SIZEOF_VOID_P', voidp_size)
1184
1185 if short_size == 2
1186   gint16 = 'short'
1187   gint16_modifier='h'
1188   gint16_format='hi'
1189   guint16_format='hu'
1190 elif int_size == 2
1191   gint16 = 'int'
1192   gint16_modifier=''
1193   gint16_format='i'
1194   guint16_format='u'
1195 else
1196   error('Compiler provides no native 16-bit integer type')
1197 endif
1198 glibconfig_conf.set('gint16', gint16)
1199 glibconfig_conf.set_quoted('gint16_modifier', gint16_modifier)
1200 glibconfig_conf.set_quoted('gint16_format', gint16_format)
1201 glibconfig_conf.set_quoted('guint16_format', guint16_format)
1202
1203 if short_size == 4
1204   gint32 = 'short'
1205   gint32_modifier='h'
1206   gint32_format='hi'
1207   guint32_format='hu'
1208   guint32_align = short_align
1209 elif int_size == 4
1210   gint32 = 'int'
1211   gint32_modifier=''
1212   gint32_format='i'
1213   guint32_format='u'
1214   guint32_align = int_align
1215 elif long_size == 4
1216   gint32 = 'long'
1217   gint32_modifier='l'
1218   gint32_format='li'
1219   guint32_format='lu'
1220   guint32_align = long_align
1221 else
1222   error('Compiler provides no native 32-bit integer type')
1223 endif
1224 glibconfig_conf.set('gint32', gint32)
1225 glibconfig_conf.set_quoted('gint32_modifier', gint32_modifier)
1226 glibconfig_conf.set_quoted('gint32_format', gint32_format)
1227 glibconfig_conf.set_quoted('guint32_format', guint32_format)
1228 glib_conf.set('ALIGNOF_GUINT32', guint32_align)
1229
1230 if int_size == 8
1231   gint64 = 'int'
1232   gint64_modifier=''
1233   gint64_format='i'
1234   guint64_format='u'
1235   glib_extension=''
1236   gint64_constant='(val)'
1237   guint64_constant='(val)'
1238   guint64_align = int_align
1239 elif long_size == 8 and (long_long_size != long_size or int64_t_typedef == 'long')
1240   gint64 = 'long'
1241   glib_extension=''
1242   gint64_modifier='l'
1243   gint64_format='li'
1244   guint64_format='lu'
1245   gint64_constant='(val##L)'
1246   guint64_constant='(val##UL)'
1247   guint64_align = long_align
1248 elif long_long_size == 8 and (long_long_size != long_size or int64_t_typedef == 'long long')
1249   gint64 = 'long long'
1250   glib_extension='G_GNUC_EXTENSION '
1251   gint64_modifier=int64_m
1252   gint64_format=int64_m + 'i'
1253   guint64_format=int64_m + 'u'
1254   gint64_constant='(G_GNUC_EXTENSION (val##LL))'
1255   guint64_constant='(G_GNUC_EXTENSION (val##ULL))'
1256   guint64_align = long_long_align
1257 else
1258   error('Compiler provides no native 64-bit integer type')
1259 endif
1260 glibconfig_conf.set('glib_extension', glib_extension)
1261 glibconfig_conf.set('gint64', gint64)
1262 glibconfig_conf.set_quoted('gint64_modifier', gint64_modifier)
1263 glibconfig_conf.set_quoted('gint64_format', gint64_format)
1264 glibconfig_conf.set_quoted('guint64_format', guint64_format)
1265 glibconfig_conf.set('gint64_constant', gint64_constant)
1266 glibconfig_conf.set('guint64_constant', guint64_constant)
1267 glib_conf.set('ALIGNOF_GUINT64', guint64_align)
1268
1269 if host_system == 'windows'
1270   glibconfig_conf.set('g_pid_type', 'void*')
1271   glibconfig_conf.set_quoted('g_pid_format', 'p')
1272   if host_machine.cpu_family() == 'x86_64'
1273     glibconfig_conf.set_quoted('g_pollfd_format', '%#' + int64_m + 'x')
1274   else
1275     glibconfig_conf.set_quoted('g_pollfd_format', '%#x')
1276   endif
1277   glibconfig_conf.set('g_dir_separator', '\\\\')
1278   glibconfig_conf.set('g_searchpath_separator', ';')
1279 else
1280   glibconfig_conf.set('g_pid_type', 'int')
1281   glibconfig_conf.set_quoted('g_pid_format', 'i')
1282   glibconfig_conf.set_quoted('g_pollfd_format', '%d')
1283   glibconfig_conf.set('g_dir_separator', '/')
1284   glibconfig_conf.set('g_searchpath_separator', ':')
1285 endif
1286
1287 if sizet_size == short_size
1288   glibconfig_conf.set('glib_size_type_define', 'short')
1289   glibconfig_conf.set_quoted('gsize_modifier', 'h')
1290   glibconfig_conf.set_quoted('gssize_modifier', 'h')
1291   glibconfig_conf.set_quoted('gsize_format', 'hu')
1292   glibconfig_conf.set_quoted('gssize_format', 'hi')
1293   glibconfig_conf.set('glib_msize_type', 'SHRT')
1294 elif sizet_size == int_size
1295   glibconfig_conf.set('glib_size_type_define', 'int')
1296   glibconfig_conf.set_quoted('gsize_modifier', '')
1297   glibconfig_conf.set_quoted('gssize_modifier', '')
1298   glibconfig_conf.set_quoted('gsize_format', 'u')
1299   glibconfig_conf.set_quoted('gssize_format', 'i')
1300   glibconfig_conf.set('glib_msize_type', 'INT')
1301 elif sizet_size == long_size
1302   glibconfig_conf.set('glib_size_type_define', 'long')
1303   glibconfig_conf.set_quoted('gsize_modifier', 'l')
1304   glibconfig_conf.set_quoted('gssize_modifier', 'l')
1305   glibconfig_conf.set_quoted('gsize_format', 'lu')
1306   glibconfig_conf.set_quoted('gssize_format', 'li')
1307   glibconfig_conf.set('glib_msize_type', 'LONG')
1308 elif sizet_size == long_long_size
1309   glibconfig_conf.set('glib_size_type_define', 'long long')
1310   glibconfig_conf.set_quoted('gsize_modifier', int64_m)
1311   glibconfig_conf.set_quoted('gssize_modifier', int64_m)
1312   glibconfig_conf.set_quoted('gsize_format', int64_m + 'u')
1313   glibconfig_conf.set_quoted('gssize_format', int64_m + 'i')
1314   glibconfig_conf.set('glib_msize_type', 'INT64')
1315 else
1316   error('Could not determine size of size_t.')
1317 endif
1318
1319 if voidp_size == int_size
1320   glibconfig_conf.set('glib_intptr_type_define', 'int')
1321   glibconfig_conf.set_quoted('gintptr_modifier', '')
1322   glibconfig_conf.set_quoted('gintptr_format', 'i')
1323   glibconfig_conf.set_quoted('guintptr_format', 'u')
1324   glibconfig_conf.set('glib_gpi_cast', '(gint)')
1325   glibconfig_conf.set('glib_gpui_cast', '(guint)')
1326 elif voidp_size == long_size
1327   glibconfig_conf.set('glib_intptr_type_define', 'long')
1328   glibconfig_conf.set_quoted('gintptr_modifier', 'l')
1329   glibconfig_conf.set_quoted('gintptr_format', 'li')
1330   glibconfig_conf.set_quoted('guintptr_format', 'lu')
1331   glibconfig_conf.set('glib_gpi_cast', '(glong)')
1332   glibconfig_conf.set('glib_gpui_cast', '(gulong)')
1333 elif voidp_size == long_long_size
1334   glibconfig_conf.set('glib_intptr_type_define', 'long long')
1335   glibconfig_conf.set_quoted('gintptr_modifier', int64_m)
1336   glibconfig_conf.set_quoted('gintptr_format', int64_m + 'i')
1337   glibconfig_conf.set_quoted('guintptr_format', int64_m + 'u')
1338   glibconfig_conf.set('glib_gpi_cast', '(gint64)')
1339   glibconfig_conf.set('glib_gpui_cast', '(guint64)')
1340 else
1341   error('Could not determine size of void *')
1342 endif
1343
1344 if long_size != 8 and long_long_size != 8 and int_size != 8
1345   error('GLib requires a 64-bit type. You might want to consider using the GNU C compiler.')
1346 endif
1347
1348 glibconfig_conf.set('gintbits', int_size * 8)
1349 glibconfig_conf.set('glongbits', long_size * 8)
1350 glibconfig_conf.set('gsizebits', sizet_size * 8)
1351 glibconfig_conf.set('gssizebits', ssizet_size * 8)
1352
1353 # XXX: https://gitlab.gnome.org/GNOME/glib/issues/1413
1354 if host_system == 'windows'
1355   g_module_suffix = 'dll'
1356 else
1357   g_module_suffix = 'so'
1358 endif
1359 glibconfig_conf.set('g_module_suffix', g_module_suffix)
1360
1361 glibconfig_conf.set('GLIB_MAJOR_VERSION', major_version)
1362 glibconfig_conf.set('GLIB_MINOR_VERSION', minor_version)
1363 glibconfig_conf.set('GLIB_MICRO_VERSION', micro_version)
1364 glibconfig_conf.set('GLIB_VERSION', glib_version)
1365
1366 glibconfig_conf.set('glib_void_p', voidp_size)
1367 glibconfig_conf.set('glib_long', long_size)
1368 glibconfig_conf.set('glib_size_t', sizet_size)
1369 glibconfig_conf.set('glib_ssize_t', ssizet_size)
1370 if host_machine.endian() == 'big'
1371   glibconfig_conf.set('g_byte_order', 'G_BIG_ENDIAN')
1372   glibconfig_conf.set('g_bs_native', 'BE')
1373   glibconfig_conf.set('g_bs_alien', 'LE')
1374 else
1375   glibconfig_conf.set('g_byte_order', 'G_LITTLE_ENDIAN')
1376   glibconfig_conf.set('g_bs_native', 'LE')
1377   glibconfig_conf.set('g_bs_alien', 'BE')
1378 endif
1379
1380 # === va_copy checks ===
1381 # we currently check for all three va_copy possibilities, so we get
1382 # all results in config.log for bug reports.
1383
1384 va_copy_func = ''
1385 foreach try_func : [ '__va_copy', 'va_copy' ]
1386   if cc.compiles('''#include <stdarg.h>
1387                     #include <stdlib.h>
1388                     #ifdef _MSC_VER
1389                     # include "msvc_recommended_pragmas.h"
1390                     #endif
1391                     void f (int i, ...) {
1392                     va_list args1, args2;
1393                     va_start (args1, i);
1394                     @0@ (args2, args1);
1395                     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1396                       exit (1);
1397                     va_end (args1); va_end (args2);
1398                     }
1399                     int main() {
1400                       f (0, 42);
1401                       return 0;
1402                     }'''.format(try_func),
1403                     name : try_func + ' check')
1404     va_copy_func = try_func
1405   endif
1406 endforeach
1407 if va_copy_func != ''
1408   glib_conf.set('G_VA_COPY', va_copy_func)
1409   glib_vacopy = '#define G_VA_COPY ' + va_copy_func
1410 else
1411   glib_vacopy = '/* #undef G_VA_COPY */'
1412 endif
1413
1414 va_list_val_copy_prog = '''
1415   #include <stdarg.h>
1416   #include <stdlib.h>
1417   void f (int i, ...) {
1418     va_list args1, args2;
1419     va_start (args1, i);
1420     args2 = args1;
1421     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1422       exit (1);
1423     va_end (args1); va_end (args2);
1424   }
1425   int main() {
1426     f (0, 42);
1427     return 0;
1428   }'''
1429
1430 if cc_can_run
1431   rres = cc.run(va_list_val_copy_prog, name : 'va_lists can be copied as values')
1432   glib_va_val_copy = rres.returncode() == 0
1433 else
1434   glib_va_val_copy = meson.get_cross_property('va_val_copy', true)
1435 endif
1436 if not glib_va_val_copy
1437   glib_vacopy = glib_vacopy + '\n#define G_VA_COPY_AS_ARRAY 1'
1438   glib_conf.set('G_VA_COPY_AS_ARRAY', 1)
1439 endif
1440 glibconfig_conf.set('glib_vacopy', glib_vacopy)
1441
1442 # check for flavours of varargs macros
1443 g_have_iso_c_varargs = cc.compiles('''
1444   void some_func (void) {
1445     int a(int p1, int p2, int p3);
1446     #define call_a(...) a(1,__VA_ARGS__)
1447     call_a(2,3);
1448   }''', name : 'ISO C99 varargs macros in C')
1449
1450 if g_have_iso_c_varargs
1451   glibconfig_conf.set('g_have_iso_c_varargs', '''
1452 #ifndef __cplusplus
1453 # define G_HAVE_ISO_VARARGS 1
1454 #endif''')
1455 endif
1456
1457 g_have_iso_cxx_varargs = cxx.compiles('''
1458   void some_func (void) {
1459     int a(int p1, int p2, int p3);
1460     #define call_a(...) a(1,__VA_ARGS__)
1461     call_a(2,3);
1462   }''', name : 'ISO C99 varargs macros in C++')
1463
1464 if g_have_iso_cxx_varargs
1465   glibconfig_conf.set('g_have_iso_cxx_varargs', '''
1466 #ifdef __cplusplus
1467 # define G_HAVE_ISO_VARARGS 1
1468 #endif''')
1469 endif
1470
1471 g_have_gnuc_varargs = cc.compiles('''
1472   void some_func (void) {
1473     int a(int p1, int p2, int p3);
1474     #define call_a(params...) a(1,params)
1475     call_a(2,3);
1476   }''', name : 'GNUC varargs macros')
1477
1478 if cc.has_header('alloca.h')
1479   glibconfig_conf.set('GLIB_HAVE_ALLOCA_H', true)
1480 endif
1481 has_syspoll = cc.has_header('sys/poll.h')
1482 has_systypes = cc.has_header('sys/types.h')
1483 if has_syspoll
1484   glibconfig_conf.set('GLIB_HAVE_SYS_POLL_H', true)
1485 endif
1486 has_winsock2 = cc.has_header('winsock2.h')
1487
1488 if has_syspoll and has_systypes
1489   poll_includes = '''
1490       #include<sys/poll.h>
1491       #include<sys/types.h>'''
1492 elif has_winsock2
1493   poll_includes = '''
1494       #define _WIN32_WINNT @0@
1495       #include <winsock2.h>'''.format(glib_conf.get('_WIN32_WINNT'))
1496 else
1497   # FIXME?
1498   error('FIX POLL* defines')
1499 endif
1500
1501 poll_defines = [
1502   [ 'POLLIN', 'g_pollin', 1 ],
1503   [ 'POLLOUT', 'g_pollout', 4 ],
1504   [ 'POLLPRI', 'g_pollpri', 2 ],
1505   [ 'POLLERR', 'g_pollerr', 8 ],
1506   [ 'POLLHUP', 'g_pollhup', 16 ],
1507   [ 'POLLNVAL', 'g_pollnval', 32 ],
1508 ]
1509
1510 if has_syspoll and has_systypes
1511   foreach d : poll_defines
1512     val = cc.compute_int(d[0], prefix: poll_includes)
1513     glibconfig_conf.set(d[1], val)
1514   endforeach
1515 elif has_winsock2
1516   # Due to a missed bug in configure.ac the poll test
1517   # never succeeded on Windows and used some pre-defined
1518   # values as a fallback. Keep using them to maintain
1519   # ABI compatibility with autotools builds of glibs
1520   # and with *any* glib-using code compiled against them,
1521   # since these values end up in a public header glibconfig.h.
1522   foreach d : poll_defines
1523     glibconfig_conf.set(d[1], d[2])
1524   endforeach
1525 endif
1526
1527 # Internet address families
1528 # FIXME: what about Cygwin (G_WITH_CYGWIN)
1529 if host_system == 'windows'
1530   inet_includes = '''
1531       #include <winsock2.h>'''
1532 else
1533   inet_includes = '''
1534       #include <sys/types.h>
1535       #include <sys/socket.h>'''
1536 endif
1537
1538 inet_defines = [
1539   [ 'AF_UNIX', 'g_af_unix' ],
1540   [ 'AF_INET', 'g_af_inet' ],
1541   [ 'AF_INET6', 'g_af_inet6' ],
1542   [ 'MSG_OOB', 'g_msg_oob' ],
1543   [ 'MSG_PEEK', 'g_msg_peek' ],
1544   [ 'MSG_DONTROUTE', 'g_msg_dontroute' ],
1545 ]
1546 foreach d : inet_defines
1547   val = cc.compute_int(d[0], prefix: inet_includes)
1548   glibconfig_conf.set(d[1], val)
1549 endforeach
1550
1551 # We need a more robust approach here...
1552 host_cpu_family = host_machine.cpu_family()
1553 if host_cpu_family == 'x86' or host_cpu_family == 'x86_64' or host_cpu_family == 's390' or host_cpu_family == 's390x' or host_cpu_family.startswith('arm') or host_cpu_family == 'aarch64' or host_cpu_family.startswith('crisv32') or host_cpu_family.startswith('etrax')
1554   glib_memory_barrier_needed = false
1555 elif host_cpu_family.startswith('sparc') or host_cpu_family.startswith('alpha') or host_cpu_family.startswith('powerpc') or host_cpu_family == 'ia64'
1556   glib_memory_barrier_needed = true
1557 else
1558   warning('Unknown host cpu: ' + host_cpu_family)
1559   glib_memory_barrier_needed = true
1560 endif
1561 glibconfig_conf.set('G_ATOMIC_OP_MEMORY_BARRIER_NEEDED', glib_memory_barrier_needed)
1562
1563 # We need to decide at configure time if GLib will use real atomic
1564 # operations ("lock free") or emulated ones with a mutex.  This is
1565 # because we must put this information in glibconfig.h so we know if
1566 # it is safe or not to inline using compiler intrinsics directly from
1567 # the header.
1568 #
1569 # We also publish the information via G_ATOMIC_LOCK_FREE in case the
1570 # user is interested in knowing if they can use the atomic ops across
1571 # processes.
1572 #
1573 # We can currently support the atomic ops natively when building GLib
1574 # with recent versions of GCC or MSVC.
1575 #
1576 # Note that the atomic ops are only available with GCC on x86 when
1577 # using -march=i486 or higher.  If we detect that the atomic ops are
1578 # not available but would be available given the right flags, we want
1579 # to abort and advise the user to fix their CFLAGS.  It's better to do
1580 # that then to silently fall back on emulated atomic ops just because
1581 # the user had the wrong build environment.
1582 atomictest = '''int main() {
1583   volatile int atomic = 2;
1584   __sync_bool_compare_and_swap (&atomic, 2, 3);
1585   return 0;
1586 }
1587 '''
1588
1589 atomicdefine = '''
1590 #ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1591 #error "compiler has atomic ops, but doesn't define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"
1592 #endif
1593 '''
1594
1595 # We know that we can always use real ("lock free") atomic operations with MSVC
1596 if cc.get_id() == 'msvc' or cc.links(atomictest, name : 'atomic ops')
1597   have_atomic_lock_free = true
1598   if (host_system == 'android' or host_system == 'linux') and not cc.compiles(atomicdefine, name : 'atomic ops define')
1599     # When building for armv5 on Linux, gcc provides
1600     # __sync_bool_compare_and_swap but doesn't define
1601     # __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1602     glib_conf.set('__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4', true)
1603   endif
1604 else
1605   have_atomic_lock_free = false
1606   if host_machine.cpu_family() == 'x86' and cc.links(atomictest, args : '-march=i486')
1607     error('GLib must be built with -march=i486 or later.')
1608   endif
1609 endif
1610 glibconfig_conf.set('G_ATOMIC_LOCK_FREE', have_atomic_lock_free)
1611
1612 # === Threads ===
1613
1614 # Determination of thread implementation
1615 if host_system == 'windows' and not get_option('force_posix_threads')
1616   thread_dep = []
1617   threads_implementation = 'win32'
1618   glibconfig_conf.set('g_threads_impl_def', 'WIN32')
1619   glib_conf.set('THREADS_WIN32', 1)
1620 else
1621   thread_dep = dependency('threads')
1622   threads_implementation = 'posix'
1623   pthread_prefix = '''
1624       #ifndef _GNU_SOURCE
1625       # define _GNU_SOURCE
1626       #endif
1627       #include <pthread.h>'''
1628   glibconfig_conf.set('g_threads_impl_def', 'POSIX')
1629   glib_conf.set('THREADS_POSIX', 1)
1630   if cc.has_header_symbol('pthread.h', 'pthread_attr_setstacksize')
1631     glib_conf.set('HAVE_PTHREAD_ATTR_SETSTACKSIZE', 1)
1632   endif
1633   if cc.has_header_symbol('pthread.h', 'pthread_condattr_setclock')
1634     glib_conf.set('HAVE_PTHREAD_CONDATTR_SETCLOCK', 1)
1635   endif
1636   if cc.has_header_symbol('pthread.h', 'pthread_cond_timedwait_relative_np')
1637     glib_conf.set('HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP', 1)
1638   endif
1639   if cc.has_header_symbol('pthread.h', 'pthread_getname_np', prefix : pthread_prefix)
1640     glib_conf.set('HAVE_PTHREAD_GETNAME_NP', 1)
1641   endif
1642   # Assume that pthread_setname_np is available in some form; same as configure
1643   if cc.links(pthread_prefix + '''
1644               int main() {
1645                 pthread_setname_np("example");
1646                 return 0;
1647               }''',
1648               name : 'pthread_setname_np(const char*)',
1649               dependencies : thread_dep)
1650     # macOS and iOS
1651     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID', 1)
1652   elif cc.links(pthread_prefix + '''
1653                 int main() {
1654                   pthread_setname_np(pthread_self(), "example");
1655                   return 0;
1656                 }''',
1657                 name : 'pthread_setname_np(pthread_t, const char*)',
1658                 dependencies : thread_dep)
1659     # Linux, Solaris, etc.
1660     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITH_TID', 1)
1661   endif
1662 endif
1663
1664 # FIXME: we should make it print the result and always return 0, so that
1665 # the output in meson shows up as green
1666 stack_grows_check_prog = '''
1667   volatile int *a = 0, *b = 0;
1668   void f (int i) {
1669     volatile int x = 5;
1670     if (i == 0)
1671       b = &x;
1672     else
1673       f (i - 1);
1674   }
1675   int main () {
1676     volatile int y = 7;
1677     a = &y;
1678     f (100);
1679     return b > a ? 0 : 1;
1680   }'''
1681
1682 if cc_can_run
1683   rres = cc.run(stack_grows_check_prog, name : 'stack grows check')
1684   growing_stack = rres.returncode() == 0
1685 else
1686   growing_stack = meson.get_cross_property('growing_stack', false)
1687 endif
1688
1689 glibconfig_conf.set10('G_HAVE_GROWING_STACK', growing_stack)
1690
1691 # Tests for iconv
1692 #
1693 # We should never use the MinGW C library's iconv because it may not be
1694 # available in the actual runtime environment. On Windows, we always use
1695 # the built-in implementation
1696 iconv_opt = get_option('iconv')
1697 if host_system == 'windows'
1698   libiconv = []
1699   # We have a #include "win_iconv.c" in gconvert.c on Windows, so we don't need
1700   # any external library for it
1701   if iconv_opt != 'auto'
1702     warning('-Diconv was set to @0@, which was ignored')
1703   endif
1704 else
1705   found_iconv = false
1706   if ['auto', 'libc'].contains(iconv_opt) and cc.has_function('iconv_open')
1707     libiconv = []
1708     found_iconv = true
1709   endif
1710   if not found_iconv and ['auto', 'external'].contains(iconv_opt) and cc.has_header_symbol('iconv.h', 'iconv_open')
1711     libiconv = [cc.find_library('iconv')]
1712     found_iconv = true
1713   endif
1714
1715   if not found_iconv
1716     error('iconv implementation "@0@" not found'.format(iconv_opt))
1717   endif
1718 endif
1719
1720 if get_option('internal_pcre')
1721   pcre = []
1722   use_system_pcre = false
1723 else
1724   pcre = dependency('libpcre', version: '>= 8.31', required : false) # Should check for Unicode support, too. FIXME
1725   if not pcre.found()
1726     if cc.get_id() == 'msvc'
1727     # MSVC: Search for the PCRE library by the configuration, which corresponds
1728     # to the output of CMake builds of PCRE.  Note that debugoptimized
1729     # is really a Release build with .PDB files.
1730       if buildtype == 'debug'
1731         pcre = cc.find_library('pcred', required : false)
1732       else
1733         pcre = cc.find_library('pcre', required : false)
1734       endif
1735     endif
1736   endif
1737   use_system_pcre = pcre.found()
1738 endif
1739 glib_conf.set('USE_SYSTEM_PCRE', use_system_pcre)
1740
1741 use_pcre_static_flag = false
1742
1743 if host_system == 'windows'
1744   if not use_system_pcre
1745     use_pcre_static_flag = true
1746   else
1747     pcre_static = cc.links('''#define PCRE_STATIC
1748                               #include <pcre.h>
1749                               int main() {
1750                                 void *p = NULL;
1751                                 pcre_free(p);
1752                                 return 0;
1753                               }''',
1754                            dependencies: pcre,
1755                            name : 'Windows system PCRE is a static build')
1756     if pcre_static
1757       use_pcre_static_flag = true
1758     endif
1759   endif
1760 endif
1761
1762 libm = cc.find_library('m', required : false)
1763 libffi_dep = dependency('libffi', version : '>= 3.0.0', fallback : ['libffi', 'ffi_dep'])
1764
1765 # Don't use the bundled ZLib sources until we are sure that we can't find it on
1766 # the system
1767 libz_dep = dependency('zlib', required : false)
1768 if not libz_dep.found()
1769   if cc.get_id() != 'msvc'
1770     libz_dep = cc.find_library('z', required : false)
1771   else
1772     libz_dep = cc.find_library('zlib1', required : false)
1773     if not libz_dep.found()
1774       libz_dep = cc.find_library('zlib', required : false)
1775     endif
1776   endif
1777   if not libz_dep.found() or not cc.has_header('zlib.h')
1778     libz_dep = subproject('zlib').get_variable('zlib_dep')
1779   endif
1780 endif
1781
1782 # First check in libc, fallback to libintl, and as last chance build
1783 # proxy-libintl subproject.
1784 # FIXME: glib-gettext.m4 has much more checks to detect broken/uncompatible
1785 # implementations. This could be extended if issues are found in some platforms.
1786 if cc.has_function('ngettext')
1787   libintl = []
1788   have_bind_textdomain_codeset = cc.has_function('bind_textdomain_codeset')
1789 else
1790   libintl = cc.find_library('intl', required : false)
1791   if not libintl.found()
1792     libintl = subproject('proxy-libintl').get_variable('intl_dep')
1793     have_bind_textdomain_codeset = true  # proxy-libintl supports it
1794   else
1795     have_bind_textdomain_codeset = cc.has_function('bind_textdomain_codeset',
1796                                                    dependencies : libintl)
1797   endif
1798 endif
1799
1800 glib_conf.set('HAVE_BIND_TEXTDOMAIN_CODESET', have_bind_textdomain_codeset)
1801
1802 # We require gettext to always be present
1803 glib_conf.set('HAVE_DCGETTEXT', 1)
1804 glib_conf.set('HAVE_GETTEXT', 1)
1805
1806 glib_conf.set_quoted('GLIB_LOCALE_DIR', join_paths(glib_datadir, 'locale'))
1807
1808 # libmount is only used by gio, but we need to fetch the libs to generate the
1809 # pkg-config file below
1810 libmount_dep = []
1811 if host_system == 'linux' and get_option('libmount')
1812   libmount_dep = [dependency('mount', version : '>=2.23', required : true)]
1813   glib_conf.set('HAVE_LIBMOUNT', 1)
1814 endif
1815
1816 if host_system == 'windows'
1817   winsock2 = cc.find_library('ws2_32')
1818 endif
1819
1820 selinux_dep = []
1821 if host_system == 'linux'
1822   selinux_dep = dependency('libselinux', required: get_option('selinux'))
1823
1824   glib_conf.set('HAVE_SELINUX', selinux_dep.found())
1825 endif
1826
1827 xattr_dep = []
1828 if host_system != 'windows' and get_option('xattr')
1829   # either glibc or libattr can provide xattr support
1830   # for both of them, we check for getxattr being in
1831   # the library and a valid xattr header.
1832
1833   # try glibc
1834   if cc.has_function('getxattr') and cc.has_header('sys/xattr.h')
1835     glib_conf.set('HAVE_SYS_XATTR_H', 1)
1836     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format('HAVE_SYS_XATTR_H')
1837   #failure. try libattr
1838   elif cc.has_header_symbol('attr/xattr.h', 'getxattr')
1839     glib_conf.set('HAVE_ATTR_XATTR_H', 1)
1840     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format('HAVE_ATTR_XATTR_H')
1841     xattr_dep = [cc.find_library('xattr')]
1842   else
1843     error('No getxattr implementation found in C library or libxattr')
1844   endif
1845
1846   glib_conf.set('HAVE_XATTR', 1)
1847   if cc.compiles(glib_conf_prefix + '''
1848                  #include <stdio.h>
1849                  #ifdef HAVE_SYS_TYPES_H
1850                  #include <sys/types.h>
1851                  #endif
1852                  #ifdef HAVE_SYS_XATTR_H
1853                  #include <sys/xattr.h>
1854                  #elif HAVE_ATTR_XATTR_H
1855                  #include <attr/xattr.h>
1856                  #endif
1857
1858                  int main (void) {
1859                    ssize_t len = getxattr("", "", NULL, 0, 0, XATTR_NOFOLLOW);
1860                    return len;
1861                  }''',
1862                  name : 'XATTR_NOFOLLOW')
1863     glib_conf.set('HAVE_XATTR_NOFOLLOW', 1)
1864   endif
1865 endif
1866
1867 # If strlcpy is present (BSD and similar), check that it conforms to the BSD
1868 # specification. Specifically Solaris 8's strlcpy() does not, see
1869 # https://bugzilla.gnome.org/show_bug.cgi?id=53933 for further context.
1870 if cc.has_function('strlcpy')
1871   if cc_can_run
1872     rres = cc.run('''#include <stdlib.h>
1873                     #include <string.h>
1874                     int main() {
1875                       char p[10];
1876                       (void) strlcpy (p, "hi", 10);
1877                       if (strlcat (p, "bye", 0) != 3)
1878                         return 1;
1879                       return 0;
1880                     }''',
1881                   name : 'OpenBSD strlcpy/strlcat')
1882     if rres.compiled() and rres.returncode() == 0
1883       glib_conf.set('HAVE_STRLCPY', 1)
1884     endif
1885   elif meson.get_cross_property('have_strlcpy', false)
1886     glib_conf.set('HAVE_STRLCPY', 1)
1887   endif
1888 endif
1889
1890 cmdline_test_code = '''
1891 #include <fcntl.h>
1892 #include <sys/stat.h>
1893 #include <stdio.h>
1894 #include <stdlib.h>
1895 #undef NDEBUG
1896 #include <assert.h>
1897
1898 static int
1899 __getcmdline (void)
1900 {
1901 /* This code is a dumbed-down version of g_file_get_contents() */
1902 #ifndef O_BINARY
1903 #define O_BINARY 0
1904 #endif
1905 #define BUFSIZE 1024
1906   char result[BUFSIZE];
1907   struct stat stat_buf;
1908
1909   int fd = open ("/proc/self/cmdline", O_RDONLY|O_BINARY);
1910   assert (fd >= 0);
1911   assert (fstat (fd, &stat_buf) == 0);
1912
1913   if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
1914     assert (read (fd, result, BUFSIZE) > 0);
1915   else
1916     {
1917       FILE *f = fdopen (fd, "r");
1918       assert (f != NULL);
1919       assert (fread (result, 1, BUFSIZE, f) > 0);
1920     }
1921
1922   return 0;
1923 }
1924
1925 int
1926 main (void)
1927 {
1928   exit (__getcmdline ());
1929 }'''
1930
1931 if cc_can_run
1932   rres = cc.run(cmdline_test_code, name : '/proc/self/cmdline')
1933   have_proc_self_cmdline = rres.compiled() and rres.returncode() == 0
1934 else
1935   have_proc_self_cmdline = meson.get_cross_property('have_proc_self_cmdline', false)
1936 endif
1937
1938 glib_conf.set('HAVE_PROC_SELF_CMDLINE', have_proc_self_cmdline)
1939
1940 python = import('python').find_installation('python3')
1941 # used for '#!/usr/bin/env <name>'
1942 python_name = 'python3'
1943
1944 python_version = python.language_version()
1945 python_version_req = '>=3.4'
1946 if not python_version.version_compare(python_version_req)
1947   error('Requires Python @0@, @1@ found.'.format(python_version_req, python_version))
1948 endif
1949
1950 # Determine which user environment-dependent files that we want to install
1951 have_bash = find_program('bash', required : false).found() # For completion scripts
1952 have_sh = find_program('sh', required : false).found() # For glib-gettextize
1953
1954 # Some installed tests require a custom environment
1955 env_program = find_program('env', required: installed_tests_enabled)
1956
1957 # FIXME: How to detect Solaris? https://github.com/mesonbuild/meson/issues/1578
1958 if host_system == 'sunos'
1959   glib_conf.set('_XOPEN_SOURCE_EXTENDED', 1)
1960   glib_conf.set('_XOPEN_SOURCE', 2)
1961   glib_conf.set('__EXTENSIONS__',1)
1962 endif
1963
1964 # Sadly Meson does not expose this value:
1965 # https://github.com/mesonbuild/meson/pull/3460
1966 if host_system == 'windows'
1967   # Autotools explicitly removed --Wl,--export-all-symbols from windows builds,
1968   # with no explanation. Do the same here for now but this could be revisited if
1969   # if causes issues.
1970   export_dynamic_ldflags = []
1971 elif host_system == 'cygwin'
1972   export_dynamic_ldflags = ['-Wl,--export-all-symbols']
1973 elif host_system == 'darwin'
1974   export_dynamic_ldflags = []
1975 else
1976   export_dynamic_ldflags = ['-Wl,--export-dynamic']
1977 endif
1978
1979 win32_cflags = []
1980 win32_ldflags = []
1981 if host_system == 'windows' and cc.get_id() != 'msvc'
1982   # Ensure MSVC-compatible struct packing convention is used when
1983   # compiling for Win32 with gcc. It is used for the whole project and exposed
1984   # in glib-2.0.pc.
1985   win32_cflags = ['-mms-bitfields']
1986   add_project_arguments(win32_cflags, language : 'c')
1987
1988   # Win32 API libs, used only by libglib and exposed in glib-2.0.pc
1989   win32_ldflags = ['-lws2_32', '-lole32', '-lwinmm', '-lshlwapi']
1990 elif host_system == 'cygwin'
1991   win32_ldflags = ['-luser32', '-lkernel32']
1992 endif
1993
1994 # Tracing: dtrace
1995 want_dtrace = get_option('dtrace')
1996 enable_dtrace = false
1997
1998 # Since dtrace support is opt-in we just error out if it was requested but
1999 # is not available. We don't bother with autodetection yet.
2000 if want_dtrace
2001   if glib_have_carbon
2002     error('GLib dtrace support not yet compatible with macOS dtrace')
2003   endif
2004   dtrace = find_program('dtrace', required : true) # error out if not found
2005   if not cc.has_header('sys/sdt.h')
2006     error('dtrace support needs sys/sdt.h header')
2007   endif
2008   # FIXME: autotools build also passes -fPIC -DPIC but is it needed in this case?
2009   dtrace_obj_gen = generator(dtrace,
2010     output : '@BASENAME@.o',
2011     arguments : ['-G', '-s', '@INPUT@', '-o', '@OUTPUT@'])
2012   # FIXME: $(SED) -e "s,define STAP_HAS_SEMAPHORES 1,undef STAP_HAS_SEMAPHORES,"
2013   #               -e "s,define _SDT_HAS_SEMAPHORES 1,undef _SDT_HAS_SEMAPHORES,"
2014   dtrace_hdr_gen = generator(dtrace,
2015     output : '@BASENAME@.h',
2016     arguments : ['-h', '-s', '@INPUT@', '-o', '@OUTPUT@'])
2017   glib_conf.set('HAVE_DTRACE', 1)
2018   enable_dtrace = true
2019 endif
2020
2021 # systemtap
2022 want_systemtap = get_option('systemtap')
2023 enable_systemtap = false
2024
2025 if want_systemtap and enable_dtrace
2026   tapset_install_dir = get_option('tapset_install_dir')
2027   if tapset_install_dir == ''
2028     tapset_install_dir = join_paths(get_option('datadir'), 'systemtap/tapset', host_machine.cpu_family())
2029   endif
2030   stp_cdata = configuration_data()
2031   stp_cdata.set('ABS_GLIB_RUNTIME_LIBDIR', glib_libdir)
2032   stp_cdata.set('LT_CURRENT', minor_version * 100)
2033   stp_cdata.set('LT_REVISION', micro_version)
2034   enable_systemtap = true
2035 endif
2036
2037 test_timeout = 60
2038 test_timeout_slow = 120
2039
2040 pkg = import('pkgconfig')
2041 windows = import('windows')
2042 subdir('glib')
2043 subdir('gobject')
2044 subdir('gthread')
2045 subdir('gmodule')
2046 subdir('gio')
2047 subdir('fuzzing')
2048 # Don’t build the tests unless we can run them (either natively or in an exe wrapper)
2049 build_tests = not meson.is_cross_build() or (meson.is_cross_build() and meson.has_exe_wrapper())
2050 if build_tests
2051   subdir('tests')
2052 endif
2053
2054 # xgettext is optional (on Windows for instance)
2055 if find_program('xgettext', required : get_option('nls')).found()
2056   subdir('po')
2057 endif
2058
2059 # Install glib-gettextize executable, if a UNIX-style shell is found
2060 if have_sh
2061   # These should not contain " quotes around the values
2062   gettextize_conf = configuration_data()
2063   gettextize_conf.set('PACKAGE', 'glib')
2064   gettextize_conf.set('VERSION', meson.project_version())
2065   gettextize_conf.set('prefix', glib_prefix)
2066   gettextize_conf.set('datarootdir', glib_datadir)
2067   gettextize_conf.set('datadir', glib_datadir)
2068   configure_file(input : 'glib-gettextize.in',
2069     install : true,
2070     install_dir : glib_bindir,
2071     output : 'glib-gettextize',
2072     configuration : gettextize_conf)
2073 endif
2074
2075 # Install m4 macros that other projects use
2076 install_data('m4macros/glib-2.0.m4', 'm4macros/glib-gettext.m4', 'm4macros/gsettings.m4',
2077   install_dir : join_paths(get_option('datadir'), 'aclocal'))
2078
2079 if host_system != 'windows'
2080   # Install Valgrind suppression file (except on Windows,
2081   # as Valgrind is currently not supported on Windows)
2082   install_data('glib.supp',
2083     install_dir : join_paths(get_option('datadir'), 'glib-2.0', 'valgrind'))
2084 endif
2085
2086 configure_file(output : 'config.h', configuration : glib_conf)
2087
2088 if host_system == 'windows'
2089   install_headers([ 'msvc_recommended_pragmas.h' ], subdir : 'glib-2.0')
2090 endif
2091
2092 if get_option('man')
2093   xsltproc = find_program('xsltproc', required : true)
2094   xsltproc_command = [
2095     xsltproc,
2096     '--nonet',
2097     '--stringparam', 'man.output.quietly', '1',
2098     '--stringparam', 'funcsynopsis.style', 'ansi',
2099     '--stringparam', 'man.th.extra1.suppress', '1',
2100     '--stringparam', 'man.authors.section.enabled', '0',
2101     '--stringparam', 'man.copyright.section.enabled', '0',
2102     '-o', '@OUTPUT@',
2103     'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl',
2104     '@INPUT@',
2105   ]
2106   man1_dir = get_option('mandir') + '/man1'
2107 endif
2108
2109 gnome = import('gnome')
2110 subdir('docs/reference')