build: Include host_machine.cpu_family() in tapset directory (Meson)
[glib.git] / meson.build
blobb7755d69a8373f54f5a971263efe1cfaa76a2e45
1 project('glib', 'c', 'cpp',
2   version : '2.55.0',
3   meson_version : '>= 0.38.1',
4   default_options : [
5     'warning_level=1',
6     'c_std=gnu89'
7   ]
10 cc = meson.get_compiler('c')
11 cxx = meson.get_compiler('cpp')
13 if cc.get_id() == 'msvc'
14   # Ignore several spurious warnings for things glib does very commonly
15   # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it
16   # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once
17   # NOTE: Only add warnings here if you are sure they're spurious
18   add_project_arguments('/wd4035', '/wd4715', '/wd4116',
19     '/wd4046', '/wd4068', '/wo4090', '/FImsvc_recommended_pragmas.h',language : 'c')
20   # Disable SAFESEH with MSVC for plugins and libs that use external deps that
21   # are built with MinGW
22   noseh_link_args = ['/SAFESEH:NO']
23 else
24   noseh_link_args = []
25   # -mms-bitfields vs -fnative-struct ?
26 endif
28 host_system = host_machine.system()
30 glib_version = meson.project_version()
31 glib_api_version = '2.0'
32 version_arr = glib_version.split('.')
33 major_version = version_arr[0].to_int()
34 minor_version = version_arr[1].to_int()
35 micro_version = version_arr[2].to_int()
37 interface_age = minor_version.is_odd() ? 0 : micro_version
38 binary_age = 100 * minor_version + micro_version
40 soversion = 0
41 # Maintain compatibility with previous libtool versioning
42 # current = minor * 100 + micro
43 library_version = '@0@.@1@.@2@'.format(soversion, binary_age - interface_age, interface_age)
45 configinc = include_directories('.')
46 glibinc = include_directories('glib')
47 gobjectinc = include_directories('gobject')
48 gmoduleinc = include_directories('gmodule')
49 gioinc = include_directories('gio')
51 glib_prefix = get_option('prefix')
52 glib_bindir = join_paths(glib_prefix, get_option('bindir'))
53 glib_libdir = join_paths(glib_prefix, get_option('libdir'))
54 glib_datadir = join_paths(glib_prefix, get_option('datadir'))
55 glib_pkgdatadir = join_paths(glib_datadir, 'glib-2.0')
56 glib_includedir = join_paths(glib_prefix, get_option('includedir'))
57 glib_giomodulesdir = join_paths(glib_libdir, 'gio', 'modules')
59 glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
61 add_project_arguments('-D_GNU_SOURCE', language: 'c')
63 ########################
64 # Configuration begins #
65 ########################
66 glib_conf = configuration_data()
67 glibconfig_conf = configuration_data()
69 # accumulated list of defines as we check for them, so we can easily
70 # use them later in test programs (autoconf does this automatically)
71 glib_conf_prefix = ''
73 glib_conf.set('GLIB_VERSION', glib_version)
74 glib_conf.set('GLIB_MAJOR_VERSION', major_version)
75 glib_conf.set('GLIB_MINOR_VERSION', minor_version)
76 glib_conf.set('GLIB_MICRO_VERSION', micro_version)
77 glib_conf.set('GLIB_INTERFACE_AGE', interface_age)
78 glib_conf.set('GLIB_BINARY_AGE', binary_age)
79 glib_conf.set_quoted('GETTEXT_PACKAGE', 'glib20')
80 glib_conf.set_quoted('PACKAGE_BUGREPORT', 'http://bugzilla.gnome.org/enter_bug.cgi?product=glib')
81 glib_conf.set_quoted('PACKAGE_NAME', 'glib')
82 glib_conf.set_quoted('PACKAGE_STRING', 'glib @0@'.format(meson.project_version()))
83 glib_conf.set_quoted('PACKAGE_TARNAME', 'glib')
84 glib_conf.set_quoted('PACKAGE_URL', '')
85 glib_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
86 glib_conf.set('ENABLE_NLS', 1)
88 # Variables used in glib-gettextize and pkg-config files
89 # These should not contain " quotes around the values
90 glib_conf.set('PACKAGE', 'glib')
91 glib_conf.set('VERSION', meson.project_version())
92 glib_conf.set('prefix', glib_prefix)
93 glib_conf.set('exec_prefix', glib_prefix)
94 glib_conf.set('libdir', glib_libdir)
95 glib_conf.set('includedir', glib_includedir)
96 glib_conf.set('datadir', glib_datadir)
97 glib_conf.set('datarootdir', glib_datadir)
99 glib_conf.set('_GNU_SOURCE', 1)
101 if host_system == 'windows'
102   # Poll doesn't work on devices on Windows
103   glib_conf.set('BROKEN_POLL', true)
104 endif
106 # Detect and set symbol visibility
107 glib_hidden_visibility_args = []
108 if get_option('default_library') != 'static'
109   if host_system == 'windows'
110     glib_conf.set('DLL_EXPORT', true)
111     if cc.get_id() == 'msvc'
112       glib_conf.set('_GLIB_EXTERN', '__declspec(dllexport) extern')
113     elif cc.has_argument('-fvisibility=hidden')
114       glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) __declspec(dllexport) extern')
115       glib_hidden_visibility_args = ['-fvisibility=hidden']
116     endif
117   elif cc.has_argument('-fvisibility=hidden')
118     glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) extern')
119     glib_hidden_visibility_args = ['-fvisibility=hidden']
120   endif
121 endif
123 # FIXME: what about Cygwin (G_WITH_CYGWIN)
124 if host_system == 'windows'
125   glib_os = '''#define G_OS_WIN32
126 #define G_PLATFORM_WIN32'''
127 else
128   glib_os = '#define G_OS_UNIX'
129 endif
130 glibconfig_conf.set('glib_os', glib_os)
132 # We need to know the build type to determine what .lib files we need on Visual Studio
133 # for dependencies that don't normally come with pkg-config files for Visual Studio builds
134 buildtype = get_option('buildtype')
136 # check for header files
138 headers = [
139   'stdlib.h',
140   'string.h',
141   'strings.h',
142   'memory.h',
143   'alloca.h',
144   'locale.h',
145   'xlocale.h',
146   'float.h',
147   'limits.h',
148   'pwd.h',
149   'grp.h',
150   'poll.h',
151   'termios.h',
152   'sys/param.h',
153   'sys/resource.h',
154   'mach/mach_time.h',
155   'sys/select.h',
156   'stdint.h',
157   'inttypes.h',
158   'sched.h',
159   'malloc.h',
160   'sys/vfs.h',
161   'sys/vmount.h',
162   'sys/statfs.h',
163   'sys/statvfs.h',
164   'sys/filio.h',
165   'mntent.h',
166   'sys/mnttab.h',
167   'sys/vfstab.h',
168   'sys/mntctl.h',
169   'fstab.h',
170   'linux/magic.h',
171   'termios.h',
172   'dirent.h', # MSC does not come with this by default
173   'sys/time.h', # MSC does not come with this by default
174   'sys/times.h',
175   'sys/wait.h',
176   'unistd.h',
177   'values.h',
178   'sys/types.h',
179   'sys/uio.h',
180   'sys/mkdev.h',
181   'sys/mount.h',
182   'sys/sysctl.h',
183   'crt_externs.h',
184   'sys/inotify.h',
185   'sys/event.h',
186   'sys/stat.h',
189 foreach h : headers
190   if cc.has_header(h)
191     define = 'HAVE_' + h.underscorify().to_upper()
192     glib_conf.set(define, 1)
193     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
194   endif
195 endforeach
197 if cc.has_header('linux/netlink.h')
198   glib_conf.set('HAVE_NETLINK', 1)
199 endif
201 if glib_conf.has('HAVE_LOCALE_H')
202   if cc.has_header_symbol('locale.h', 'LC_MESSAGES')
203     glib_conf.set('HAVE_LC_MESSAGES', 1)
204   endif
205 endif
207 struct_stat_blkprefix = '''
208 #include <sys/types.h>
209 #include <sys/stat.h>
210 #ifdef HAVE_UNISTD_H
211 #include <unistd.h>
212 #endif
213 #ifdef HAVE_SYS_STATFS_H
214 #include <sys/statfs.h>
215 #endif
216 #ifdef HAVE_SYS_PARAM_H
217 #include <sys/param.h>
218 #endif
219 #ifdef HAVE_SYS_MOUNT_H
220 #include <sys/mount.h>
221 #endif
224 struct_members = [
225   [ 'stat', 'st_mtimensec' ],
226   [ 'stat', 'st_mtim.tv_nsec' ],
227   [ 'stat', 'st_atimensec' ],
228   [ 'stat', 'st_atim.tv_nsec' ],
229   [ 'stat', 'st_ctimensec' ],
230   [ 'stat', 'st_ctim.tv_nsec' ],
231   [ 'stat', 'st_birthtime' ],
232   [ 'stat', 'st_birthtimensec' ],
233   [ 'stat', 'st_birthtim' ],
234   [ 'stat', 'st_birthtim.tv_nsec' ],
235   [ 'stat', 'st_blksize', struct_stat_blkprefix ],
236   [ 'stat', 'st_blocks', struct_stat_blkprefix ],
237   [ 'statfs', 'f_fstypename', struct_stat_blkprefix ],
238   [ 'statfs', 'f_bavail', struct_stat_blkprefix ],
239   [ 'dirent', 'd_type', '''#include <sys/types.h>
240                            #include <dirent.h>''' ],
243 foreach m : struct_members
244   header_check_prefix = glib_conf_prefix
245   if m.length() == 3
246     header_check_prefix = header_check_prefix + m[2]
247   else
248     header_check_prefix = header_check_prefix + '#include <sys/stat.h>'
249   endif
250   if cc.has_member('struct ' + m[0], m[1], prefix : header_check_prefix)
251     define = 'HAVE_STRUCT_@0@_@1@'.format(m[0].to_upper(), m[1].underscorify().to_upper())
252     glib_conf.set(define, 1)
253     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
254   else
255   endif
256 endforeach
258 # Windows Support (Vista+)
259 if host_system == 'windows'
260   glib_conf.set('_WIN32_WINNT', '0x0601')
261 endif
263 functions = [
264   'alloca',
265   'mmap',
266   'posix_memalign',
267   'memalign',
268   'valloc',
269   'fsync',
270   'pipe2',
271   'issetugid',
272   'timegm',
273   'gmtime_r',
274   'strerror_r',
275   'lstat',
276   'strsignal',
277   'vsnprintf',
278   'stpcpy',
279   'poll',
280   'vasprintf',
281   'setenv',
282   'unsetenv',
283   'getc_unlocked',
284   'readlink',
285   'symlink',
286   'fdwalk',
287   'lchmod',
288   'lchown',
289   'fchmod',
290   'fchown',
291   'utimes',
292   'getresuid',
293   'getmntent_r',
294   'setmntent',
295   'endmntent',
296   'hasmntopt',
297   'getfsstat',
298   'getvfsstat',
299   'fallocate',
300   'localtime_r',
301   'gmtime_r',
302   'getpwuid_r',
303   'getgrgid_r',
304   'prlimit',
305   'strnlen',
306   'wcslen',
307   'wcsnlen',
308   'mbrtowc',
309   'wcrtomb',
310   'newlocale',
311   'uselocale',
312   'strtod_l',
313   'strtoll_l',
314   'strtoull_l',
315   'inotify_init1',
316   'kqueue',
317   'kevent',
318   'endservent',
319   'sendmmsg',
320   'recvmmsg',
323 if glib_conf.has('HAVE_SYS_STATVFS_H')
324   functions += ['statvfs']
325 else
326   have_func_statvfs = false
327 endif
328 if glib_conf.has('HAVE_SYS_STATFS_H') or glib_conf.has('HAVE_SYS_MOUNT_H')
329   functions += ['statfs']
330 else
331   have_func_statfs = false
332 endif
334 if host_system == 'windows'
335   iphlpapi_dep = cc.find_library('iphlpapi')
336   iphlpapi_funcs = ['if_nametoindex', 'if_indextoname']
337   foreach ifunc : iphlpapi_funcs
338     if cc.has_function(ifunc,
339                        prefix : '#define _WIN32_WINNT 0x0601\n#include <winsock2.h>\n#include <iphlpapi.h>',
340                        dependencies : iphlpapi_dep)
341       idefine = 'HAVE_' + ifunc.underscorify().to_upper()
342       glib_conf.set(idefine, 1)
343       glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(idefine)
344       set_variable('have_func_' + ifunc, true)
345     else
346       set_variable('have_func_' + ifunc, false)
347     endif
348   endforeach
349 else
350   functions += ['if_indextoname', 'if_nametoindex']
351 endif
353 # AIX splice is something else
354 if host_system != 'aix'
355   functions += ['splice']
356 endif
358 foreach f : functions
359   if cc.has_function(f)
360     define = 'HAVE_' + f.underscorify().to_upper()
361     glib_conf.set(define, 1)
362     glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
363     set_variable('have_func_' + f, true)
364   else
365     set_variable('have_func_' + f, false)
366   endif
367 endforeach
369 # Check whether strerror_r returns char *
370 if have_func_strerror_r
371   if cc.compiles('''#define _GNU_SOURCE
372                     #include <string.h>
373                     int func (void) {
374                       char error_string[256];
375                       char *ptr = strerror_r (-2, error_string, 256);
376                       char c = *strerror_r (-2, error_string, 256);
377                       return c != 0 && ptr != (void*) 0L;
378                     }
379                  ''',
380                  name : 'strerror_r() returns char *')
381     glib_conf.set('STRERROR_R_CHAR_P', 1,
382                   description: 'Defined if strerror_r returns char *')
383   endif
384 endif
386 # Special-case these functions that have alternative names on Windows/MSVC
387 if cc.has_function('snprintf') or cc.has_header_symbol('stdio.h', 'snprintf')
388   glib_conf.set('HAVE_SNPRINTF', 1)
389   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF 1\n'
390 elif cc.has_function('_snprintf') or cc.has_header_symbol('stdio.h', '_snprintf')
391   hack_define = '1\n#define snprintf _snprintf'
392   glib_conf.set('HAVE_SNPRINTF', hack_define)
393   glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF ' + hack_define
394 endif
396 if cc.has_function('strcasecmp')
397   glib_conf.set('HAVE_STRCASECMP', 1)
398   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP 1\n'
399 elif cc.has_function('_stricmp')
400   hack_define = '1\n#define strcasecmp _stricmp'
401   glib_conf.set('HAVE_STRCASECMP', hack_define)
402   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP ' + hack_define
403 endif
405 if cc.has_function('strncasecmp')
406   glib_conf.set('HAVE_STRNCASECMP', 1)
407   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP 1\n'
408 elif cc.has_function('_strnicmp')
409   hack_define = '1\n#define strncasecmp _strnicmp'
410   glib_conf.set('HAVE_STRNCASECMP', hack_define)
411   glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP ' + hack_define
412 endif
414 if cc.has_header_symbol('sys/sysmacros.h', 'major')
415   glib_conf.set('MAJOR_IN_SYSMACROS', 1)
416 elif cc.has_header_symbol('sys/mkdev.h', 'major')
417   glib_conf.set('MAJOR_IN_MKDEV', 1)
418 endif
420 if cc.has_header_symbol('dlfcn.h', 'RTLD_LAZY')
421   glib_conf.set('HAVE_RTLD_LAZY', 1)
422 endif
424 if cc.has_header_symbol('dlfcn.h', 'RTLD_NOW')
425   glib_conf.set('HAVE_RTLD_NOW', 1)
426 endif
428 if cc.has_header_symbol('dlfcn.h', 'RTLD_GLOBAL')
429   glib_conf.set('HAVE_RTLD_GLOBAL', 1)
430 endif
432 # Check whether to use statfs or statvfs
433 # Some systems have both statfs and statvfs, pick the most "native" for these
434 if have_func_statfs and have_func_statvfs
435   # on solaris and irix, statfs doesn't even have the f_bavail field
436   if not glib_conf.has('HAVE_STRUCT_STATFS_F_BAVAIL')
437     have_func_statfs = false
438   else
439     # at least on linux, statfs is the actual syscall
440     have_func_statvfs = false
441   endif
442 endif
443 if have_func_statfs
444   glib_conf.set('USE_STATFS', 1)
445   stat_func_to_use = 'statfs'
446 elif have_func_statvfs
447   glib_conf.set('USE_STATVFS', 1)
448   stat_func_to_use = 'statvfs'
449 else
450   stat_func_to_use = 'neither'
451 endif
452 message('Checking whether to use statfs or statvfs .. ' + stat_func_to_use)
454 if host_system == 'linux'
455   if cc.has_function('mkostemp',
456                      prefix: '''#define _GNU_SOURCE
457                                 #include <stdlib.h>''')
458     glib_conf.set('HAVE_MKOSTEMP', 1)
459   endif
460 endif
462 # Mac OS X Carbon support
463 glib_have_carbon = cc.compiles('''#include <Carbon/Carbon.h>
464                                   #include <CoreServices/CoreServices.h>''',
465                                name : 'Mac OS X Carbon support')
467 glib_have_os_x_9_or_later = false
469 if glib_have_carbon
470   glib_conf.set('HAVE_CARBON', true)
471   CARBON_LIBS='-Wl,-framework,Carbon' # FIXME: propagate to .pc files as well
472   glib_have_os_x_9_or_later = cc.compiles('''#include <AvailabilityMacros.h>
473                                              #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
474                                              #error Compiling for minimum OS X version before 10.9
475                                              #endif''', name : 'OS X 9 or later')
476 else
477   CARBON_LIBS=''
478 endif
480 # Mac OS X Cocoa support
481 glib_have_cocoa = cc.compiles('''#include <Cocoa/Cocoa.h>
482                                  #ifdef GNUSTEP_BASE_VERSION
483                                  #error "Detected GNUstep, not Cocoa"
484                                  #endif''',
485                               name : 'Mac OS X Cocoa support')
487 if glib_have_cocoa
488   glib_conf.set('HAVE_COCOA', true)
489   COCOA_LIBS='-Wl,-framework,Foundation' # FIXME: propagate to .pc files as well
490 else
491   COCOA_LIBS=''
492 endif
494 # Check for futex(2)
495 if cc.links('''#include <linux/futex.h>
496                #include <sys/syscall.h>
497                #include <unistd.h>
498                int main (int argc, char ** argv) {
499                  syscall (__NR_futex, NULL, FUTEX_WAKE, FUTEX_WAIT);
500                  return 0;
501                }''', name : 'futex(2) system call')
502   glib_conf.set('HAVE_FUTEX', 1)
503 endif
505 # Check for eventfd(2)
506 if cc.links('''#include <sys/eventfd.h>
507                #include <unistd.h>
508                int main (int argc, char ** argv) {
509                  eventfd (0, EFD_CLOEXEC);
510                  return 0;
511                }''', name : 'eventfd(2) system call')
512   glib_conf.set('HAVE_EVENTFD', 1)
513 endif
515 clock_gettime_test_code = '''
516   #include <time.h>
517   struct timespec t;
518   int main (int argc, char ** argv) {
519     return clock_gettime(CLOCK_REALTIME, &t);
520   }'''
521 librt = []
522 if cc.links(clock_gettime_test_code, name : 'clock_gettime')
523   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
524 elif cc.links(clock_gettime_test_code, args : '-lrt', name : 'clock_gettime in librt')
525   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
526   librt = cc.find_library('rt')
527 endif
529 # if statfs() takes 2 arguments (Posix) or 4 (Solaris)
530 if have_func_statfs
531   if cc.compiles(glib_conf_prefix + '''
532                  #include <unistd.h>
533                         #ifdef HAVE_SYS_PARAM_H
534                         #include <sys/param.h>
535                         #endif
536                         #ifdef HAVE_SYS_VFS_H
537                         #include <sys/vfs.h>
538                         #endif
539                         #ifdef HAVE_SYS_MOUNT_H
540                         #include <sys/mount.h>
541                         #endif
542                         #ifdef HAVE_SYS_STATFS_H
543                         #include <sys/statfs.h>
544                         #endif
545                         void some_func (void) {
546                           struct statfs st;
547                           statfs("/", &st);
548                         }''', name : 'number of arguments to statfs() (n=2)')
549     glib_conf.set('STATFS_ARGS', 2)
550   elif cc.compiles(glib_conf_prefix + '''
551                    #include <unistd.h>
552                           #ifdef HAVE_SYS_PARAM_H
553                           #include <sys/param.h>
554                           #endif
555                           #ifdef HAVE_SYS_VFS_H
556                           #include <sys/vfs.h>
557                           #endif
558                           #ifdef HAVE_SYS_MOUNT_H
559                           #include <sys/mount.h>
560                           #endif
561                           #ifdef HAVE_SYS_STATFS_H
562                           #include <sys/statfs.h>
563                           #endif
564                           void some_func (void) {
565                             struct statfs st;
566                             statfs("/", &st, sizeof (st), 0);
567                           }''', name : 'number of arguments to statfs() (n=4)')
568     glib_conf.set('STATFS_ARGS', 4)
569   else
570     error('Unable to determine number of arguments to statfs()')
571   endif
572 endif
574 # open takes O_DIRECTORY as an option
575 #AC_MSG_CHECKING([])
576 if cc.compiles('''#include <fcntl.h>
577                   #include <sys/types.h>
578                   #include <sys/stat.h>],
579                   void some_func (void) {
580                     open(0, O_DIRECTORY, 0);
581                   }''', name : 'open() option O_DIRECTORY')
582   glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
583 endif
585 # Check whether there is a vsnprintf() function with C99 semantics installed.
586 # AC_FUNC_VSNPRINTF_C99
587 # Check whether there is a snprintf() function with C99 semantics installed.
588 # AC_FUNC_SNPRINTF_C99
590 have_good_vsnprintf = false
591 have_good_snprintf = false
593 if host_system == 'windows'
594   # Unfortunately the mingw and Visual Studio 2015+ implementations of C99-style
595   # snprintf and vsnprintf don't seem to be quite good enough, at least not in
596   # mingw-runtime-3.14.  (Sorry, I don't know exactly what is the problem,
597   # but it is related to floating point formatting and decimal point vs. comma.)
598   # The simple tests in AC_FUNC_VSNPRINTF_C99 and AC_FUNC_SNPRINTF_C99 aren't
599   # rigorous enough to notice, though.
600   glib_conf.set('HAVE_C99_SNPRINTF', false)
601   glib_conf.set('HAVE_C99_VSNPRINTF', false)
602 else
603   vsnprintf_c99_test_code = '''
604 #include <stdio.h>
605 #include <stdarg.h>
608 doit(char * s, ...)
610   char buffer[32];
611   va_list args;
612   int r;
614   va_start(args, s);
615   r = vsnprintf(buffer, 5, s, args);
616   va_end(args);
618   if (r != 7)
619     exit(1);
621   /* AIX 5.1 and Solaris seems to have a half-baked vsnprintf()
622      implementation. The above will return 7 but if you replace
623      the size of the buffer with 0, it borks! */
624   va_start(args, s);
625   r = vsnprintf(buffer, 0, s, args);
626   va_end(args);
628   if (r != 7)
629     exit(1);
631   exit(0);
635 main(void)
637   doit("1234567");
638   exit(1);
639 }'''
641   rres = cc.run(vsnprintf_c99_test_code, name : 'C99 vsnprintf')
642   if rres.compiled() and rres.returncode() == 0
643     glib_conf.set('HAVE_C99_VSNPRINTF', 1)
644     have_good_vsnprintf = true
645   endif
647   snprintf_c99_test_code = '''
648 #include <stdio.h>
649 #include <stdarg.h>
652 doit()
654   char buffer[32];
655   va_list args;
656   int r;
658   r = snprintf(buffer, 5, "1234567");
660   if (r != 7)
661     exit(1);
663   r = snprintf(buffer, 0, "1234567");
665   if (r != 7)
666     exit(1);
668   r = snprintf(NULL, 0, "1234567");
670   if (r != 7)
671     exit(1);
673   exit(0);
677 main(void)
679   doit();
680   exit(1);
681 }'''
683   rres = cc.run(snprintf_c99_test_code, name : 'C99 snprintf')
684   if rres.compiled() and rres.returncode() == 0
685     glib_conf.set('HAVE_C99_SNPRINTF', 1)
686     have_good_snprintf = true
687   endif
688 endif
690 if host_system == 'windows'
691   glib_conf.set_quoted('EXEEXT', '.exe')
692 else
693   glib_conf.set('EXEEXT', '')
694 endif
696 if have_good_vsnprintf and have_good_snprintf
697   # Our printf is 'good' only if vsnpintf()/snprintf() supports C99 well enough
698   glib_conf.set('HAVE_GOOD_PRINTF', 1) # FIXME: Check for HAVE_UNIX98_PRINTF?
699 else
700   glib_conf.set('HAVE_VASPRINTF', 1)
701 endif
703 # Check whether the printf() family supports Unix98 %n$ positional parameters
704 # AC_FUNC_PRINTF_UNIX98
705 # Nothing uses HAVE_UNIX98_PRINTF
708 # Check for nl_langinfo and CODESET
709 # FIXME: Check for HAVE_BIND_TEXTDOMAIN_CODESET
710 if cc.links('''#include <langinfo.h>
711                int main (int argc, char ** argv) {
712                  char *codeset = nl_langinfo (CODESET);
713                  return 0;
714                }''', name : 'nl_langinfo and CODESET')
715   glib_conf.set('HAVE_LANGINFO_CODESET', 1)
716   glib_conf.set('HAVE_CODESET', 1)
717 endif
719 # Check for nl_langinfo and LC_TIME parts that are needed in gdatetime.c
720 if cc.links('''#include <langinfo.h>
721                int main (int argc, char ** argv) {
722                  char *str;
723                  str = nl_langinfo (PM_STR);
724                  str = nl_langinfo (D_T_FMT);
725                  str = nl_langinfo (D_FMT);
726                  str = nl_langinfo (T_FMT);
727                  str = nl_langinfo (T_FMT_AMPM);
728                  str = nl_langinfo (MON_1);
729                  str = nl_langinfo (ABMON_12);
730                  str = nl_langinfo (DAY_1);
731                  str = nl_langinfo (ABDAY_7);
732                  return 0;
733                }''', name : 'nl_langinfo (PM_STR)')
734   glib_conf.set('HAVE_LANGINFO_TIME', 1)
735 endif
736 if cc.links('''#include <langinfo.h>
737                int main (int argc, char ** argv) {
738                  char *str;
739                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB);
740                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT1_MB);
741                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT2_MB);
742                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT3_MB);
743                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT4_MB);
744                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT5_MB);
745                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT6_MB);
746                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT7_MB);
747                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT8_MB);
748                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT9_MB);
749                  return 0;
750                }''', name : 'nl_langinfo (_NL_CTYPE_OUTDIGITn_MB)')
751   glib_conf.set('HAVE_LANGINFO_OUTDIGIT', 1)
752 endif
754 # Check if C compiler supports the 'signed' keyword
755 if not cc.compiles('''signed char x;''', name : 'signed')
756   glib_conf.set('signed', '/* NOOP */')
757 endif
759 # Check if the ptrdiff_t type exists
760 if cc.has_header_symbol('stddef.h', 'ptrdiff_t')
761   glib_conf.set('HAVE_PTRDIFF_T', 1)
762 endif
764 # Check for sig_atomic_t type
765 if cc.links('''#include <signal.h>
766                #include <sys/types.h>
767                sig_atomic_t val = 42;
768                int main (int argc, char ** argv) {
769                  return val == 42 ? 0 : 1;
770                }''', name : 'sig_atomic_t')
771   glib_conf.set('HAVE_SIG_ATOMIC_T', 1)
772 endif
774 # Check if 'long long' works and what format can be used to print it
775 # jm_AC_TYPE_LONG_LONG
776 # Nothing uses HAVE_LONG_LONG_FORMAT and HAVE_INT64_AND_I64
777 if cc.compiles('''long long ll = 1LL;
778                   int i = 63;
779                   int some_func (void) {
780                     long long llmax = (long long) -1;
781                     return ll << i | ll >> i | llmax / ll | llmax % ll;
782                   }''', name : 'long long')
783   glib_conf.set('HAVE_LONG_LONG', 1)
784   have_long_long = true
785 else
786   have_long_long = false
787 endif
789 # Test whether the compiler supports the 'long double' type.
790 if cc.compiles('''/* The Stardent Vistra knows sizeof(long double), but does not support it.  */
791                   long double foo = 0.0;
792                   /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
793                   int array [2*(sizeof(long double) >= sizeof(double)) - 1];''',
794                name : 'long double')
795   glib_conf.set('HAVE_LONG_DOUBLE', 1)
796 endif
798 #dnl Test whether <stddef.h> has the 'wchar_t' type.
799 if cc.has_header_symbol('stddef.h', 'wchar_t')
800   glib_conf.set('HAVE_WCHAR_T', 1)
801 endif
803 # Test whether <wchar.h> has the 'wint_t' type.
804 if cc.has_header_symbol('wchar.h', 'wint_t')
805   glib_conf.set('HAVE_WINT_T', 1)
806 endif
808 found_uintmax_t = false
810 # Define HAVE_INTTYPES_H_WITH_UINTMAX if <inttypes.h> exists,
811 # doesn't clash with <sys/types.h>, and declares uintmax_t.
812 # jm_AC_HEADER_INTTYPES_H
813 if cc.compiles('''#include <sys/types.h>
814                   #include <inttypes.h>
815                   void some_func (void) {
816                     uintmax_t i = (uintmax_t) -1;
817                   }''', name : 'uintmax_t in inttypes.h')
818   glib_conf.set('HAVE_INTTYPES_H_WITH_UINTMAX', 1)
819   found_uintmax_t = true
820 endif
822 # Define HAVE_STDINT_H_WITH_UINTMAX if <stdint.h> exists,
823 # doesn't clash with <sys/types.h>, and declares uintmax_t.
824 # jm_AC_HEADER_STDINT_H
825 if cc.compiles('''#include <sys/types.h>
826                   #include <stdint.h>
827                   void some_func (void) {
828                     uintmax_t i = (uintmax_t) -1;
829                   }''', name : 'uintmax_t in stdint.h')
830   glib_conf.set('HAVE_STDINT_H_WITH_UINTMAX', 1)
831   found_uintmax_t = true
832 endif
834 # Define intmax_t to 'long' or 'long long'
835 # if it is not already defined in <stdint.h> or <inttypes.h>.
836 # For simplicity, we assume that a header file defines 'intmax_t' if and
837 # only if it defines 'uintmax_t'.
838 if found_uintmax_t
839   glib_conf.set('HAVE_INTMAX_T', 1)
840 elif have_long_long
841   glib_conf.set('intmax_t', 'long long')
842 else
843   glib_conf.set('intmax_t', 'long')
844 endif
846 char_size = cc.sizeof('char')
847 short_size = cc.sizeof('short')
848 int_size = cc.sizeof('int')
849 voidp_size = cc.sizeof('void*')
850 long_size = cc.sizeof('long')
851 if have_long_long
852   long_long_size = cc.sizeof('long long')
853 else
854   long_long_size = 0
855 endif
856 sizet_size = cc.sizeof('size_t')
857 if cc.get_id() == 'msvc'
858   ssizet_size = cc.sizeof('SSIZE_T', prefix : '#include <BaseTsd.h>')
859 else
860   ssizet_size = cc.sizeof('ssize_t')
861 endif
863 # On Windows, MSVC supports both ll and I64 as format specifiers for 64-bit
864 # integers, but some versions (at least 4.7.x) of MinGW only support I64.
865 if host_system == 'windows'
866   int64_m = 'I64'
867 else
868   int64_m = 'll'
869 endif
871 char_align = cc.alignment('char')
872 short_align = cc.alignment('short')
873 int_align = cc.alignment('int')
874 voidp_align = cc.alignment('void*')
875 long_align = cc.alignment('long')
876 long_long_align = cc.alignment('long long')
877 # NOTE: We don't check for size of __int64 because long long is guaranteed to
878 # be 64-bit in C99, and it is available on all supported compilers
879 sizet_align = cc.alignment('size_t')
881 glib_conf.set('ALIGNOF_UNSIGNED_LONG', long_align)
883 glib_conf.set('SIZEOF_CHAR', char_size)
884 glib_conf.set('SIZEOF_INT', int_size)
885 glib_conf.set('SIZEOF_SHORT', short_size)
886 glib_conf.set('SIZEOF_LONG', long_size)
887 glib_conf.set('SIZEOF_LONG_LONG', long_long_size)
888 glib_conf.set('SIZEOF_SIZE_T', sizet_size)
889 glib_conf.set('SIZEOF_SSIZE_T', ssizet_size)
890 glib_conf.set('SIZEOF_VOID_P', voidp_size)
892 if short_size == 2
893   gint16 = 'short'
894   gint16_modifier='h'
895   gint16_format='hi'
896   guint16_format='hu'
897 elif int_size == 2
898   gint16 = 'int'
899   gint16_modifier=''
900   gint16_format='i'
901   guint16_format='u'
902 else
903   error('Compiler provides no native 16-bit integer type')
904 endif
905 glibconfig_conf.set('gint16', gint16)
906 glibconfig_conf.set_quoted('gint16_modifier', gint16_modifier)
907 glibconfig_conf.set_quoted('gint16_format', gint16_format)
908 glibconfig_conf.set_quoted('guint16_format', guint16_format)
910 if short_size == 4
911   gint32 = 'short'
912   gint32_modifier='h'
913   gint32_format='hi'
914   guint32_format='hu'
915   guint32_align = short_align
916 elif int_size == 4
917   gint32 = 'int'
918   gint32_modifier=''
919   gint32_format='i'
920   guint32_format='u'
921   guint32_align = int_align
922 elif long_size == 4
923   gint32 = 'long'
924   gint32_modifier='l'
925   gint32_format='li'
926   guint32_format='lu'
927   guint32_align = long_align
928 else
929   error('Compiler provides no native 32-bit integer type')
930 endif
931 glibconfig_conf.set('gint32', gint32)
932 glibconfig_conf.set_quoted('gint32_modifier', gint32_modifier)
933 glibconfig_conf.set_quoted('gint32_format', gint32_format)
934 glibconfig_conf.set_quoted('guint32_format', guint32_format)
935 glib_conf.set('ALIGNOF_GUINT32', guint32_align)
937 if int_size == 8
938   gint64 = 'int'
939   gint64_modifier=''
940   gint64_format='i'
941   guint64_format='u'
942   glib_extension=''
943   gint64_constant='(val)'
944   guint64_constant='(val)'
945   guint64_align = int_align
946 elif long_size == 8
947   gint64 = 'long'
948   glib_extension=''
949   gint64_modifier='l'
950   gint64_format='li'
951   guint64_format='lu'
952   gint64_constant='(val##L)'
953   guint64_constant='(val##UL)'
954   guint64_align = long_align
955 elif long_long_size == 8
956   gint64 = 'long long'
957   glib_extension='G_GNUC_EXTENSION '
958   gint64_modifier=int64_m
959   gint64_format=int64_m + 'i'
960   guint64_format=int64_m + 'u'
961   gint64_constant='(G_GNUC_EXTENSION (val##LL))'
962   guint64_constant='(G_GNUC_EXTENSION (val##ULL))'
963   guint64_align = long_long_align
964 else
965   error('Compiler provides no native 64-bit integer type')
966 endif
967 glibconfig_conf.set('glib_extension', glib_extension)
968 glibconfig_conf.set('gint64', gint64)
969 glibconfig_conf.set_quoted('gint64_modifier', gint64_modifier)
970 glibconfig_conf.set_quoted('gint64_format', gint64_format)
971 glibconfig_conf.set_quoted('guint64_format', guint64_format)
972 glibconfig_conf.set('gint64_constant', gint64_constant)
973 glibconfig_conf.set('guint64_constant', guint64_constant)
974 glib_conf.set('ALIGNOF_GUINT64', guint64_align)
976 if host_system == 'windows'
977   glibconfig_conf.set('g_pid_type', 'void*')
978   glibconfig_conf.set_quoted('g_pid_format', 'p')
979   if host_machine.cpu_family() == 'x86_64'
980     glibconfig_conf.set_quoted('g_pollfd_format', '%#I64x')
981   else
982     glibconfig_conf.set_quoted('g_pollfd_format', '%#x')
983   endif
984 else
985   glibconfig_conf.set('g_pid_type', 'int')
986   glibconfig_conf.set_quoted('g_pid_format', 'i')
987   glibconfig_conf.set_quoted('g_pollfd_format', '%d')
988 endif
990 if sizet_size == short_size
991   glibconfig_conf.set('glib_size_type_define', 'short')
992   glibconfig_conf.set_quoted('gsize_modifier', 'h')
993   glibconfig_conf.set_quoted('gssize_modifier', 'h')
994   glibconfig_conf.set_quoted('gsize_format', 'hu')
995   glibconfig_conf.set_quoted('gssize_format', 'hi')
996   glibconfig_conf.set('glib_msize_type', 'SHRT')
997 elif sizet_size == int_size
998   glibconfig_conf.set('glib_size_type_define', 'int')
999   glibconfig_conf.set_quoted('gsize_modifier', '')
1000   glibconfig_conf.set_quoted('gssize_modifier', '')
1001   glibconfig_conf.set_quoted('gsize_format', 'u')
1002   glibconfig_conf.set_quoted('gssize_format', 'i')
1003   glibconfig_conf.set('glib_msize_type', 'INT')
1004 elif sizet_size == long_size
1005   glibconfig_conf.set('glib_size_type_define', 'long')
1006   glibconfig_conf.set_quoted('gsize_modifier', 'l')
1007   glibconfig_conf.set_quoted('gssize_modifier', 'l')
1008   glibconfig_conf.set_quoted('gsize_format', 'lu')
1009   glibconfig_conf.set_quoted('gssize_format', 'li')
1010   glibconfig_conf.set('glib_msize_type', 'LONG')
1011 elif sizet_size == long_long_size
1012   glibconfig_conf.set('glib_size_type_define', 'long long')
1013   glibconfig_conf.set_quoted('gsize_modifier', int64_m)
1014   glibconfig_conf.set_quoted('gssize_modifier', int64_m)
1015   glibconfig_conf.set_quoted('gsize_format', int64_m + 'u')
1016   glibconfig_conf.set_quoted('gssize_format', int64_m + 'i')
1017   glibconfig_conf.set('glib_msize_type', 'INT64')
1018 else
1019   error('Could not determine size of size_t.')
1020 endif
1022 if voidp_size == int_size
1023   glibconfig_conf.set('glib_intptr_type_define', 'int')
1024   glibconfig_conf.set_quoted('gintptr_modifier', '')
1025   glibconfig_conf.set_quoted('gintptr_format', 'i')
1026   glibconfig_conf.set_quoted('guintptr_format', 'u')
1027   glibconfig_conf.set('glib_gpi_cast', '(gint)')
1028   glibconfig_conf.set('glib_gpui_cast', '(guint)')
1029 elif voidp_size == long_size
1030   glibconfig_conf.set('glib_intptr_type_define', 'long')
1031   glibconfig_conf.set_quoted('gintptr_modifier', 'l')
1032   glibconfig_conf.set_quoted('gintptr_format', 'li')
1033   glibconfig_conf.set_quoted('guintptr_format', 'lu')
1034   glibconfig_conf.set('glib_gpi_cast', '(glong)')
1035   glibconfig_conf.set('glib_gpui_cast', '(gulong)')
1036 elif voidp_size == long_long_size
1037   glibconfig_conf.set('glib_intptr_type_define', 'long long')
1038   glibconfig_conf.set_quoted('gintptr_modifier', int64_m)
1039   glibconfig_conf.set_quoted('gintptr_format', int64_m + 'i')
1040   glibconfig_conf.set_quoted('guintptr_format', int64_m + 'u')
1041   glibconfig_conf.set('glib_gpi_cast', '(gint64)')
1042   glibconfig_conf.set('glib_gpui_cast', '(guint64)')
1043 else
1044   error('Could not determine size of void *')
1045 endif
1047 if long_size != 8 and long_long_size != 8 and int_size != 8
1048   error('GLib requires a 64-bit type. You might want to consider using the GNU C compiler.')
1049 endif
1051 glibconfig_conf.set('gintbits', int_size * 8)
1052 glibconfig_conf.set('glongbits', long_size * 8)
1053 glibconfig_conf.set('gsizebits', sizet_size * 8)
1054 glibconfig_conf.set('gssizebits', ssizet_size * 8)
1056 # FIXME: maybe meson should tell us the libsuffix?
1057 if host_system == 'windows'
1058   g_module_suffix = 'dll'
1059 elif host_system == 'darwin'
1060   g_module_suffix = 'dylib'
1061 else
1062   g_module_suffix = 'so'
1063 endif
1064 glibconfig_conf.set('g_module_suffix', g_module_suffix)
1066 glibconfig_conf.set('GLIB_MAJOR_VERSION', major_version)
1067 glibconfig_conf.set('GLIB_MINOR_VERSION', minor_version)
1068 glibconfig_conf.set('GLIB_MICRO_VERSION', micro_version)
1070 glibconfig_conf.set('glib_void_p', voidp_size)
1071 glibconfig_conf.set('glib_long', long_size)
1072 glibconfig_conf.set('glib_size_t', sizet_size)
1073 glibconfig_conf.set('glib_ssize_t', ssizet_size)
1074 if host_machine.endian() == 'big'
1075   glibconfig_conf.set('g_byte_order', 'G_BIG_ENDIAN')
1076   glibconfig_conf.set('g_bs_native', 'BE')
1077   glibconfig_conf.set('g_bs_alien', 'LE')
1078 else
1079   glibconfig_conf.set('g_byte_order', 'G_LITTLE_ENDIAN')
1080   glibconfig_conf.set('g_bs_native', 'LE')
1081   glibconfig_conf.set('g_bs_alien', 'BE')
1082 endif
1084 # === va_copy checks ===
1085 # we currently check for all three va_copy possibilities, so we get
1086 # all results in config.log for bug reports.
1088 va_copy_func = ''
1089 foreach try_func : [ '__va_copy', 'va_copy' ]
1090   if cc.compiles('''#include <stdarg.h>
1091                     #include <stdlib.h>
1092                     #ifdef _MSC_VER
1093                     # include "msvc_recommended_pragmas.h"
1094                     #endif
1095                     void f (int i, ...) {
1096                     va_list args1, args2;
1097                     va_start (args1, i);
1098                     @0@ (args2, args1);
1099                     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1100                       exit (1);
1101                     va_end (args1); va_end (args2);
1102                     }
1103                     int main() {
1104                       f (0, 42);
1105                       return 0;
1106                     }'''.format(try_func),
1107                     name : try_func + ' check')
1108     va_copy_func = try_func
1109   endif
1110 endforeach
1111 if va_copy_func != ''
1112   glib_conf.set('G_VA_COPY', va_copy_func)
1113   glib_vacopy = '#define G_VA_COPY ' + va_copy_func
1114 else
1115   glib_vacopy = '/* #undef G_VA_COPY */'
1116 endif
1118 va_list_val_copy_prog = '''
1119   #include <stdarg.h>
1120   #include <stdlib.h>
1121   void f (int i, ...) {
1122     va_list args1, args2;
1123     va_start (args1, i);
1124     args2 = args1;
1125     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1126       exit (1);
1127     va_end (args1); va_end (args2);
1128   }
1129   int main() {
1130     f (0, 42);
1131     return 0;
1132   }'''
1134 # We do this in two steps so if compilation fails already it looks less alarming
1135 glib_va_val_copy = false
1136 if cc.compiles(va_list_val_copy_prog, name : 'va_lists can be copied as values')
1137   # FIXME: what to do when cross-compiling?
1138   if cc.run(va_list_val_copy_prog, name : 'va_lists can be copied as values').returncode() == 0
1139     glib_va_val_copy = true
1140   endif
1141 endif
1142 if not glib_va_val_copy
1143   glib_va_val_copy = false
1144   glib_vacopy = glib_vacopy + '\n#define G_VA_COPY_AS_ARRAY 1'
1145   glib_conf.set('G_VA_COPY_AS_ARRAY', 1)
1146 endif
1147 glibconfig_conf.set('glib_vacopy', glib_vacopy)
1149 # check for flavours of varargs macros
1150 g_have_iso_c_varargs = cc.compiles('''
1151   void some_func (void) {
1152     int a(int p1, int p2, int p3);
1153     #define call_a(...) a(1,__VA_ARGS__)
1154     call_a(2,3);
1155   }''', name : 'ISO C99 varargs macros in C')
1157 if g_have_iso_c_varargs
1158   glibconfig_conf.set('g_have_iso_c_varargs', '''
1159 #ifndef __cplusplus
1160 # define G_HAVE_ISO_VARARGS 1
1161 #endif''')
1162 endif
1164 g_have_iso_cxx_varargs = cxx.compiles('''
1165   void some_func (void) {
1166     int a(int p1, int p2, int p3);
1167     #define call_a(...) a(1,__VA_ARGS__)
1168     call_a(2,3);
1169   }''', name : 'ISO C99 varargs macros in C++')
1171 if g_have_iso_cxx_varargs
1172   glibconfig_conf.set('g_have_iso_cxx_varargs', '''
1173 #ifdef __cplusplus
1174 # define G_HAVE_ISO_VARARGS 1
1175 #endif''')
1176 endif
1178 g_have_gnuc_varargs = cc.compiles('''
1179   void some_func (void) {
1180     int a(int p1, int p2, int p3);
1181     #define call_a(params...) a(1,params)
1182     call_a(2,3);
1183   }''', name : 'GNUC varargs macros')
1185 if cc.has_header('alloca.h')
1186   glibconfig_conf.set('GLIB_HAVE_ALLOCA_H', true)
1187 endif
1188 has_syspoll = cc.has_header('sys/poll.h')
1189 has_systypes = cc.has_header('sys/types.h')
1190 if has_syspoll
1191   glibconfig_conf.set('GLIB_HAVE_SYS_POLL_H', true)
1192 endif
1193 has_winsock2 = cc.has_header('winsock2.h')
1195 if has_syspoll and has_systypes
1196   templ = '''#include<sys/poll.h>
1197 #include<sys/types.h>
1198 #include<stdio.h>
1199 int main(int argc, char **argv) {
1200   printf("%d\n", (int)@0@);
1201   return 0;
1202 }'''
1203 elif has_winsock2
1204   templ = '''#define _WIN32_WINNT 0x0600
1205 #include <stdio.h>
1206 #include <winsock2.h>
1207 int main(int argc, char **argv) {
1208   printf("%d\n", (int)@0@);
1209   return 0;
1210 }'''
1211 else
1212   # FIXME?
1213   error('FIX POLL* defines')
1214 endif
1216 value_POLLIN = cc.run(templ.format('POLLIN'), name : 'POLLIN value').stdout().strip()
1217 value_POLLOUT = cc.run(templ.format('POLLOUT'), name : 'POLLOUT value').stdout().strip()
1218 value_POLLPRI = cc.run(templ.format('POLLPRI'), name : 'POLLPRI value').stdout().strip()
1219 value_POLLERR = cc.run(templ.format('POLLERR'), name : 'POLLERR value').stdout().strip()
1220 value_POLLHUP = cc.run(templ.format('POLLHUP'), name : 'POLLHUP value').stdout().strip()
1221 value_POLLNVAL = cc.run(templ.format('POLLNVAL'), name : 'POLLNVAL value').stdout().strip()
1223 glibconfig_conf.set('g_pollin', value_POLLIN)
1224 glibconfig_conf.set('g_pollout', value_POLLOUT)
1225 glibconfig_conf.set('g_pollpri', value_POLLPRI)
1226 glibconfig_conf.set('g_pollerr', value_POLLERR)
1227 glibconfig_conf.set('g_pollhup', value_POLLHUP)
1228 glibconfig_conf.set('g_pollnval', value_POLLNVAL)
1230 # Internet address families
1231 # FIXME: what about Cygwin (G_WITH_CYGWIN)
1232 if host_system == 'windows'
1233   glib_inet_includes= '''
1234 #include <winsock2.h>
1236 else
1237   glib_inet_includes='''
1238 #include <sys/types.h>
1239 #include <sys/socket.h>
1241 endif
1243 net_defines = [
1244   [ 'AF_UNIX', 'g_af_unix' ],
1245   [ 'AF_INET', 'g_af_inet' ],
1246   [ 'AF_INET6', 'g_af_inet6' ],
1247   [ 'MSG_OOB', 'g_msg_oob' ],
1248   [ 'MSG_PEEK', 'g_msg_peek' ],
1249   [ 'MSG_DONTROUTE', 'g_msg_dontroute' ],
1251 foreach d : net_defines
1252   templ = '''@0@
1253 #include <stdio.h>
1254 int main(int argc, char **argv) {
1255   printf("%d\n", (int)@1@);
1256   return 0;
1257 }'''
1258   # FIXME: fix for cross-compilation
1259   if not meson.has_exe_wrapper()
1260     error('Fix sys define detection for cross build')
1261   endif
1262   val = cc.run(templ.format(glib_inet_includes, d[0]), name : d[0] + ' value').stdout().strip()
1263   glibconfig_conf.set(d[1], val)
1264 endforeach
1266 glibconfig_conf.set('GLIB_USING_SYSTEM_PRINTF', true) # FIXME!
1268 # We need a more robust approach here...
1269 host_cpu_family = host_machine.cpu_family()
1270 if host_cpu_family == 'x86' or host_cpu_family == 'x86_64' or host_cpu_family == 's390' or host_cpu_family == 's390x' or host_cpu_family.startswith('arm') or host_cpu_family.startswith('crisv32') or host_cpu_family.startswith('etrax')
1271   glib_memory_barrier_needed = false
1272 elif host_cpu_family.startswith('sparc') or host_cpu_family.startswith('alpha') or host_cpu_family.startswith('powerpc') or host_cpu_family == 'ia64'
1273   glib_memory_barrier_needed = true
1274 else
1275   error('Unknown host cpu: ' + host_cpu_family)
1276   glib_memory_barrier_needed = true
1277 endif
1278 glibconfig_conf.set('G_ATOMIC_OP_MEMORY_BARRIER_NEEDED', glib_memory_barrier_needed)
1280 # Note that the atomic ops are only available with GCC on x86 when
1281 # using -march=i486 or higher.  If we detect that the atomic ops are
1282 # not available but would be available given the right flags, we want
1283 # to abort and advise the user to fix their CFLAGS.  It's better to do
1284 # that then to silently fall back on emulated atomic ops just because
1285 # the user had the wrong build environment.
1286 atomictest = '''void func() {
1287   volatile int atomic = 2;
1288   __sync_bool_compare_and_swap (&atomic, 2, 3);
1291 if cc.compiles(atomictest)
1292   glibconfig_conf.set('G_ATOMIC_LOCK_FREE', true)
1293 else
1294   if host_machine.cpu_family() == 'x86' and cc.compiles(atomictest, args : '-march=i486')
1295     error('GLib must be built with -march=i486 or later.')
1296   endif
1297   glibconfig_conf.set('G_ATOMIC_LOCK_FREE', false)
1298 endif
1300 # === Threads ===
1302 # Let meson figure out all this business and whether -pthread or whatnot is needed
1303 # FIXME: probably needs more tweaking in meson for things like -D_REENTRANT etc.
1304 thread_dep = dependency('threads')
1306 # Determination of thread implementation
1307 if host_system == 'windows'
1308   glibconfig_conf.set('g_threads_impl_def', 'WIN32')
1309   glib_conf.set('THREADS_WIN32', 1)
1310 else
1311   glibconfig_conf.set('g_threads_impl_def', 'POSIX')
1312   glib_conf.set('THREADS_POSIX', 1)
1313   if cc.has_header_symbol('pthread.h', 'pthread_attr_setstacksize')
1314     glib_conf.set('HAVE_PTHREAD_ATTR_SETSTACKSIZE', 1)
1315   endif
1316   if cc.has_header_symbol('pthread.h', 'pthread_condattr_setclock')
1317     glib_conf.set('HAVE_PTHREAD_CONDATTR_SETCLOCK', 1)
1318   endif
1319   if cc.has_header_symbol('pthread.h', 'pthread_cond_timedwait_relative_np')
1320     glib_conf.set('HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP', 1)
1321   endif
1322   # Assume that pthread_setname_np is available in some form; same as configure
1323   if cc.links('''#ifndef _GNU_SOURCE
1324               # define _GNU_SOURCE
1325               #endif
1326               #include <pthread.h>
1327               int main() {
1328                 pthread_setname_np("example");
1329               }''',
1330               name : 'pthread_setname_np(const char*)',
1331               dependencies : thread_dep)
1332     # macOS and iOS
1333     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID', 1)
1334   elif cc.links('''#ifndef _GNU_SOURCE
1335                 # define _GNU_SOURCE
1336                 #endif
1337                 #include <pthread.h>
1338                 int main() {
1339                   pthread_setname_np(pthread_self(), "example");
1340                 }''',
1341                 name : 'pthread_setname_np(pthread_t, const char*)',
1342                 dependencies : thread_dep)
1343     # Linux, Solaris, etc.
1344     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITH_TID', 1)
1345   endif
1346 endif
1348 # FIXME: how to do this when cross-compiling?
1349 # FIXME: we should make it print the result and always return 0, so that
1350 # the output in meson shows up as green
1351 stack_grows_check_prog = '''
1352   volatile int *a = 0, *b = 0;
1353   void f (int i) {
1354     volatile int x = 5;
1355     if (i == 0)
1356       b = &x;
1357     else
1358       f (i - 1);
1359   }
1360   int main () {
1361     volatile int y = 7;
1362     a = &y;
1363     f (100);
1364     return b > a ? 0 : 1;
1365   }'''
1366 stack_grows_run_result = cc.run(stack_grows_check_prog, name : 'stack grows check')
1367 if stack_grows_run_result.compiled() and stack_grows_run_result.returncode() == 0
1368   glibconfig_conf.set('G_HAVE_GROWING_STACK', 1)
1369 else
1370   glibconfig_conf.set('G_HAVE_GROWING_STACK', 0)
1371 endif
1373 # Tests for iconv
1375 # First, we check if the C library provides iconv, then GNU libiconv, then
1376 # a native implementation
1377 # FIXME: add option as well
1379 # USE_LIBICONV_GNU: Using GNU libiconv
1380 # USE_LIBICONV_NATIVE: Using a native impl of iconv in a separate library
1382 # We should never use the MinGW C library's iconv. On Windows we use the
1383 # GNU implementation that ships with MinGW.
1385 # On Windows, just always use the built-in implementation
1386 if host_system == 'windows'
1387   libiconv = []
1388   glib_conf.set('USE_LIBICONV_NATIVE', true)
1389 # Check C library; never check MinGW C library
1390 elif cc.has_function('iconv_open')
1391   libiconv = []
1392 # Check for libiconv
1393 elif cc.has_header_symbol('iconv.h', 'libiconv_open')
1394   glib_conf.set('USE_LIBICONV_GNU', true)
1395   libiconv = [cc.find_library('iconv')]
1396 # Check for a custom iconv implementation
1397 elif cc.has_header_symbol('iconv.h', 'iconv_open')
1398   glib_conf.set('USE_LIBICONV_NATIVE', true)
1399   libiconv = [cc.find_library('iconv')]
1400 else
1401   error('No iconv() implementation found in C library or libiconv')
1402 endif
1404 if get_option('with-pcre') == 'internal'
1405   pcre = []
1406   use_system_pcre = false
1407 else
1408   pcre = dependency('libpcre', required : false) # Should check for Unicode support, too. FIXME
1409   if not pcre.found()
1410     if cc.get_id() == 'msvc'
1411     # MSVC: Search for the PCRE library by the configuration, which corresponds
1412     # to the output of CMake builds of PCRE.  Note that debugoptimized
1413     # is really a Release build with .PDB files.
1414       if buildtype == 'debug'
1415         pcre = cc.find_library('pcred', required : false)
1416       else
1417         pcre = cc.find_library('pcre', required : false)
1418       endif
1419     endif
1420   endif
1421   use_system_pcre = pcre.found()
1422 endif
1423 glib_conf.set('USE_SYSTEM_PCRE', use_system_pcre)
1425 use_pcre_static_flag = false
1427 if host_system == 'windows'
1428   if not use_system_pcre
1429     use_pcre_static_flag = true
1430   else
1431     pcre_static = cc.links('''#define PCRE_STATIC
1432                               #include <pcre.h>
1433                               int main() {
1434                                 void *p = NULL;
1435                                 pcre_free(p);
1436                                 return 0;
1437                               }''',
1438                            dependencies: pcre,
1439                            name : 'Windows system PCRE is a static build')
1440     if pcre_static
1441       use_pcre_static_flag = true
1442     endif
1443   endif
1444 endif
1446 libm = cc.find_library('m', required : false)
1447 libffi_dep = dependency('libffi', version : '>= 3.0.0', fallback : ['libffi', 'ffi_dep'])
1448 zlib_libname = '-lz'
1449 if cc.get_id() != 'msvc'
1450   libz_dep = dependency('zlib', fallback : ['zlib', 'zlib_dep'])
1451 else
1452   # MSVC: Don't use the bundled ZLib sources until we are sure that we can't
1453   # find the ZLib .lib
1454   libz_dep = dependency('zlib', required : false)
1456   # MSVC: Search for the ZLib .lib, which corresponds to the results of
1457   # of using ZLib's win32/makefile.msc.
1458   if not libz_dep.found()
1459     libz_dep = cc.find_library('zlib1', required : false)
1460     if libz_dep.found()
1461       zlib_libname = '-lzlib1'
1462     else
1463       libz_dep = cc.find_library('zlib', required : false)
1464       if libz_dep.found()
1465         zlib_libname = '-lzlib'
1466       else
1467         libz_dep = subproject('zlib').get_variable('zlib_dep')
1468       endif
1469     endif
1470   endif
1471 endif
1473 # Only used on non-glibc targets
1474 libintl = cc.find_library('intl', required : false)
1475 if host_system == 'windows' and not libintl.found()
1476   # Used only when the gettext library is not available (MSVC, not MinGW)
1477   libintl = subproject('proxy-libintl').get_variable('intl_dep')
1478   glib_conf.set('HAVE_DCGETTEXT', 1)
1479 else
1480   glib_conf.set('HAVE_DCGETTEXT', cc.has_header_symbol('libintl.h', 'dcgettext'))
1481 endif
1482 # We require gettext to always be present
1483 glib_conf.set('HAVE_GETTEXT', 1)
1484 glib_conf.set_quoted('GLIB_LOCALE_DIR', join_paths(glib_datadir, 'locale'))
1485 # xgettext is optional (on Windows for instance)
1486 xgettext = find_program('xgettext', required : false)
1488 # libmount is only used by gio, but we need to fetch the libs to generate the
1489 # pkg-config file below
1490 libmount_dep = []
1491 libmount_opt = get_option('enable-libmount')
1492 if host_system == 'linux' and libmount_opt != 'no'
1493   libmount_dep = [dependency('mount', version : '>=2.28', required : false)]
1494   if not libmount_dep[0].found()
1495     libmount_dep = [cc.find_library('mount', required : false)]
1496     libmount_h = cc.has_header('libmount/libmount.h')
1497     libmount_needed = libmount_opt == 'yes' and host_system == 'linux'
1498     if libmount_needed and (not libmount_dep[0].found() or not libmount_h)
1499       error('Need libmount but couldn\'t find it')
1500     endif
1501   endif
1502 endif
1504 if host_system == 'windows'
1505   winsock2 = cc.find_library('ws2_32')
1506 endif
1508 python = import('python3').find_python()
1510 # Determine which user environment-dependent files that we want to install
1511 have_bash = find_program('bash', required : false).found() # For completion scripts
1512 have_m4 = find_program('m4', required : false).found() # For m4 macros
1513 have_sh = find_program('sh', required : false).found() # For glib-gettextize
1515 # FIXME: defines in config.h that are not actually used anywhere
1516 # (we add them for now to minimise the diff)
1517 glib_conf.set('HAVE_DLFCN_H', 1)
1518 glib_conf.set('__EXTENSIONS__', 1)
1519 glib_conf.set('STDC_HEADERS', 1)
1520 # THREADS_NONE
1521 glib_conf.set('SIZEOF___INT64', 8)
1523 # Various substs needed for our pkg-config files
1524 # FIXME: Derive these from the dependency() objects (Meson support needed)
1525 glib_conf.set('ZLIB_LIBS', zlib_libname)
1526 glib_conf.set('LIBFFI_LIBS', '-lffi')
1527 if libintl.found()
1528   glib_conf.set('INTLLIBS', '-lintl')
1529 endif
1530 if libiconv.length() != 0
1531   glib_conf.set('ICONV_LIBS', '-liconv')
1532 endif
1533 if use_system_pcre
1534   glib_conf.set('PCRE_LIBS', '-lpcre')
1535 endif
1536 if libmount_dep.length() == 1 and libmount_dep[0].found()
1537   glib_conf.set('LIBMOUNT_LIBS', '-lmount')
1538   glib_conf.set('HAVE_LIBMOUNT', 1)
1539 endif
1540 glib_conf.set('GIO_MODULE_DIR', '${libdir}/gio/modules')
1541 # FIXME: Missing:
1542 # @G_MODULE_LIBS@ @SELINUX_LIBS@ @COCOA_LIBS@ @CARBON_LIBS@ @G_LIBS_EXTRA@
1543 # @PCRE_REQUIRES@ @GLIB_EXTRA_CFLAGS@ @G_THREAD_CFLAGS@
1545 # Tracing: dtrace
1546 want_dtrace = get_option('enable-dtrace')
1547 enable_dtrace = false
1549 # Since dtrace support is opt-in we just error out if it was requested but
1550 # is not available. We don't bother with autodetection yet.
1551 if want_dtrace
1552   if glib_have_carbon
1553     error('GLib dtrace support not yet compatible with macOS dtrace')
1554   endif
1555   dtrace = find_program('dtrace', required : true) # error out if not found
1556   if not cc.has_header('sys/sdt.h')
1557     error('dtrace support needs sys/sdt.h header')
1558   endif
1559   # FIXME: autotools build also passes -fPIC -DPIC but is it needed in this case?
1560   dtrace_obj_gen = generator(dtrace,
1561     output : '@BASENAME@.o',
1562     arguments : ['-G', '-s', '@INPUT@', '-o', '@OUTPUT@'])
1563   # FIXME: $(SED) -e "s,define STAP_HAS_SEMAPHORES 1,undef STAP_HAS_SEMAPHORES,"
1564   #               -e "s,define _SDT_HAS_SEMAPHORES 1,undef _SDT_HAS_SEMAPHORES,"
1565   dtrace_hdr_gen = generator(dtrace,
1566     output : '@BASENAME@.h',
1567     arguments : ['-h', '-s', '@INPUT@', '-o', '@OUTPUT@'])
1568   glib_conf.set('HAVE_DTRACE', 1)
1569   enable_dtrace = true
1570 endif
1572 # systemtap
1573 want_systemtap = get_option('enable-systemtap')
1574 enable_systemtap = false
1576 if want_systemtap and enable_dtrace
1577   tapset_install_dir = get_option('tapset-install-dir')
1578   if tapset_install_dir == ''
1579     tapset_install_dir = join_paths(get_option('datadir'), 'systemtap/tapset', host_machine.cpu_family())
1580   endif
1581   stp_cdata = configuration_data()
1582   stp_cdata.set('ABS_GLIB_RUNTIME_LIBDIR', glib_libdir)
1583   stp_cdata.set('LT_CURRENT', minor_version.to_int() * 100)
1584   stp_cdata.set('LT_REVISION', micro_version.to_int())
1585   enable_systemtap = true
1586 endif
1589 subdir('glib')
1590 subdir('gobject')
1591 subdir('gthread')
1592 subdir('gmodule')
1593 subdir('gio')
1594 if xgettext.found()
1595   subdir('po')
1596 endif
1597 subdir('tests')
1599 # Configure and install pkg-config files
1600 pc_files = [
1601   'gobject-2.0.pc',
1602   'glib-2.0.pc',
1603   'gthread-2.0.pc',
1604   'gmodule-2.0.pc',
1605   'gmodule-export-2.0.pc',
1606   'gmodule-no-export-2.0.pc',
1607   'gio-2.0.pc',
1609 if host_system == 'windows'
1610   pc_files += ['gio-windows-2.0.pc']
1611 else
1612   pc_files += ['gio-unix-2.0.pc']
1613 endif
1615 foreach pc : pc_files
1616   configure_file(input : pc + '.in',
1617     install : true,
1618     install_dir : glib_pkgconfigreldir,
1619     output : pc,
1620     configuration : glib_conf)
1621 endforeach
1623 # NOTE: We skip glib-zip.in because the filenames it assumes don't match ours
1625 # Install glib-gettextize executable, if a UNIX-style shell is found
1626 if have_sh
1627   configure_file(input : 'glib-gettextize.in',
1628     install : true,
1629     install_dir : 'bin',
1630     output : 'glib-gettextize',
1631     configuration : glib_conf)
1632 endif
1634 if have_m4
1635   # Install m4 macros that other projects use
1636   install_data('m4macros/glib-2.0.m4', 'm4macros/glib-gettext.m4', 'm4macros/gsettings.m4',
1637     install_dir : join_paths(get_option('datadir'), 'aclocal'))
1638 endif
1640 if host_system != 'windows'
1641   # Install Valgrind suppression file (except on Windows,
1642   # as Valgrind is currently not supported on Windows)
1643   install_data('glib.supp',
1644     install_dir : join_paths(get_option('datadir'), 'glib-2.0', 'valgrind'))
1645 endif
1647 configure_file(input : 'config.h.meson',
1648   output : 'config.h',
1649   configuration : glib_conf)
1651 if host_system == 'windows'
1652   install_headers([ 'msvc_recommended_pragmas.h' ], subdir : 'glib-2.0')
1653 endif
1655 if get_option('with-man') != 'no'
1656   xsltproc = find_program('xsltproc', required : false)
1657   if not xsltproc.found() and get_option('with-man') == 'yes'
1658     error('man pages enabled and xsltproc not found')
1659   endif
1660   xsltproc_command = [
1661     xsltproc,
1662     '--nonet',
1663     '--stringparam', 'man.output.quietly', '1',
1664     '--stringparam', 'funcsynopsis.style', 'ansi',
1665     '--stringparam', 'man.th.extra1.suppress', '1',
1666     '--stringparam', 'man.authors.section.enabled', '0',
1667     '--stringparam', 'man.copyright.section.enabled', '0',
1668     '-o', '@OUTPUT@',
1669     'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl',
1670     '@INPUT@',
1671   ]
1672   man1_dir = get_option('mandir') + '/man1'
1673 endif
1675 gnome = import('gnome')
1676 subdir('docs/reference/glib')
1677 subdir('docs/reference/gobject')
1678 subdir('docs/reference/gio')