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