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