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