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