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