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