Imported Upstream version 2.54.3
[platform/upstream/glib.git] / meson.build
1 project('glib', 'c', 'cpp',
2   version : '2.54.3',
3   meson_version : '>= 0.38.1',
4   default_options : [
5     'warning_level=1',
6     'c_std=gnu89'
7   ]
8 )
9
10 cc = meson.get_compiler('c')
11 cxx = meson.get_compiler('cpp')
12
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
27
28 host_system = host_machine.system()
29
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()
36
37 interface_age = minor_version.is_odd() ? 0 : micro_version
38 binary_age = 100 * minor_version + micro_version
39
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)
44
45 configinc = include_directories('.')
46 glibinc = include_directories('glib')
47 gobjectinc = include_directories('gobject')
48 gmoduleinc = include_directories('gmodule')
49 gioinc = include_directories('gio')
50
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')
58
59 glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
60
61 add_project_arguments('-D_GNU_SOURCE', language: 'c')
62
63 ########################
64 # Configuration begins #
65 ########################
66 glib_conf = configuration_data()
67 glibconfig_conf = configuration_data()
68
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 = ''
72
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)
87
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)
98
99 glib_conf.set('_GNU_SOURCE', 1)
100
101 if host_system == 'windows'
102   # Poll doesn't work on devices on Windows
103   glib_conf.set('BROKEN_POLL', true)
104 endif
105
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
122
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)
131
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')
135
136 # check for header files
137
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',
187 ]
188
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
196
197 if cc.has_header('linux/netlink.h')
198   glib_conf.set('HAVE_NETLINK', 1)
199 endif
200
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
206
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
222 '''
223
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>''' ],
241 ]
242
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
257
258 # Windows Support (Vista+)
259 if host_system == 'windows'
260   glib_conf.set('_WIN32_WINNT', '0x0601')
261 endif
262
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',
321 ]
322
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
333
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
352
353 # AIX splice is something else
354 if host_system != 'aix'
355   functions += ['splice']
356 endif
357
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
368
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
385
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
395
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
404
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
413
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
419
420 if cc.has_header_symbol('dlfcn.h', 'RTLD_LAZY')
421   glib_conf.set('HAVE_RTLD_LAZY', 1)
422 endif
423
424 if cc.has_header_symbol('dlfcn.h', 'RTLD_NOW')
425   glib_conf.set('HAVE_RTLD_NOW', 1)
426 endif
427
428 if cc.has_header_symbol('dlfcn.h', 'RTLD_GLOBAL')
429   glib_conf.set('HAVE_RTLD_GLOBAL', 1)
430 endif
431
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)
453
454 # Mac OS X Carbon support
455 glib_have_carbon = cc.compiles('''#include <Carbon/Carbon.h>
456                                   #include <CoreServices/CoreServices.h>''',
457                                name : 'Mac OS X Carbon support')
458
459 glib_have_os_x_9_or_later = false
460
461 if glib_have_carbon
462   glib_conf.set('HAVE_CARBON', true)
463   CARBON_LIBS='-Wl,-framework,Carbon' # FIXME: propagate to .pc files as well
464   glib_have_os_x_9_or_later = cc.compiles('''#include <AvailabilityMacros.h>
465                                              #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
466                                              #error Compiling for minimum OS X version before 10.9
467                                              #endif''', name : 'OS X 9 or later')
468 else
469   CARBON_LIBS=''
470 endif
471
472 # Mac OS X Cocoa support
473 glib_have_cocoa = cc.compiles('''#include <Cocoa/Cocoa.h>
474                                  #ifdef GNUSTEP_BASE_VERSION
475                                  #error "Detected GNUstep, not Cocoa"
476                                  #endif''',
477                               name : 'Mac OS X Cocoa support')
478
479 if glib_have_cocoa
480   glib_conf.set('HAVE_COCOA', true)
481   COCOA_LIBS='-Wl,-framework,Foundation' # FIXME: propagate to .pc files as well
482 else
483   COCOA_LIBS=''
484 endif
485
486 # Check for futex(2)
487 if cc.links('''#include <linux/futex.h>
488                #include <sys/syscall.h>
489                #include <unistd.h>
490                int main (int argc, char ** argv) {
491                  syscall (__NR_futex, NULL, FUTEX_WAKE, FUTEX_WAIT);
492                  return 0;
493                }''', name : 'futex(2) system call')
494   glib_conf.set('HAVE_FUTEX', 1)
495 endif
496
497 # Check for eventfd(2)
498 if cc.links('''#include <sys/eventfd.h>
499                #include <unistd.h>
500                int main (int argc, char ** argv) {
501                  eventfd (0, EFD_CLOEXEC);
502                  return 0;
503                }''', name : 'eventfd(2) system call')
504   glib_conf.set('HAVE_EVENTFD', 1)
505 endif
506
507 clock_gettime_test_code = '''
508   #include <time.h>
509   struct timespec t;
510   int main (int argc, char ** argv) {
511     return clock_gettime(CLOCK_REALTIME, &t);
512   }'''
513 librt = []
514 if cc.links(clock_gettime_test_code, name : 'clock_gettime')
515   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
516 elif cc.links(clock_gettime_test_code, args : '-lrt', name : 'clock_gettime in librt')
517   glib_conf.set('HAVE_CLOCK_GETTIME', 1)
518   librt = cc.find_library('rt')
519 endif
520
521 # if statfs() takes 2 arguments (Posix) or 4 (Solaris)
522 if have_func_statfs
523   if cc.compiles(glib_conf_prefix + '''
524                  #include <unistd.h>
525                         #ifdef HAVE_SYS_PARAM_H
526                         #include <sys/param.h>
527                         #endif
528                         #ifdef HAVE_SYS_VFS_H
529                         #include <sys/vfs.h>
530                         #endif
531                         #ifdef HAVE_SYS_MOUNT_H
532                         #include <sys/mount.h>
533                         #endif
534                         #ifdef HAVE_SYS_STATFS_H
535                         #include <sys/statfs.h>
536                         #endif
537                         void some_func (void) {
538                           struct statfs st;
539                           statfs("/", &st);
540                         }''', name : 'number of arguments to statfs() (n=2)')
541     glib_conf.set('STATFS_ARGS', 2)
542   elif cc.compiles(glib_conf_prefix + '''
543                    #include <unistd.h>
544                           #ifdef HAVE_SYS_PARAM_H
545                           #include <sys/param.h>
546                           #endif
547                           #ifdef HAVE_SYS_VFS_H
548                           #include <sys/vfs.h>
549                           #endif
550                           #ifdef HAVE_SYS_MOUNT_H
551                           #include <sys/mount.h>
552                           #endif
553                           #ifdef HAVE_SYS_STATFS_H
554                           #include <sys/statfs.h>
555                           #endif
556                           void some_func (void) {
557                             struct statfs st;
558                             statfs("/", &st, sizeof (st), 0);
559                           }''', name : 'number of arguments to statfs() (n=4)')
560     glib_conf.set('STATFS_ARGS', 4)
561   else
562     error('Unable to determine number of arguments to statfs()')
563   endif
564 endif
565
566 # open takes O_DIRECTORY as an option
567 #AC_MSG_CHECKING([])
568 if cc.compiles('''#include <fcntl.h>
569                   #include <sys/types.h>
570                   #include <sys/stat.h>],
571                   void some_func (void) {
572                     open(0, O_DIRECTORY, 0);
573                   }''', name : 'open() option O_DIRECTORY')
574   glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
575 endif
576
577 # Check whether there is a vsnprintf() function with C99 semantics installed.
578 # AC_FUNC_VSNPRINTF_C99
579 # Check whether there is a snprintf() function with C99 semantics installed.
580 # AC_FUNC_SNPRINTF_C99
581
582 have_good_vsnprintf = false
583 have_good_snprintf = false
584
585 if host_system == 'windows'
586   # Unfortunately the mingw and Visual Studio 2015+ implementations of C99-style
587   # snprintf and vsnprintf don't seem to be quite good enough, at least not in
588   # mingw-runtime-3.14.  (Sorry, I don't know exactly what is the problem,
589   # but it is related to floating point formatting and decimal point vs. comma.)
590   # The simple tests in AC_FUNC_VSNPRINTF_C99 and AC_FUNC_SNPRINTF_C99 aren't
591   # rigorous enough to notice, though.
592   glib_conf.set('HAVE_C99_SNPRINTF', false)
593   glib_conf.set('HAVE_C99_VSNPRINTF', false)
594 else
595   vsnprintf_c99_test_code = '''
596 #include <stdio.h>
597 #include <stdarg.h>
598
599 int
600 doit(char * s, ...)
601 {
602   char buffer[32];
603   va_list args;
604   int r;
605
606   va_start(args, s);
607   r = vsnprintf(buffer, 5, s, args);
608   va_end(args);
609
610   if (r != 7)
611     exit(1);
612
613   /* AIX 5.1 and Solaris seems to have a half-baked vsnprintf()
614      implementation. The above will return 7 but if you replace
615      the size of the buffer with 0, it borks! */
616   va_start(args, s);
617   r = vsnprintf(buffer, 0, s, args);
618   va_end(args);
619
620   if (r != 7)
621     exit(1);
622
623   exit(0);
624 }
625
626 int
627 main(void)
628 {
629   doit("1234567");
630   exit(1);
631 }'''
632
633   rres = cc.run(vsnprintf_c99_test_code, name : 'C99 vsnprintf')
634   if rres.compiled() and rres.returncode() == 0
635     glib_conf.set('HAVE_C99_VSNPRINTF', 1)
636     have_good_vsnprintf = true
637   endif
638
639   snprintf_c99_test_code = '''
640 #include <stdio.h>
641 #include <stdarg.h>
642
643 int
644 doit()
645 {
646   char buffer[32];
647   va_list args;
648   int r;
649
650   r = snprintf(buffer, 5, "1234567");
651
652   if (r != 7)
653     exit(1);
654
655   r = snprintf(buffer, 0, "1234567");
656
657   if (r != 7)
658     exit(1);
659
660   r = snprintf(NULL, 0, "1234567");
661
662   if (r != 7)
663     exit(1);
664
665   exit(0);
666 }
667
668 int
669 main(void)
670 {
671   doit();
672   exit(1);
673 }'''
674
675   rres = cc.run(snprintf_c99_test_code, name : 'C99 snprintf')
676   if rres.compiled() and rres.returncode() == 0
677     glib_conf.set('HAVE_C99_SNPRINTF', 1)
678     have_good_snprintf = true
679   endif
680 endif
681
682 if host_system == 'windows'
683   glib_conf.set_quoted('EXEEXT', '.exe')
684 else
685   glib_conf.set('EXEEXT', '')
686 endif
687
688 if have_good_vsnprintf and have_good_snprintf
689   # Our printf is 'good' only if vsnpintf()/snprintf() supports C99 well enough
690   glib_conf.set('HAVE_GOOD_PRINTF', 1) # FIXME: Check for HAVE_UNIX98_PRINTF?
691 else
692   glib_conf.set('HAVE_VASPRINTF', 1)
693 endif
694
695 # Check whether the printf() family supports Unix98 %n$ positional parameters
696 # AC_FUNC_PRINTF_UNIX98
697 # Nothing uses HAVE_UNIX98_PRINTF
698
699
700 # Check for nl_langinfo and CODESET
701 # FIXME: Check for HAVE_BIND_TEXTDOMAIN_CODESET
702 if cc.links('''#include <langinfo.h>
703                int main (int argc, char ** argv) {
704                  char *codeset = nl_langinfo (CODESET);
705                  return 0;
706                }''', name : 'nl_langinfo and CODESET')
707   glib_conf.set('HAVE_LANGINFO_CODESET', 1)
708   glib_conf.set('HAVE_CODESET', 1)
709 endif
710
711 # Check for nl_langinfo and LC_TIME parts that are needed in gdatetime.c
712 if cc.links('''#include <langinfo.h>
713                int main (int argc, char ** argv) {
714                  char *str;
715                  str = nl_langinfo (PM_STR);
716                  str = nl_langinfo (D_T_FMT);
717                  str = nl_langinfo (D_FMT);
718                  str = nl_langinfo (T_FMT);
719                  str = nl_langinfo (T_FMT_AMPM);
720                  str = nl_langinfo (MON_1);
721                  str = nl_langinfo (ABMON_12);
722                  str = nl_langinfo (DAY_1);
723                  str = nl_langinfo (ABDAY_7);
724                  return 0;
725                }''', name : 'nl_langinfo (PM_STR)')
726   glib_conf.set('HAVE_LANGINFO_TIME', 1)
727 endif
728 if cc.links('''#include <langinfo.h>
729                int main (int argc, char ** argv) {
730                  char *str;
731                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB);
732                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT1_MB);
733                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT2_MB);
734                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT3_MB);
735                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT4_MB);
736                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT5_MB);
737                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT6_MB);
738                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT7_MB);
739                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT8_MB);
740                  str = nl_langinfo (_NL_CTYPE_OUTDIGIT9_MB);
741                  return 0;
742                }''', name : 'nl_langinfo (_NL_CTYPE_OUTDIGITn_MB)')
743   glib_conf.set('HAVE_LANGINFO_OUTDIGIT', 1)
744 endif
745
746 # Check if C compiler supports the 'signed' keyword
747 if not cc.compiles('''signed char x;''', name : 'signed')
748   glib_conf.set('signed', '/* NOOP */')
749 endif
750
751 # Check if the ptrdiff_t type exists
752 if cc.has_header_symbol('stddef.h', 'ptrdiff_t')
753   glib_conf.set('HAVE_PTRDIFF_T', 1)
754 endif
755
756 # Check for sig_atomic_t type
757 if cc.links('''#include <signal.h>
758                #include <sys/types.h>
759                sig_atomic_t val = 42;
760                int main (int argc, char ** argv) {
761                  return val == 42 ? 0 : 1;
762                }''', name : 'sig_atomic_t')
763   glib_conf.set('HAVE_SIG_ATOMIC_T', 1)
764 endif
765
766 # Check if 'long long' works and what format can be used to print it
767 # jm_AC_TYPE_LONG_LONG
768 # Nothing uses HAVE_LONG_LONG_FORMAT and HAVE_INT64_AND_I64
769 if cc.compiles('''long long ll = 1LL;
770                   int i = 63;
771                   int some_func (void) {
772                     long long llmax = (long long) -1;
773                     return ll << i | ll >> i | llmax / ll | llmax % ll;
774                   }''', name : 'long long')
775   glib_conf.set('HAVE_LONG_LONG', 1)
776   have_long_long = true
777 else
778   have_long_long = false
779 endif
780
781 # Test whether the compiler supports the 'long double' type.
782 if cc.compiles('''/* The Stardent Vistra knows sizeof(long double), but does not support it.  */
783                   long double foo = 0.0;
784                   /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
785                   int array [2*(sizeof(long double) >= sizeof(double)) - 1];''',
786                name : 'long double')
787   glib_conf.set('HAVE_LONG_DOUBLE', 1)
788 endif
789
790 #dnl Test whether <stddef.h> has the 'wchar_t' type.
791 if cc.has_header_symbol('stddef.h', 'wchar_t')
792   glib_conf.set('HAVE_WCHAR_T', 1)
793 endif
794
795 # Test whether <wchar.h> has the 'wint_t' type.
796 if cc.has_header_symbol('wchar.h', 'wint_t')
797   glib_conf.set('HAVE_WINT_T', 1)
798 endif
799
800 found_uintmax_t = false
801
802 # Define HAVE_INTTYPES_H_WITH_UINTMAX if <inttypes.h> exists,
803 # doesn't clash with <sys/types.h>, and declares uintmax_t.
804 # jm_AC_HEADER_INTTYPES_H
805 if cc.compiles('''#include <sys/types.h>
806                   #include <inttypes.h>
807                   void some_func (void) {
808                     uintmax_t i = (uintmax_t) -1;
809                   }''', name : 'uintmax_t in inttypes.h')
810   glib_conf.set('HAVE_INTTYPES_H_WITH_UINTMAX', 1)
811   found_uintmax_t = true
812 endif
813
814 # Define HAVE_STDINT_H_WITH_UINTMAX if <stdint.h> exists,
815 # doesn't clash with <sys/types.h>, and declares uintmax_t.
816 # jm_AC_HEADER_STDINT_H
817 if cc.compiles('''#include <sys/types.h>
818                   #include <stdint.h>
819                   void some_func (void) {
820                     uintmax_t i = (uintmax_t) -1;
821                   }''', name : 'uintmax_t in stdint.h')
822   glib_conf.set('HAVE_STDINT_H_WITH_UINTMAX', 1)
823   found_uintmax_t = true
824 endif
825
826 # Define intmax_t to 'long' or 'long long'
827 # if it is not already defined in <stdint.h> or <inttypes.h>.
828 # For simplicity, we assume that a header file defines 'intmax_t' if and
829 # only if it defines 'uintmax_t'.
830 if found_uintmax_t
831   glib_conf.set('HAVE_INTMAX_T', 1)
832 elif have_long_long
833   glib_conf.set('intmax_t', 'long long')
834 else
835   glib_conf.set('intmax_t', 'long')
836 endif
837
838 char_size = cc.sizeof('char')
839 short_size = cc.sizeof('short')
840 int_size = cc.sizeof('int')
841 voidp_size = cc.sizeof('void*')
842 long_size = cc.sizeof('long')
843 if have_long_long
844   long_long_size = cc.sizeof('long long')
845 else
846   long_long_size = 0
847 endif
848 sizet_size = cc.sizeof('size_t')
849 if cc.get_id() == 'msvc'
850   ssizet_size = cc.sizeof('SSIZE_T', prefix : '#include <BaseTsd.h>')
851 else
852   ssizet_size = cc.sizeof('ssize_t')
853 endif
854
855 # On Windows, MSVC supports both ll and I64 as format specifiers for 64-bit
856 # integers, but some versions (at least 4.7.x) of MinGW only support I64.
857 if host_system == 'windows'
858   int64_m = 'I64'
859 else
860   int64_m = 'll'
861 endif
862
863 char_align = cc.alignment('char')
864 short_align = cc.alignment('short')
865 int_align = cc.alignment('int')
866 voidp_align = cc.alignment('void*')
867 long_align = cc.alignment('long')
868 long_long_align = cc.alignment('long long')
869 # NOTE: We don't check for size of __int64 because long long is guaranteed to
870 # be 64-bit in C99, and it is available on all supported compilers
871 sizet_align = cc.alignment('size_t')
872
873 glib_conf.set('ALIGNOF_UNSIGNED_LONG', long_align)
874
875 glib_conf.set('SIZEOF_CHAR', char_size)
876 glib_conf.set('SIZEOF_INT', int_size)
877 glib_conf.set('SIZEOF_SHORT', short_size)
878 glib_conf.set('SIZEOF_LONG', long_size)
879 glib_conf.set('SIZEOF_LONG_LONG', long_long_size)
880 glib_conf.set('SIZEOF_SIZE_T', sizet_size)
881 glib_conf.set('SIZEOF_SSIZE_T', ssizet_size)
882 glib_conf.set('SIZEOF_VOID_P', voidp_size)
883
884 if short_size == 2
885   gint16 = 'short'
886   gint16_modifier='h'
887   gint16_format='hi'
888   guint16_format='hu'
889 elif int_size == 2
890   gint16 = 'int'
891   gint16_modifier=''
892   gint16_format='i'
893   guint16_format='u'
894 else
895   error('Compiler provides no native 16-bit integer type')
896 endif
897 glibconfig_conf.set('gint16', gint16)
898 glibconfig_conf.set_quoted('gint16_modifier', gint16_modifier)
899 glibconfig_conf.set_quoted('gint16_format', gint16_format)
900 glibconfig_conf.set_quoted('guint16_format', guint16_format)
901
902 if short_size == 4
903   gint32 = 'short'
904   gint32_modifier='h'
905   gint32_format='hi'
906   guint32_format='hu'
907   guint32_align = short_align
908 elif int_size == 4
909   gint32 = 'int'
910   gint32_modifier=''
911   gint32_format='i'
912   guint32_format='u'
913   guint32_align = int_align
914 elif long_size == 4
915   gint32 = 'long'
916   gint32_modifier='l'
917   gint32_format='li'
918   guint32_format='lu'
919   guint32_align = long_align
920 else
921   error('Compiler provides no native 32-bit integer type')
922 endif
923 glibconfig_conf.set('gint32', gint32)
924 glibconfig_conf.set_quoted('gint32_modifier', gint32_modifier)
925 glibconfig_conf.set_quoted('gint32_format', gint32_format)
926 glibconfig_conf.set_quoted('guint32_format', guint32_format)
927 glib_conf.set('ALIGNOF_GUINT32', guint32_align)
928
929 if int_size == 8
930   gint64 = 'int'
931   gint64_modifier=''
932   gint64_format='i'
933   guint64_format='u'
934   glib_extension=''
935   gint64_constant='(val)'
936   guint64_constant='(val)'
937   guint64_align = int_align
938 elif long_size == 8
939   gint64 = 'long'
940   glib_extension=''
941   gint64_modifier='l'
942   gint64_format='li'
943   guint64_format='lu'
944   gint64_constant='(val##L)'
945   guint64_constant='(val##UL)'
946   guint64_align = long_align
947 elif long_long_size == 8
948   gint64 = 'long long'
949   glib_extension='G_GNUC_EXTENSION '
950   gint64_modifier=int64_m
951   gint64_format=int64_m + 'i'
952   guint64_format=int64_m + 'u'
953   gint64_constant='(G_GNUC_EXTENSION (val##LL))'
954   guint64_constant='(G_GNUC_EXTENSION (val##ULL))'
955   guint64_align = long_long_align
956 else
957   error('Compiler provides no native 64-bit integer type')
958 endif
959 glibconfig_conf.set('glib_extension', glib_extension)
960 glibconfig_conf.set('gint64', gint64)
961 glibconfig_conf.set_quoted('gint64_modifier', gint64_modifier)
962 glibconfig_conf.set_quoted('gint64_format', gint64_format)
963 glibconfig_conf.set_quoted('guint64_format', guint64_format)
964 glibconfig_conf.set('gint64_constant', gint64_constant)
965 glibconfig_conf.set('guint64_constant', guint64_constant)
966 glib_conf.set('ALIGNOF_GUINT64', guint64_align)
967
968 if host_system == 'windows'
969   glibconfig_conf.set('g_pid_type', 'void*')
970   glibconfig_conf.set_quoted('g_pid_format', 'p')
971   if host_machine.cpu_family() == 'x86_64'
972     glibconfig_conf.set_quoted('g_pollfd_format', '%#I64x')
973   else
974     glibconfig_conf.set_quoted('g_pollfd_format', '%#x')
975   endif
976 else
977   glibconfig_conf.set('g_pid_type', 'int')
978   glibconfig_conf.set_quoted('g_pid_format', 'i')
979   glibconfig_conf.set_quoted('g_pollfd_format', '%d')
980 endif
981
982 if sizet_size == short_size
983   glibconfig_conf.set('glib_size_type_define', 'short')
984   glibconfig_conf.set_quoted('gsize_modifier', 'h')
985   glibconfig_conf.set_quoted('gssize_modifier', 'h')
986   glibconfig_conf.set_quoted('gsize_format', 'hu')
987   glibconfig_conf.set_quoted('gssize_format', 'hi')
988   glibconfig_conf.set('glib_msize_type', 'SHRT')
989 elif sizet_size == int_size
990   glibconfig_conf.set('glib_size_type_define', 'int')
991   glibconfig_conf.set_quoted('gsize_modifier', '')
992   glibconfig_conf.set_quoted('gssize_modifier', '')
993   glibconfig_conf.set_quoted('gsize_format', 'u')
994   glibconfig_conf.set_quoted('gssize_format', 'i')
995   glibconfig_conf.set('glib_msize_type', 'INT')
996 elif sizet_size == long_size
997   glibconfig_conf.set('glib_size_type_define', 'long')
998   glibconfig_conf.set_quoted('gsize_modifier', 'l')
999   glibconfig_conf.set_quoted('gssize_modifier', 'l')
1000   glibconfig_conf.set_quoted('gsize_format', 'lu')
1001   glibconfig_conf.set_quoted('gssize_format', 'li')
1002   glibconfig_conf.set('glib_msize_type', 'LONG')
1003 elif sizet_size == long_long_size
1004   glibconfig_conf.set('glib_size_type_define', 'long long')
1005   glibconfig_conf.set_quoted('gsize_modifier', int64_m)
1006   glibconfig_conf.set_quoted('gssize_modifier', int64_m)
1007   glibconfig_conf.set_quoted('gsize_format', int64_m + 'u')
1008   glibconfig_conf.set_quoted('gssize_format', int64_m + 'i')
1009   glibconfig_conf.set('glib_msize_type', 'INT64')
1010 else
1011   error('Could not determine size of size_t.')
1012 endif
1013
1014 if voidp_size == int_size
1015   glibconfig_conf.set('glib_intptr_type_define', 'int')
1016   glibconfig_conf.set_quoted('gintptr_modifier', '')
1017   glibconfig_conf.set_quoted('gintptr_format', 'i')
1018   glibconfig_conf.set_quoted('guintptr_format', 'u')
1019   glibconfig_conf.set('glib_gpi_cast', '(gint)')
1020   glibconfig_conf.set('glib_gpui_cast', '(guint)')
1021 elif voidp_size == long_size
1022   glibconfig_conf.set('glib_intptr_type_define', 'long')
1023   glibconfig_conf.set_quoted('gintptr_modifier', 'l')
1024   glibconfig_conf.set_quoted('gintptr_format', 'li')
1025   glibconfig_conf.set_quoted('guintptr_format', 'lu')
1026   glibconfig_conf.set('glib_gpi_cast', '(glong)')
1027   glibconfig_conf.set('glib_gpui_cast', '(gulong)')
1028 elif voidp_size == long_long_size
1029   glibconfig_conf.set('glib_intptr_type_define', 'long long')
1030   glibconfig_conf.set_quoted('gintptr_modifier', int64_m)
1031   glibconfig_conf.set_quoted('gintptr_format', int64_m + 'i')
1032   glibconfig_conf.set_quoted('guintptr_format', int64_m + 'u')
1033   glibconfig_conf.set('glib_gpi_cast', '(gint64)')
1034   glibconfig_conf.set('glib_gpui_cast', '(guint64)')
1035 else
1036   error('Could not determine size of void *')
1037 endif
1038
1039 if long_size != 8 and long_long_size != 8 and int_size != 8
1040   error('GLib requires a 64-bit type. You might want to consider using the GNU C compiler.')
1041 endif
1042
1043 glibconfig_conf.set('gintbits', int_size * 8)
1044 glibconfig_conf.set('glongbits', long_size * 8)
1045 glibconfig_conf.set('gsizebits', sizet_size * 8)
1046 glibconfig_conf.set('gssizebits', ssizet_size * 8)
1047
1048 # FIXME: maybe meson should tell us the libsuffix?
1049 if host_system == 'windows'
1050   g_module_suffix = 'dll'
1051 elif host_system == 'darwin'
1052   g_module_suffix = 'dylib'
1053 else
1054   g_module_suffix = 'so'
1055 endif
1056 glibconfig_conf.set('g_module_suffix', g_module_suffix)
1057
1058 glibconfig_conf.set('GLIB_MAJOR_VERSION', major_version)
1059 glibconfig_conf.set('GLIB_MINOR_VERSION', minor_version)
1060 glibconfig_conf.set('GLIB_MICRO_VERSION', micro_version)
1061
1062 glibconfig_conf.set('glib_void_p', voidp_size)
1063 glibconfig_conf.set('glib_long', long_size)
1064 glibconfig_conf.set('glib_size_t', sizet_size)
1065 glibconfig_conf.set('glib_ssize_t', ssizet_size)
1066 if host_machine.endian() == 'big'
1067   glibconfig_conf.set('g_byte_order', 'G_BIG_ENDIAN')
1068   glibconfig_conf.set('g_bs_native', 'BE')
1069   glibconfig_conf.set('g_bs_alien', 'LE')
1070 else
1071   glibconfig_conf.set('g_byte_order', 'G_LITTLE_ENDIAN')
1072   glibconfig_conf.set('g_bs_native', 'LE')
1073   glibconfig_conf.set('g_bs_alien', 'BE')
1074 endif
1075
1076 # === va_copy checks ===
1077 # we currently check for all three va_copy possibilities, so we get
1078 # all results in config.log for bug reports.
1079
1080 va_copy_func = ''
1081 foreach try_func : [ '__va_copy', 'va_copy' ]
1082   if cc.compiles('''#include <stdarg.h>
1083                     #include <stdlib.h>
1084                     #ifdef _MSC_VER
1085                     # include "msvc_recommended_pragmas.h"
1086                     #endif
1087                     void f (int i, ...) {
1088                     va_list args1, args2;
1089                     va_start (args1, i);
1090                     @0@ (args2, args1);
1091                     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1092                       exit (1);
1093                     va_end (args1); va_end (args2);
1094                     }
1095                     int main() {
1096                       f (0, 42);
1097                       return 0;
1098                     }'''.format(try_func),
1099                     name : try_func + ' check')
1100     va_copy_func = try_func
1101   endif
1102 endforeach
1103 if va_copy_func != ''
1104   glib_conf.set('G_VA_COPY', va_copy_func)
1105   glib_vacopy = '#define G_VA_COPY ' + va_copy_func
1106 else
1107   glib_vacopy = '/* #undef G_VA_COPY */'
1108 endif
1109
1110 va_list_val_copy_prog = '''
1111   #include <stdarg.h>
1112   #include <stdlib.h>
1113   void f (int i, ...) {
1114     va_list args1, args2;
1115     va_start (args1, i);
1116     args2 = args1;
1117     if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
1118       exit (1);
1119     va_end (args1); va_end (args2);
1120   }
1121   int main() {
1122     f (0, 42);
1123     return 0;
1124   }'''
1125
1126 # We do this in two steps so if compilation fails already it looks less alarming
1127 glib_va_val_copy = false
1128 if cc.compiles(va_list_val_copy_prog, name : 'va_lists can be copied as values')
1129   # FIXME: what to do when cross-compiling?
1130   if cc.run(va_list_val_copy_prog, name : 'va_lists can be copied as values').returncode() == 0
1131     glib_va_val_copy = true
1132   endif
1133 endif
1134 if not glib_va_val_copy
1135   glib_va_val_copy = false
1136   glib_vacopy = glib_vacopy + '\n#define G_VA_COPY_AS_ARRAY 1'
1137   glib_conf.set('G_VA_COPY_AS_ARRAY', 1)
1138 endif
1139 glibconfig_conf.set('glib_vacopy', glib_vacopy)
1140
1141 # check for flavours of varargs macros
1142 g_have_iso_c_varargs = cc.compiles('''
1143   void some_func (void) {
1144     int a(int p1, int p2, int p3);
1145     #define call_a(...) a(1,__VA_ARGS__)
1146     call_a(2,3);
1147   }''', name : 'ISO C99 varargs macros in C')
1148
1149 if g_have_iso_c_varargs
1150   glibconfig_conf.set('g_have_iso_c_varargs', '''
1151 #ifndef __cplusplus
1152 # define G_HAVE_ISO_VARARGS 1
1153 #endif''')
1154 endif
1155
1156 g_have_iso_cxx_varargs = cxx.compiles('''
1157   void some_func (void) {
1158     int a(int p1, int p2, int p3);
1159     #define call_a(...) a(1,__VA_ARGS__)
1160     call_a(2,3);
1161   }''', name : 'ISO C99 varargs macros in C++')
1162
1163 if g_have_iso_cxx_varargs
1164   glibconfig_conf.set('g_have_iso_cxx_varargs', '''
1165 #ifdef __cplusplus
1166 # define G_HAVE_ISO_VARARGS 1
1167 #endif''')
1168 endif
1169
1170 g_have_gnuc_varargs = cc.compiles('''
1171   void some_func (void) {
1172     int a(int p1, int p2, int p3);
1173     #define call_a(params...) a(1,params)
1174     call_a(2,3);
1175   }''', name : 'GNUC varargs macros')
1176
1177 if cc.has_header('alloca.h')
1178   glibconfig_conf.set('GLIB_HAVE_ALLOCA_H', true)
1179 endif
1180 has_syspoll = cc.has_header('sys/poll.h')
1181 has_systypes = cc.has_header('sys/types.h')
1182 if has_syspoll
1183   glibconfig_conf.set('GLIB_HAVE_SYS_POLL_H', true)
1184 endif
1185 has_winsock2 = cc.has_header('winsock2.h')
1186
1187 if has_syspoll and has_systypes
1188   templ = '''#include<sys/poll.h>
1189 #include<sys/types.h>
1190 #include<stdio.h>
1191 int main(int argc, char **argv) {
1192   printf("%d\n", (int)@0@);
1193   return 0;
1194 }'''
1195 elif has_winsock2
1196   templ = '''#define _WIN32_WINNT 0x0600
1197 #include <stdio.h>
1198 #include <winsock2.h>
1199 int main(int argc, char **argv) {
1200   printf("%d\n", (int)@0@);
1201   return 0;
1202 }'''
1203 else
1204   # FIXME?
1205   error('FIX POLL* defines')
1206 endif
1207
1208 value_POLLIN = cc.run(templ.format('POLLIN'), name : 'POLLIN value').stdout().strip()
1209 value_POLLOUT = cc.run(templ.format('POLLOUT'), name : 'POLLOUT value').stdout().strip()
1210 value_POLLPRI = cc.run(templ.format('POLLPRI'), name : 'POLLPRI value').stdout().strip()
1211 value_POLLERR = cc.run(templ.format('POLLERR'), name : 'POLLERR value').stdout().strip()
1212 value_POLLHUP = cc.run(templ.format('POLLHUP'), name : 'POLLHUP value').stdout().strip()
1213 value_POLLNVAL = cc.run(templ.format('POLLNVAL'), name : 'POLLNVAL value').stdout().strip()
1214
1215 glibconfig_conf.set('g_pollin', value_POLLIN)
1216 glibconfig_conf.set('g_pollout', value_POLLOUT)
1217 glibconfig_conf.set('g_pollpri', value_POLLPRI)
1218 glibconfig_conf.set('g_pollerr', value_POLLERR)
1219 glibconfig_conf.set('g_pollhup', value_POLLHUP)
1220 glibconfig_conf.set('g_pollnval', value_POLLNVAL)
1221
1222 # Internet address families
1223 # FIXME: what about Cygwin (G_WITH_CYGWIN)
1224 if host_system == 'windows'
1225   glib_inet_includes= '''
1226 #include <winsock2.h>
1227 '''
1228 else
1229   glib_inet_includes='''
1230 #include <sys/types.h>
1231 #include <sys/socket.h>
1232 '''
1233 endif
1234
1235 net_defines = [
1236   [ 'AF_UNIX', 'g_af_unix' ],
1237   [ 'AF_INET', 'g_af_inet' ],
1238   [ 'AF_INET6', 'g_af_inet6' ],
1239   [ 'MSG_OOB', 'g_msg_oob' ],
1240   [ 'MSG_PEEK', 'g_msg_peek' ],
1241   [ 'MSG_DONTROUTE', 'g_msg_dontroute' ],
1242 ]
1243 foreach d : net_defines
1244   templ = '''@0@
1245 #include <stdio.h>
1246 int main(int argc, char **argv) {
1247   printf("%d\n", (int)@1@);
1248   return 0;
1249 }'''
1250   # FIXME: fix for cross-compilation
1251   if not meson.has_exe_wrapper()
1252     error('Fix sys define detection for cross build')
1253   endif
1254   val = cc.run(templ.format(glib_inet_includes, d[0]), name : d[0] + ' value').stdout().strip()
1255   glibconfig_conf.set(d[1], val)
1256 endforeach
1257
1258 glibconfig_conf.set('GLIB_USING_SYSTEM_PRINTF', true) # FIXME!
1259
1260 # We need a more robust approach here...
1261 host_cpu_family = host_machine.cpu_family()
1262 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')
1263   glib_memory_barrier_needed = false
1264 elif host_cpu_family.startswith('sparc') or host_cpu_family.startswith('alpha') or host_cpu_family.startswith('powerpc') or host_cpu_family == 'ia64'
1265   glib_memory_barrier_needed = true
1266 else
1267   error('Unknown host cpu: ' + host_cpu_family)
1268   glib_memory_barrier_needed = true
1269 endif
1270 glibconfig_conf.set('G_ATOMIC_OP_MEMORY_BARRIER_NEEDED', glib_memory_barrier_needed)
1271
1272 # Note that the atomic ops are only available with GCC on x86 when
1273 # using -march=i486 or higher.  If we detect that the atomic ops are
1274 # not available but would be available given the right flags, we want
1275 # to abort and advise the user to fix their CFLAGS.  It's better to do
1276 # that then to silently fall back on emulated atomic ops just because
1277 # the user had the wrong build environment.
1278 atomictest = '''void func() {
1279   volatile int atomic = 2;
1280   __sync_bool_compare_and_swap (&atomic, 2, 3);
1281 }
1282 '''
1283 if cc.compiles(atomictest)
1284   glibconfig_conf.set('G_ATOMIC_LOCK_FREE', true)
1285 else
1286   if host_machine.cpu_family() == 'x86' and cc.compiles(atomictest, args : '-march=i486')
1287     error('GLib must be built with -march=i486 or later.')
1288   endif
1289   glibconfig_conf.set('G_ATOMIC_LOCK_FREE', false)
1290 endif
1291
1292 # === Threads ===
1293
1294 # Let meson figure out all this business and whether -pthread or whatnot is needed
1295 # FIXME: probably needs more tweaking in meson for things like -D_REENTRANT etc.
1296 thread_dep = dependency('threads')
1297
1298 # Determination of thread implementation
1299 if host_system == 'windows'
1300   glibconfig_conf.set('g_threads_impl_def', 'WIN32')
1301   glib_conf.set('THREADS_WIN32', 1)
1302 else
1303   glibconfig_conf.set('g_threads_impl_def', 'POSIX')
1304   glib_conf.set('THREADS_POSIX', 1)
1305   if cc.has_header_symbol('pthread.h', 'pthread_attr_setstacksize')
1306     glib_conf.set('HAVE_PTHREAD_ATTR_SETSTACKSIZE', 1)
1307   endif
1308   if cc.has_header_symbol('pthread.h', 'pthread_condattr_setclock')
1309     glib_conf.set('HAVE_PTHREAD_CONDATTR_SETCLOCK', 1)
1310   endif
1311   if cc.has_header_symbol('pthread.h', 'pthread_cond_timedwait_relative_np')
1312     glib_conf.set('HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP', 1)
1313   endif
1314   # Assume that pthread_setname_np is available in some form; same as configure
1315   if cc.links('''#ifndef _GNU_SOURCE
1316               # define _GNU_SOURCE
1317               #endif
1318               #include <pthread.h>
1319               int main() {
1320                 pthread_setname_np("example");
1321               }''',
1322               name : 'pthread_setname_np(const char*)',
1323               dependencies : thread_dep)
1324     # macOS and iOS
1325     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID', 1)
1326   elif cc.links('''#ifndef _GNU_SOURCE
1327                 # define _GNU_SOURCE
1328                 #endif
1329                 #include <pthread.h>
1330                 int main() {
1331                   pthread_setname_np(pthread_self(), "example");
1332                 }''',
1333                 name : 'pthread_setname_np(pthread_t, const char*)',
1334                 dependencies : thread_dep)
1335     # Linux, Solaris, etc.
1336     glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITH_TID', 1)
1337   endif
1338 endif
1339
1340 # FIXME: how to do this when cross-compiling?
1341 # FIXME: we should make it print the result and always return 0, so that
1342 # the output in meson shows up as green
1343 stack_grows_check_prog = '''
1344   volatile int *a = 0, *b = 0;
1345   void f (int i) {
1346     volatile int x = 5;
1347     if (i == 0)
1348       b = &x;
1349     else
1350       f (i - 1);
1351   }
1352   int main () {
1353     volatile int y = 7;
1354     a = &y;
1355     f (100);
1356     return b > a ? 0 : 1;
1357   }'''
1358 stack_grows_run_result = cc.run(stack_grows_check_prog, name : 'stack grows check')
1359 if stack_grows_run_result.compiled() and stack_grows_run_result.returncode() == 0
1360   glibconfig_conf.set('G_HAVE_GROWING_STACK', 1)
1361 else
1362   glibconfig_conf.set('G_HAVE_GROWING_STACK', 0)
1363 endif
1364
1365 # Tests for iconv
1366 #
1367 # First, we check if the C library provides iconv, then GNU libiconv, then
1368 # a native implementation
1369 # FIXME: add option as well
1370 #
1371 # USE_LIBICONV_GNU: Using GNU libiconv
1372 # USE_LIBICONV_NATIVE: Using a native impl of iconv in a separate library
1373 #
1374 # We should never use the MinGW C library's iconv. On Windows we use the
1375 # GNU implementation that ships with MinGW.
1376
1377 # On Windows, just always use the built-in implementation
1378 if host_system == 'windows'
1379   libiconv = []
1380   glib_conf.set('USE_LIBICONV_NATIVE', true)
1381 # Check C library; never check MinGW C library
1382 elif cc.has_function('iconv_open')
1383   libiconv = []
1384 # Check for libiconv
1385 elif cc.has_header_symbol('iconv.h', 'libiconv_open')
1386   glib_conf.set('USE_LIBICONV_GNU', true)
1387   libiconv = [cc.find_library('iconv')]
1388 # Check for a custom iconv implementation
1389 elif cc.has_header_symbol('iconv.h', 'iconv_open')
1390   glib_conf.set('USE_LIBICONV_NATIVE', true)
1391   libiconv = [cc.find_library('iconv')]
1392 else
1393   error('No iconv() implementation found in C library or libiconv')
1394 endif
1395
1396 if get_option('with-pcre') == 'internal'
1397   pcre = []
1398   use_system_pcre = false
1399 else
1400   pcre = dependency('libpcre', required : false) # Should check for Unicode support, too. FIXME
1401   if not pcre.found()
1402     if cc.get_id() == 'msvc'
1403     # MSVC: Search for the PCRE library by the configuration, which corresponds
1404     # to the output of CMake builds of PCRE.  Note that debugoptimized
1405     # is really a Release build with .PDB files.
1406       if buildtype == 'debug'
1407         pcre = cc.find_library('pcred', required : false)
1408       else
1409         pcre = cc.find_library('pcre', required : false)
1410       endif
1411     endif
1412   endif
1413   use_system_pcre = pcre.found()
1414 endif
1415 glib_conf.set('USE_SYSTEM_PCRE', use_system_pcre)
1416
1417 use_pcre_static_flag = false
1418
1419 if host_system == 'windows'
1420   if not use_system_pcre
1421     use_pcre_static_flag = true
1422   else
1423     pcre_static = cc.links('''#define PCRE_STATIC
1424                               #include <pcre.h>
1425                               int main() {
1426                                 void *p = NULL;
1427                                 pcre_free(p);
1428                                 return 0;
1429                               }''',
1430                            dependencies: pcre,
1431                            name : 'Windows system PCRE is a static build')
1432     if pcre_static
1433       use_pcre_static_flag = true
1434     endif
1435   endif
1436 endif
1437
1438 libm = cc.find_library('m', required : false)
1439 libffi_dep = dependency('libffi', version : '>= 3.0.0', fallback : ['libffi', 'ffi_dep'])
1440 zlib_libname = '-lz'
1441 if cc.get_id() != 'msvc'
1442   libz_dep = dependency('zlib', fallback : ['zlib', 'zlib_dep'])
1443 else
1444   # MSVC: Don't use the bundled ZLib sources until we are sure that we can't
1445   # find the ZLib .lib
1446   libz_dep = dependency('zlib', required : false)
1447
1448   # MSVC: Search for the ZLib .lib, which corresponds to the results of
1449   # of using ZLib's win32/makefile.msc.
1450   if not libz_dep.found()
1451     libz_dep = cc.find_library('zlib1', required : false)
1452     if libz_dep.found()
1453       zlib_libname = '-lzlib1'
1454     else
1455       libz_dep = cc.find_library('zlib', required : false)
1456       if libz_dep.found()
1457         zlib_libname = '-lzlib'
1458       else
1459         zlib_dep = subproject('zlib').get_variable('zlib_dep')
1460       endif
1461     endif
1462   endif
1463 endif
1464
1465 # Only used on non-glibc targets
1466 libintl = cc.find_library('intl', required : false)
1467 if host_system == 'windows' and not libintl.found()
1468   # Used only when the gettext library is not available (MSVC, not MinGW)
1469   libintl = subproject('proxy-libintl').get_variable('intl_dep')
1470   glib_conf.set('HAVE_DCGETTEXT', 1)
1471 else
1472   glib_conf.set('HAVE_DCGETTEXT', cc.has_header_symbol('libintl.h', 'dcgettext'))
1473 endif
1474 # We require gettext to always be present
1475 glib_conf.set('HAVE_GETTEXT', 1)
1476 glib_conf.set_quoted('GLIB_LOCALE_DIR', join_paths(glib_datadir, 'locale'))
1477 # xgettext is optional (on Windows for instance)
1478 xgettext = find_program('xgettext', required : false)
1479
1480 # libmount is only used by gio, but we need to fetch the libs to generate the
1481 # pkg-config file below
1482 libmount_dep = []
1483 libmount_opt = get_option('enable-libmount')
1484 if host_system == 'linux' and libmount_opt != 'no'
1485   libmount_dep = [dependency('mount', version : '>=2.28', required : false)]
1486   if not libmount_dep[0].found()
1487     libmount_dep = [cc.find_library('mount')]
1488     libmount_h = cc.has_header('libmount/libmount.h')
1489     libmount_needed = libmount_opt == 'yes' and host_system == 'linux'
1490     if libmount_needed and (not libmount_dep[0].found() or not libmount_h)
1491       error('Need libmount but couldn\'t find it')
1492     endif
1493   endif
1494 endif
1495
1496 if host_system == 'windows'
1497   winsock2 = cc.find_library('ws2_32')
1498 endif
1499
1500 python = import('python3').find_python()
1501
1502 # Determine which user environment-dependent files that we want to install
1503 have_bash = find_program('bash', required : false).found() # For completion scripts
1504 have_m4 = find_program('m4', required : false).found() # For m4 macros
1505 have_sh = find_program('sh', required : false).found() # For glib-gettextize
1506
1507 # FIXME: defines in config.h that are not actually used anywhere
1508 # (we add them for now to minimise the diff)
1509 glib_conf.set('HAVE_DLFCN_H', 1)
1510 glib_conf.set('__EXTENSIONS__', 1)
1511 glib_conf.set('STDC_HEADERS', 1)
1512 # THREADS_NONE
1513 glib_conf.set('SIZEOF___INT64', 8)
1514
1515 # Various substs needed for our pkg-config files
1516 # FIXME: Derive these from the dependency() objects (Meson support needed)
1517 glib_conf.set('ZLIB_LIBS', zlib_libname)
1518 glib_conf.set('LIBFFI_LIBS', '-lffi')
1519 if libintl.found()
1520   glib_conf.set('INTLLIBS', '-lintl')
1521 endif
1522 if libiconv.length() != 0
1523   glib_conf.set('ICONV_LIBS', '-liconv')
1524 endif
1525 if use_system_pcre
1526   glib_conf.set('PCRE_LIBS', '-lpcre')
1527 endif
1528 if libmount_dep.length() == 1 and libmount_dep[0].found()
1529   glib_conf.set('LIBMOUNT_LIBS', '-lmount')
1530 endif
1531 glib_conf.set('GIO_MODULE_DIR', '${libdir}/gio/modules')
1532 # FIXME: Missing:
1533 # @G_MODULE_LIBS@ @SELINUX_LIBS@ @COCOA_LIBS@ @CARBON_LIBS@ @G_LIBS_EXTRA@
1534 # @PCRE_REQUIRES@ @GLIB_EXTRA_CFLAGS@ @G_THREAD_CFLAGS@
1535
1536 # Tracing: dtrace
1537 want_dtrace = get_option('enable-dtrace')
1538 enable_dtrace = false
1539
1540 # Since dtrace support is opt-in we just error out if it was requested but
1541 # is not available. We don't bother with autodetection yet.
1542 if want_dtrace
1543   if glib_have_carbon
1544     error('GLib dtrace support not yet compatible with macOS dtrace')
1545   endif
1546   dtrace = find_program('dtrace', required : true) # error out if not found
1547   if not cc.has_header('sys/sdt.h')
1548     error('dtrace support needs sys/sdt.h header')
1549   endif
1550   # FIXME: autotools build also passes -fPIC -DPIC but is it needed in this case?
1551   dtrace_obj_gen = generator(dtrace,
1552     output : '@BASENAME@.o',
1553     arguments : ['-G', '-s', '@INPUT@', '-o', '@OUTPUT@'])
1554   # FIXME: $(SED) -e "s,define STAP_HAS_SEMAPHORES 1,undef STAP_HAS_SEMAPHORES,"
1555   #               -e "s,define _SDT_HAS_SEMAPHORES 1,undef _SDT_HAS_SEMAPHORES,"
1556   dtrace_hdr_gen = generator(dtrace,
1557     output : '@BASENAME@.h',
1558     arguments : ['-h', '-s', '@INPUT@', '-o', '@OUTPUT@'])
1559   glib_conf.set('HAVE_DTRACE', 1)
1560   enable_dtrace = true
1561 endif
1562
1563 # systemtap
1564 want_systemtap = get_option('enable-systemtap')
1565 enable_systemtap = false
1566
1567 if want_systemtap and enable_dtrace
1568   tapset_install_dir = get_option('tapset-install-dir')
1569   if tapset_install_dir == ''
1570     tapset_install_dir = join_paths(get_option('datadir'), 'systemtap/tapset')
1571   endif
1572   stp_cdata = configuration_data()
1573   stp_cdata.set('ABS_GLIB_RUNTIME_LIBDIR', glib_libdir)
1574   stp_cdata.set('LT_CURRENT', minor_version.to_int() * 100)
1575   stp_cdata.set('LT_REVISION', micro_version.to_int())
1576   enable_systemtap = true
1577 endif
1578
1579
1580 subdir('glib')
1581 subdir('gobject')
1582 subdir('gthread')
1583 subdir('gmodule')
1584 subdir('gio')
1585 if xgettext.found()
1586   subdir('po')
1587 endif
1588 subdir('tests')
1589
1590 # Configure and install pkg-config files
1591 pc_files = [
1592   'gobject-2.0.pc',
1593   'glib-2.0.pc',
1594   'gthread-2.0.pc',
1595   'gmodule-2.0.pc',
1596   'gmodule-export-2.0.pc',
1597   'gmodule-no-export-2.0.pc',
1598   'gio-2.0.pc',
1599 ]
1600 if host_system == 'windows'
1601   pc_files += ['gio-windows-2.0.pc']
1602 else
1603   pc_files += ['gio-unix-2.0.pc']
1604 endif
1605
1606 foreach pc : pc_files
1607   configure_file(input : pc + '.in',
1608     install : true,
1609     install_dir : glib_pkgconfigreldir,
1610     output : pc,
1611     configuration : glib_conf)
1612 endforeach
1613
1614 # NOTE: We skip glib-zip.in because the filenames it assumes don't match ours
1615
1616 # Install glib-gettextize executable, if a UNIX-style shell is found
1617 if have_sh
1618   configure_file(input : 'glib-gettextize.in',
1619     install : true,
1620     install_dir : 'bin',
1621     output : 'glib-gettextize',
1622     configuration : glib_conf)
1623 endif
1624
1625 if have_m4
1626   # Install m4 macros that other projects use
1627   install_data('m4macros/glib-2.0.m4', 'm4macros/glib-gettext.m4', 'm4macros/gsettings.m4',
1628     install_dir : join_paths(get_option('datadir'), 'aclocal'))
1629 endif
1630
1631 if host_system != 'windows'
1632   # Install Valgrind suppression file (except on Windows,
1633   # as Valgrind is currently not supported on Windows)
1634   install_data('glib.supp',
1635     install_dir : join_paths(get_option('datadir'), 'glib-2.0', 'valgrind'))
1636 endif
1637
1638 configure_file(input : 'config.h.meson',
1639   output : 'config.h',
1640   configuration : glib_conf)
1641
1642 if host_system == 'windows'
1643   install_headers([ 'msvc_recommended_pragmas.h' ], subdir : 'glib-2.0')
1644 endif
1645
1646 if get_option('with-man') != 'no'
1647   xsltproc = find_program('xsltproc', required : false)
1648   if not xsltproc.found() and get_option('with-man') == 'yes'
1649     error('man pages enabled and xsltproc not found')
1650   endif
1651   xsltproc_command = [
1652     xsltproc,
1653     '--nonet',
1654     '--stringparam', 'man.output.quietly', '1',
1655     '--stringparam', 'funcsynopsis.style', 'ansi',
1656     '--stringparam', 'man.th.extra1.suppress', '1',
1657     '--stringparam', 'man.authors.section.enabled', '0',
1658     '--stringparam', 'man.copyright.section.enabled', '0',
1659     '-o', '@OUTPUT@',
1660     'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl',
1661     '@INPUT@',
1662   ]
1663   man1_dir = get_option('mandir') + '/man1'
1664 endif
1665
1666 gnome = import('gnome')
1667 subdir('docs/reference/glib')
1668 subdir('docs/reference/gobject')
1669 subdir('docs/reference/gio')