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