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