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