meson.build 76.6 KB
Newer Older
1
project('glib', 'c', 'cpp',
2
  version : '2.67.0',
3 4
  # NOTE: We keep this pinned at 0.49 because that's what Debian 10 ships
  meson_version : '>= 0.49.2',
Patrick Griffis's avatar
Patrick Griffis committed
5
  default_options : [
6
    'buildtype=debugoptimized',
Patrick Griffis's avatar
Patrick Griffis committed
7
    'warning_level=1',
8
    'c_std=gnu99'
Patrick Griffis's avatar
Patrick Griffis committed
9 10
  ]
)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
11 12

cc = meson.get_compiler('c')
13
cxx = meson.get_compiler('cpp')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
14

15 16
cc_can_run = not meson.is_cross_build() or meson.has_exe_wrapper()

17 18 19 20 21
if cc.get_id() == 'msvc'
  # Ignore several spurious warnings for things glib does very commonly
  # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it
  # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once
  # NOTE: Only add warnings here if you are sure they're spurious
22 23
  add_project_arguments('/wd4035', '/wd4715', '/wd4116',
    '/wd4046', '/wd4068', '/wo4090', '/FImsvc_recommended_pragmas.h',language : 'c')
24 25 26
  # Disable SAFESEH with MSVC for plugins and libs that use external deps that
  # are built with MinGW
  noseh_link_args = ['/SAFESEH:NO']
27 28
  # Set the input and exec encoding to utf-8, like is the default with GCC
  add_project_arguments(cc.get_supported_arguments(['/utf-8']), language: 'c')
29 30 31 32
else
  noseh_link_args = []
  # -mms-bitfields vs -fnative-struct ?
endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
33

34
host_system = host_machine.system()
35

36 37 38 39 40 41 42 43 44 45
if host_system == 'darwin'
  ios_test_code = '''#include <TargetConditionals.h>
  #if ! TARGET_OS_IPHONE
  #error "Not iOS/tvOS/watchOS/iPhoneSimulator"
  #endif'''
  if cc.compiles(ios_test_code, name : 'building for iOS')
    host_system = 'ios'
  endif
endif

46
glib_version = meson.project_version()
Patrick Griffis's avatar
Patrick Griffis committed
47
glib_api_version = '2.0'
48
version_arr = glib_version.split('.')
49 50 51 52 53 54
major_version = version_arr[0].to_int()
minor_version = version_arr[1].to_int()
micro_version = version_arr[2].to_int()

interface_age = minor_version.is_odd() ? 0 : micro_version
binary_age = 100 * minor_version + micro_version
55 56 57 58

soversion = 0
# Maintain compatibility with previous libtool versioning
# current = minor * 100 + micro
59 60 61
current = binary_age - interface_age
library_version = '@0@.@1@.@2@'.format(soversion, current, interface_age)
darwin_versions = [current + 1, '@0@.@1@'.format(current + 1, interface_age)]
62 63 64 65 66 67 68 69

configinc = include_directories('.')
glibinc = include_directories('glib')
gobjectinc = include_directories('gobject')
gmoduleinc = include_directories('gmodule')
gioinc = include_directories('gio')

glib_prefix = get_option('prefix')
70
glib_bindir = join_paths(glib_prefix, get_option('bindir'))
Patrick Griffis's avatar
Patrick Griffis committed
71
glib_libdir = join_paths(glib_prefix, get_option('libdir'))
Xavier Claessens's avatar
Xavier Claessens committed
72
glib_libexecdir = join_paths(glib_prefix, get_option('libexecdir'))
Patrick Griffis's avatar
Patrick Griffis committed
73 74 75
glib_datadir = join_paths(glib_prefix, get_option('datadir'))
glib_pkgdatadir = join_paths(glib_datadir, 'glib-2.0')
glib_includedir = join_paths(glib_prefix, get_option('includedir'))
76 77 78
if get_option('gio_module_dir') != ''
  glib_giomodulesdir = join_paths(glib_prefix, get_option('gio_module_dir'))
else
79 80
  glib_giomodulesdir = join_paths(glib_libdir, 'gio', 'modules')
endif
81

82
glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
83

84 85 86 87 88 89
if get_option('charsetalias_dir') != ''
  glib_charsetaliasdir = join_paths(glib_prefix, get_option('charsetalias_dir'))
else
  glib_charsetaliasdir = glib_libdir
endif

Xavier Claessens's avatar
Xavier Claessens committed
90 91 92 93
installed_tests_metadir = join_paths(glib_datadir, 'installed-tests', meson.project_name())
installed_tests_execdir = join_paths(glib_libexecdir, 'installed-tests', meson.project_name())
installed_tests_enabled = get_option('installed_tests')
installed_tests_template = files('template.test.in')
94
installed_tests_template_tap = files('template-tap.test.in')
Xavier Claessens's avatar
Xavier Claessens committed
95

96 97 98
# Don’t build the tests unless we can run them (either natively, in an exe wrapper, or by installing them for later use)
build_tests = not meson.is_cross_build() or (meson.is_cross_build() and meson.has_exe_wrapper()) or installed_tests_enabled

99 100
add_project_arguments('-D_GNU_SOURCE', language: 'c')

101 102
# Disable strict aliasing;
# see https://bugzilla.gnome.org/show_bug.cgi?id=791622
103 104 105
if cc.has_argument('-fno-strict-aliasing')
  add_project_arguments('-fno-strict-aliasing', language: 'c')
endif
106

107 108 109
########################
# Configuration begins #
########################
Jussi Pakkanen's avatar
Jussi Pakkanen committed
110 111 112
glib_conf = configuration_data()
glibconfig_conf = configuration_data()

113 114 115 116
# accumulated list of defines as we check for them, so we can easily
# use them later in test programs (autoconf does this automatically)
glib_conf_prefix = ''

Jussi Pakkanen's avatar
Jussi Pakkanen committed
117 118 119
glib_conf.set('GLIB_MAJOR_VERSION', major_version)
glib_conf.set('GLIB_MINOR_VERSION', minor_version)
glib_conf.set('GLIB_MICRO_VERSION', micro_version)
120 121
glib_conf.set('GLIB_INTERFACE_AGE', interface_age)
glib_conf.set('GLIB_BINARY_AGE', binary_age)
Patrick Griffis's avatar
Patrick Griffis committed
122
glib_conf.set_quoted('GETTEXT_PACKAGE', 'glib20')
123
glib_conf.set_quoted('PACKAGE_BUGREPORT', 'https://gitlab.gnome.org/GNOME/glib/issues/new')
Patrick Griffis's avatar
Patrick Griffis committed
124 125 126 127 128
glib_conf.set_quoted('PACKAGE_NAME', 'glib')
glib_conf.set_quoted('PACKAGE_STRING', 'glib @0@'.format(meson.project_version()))
glib_conf.set_quoted('PACKAGE_TARNAME', 'glib')
glib_conf.set_quoted('PACKAGE_URL', '')
glib_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
Jussi Pakkanen's avatar
Jussi Pakkanen committed
129
glib_conf.set('ENABLE_NLS', 1)
130

131 132 133
# used by the .rc.in files
glibconfig_conf.set('LT_CURRENT_MINUS_AGE', soversion)

134
glib_conf.set('_GNU_SOURCE', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
135

136 137 138 139 140
if host_system == 'windows'
  # Poll doesn't work on devices on Windows
  glib_conf.set('BROKEN_POLL', true)
endif

141
if host_system == 'windows' and cc.get_id() != 'msvc' and cc.get_id() != 'clang-cl'
142 143 144 145 146 147 148
  # FIXME: Ideally we shouldn't depend on this on Windows and should use
  # 64 bit capable Windows API that also works with MSVC.
  # The autotools build did set this for mingw and while meson sets it
  # for gcc/clang by default, it doesn't do so on Windows.
  glib_conf.set('_FILE_OFFSET_BITS', 64)
endif

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
# Check for GNU visibility attributes
g_have_gnuc_visibility = cc.compiles('''
  void
  __attribute__ ((visibility ("hidden")))
       f_hidden (void)
  {
  }
  void
  __attribute__ ((visibility ("internal")))
       f_internal (void)
  {
  }
  void
  __attribute__ ((visibility ("default")))
       f_default (void)
  {
  }
  int main (void)
  {
    f_hidden();
    f_internal();
    f_default();
    return 0;
  }
  ''',
  # Not supported by MSVC, but MSVC also won't support visibility,
  # so it's OK to pass -Werror explicitly. Replace with
  # override_options : 'werror=true' once that is supported
  args: ['-Werror'],
  name : 'GNU C visibility attributes test')

if g_have_gnuc_visibility
  glibconfig_conf.set('G_HAVE_GNUC_VISIBILITY', '1')
endif

184
# Detect and set symbol visibility
185
glib_hidden_visibility_args = []
186
if get_option('default_library') != 'static'
LRN's avatar
LRN committed
187
  if host_system == 'windows' or host_system == 'cygwin'
188
    glib_conf.set('DLL_EXPORT', true)
189
    if cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl'
190
      glib_conf.set('_GLIB_EXTERN', '__declspec(dllexport) extern')
191
    elif cc.has_argument('-fvisibility=hidden')
192
      glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) __declspec(dllexport) extern')
193
      glib_hidden_visibility_args = ['-fvisibility=hidden']
194
    endif
195
  elif cc.has_argument('-fvisibility=hidden')
196
    glib_conf.set('_GLIB_EXTERN', '__attribute__((visibility("default"))) extern')
197
    glib_hidden_visibility_args = ['-fvisibility=hidden']
198 199 200
  endif
endif

201
if get_option('default_library') == 'static'
202 203 204 205
    glibconfig_conf.set('GLIB_STATIC_COMPILATION', '1')
    glibconfig_conf.set('GOBJECT_STATIC_COMPILATION', '1')
endif

LRN's avatar
LRN committed
206 207 208 209 210 211
# Cygwin glib port maintainers made it clear
# (via the patches they apply) that they want no
# part of glib W32 code, therefore we do not define
# G_PLATFORM_WIN32 for host_system == 'cygwin'.
# This makes G_PLATFORM_WIN32 a synonym for
# G_OS_WIN32.
212
if host_system == 'windows'
Jussi Pakkanen's avatar
Jussi Pakkanen committed
213
  glib_os = '''#define G_OS_WIN32
214
#define G_PLATFORM_WIN32'''
LRN's avatar
LRN committed
215
elif host_system == 'cygwin'
216
  glib_os = '''#define G_OS_UNIX
LRN's avatar
LRN committed
217
#define G_WITH_CYGWIN'''
Jussi Pakkanen's avatar
Jussi Pakkanen committed
218 219 220 221 222
else
  glib_os = '#define G_OS_UNIX'
endif
glibconfig_conf.set('glib_os', glib_os)

223 224 225 226 227 228 229 230 231 232 233
# We need to know the CRT being used to determine what .lib files we need on
# Visual Studio for dependencies that don't normally come with pkg-config files
vs_crt = 'release'
vs_crt_opt = get_option('b_vscrt')
if vs_crt_opt in ['mdd', 'mtd']
  vs_crt = 'debug'
elif vs_crt_opt == 'from_buildtype'
  if get_option('buildtype') == 'debug'
    vs_crt = 'debug'
  endif
endif
234

235 236
# Use debug/optimization flags to determine whether to enable debug or disable
# cast checks
237
glib_debug_cflags = []
238
if get_option('debug')
239
  glib_debug_cflags += ['-DG_ENABLE_DEBUG']
240 241
  message('Enabling various debug infrastructure')
elif get_option('optimization') in ['2', '3', 's']
242
  glib_debug_cflags += ['-DG_DISABLE_CAST_CHECKS']
243
  message('Disabling cast checks')
244 245
endif

246 247 248 249 250 251 252 253 254 255
if not get_option('glib_assert')
  glib_debug_cflags += ['-DG_DISABLE_ASSERT']
  message('Disabling GLib asserts')
endif

if not get_option('glib_checks')
  glib_debug_cflags += ['-DG_DISABLE_CHECKS']
  message('Disabling GLib checks')
endif

256 257
add_project_arguments(glib_debug_cflags, language: 'c')

258 259 260
# check for header files

headers = [
261
  'alloca.h',
262 263
  'crt_externs.h',
  'dirent.h', # MSC does not come with this by default
264
  'float.h',
265
  'fstab.h',
266
  'grp.h',
267 268 269 270 271 272 273
  'inttypes.h',
  'limits.h',
  'linux/magic.h',
  'locale.h',
  'mach/mach_time.h',
  'memory.h',
  'mntent.h',
274
  'poll.h',
275 276
  'pwd.h',
  'sched.h',
277
  'spawn.h',
278 279 280 281
  'stdint.h',
  'stdlib.h',
  'string.h',
  'strings.h',
282
  'sys/auxv.h',
283 284 285 286 287 288 289
  'sys/event.h',
  'sys/filio.h',
  'sys/inotify.h',
  'sys/mkdev.h',
  'sys/mntctl.h',
  'sys/mnttab.h',
  'sys/mount.h',
290 291 292 293
  'sys/param.h',
  'sys/resource.h',
  'sys/select.h',
  'sys/statfs.h',
294
  'sys/stat.h',
295
  'sys/statvfs.h',
296
  'sys/sysctl.h',
297
  'sys/time.h', # MSC does not come with this by default
298
  'sys/times.h',
299 300 301 302 303
  'sys/types.h',
  'sys/uio.h',
  'sys/vfs.h',
  'sys/vfstab.h',
  'sys/vmount.h',
304
  'sys/wait.h',
305
  'termios.h',
306 307
  'unistd.h',
  'values.h',
308
  'wchar.h',
309
  'xlocale.h',
310 311 312
]

foreach h : headers
313 314 315 316
  if cc.has_header(h)
    define = 'HAVE_' + h.underscorify().to_upper()
    glib_conf.set(define, 1)
    glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
317 318 319
  endif
endforeach

320 321 322 323 324 325 326
# FIXME: Use cc.check_header from Meson 0.47.
# FreeBSD includes a malloc.h which always throw compilation error.
if cc.compiles('#include <malloc.h>', name : 'malloc.h')
  glib_conf.set('HAVE_MALLOC_H', 1)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_MALLOC_H 1\n'
endif

327 328 329 330
if cc.has_header('linux/netlink.h')
  glib_conf.set('HAVE_NETLINK', 1)
endif

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
# Is statx() supported? Android systems don’t reliably support it as of August 2020.
statx_code = '''
  #ifndef _GNU_SOURCE
  #define _GNU_SOURCE
  #endif
  #include <sys/stat.h>
  #include <fcntl.h>
  int main (void)
  {
    struct statx stat_buf;
    return statx (AT_FDCWD, "/", AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS | STATX_BTIME, &stat_buf);
  }
  '''
if host_system != 'android' and cc.compiles(statx_code, name : 'statx() test')
  glib_conf.set('HAVE_STATX', 1)
endif

348
if glib_conf.has('HAVE_LOCALE_H')
349
  if cc.has_header_symbol('locale.h', 'LC_MESSAGES')
350 351
    glib_conf.set('HAVE_LC_MESSAGES', 1)
  endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
352
endif
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

struct_stat_blkprefix = '''
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
'''

struct_members = [
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  [ 'stat', 'st_mtimensec' ],
  [ 'stat', 'st_mtim.tv_nsec' ],
  [ 'stat', 'st_atimensec' ],
  [ 'stat', 'st_atim.tv_nsec' ],
  [ 'stat', 'st_ctimensec' ],
  [ 'stat', 'st_ctim.tv_nsec' ],
  [ 'stat', 'st_birthtime' ],
  [ 'stat', 'st_birthtimensec' ],
  [ 'stat', 'st_birthtim' ],
  [ 'stat', 'st_birthtim.tv_nsec' ],
  [ 'stat', 'st_blksize', struct_stat_blkprefix ],
  [ 'stat', 'st_blocks', struct_stat_blkprefix ],
  [ 'statfs', 'f_fstypename', struct_stat_blkprefix ],
  [ 'statfs', 'f_bavail', struct_stat_blkprefix ],
  [ 'dirent', 'd_type', '''#include <sys/types.h>
                           #include <dirent.h>''' ],
388 389 390 391
  [ 'statvfs', 'f_basetype', '#include <sys/statvfs.h>' ],
  [ 'statvfs', 'f_fstypename', '#include <sys/statvfs.h>' ],
  [ 'tm', 'tm_gmtoff', '#include <time.h>' ],
  [ 'tm', '__tm_gmtoff', '#include <time.h>' ],
392 393 394 395
]

foreach m : struct_members
  header_check_prefix = glib_conf_prefix
396 397
  if m.length() == 3
    header_check_prefix = header_check_prefix + m[2]
398 399 400 401
  else
    header_check_prefix = header_check_prefix + '#include <sys/stat.h>'
  endif
  if cc.has_member('struct ' + m[0], m[1], prefix : header_check_prefix)
402 403 404
    define = 'HAVE_STRUCT_@0@_@1@'.format(m[0].to_upper(), m[1].underscorify().to_upper())
    glib_conf.set(define, 1)
    glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
405 406 407 408
  else
  endif
endforeach

409 410
# Compiler flags
if cc.get_id() == 'gcc' or cc.get_id() == 'clang'
411
  warning_c_args = [
412
    '-Wduplicated-branches',
413
    '-Wimplicit-fallthrough',
414
    '-Wmisleading-indentation',
415
    '-Wstrict-prototypes',
416
    '-Wunused',
417 418
    # Due to maintained deprecated code, we do not want to see unused parameters
    '-Wno-unused-parameter',
419 420 421
    # Due to pervasive use of things like GPOINTER_TO_UINT(), we do not support
    # building with -Wbad-function-cast.
    '-Wno-bad-function-cast',
422
    '-Wno-cast-function-type',
423 424 425
    # Due to function casts through (void*) we cannot support -Wpedantic:
    # https://wiki.gnome.org/Projects/GLib/CompilerRequirements#Function_pointer_conversions.
    '-Wno-pedantic',
426 427
    # A zero-length format string shouldn't be considered an issue.
    '-Wno-format-zero-length',
428 429 430 431 432 433 434 435
    '-Werror=declaration-after-statement',
    '-Werror=format=2',
    '-Werror=implicit-function-declaration',
    '-Werror=init-self',
    '-Werror=missing-include-dirs',
    '-Werror=missing-prototypes',
    '-Werror=pointer-arith',
  ]
436
  warning_c_link_args = [
437 438 439
    '-Wl,-z,nodelete',
  ]
  if get_option('bsymbolic_functions')
440
    warning_c_link_args += ['-Wl,-Bsymbolic-functions']
441
  endif
442
else
443 444
  warning_c_args = []
  warning_c_link_args = []
445 446
endif

447
add_project_arguments(cc.get_supported_arguments(warning_c_args), language: 'c')
448 449 450 451 452 453 454

# FIXME: We cannot build some of the GResource tests with -z nodelete, which
# means we cannot use that flag in add_project_link_arguments(), and must add
# it to the relevant targets manually. We do the same with -Bsymbolic-functions
# because that is what the autotools build did.
# See https://github.com/mesonbuild/meson/pull/3520 for a way to eventually
# improve this.
455
glib_link_flags = cc.get_supported_link_arguments(warning_c_link_args)
456

457
# Windows SDK requirements and checks
458
if host_system == 'windows'
459 460 461 462 463 464 465 466 467 468 469 470 471 472
  # Check whether we're building for UWP apps
  code = '''
  #include <windows.h>
  #if !(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
  #error "Not building for UWP"
  #endif'''
  if cc.compiles(code, name : 'building for UWP')
    glib_conf.set('G_WINAPI_ONLY_APP', true)
    # We require Windows 10+ on WinRT
    glib_conf.set('_WIN32_WINNT', '0x0A00')
  else
    # We require Windows 7+ on Win32
    glib_conf.set('_WIN32_WINNT', '0x0601')
  endif
473 474
endif

475
functions = [
476
  'close_range',
477 478 479
  'endmntent',
  'endservent',
  'fallocate',
480 481
  'fchmod',
  'fchown',
482 483
  'fdwalk',
  'fsync',
484
  'getauxval',
485
  'getc_unlocked',
486
  'getfsstat',
487 488 489 490
  'getgrgid_r',
  'getmntent_r',
  'getpwuid_r',
  'getresuid',
491 492
  'getvfsstat',
  'gmtime_r',
493 494 495 496 497 498 499 500 501 502
  'hasmntopt',
  'inotify_init1',
  'issetugid',
  'kevent',
  'kqueue',
  'lchmod',
  'lchown',
  'link',
  'localtime_r',
  'lstat',
503
  'mbrtowc',
504 505
  'memalign',
  'mmap',
506
  'newlocale',
507 508 509 510 511 512 513 514 515 516 517
  'pipe2',
  'poll',
  'prlimit',
  'readlink',
  'recvmmsg',
  'sendmmsg',
  'setenv',
  'setmntent',
  'strerror_r',
  'strnlen',
  'strsignal',
518 519 520
  'strtod_l',
  'strtoll_l',
  'strtoull_l',
521 522 523 524 525 526 527 528 529 530 531
  'symlink',
  'timegm',
  'unsetenv',
  'uselocale',
  'utimes',
  'valloc',
  'vasprintf',
  'vsnprintf',
  'wcrtomb',
  'wcslen',
  'wcsnlen',
532
  'sysctlbyname',
533 534
]

535 536 537 538 539 540
# _NSGetEnviron is available on iOS too, but its usage gets apps rejected from
# the app store since it's considered 'private API'
if host_system == 'darwin'
  functions += ['_NSGetEnviron']
endif

541
if glib_conf.has('HAVE_SYS_STATVFS_H')
542
  functions += ['statvfs']
543 544
else
  have_func_statvfs = false
Jussi Pakkanen's avatar
Jussi Pakkanen committed
545
endif
546
if glib_conf.has('HAVE_SYS_STATFS_H') or glib_conf.has('HAVE_SYS_MOUNT_H')
547
  functions += ['statfs']
548 549
else
  have_func_statfs = false
Jussi Pakkanen's avatar
Jussi Pakkanen committed
550
endif
551

552 553 554 555
if host_system == 'windows'
  iphlpapi_dep = cc.find_library('iphlpapi')
  iphlpapi_funcs = ['if_nametoindex', 'if_indextoname']
  foreach ifunc : iphlpapi_funcs
556 557 558
    iphl_prefix =  '''#define _WIN32_WINNT @0@
      #include <winsock2.h>
      #include <iphlpapi.h>'''.format(glib_conf.get('_WIN32_WINNT'))
559
    if cc.has_function(ifunc,
560
                       prefix : iphl_prefix,
561 562 563 564 565 566 567 568 569 570 571 572 573
                       dependencies : iphlpapi_dep)
      idefine = 'HAVE_' + ifunc.underscorify().to_upper()
      glib_conf.set(idefine, 1)
      glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(idefine)
      set_variable('have_func_' + ifunc, true)
    else
      set_variable('have_func_' + ifunc, false)
    endif
  endforeach
else
  functions += ['if_indextoname', 'if_nametoindex']
endif

574
# AIX splice is something else
575
if host_system != 'aix'
576
  functions += ['splice']
Jussi Pakkanen's avatar
Jussi Pakkanen committed
577
endif
578 579

foreach f : functions
580 581 582 583 584
  if cc.has_function(f)
    define = 'HAVE_' + f.underscorify().to_upper()
    glib_conf.set(define, 1)
    glib_conf_prefix = glib_conf_prefix + '#define @0@ 1\n'.format(define)
    set_variable('have_func_' + f, true)
585
  else
586
    set_variable('have_func_' + f, false)
587 588 589
  endif
endforeach

590 591 592 593 594 595 596 597 598 599 600 601
# Check that stpcpy() is usable; must use header.
# cc.has_function() in some cases (clang, gcc 10+) assumes that if the
# compiler provides a builtin of the same name that the function exists, while
# it's in fact not provided by any header or library. This is true for
# stpcpy() on Windows using clang and gcc as well as posix_memalign() using
# gcc on Windows. Skip these checks on Windows for now to avoid false
# positives. See https://github.com/mesonbuild/meson/pull/7116,
# https://github.com/mesonbuild/meson/issues/3672 and
# https://github.com/mesonbuild/meson/issues/5628.
# FIXME: Once meson no longer returns success for stpcpy() and
# posix_memalign() on Windows using GCC and clang we can remove this.
if host_system != 'windows' and cc.has_function('stpcpy', prefix : '#include <string.h>')
602 603 604
  glib_conf.set('HAVE_STPCPY', 1)
endif

605 606 607 608 609 610 611 612 613 614 615 616 617
# When building for Android-20 and earlier, require Meson 0.54.2 or newer.
# This is needed, because Meson build versions prior to 0.54.2 return false
# positive for stpcpy has_function check when building for legacy Android.
if host_system.startswith('android-')
    android_is_older = cc.compiles('''#if __ANDROID_API__ >= 21
                                        #error Android is 21 or newer
                                    #endif''')
    if android_is_older and meson.version().version_compare('< 0.54.2')
      error('Compiling for <Android-21 requires Meson 0.54.2 or newer')
    endif
endif


618
# Check that posix_memalign() is usable; must use header
619
if host_system != 'windows' and cc.has_function('posix_memalign', prefix : '#include <stdlib.h>')
620 621 622
  glib_conf.set('HAVE_POSIX_MEMALIGN', 1)
endif

623 624 625 626 627
# Check that posix_spawn() is usable; must use header
if cc.has_function('posix_spawn', prefix : '#include <spawn.h>')
  glib_conf.set('HAVE_POSIX_SPAWN', 1)
endif

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
# Check whether strerror_r returns char *
if have_func_strerror_r
  if cc.compiles('''#define _GNU_SOURCE
                    #include <string.h>
                    int func (void) {
                      char error_string[256];
                      char *ptr = strerror_r (-2, error_string, 256);
                      char c = *strerror_r (-2, error_string, 256);
                      return c != 0 && ptr != (void*) 0L;
                    }
                 ''',
                 name : 'strerror_r() returns char *')
    glib_conf.set('STRERROR_R_CHAR_P', 1,
                  description: 'Defined if strerror_r returns char *')
  endif
endif

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
# Special-case these functions that have alternative names on Windows/MSVC
if cc.has_function('snprintf') or cc.has_header_symbol('stdio.h', 'snprintf')
  glib_conf.set('HAVE_SNPRINTF', 1)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF 1\n'
elif cc.has_function('_snprintf') or cc.has_header_symbol('stdio.h', '_snprintf')
  hack_define = '1\n#define snprintf _snprintf'
  glib_conf.set('HAVE_SNPRINTF', hack_define)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_SNPRINTF ' + hack_define
endif

if cc.has_function('strcasecmp')
  glib_conf.set('HAVE_STRCASECMP', 1)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP 1\n'
elif cc.has_function('_stricmp')
  hack_define = '1\n#define strcasecmp _stricmp'
  glib_conf.set('HAVE_STRCASECMP', hack_define)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRCASECMP ' + hack_define
endif

if cc.has_function('strncasecmp')
  glib_conf.set('HAVE_STRNCASECMP', 1)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP 1\n'
elif cc.has_function('_strnicmp')
  hack_define = '1\n#define strncasecmp _strnicmp'
  glib_conf.set('HAVE_STRNCASECMP', hack_define)
  glib_conf_prefix = glib_conf_prefix + '#define HAVE_STRNCASECMP ' + hack_define
endif

673 674 675 676
if cc.has_header_symbol('sys/sysmacros.h', 'major')
  glib_conf.set('MAJOR_IN_SYSMACROS', 1)
elif cc.has_header_symbol('sys/mkdev.h', 'major')
  glib_conf.set('MAJOR_IN_MKDEV', 1)
677 678
elif cc.has_header_symbol('sys/types.h', 'major')
  glib_conf.set('MAJOR_IN_TYPES', 1)
679 680
endif

681 682 683 684 685 686 687 688 689 690 691 692
if cc.has_header_symbol('dlfcn.h', 'RTLD_LAZY')
  glib_conf.set('HAVE_RTLD_LAZY', 1)
endif

if cc.has_header_symbol('dlfcn.h', 'RTLD_NOW')
  glib_conf.set('HAVE_RTLD_NOW', 1)
endif

if cc.has_header_symbol('dlfcn.h', 'RTLD_GLOBAL')
  glib_conf.set('HAVE_RTLD_GLOBAL', 1)
endif

LRN's avatar
LRN committed
693
have_rtld_next = false
694
if cc.has_header_symbol('dlfcn.h', 'RTLD_NEXT', args: '-D_GNU_SOURCE')
LRN's avatar
LRN committed
695 696 697 698
  have_rtld_next = true
  glib_conf.set('HAVE_RTLD_NEXT', 1)
endif

699 700 701 702 703 704 705 706 707 708
# Check whether to use statfs or statvfs
# Some systems have both statfs and statvfs, pick the most "native" for these
if have_func_statfs and have_func_statvfs
  # on solaris and irix, statfs doesn't even have the f_bavail field
  if not glib_conf.has('HAVE_STRUCT_STATFS_F_BAVAIL')
    have_func_statfs = false
  else
    # at least on linux, statfs is the actual syscall
    have_func_statvfs = false
  endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
709
endif
710 711 712 713 714 715
if have_func_statfs
  glib_conf.set('USE_STATFS', 1)
  stat_func_to_use = 'statfs'
elif have_func_statvfs
  glib_conf.set('USE_STATVFS', 1)
  stat_func_to_use = 'statvfs'
Jussi Pakkanen's avatar
Jussi Pakkanen committed
716
else
717
  stat_func_to_use = 'neither'
Jussi Pakkanen's avatar
Jussi Pakkanen committed
718
endif
719 720
message('Checking whether to use statfs or statvfs .. ' + stat_func_to_use)

721 722 723 724 725 726 727 728
if host_system == 'linux'
  if cc.has_function('mkostemp',
                     prefix: '''#define _GNU_SOURCE
                                #include <stdlib.h>''')
    glib_conf.set('HAVE_MKOSTEMP', 1)
  endif
endif

729
osx_ldflags = []
730
glib_have_os_x_9_or_later = false
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
glib_have_carbon = false
glib_have_cocoa = false
if host_system == 'darwin'
  add_languages('objc')
  objcc = meson.get_compiler('objc')

  osx_ldflags += ['-Wl,-framework,CoreFoundation']

  # Mac OS X Carbon support
  glib_have_carbon = objcc.compiles('''#include <Carbon/Carbon.h>
                                       #include <CoreServices/CoreServices.h>''',
                                    name : 'Mac OS X Carbon support')

  if glib_have_carbon
    glib_conf.set('HAVE_CARBON', true)
    osx_ldflags += '-Wl,-framework,Carbon'
    glib_have_os_x_9_or_later = objcc.compiles('''#include <AvailabilityMacros.h>
                                                  #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
                                                  #error Compiling for minimum OS X version before 10.9
                                                  #endif''',
                                               name : 'OS X 9 or later')
  endif
753

754 755 756 757 758 759
  # Mac OS X Cocoa support
  glib_have_cocoa = objcc.compiles('''#include <Cocoa/Cocoa.h>
                                      #ifdef GNUSTEP_BASE_VERSION
                                      #error "Detected GNUstep, not Cocoa"
                                      #endif''',
                                   name : 'Mac OS X Cocoa support')
760

761 762 763 764
  if glib_have_cocoa
    glib_conf.set('HAVE_COCOA', true)
    osx_ldflags += ['-Wl,-framework,Foundation', '-Wl,-framework,AppKit']
  endif
765

766 767 768 769
  # FIXME: libgio mix C and objC source files and there is no way to reliably
  # know which language flags it's going to use to link. Add to both languages
  # for now. See https://github.com/mesonbuild/meson/issues/3585.
  add_project_link_arguments(osx_ldflags, language : ['objc', 'c'])
770 771
endif

772 773 774 775 776 777 778 779 780
# Check for futex(2)
if cc.links('''#include <linux/futex.h>
               #include <sys/syscall.h>
               #include <unistd.h>
               int main (int argc, char ** argv) {
                 syscall (__NR_futex, NULL, FUTEX_WAKE, FUTEX_WAIT);
                 return 0;
               }''', name : 'futex(2) system call')
  glib_conf.set('HAVE_FUTEX', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
781
endif
782 783 784 785 786 787 788 789 790

# Check for eventfd(2)
if cc.links('''#include <sys/eventfd.h>
               #include <unistd.h>
               int main (int argc, char ** argv) {
                 eventfd (0, EFD_CLOEXEC);
                 return 0;
               }''', name : 'eventfd(2) system call')
  glib_conf.set('HAVE_EVENTFD', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
791
endif
792

793 794 795 796 797 798 799 800 801 802 803
# Check for __uint128_t (gcc) by checking for 128-bit division
uint128_t_src = '''int main() {
static __uint128_t v1 = 100;
static __uint128_t v2 = 10;
static __uint128_t u;
u = v1 / v2;
}'''
if cc.compiles(uint128_t_src, name : '__uint128_t available')
  glib_conf.set('HAVE_UINT128_T', 1)
endif

804 805 806 807 808 809
clock_gettime_test_code = '''
  #include <time.h>
  struct timespec t;
  int main (int argc, char ** argv) {
    return clock_gettime(CLOCK_REALTIME, &t);
  }'''
810
librt = []
811 812 813 814
if cc.links(clock_gettime_test_code, name : 'clock_gettime')
  glib_conf.set('HAVE_CLOCK_GETTIME', 1)
elif cc.links(clock_gettime_test_code, args : '-lrt', name : 'clock_gettime in librt')
  glib_conf.set('HAVE_CLOCK_GETTIME', 1)
815
  librt = cc.find_library('rt')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
816
endif
817

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
dlopen_dlsym_test_code = '''
#include <dlfcn.h>
int glib_underscore_test (void) { return 42; }
int main (int argc, char ** argv) {
  void *f1 = (void*)0, *f2 = (void*)0, *handle;
  handle = dlopen ((void*)0, 0);
  if (handle) {
    f1 = dlsym (handle, "glib_underscore_test");
    f2 = dlsym (handle, "_glib_underscore_test");
  }
  return (!f2 || f1);
}'''
libdl_dep = []
if cc.links(dlopen_dlsym_test_code, name : 'dlopen() and dlsym() in system libraries')
  have_dlopen_dlsym = true
elif cc.links(dlopen_dlsym_test_code, args : '-ldl', name : 'dlopen() and dlsym() in libdl')
  have_dlopen_dlsym = true
  libdl_dep = cc.find_library('dl')
else
  have_dlopen_dlsym = false
endif

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
# if statfs() takes 2 arguments (Posix) or 4 (Solaris)
if have_func_statfs
  if cc.compiles(glib_conf_prefix + '''
                 #include <unistd.h>
                        #ifdef HAVE_SYS_PARAM_H
                        #include <sys/param.h>
                        #endif
                        #ifdef HAVE_SYS_VFS_H
                        #include <sys/vfs.h>
                        #endif
                        #ifdef HAVE_SYS_MOUNT_H
                        #include <sys/mount.h>
                        #endif
                        #ifdef HAVE_SYS_STATFS_H
                        #include <sys/statfs.h>
                        #endif
                        void some_func (void) {
                          struct statfs st;
858
                          statfs("/", &st);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
                        }''', name : 'number of arguments to statfs() (n=2)')
    glib_conf.set('STATFS_ARGS', 2)
  elif cc.compiles(glib_conf_prefix + '''
                   #include <unistd.h>
                          #ifdef HAVE_SYS_PARAM_H
                          #include <sys/param.h>
                          #endif
                          #ifdef HAVE_SYS_VFS_H
                          #include <sys/vfs.h>
                          #endif
                          #ifdef HAVE_SYS_MOUNT_H
                          #include <sys/mount.h>
                          #endif
                          #ifdef HAVE_SYS_STATFS_H
                          #include <sys/statfs.h>
                          #endif
                          void some_func (void) {
                            struct statfs st;
877
                            statfs("/", &st, sizeof (st), 0);
878 879 880 881 882
                          }''', name : 'number of arguments to statfs() (n=4)')
    glib_conf.set('STATFS_ARGS', 4)
  else
    error('Unable to determine number of arguments to statfs()')
  endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
883
endif
884 885 886 887 888

# open takes O_DIRECTORY as an option
#AC_MSG_CHECKING([])
if cc.compiles('''#include <fcntl.h>
                  #include <sys/types.h>
889
                  #include <sys/stat.h>
890 891 892 893
                  void some_func (void) {
                    open(0, O_DIRECTORY, 0);
                  }''', name : 'open() option O_DIRECTORY')
  glib_conf.set('HAVE_OPEN_O_DIRECTORY', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
894 895
endif

896 897 898 899 900 901 902 903 904 905 906
# fcntl takes F_FULLFSYNC as an option
# See https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
if cc.compiles('''#include <fcntl.h>
                  #include <sys/types.h>
                  #include <sys/stat.h>
                  void some_func (void) {
                    fcntl(0, F_FULLFSYNC, 0);
                  }''', name : 'fcntl() option F_FULLFSYNC')
  glib_conf.set('HAVE_FCNTL_F_FULLFSYNC', 1)
endif

907
# Check whether there is a vsnprintf() function with C99 semantics installed.
908
# (similar tests to AC_FUNC_VSNPRINTF_C99)
909
# Check whether there is a snprintf() function with C99 semantics installed.
910 911 912
# (similar tests to AC_FUNC_SNPRINTF_C99)
# Check whether there is a printf() function with Unix98 semantics installed.
# (similar tests to AC_FUNC_PRINTF_UNIX98)
913 914
have_good_vsnprintf = false
have_good_snprintf = false
915
have_good_printf = false
916

917
if host_system == 'windows' and (cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl')
918 919 920
  # Unfortunately the Visual Studio 2015+ implementations of C99-style
  # snprintf and vsnprintf don't seem to be quite good enough.
  # (Sorry, I don't know exactly what is the problem,
921
  # but it is related to floating point formatting and decimal point vs. comma.)
922 923 924 925
  # The simple tests in AC_FUNC_VSNPRINTF_C99 and AC_FUNC_SNPRINTF_C99 aren't
  # rigorous enough to notice, though.
  glib_conf.set('HAVE_C99_SNPRINTF', false)
  glib_conf.set('HAVE_C99_VSNPRINTF', false)
926
  glib_conf.set('HAVE_UNIX98_PRINTF', false)
927 928 929 930 931 932 933 934 935
elif not cc_can_run and host_system in ['ios', 'darwin']
  # All these are true when compiling natively on macOS, so we should use good
  # defaults when building for iOS and tvOS.
  glib_conf.set('HAVE_C99_SNPRINTF', true)
  glib_conf.set('HAVE_C99_VSNPRINTF', true)
  glib_conf.set('HAVE_UNIX98_PRINTF', true)
  have_good_vsnprintf = true
  have_good_snprintf = true
  have_good_printf = true
936 937 938 939
else
  vsnprintf_c99_test_code = '''
#include <stdio.h>
#include <stdarg.h>
Jussi Pakkanen's avatar
Jussi Pakkanen committed
940

941 942 943 944 945 946
int
doit(char * s, ...)
{
  char buffer[32];
  va_list args;
  int r;
Jussi Pakkanen's avatar
Jussi Pakkanen committed
947

948 949 950
  va_start(args, s);
  r = vsnprintf(buffer, 5, s, args);
  va_end(args);
Jussi Pakkanen's avatar
Jussi Pakkanen committed
951

952 953
  if (r != 7)
    exit(1);
Jussi Pakkanen's avatar
Jussi Pakkanen committed
954

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
  /* AIX 5.1 and Solaris seems to have a half-baked vsnprintf()
     implementation. The above will return 7 but if you replace
     the size of the buffer with 0, it borks! */
  va_start(args, s);
  r = vsnprintf(buffer, 0, s, args);
  va_end(args);

  if (r != 7)
    exit(1);

  exit(0);
}

int
main(void)
{
  doit("1234567");
  exit(1);
}'''

975 976 977 978 979 980 981 982 983
  if cc_can_run
    rres = cc.run(vsnprintf_c99_test_code, name : 'C99 vsnprintf')
    if rres.compiled() and rres.returncode() == 0
      glib_conf.set('HAVE_C99_VSNPRINTF', 1)
      have_good_vsnprintf = true
    endif
  else
      have_good_vsnprintf = meson.get_cross_property('have_c99_vsnprintf', false)
      glib_conf.set('HAVE_C99_VSNPRINTF', have_good_vsnprintf)
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  endif

  snprintf_c99_test_code = '''
#include <stdio.h>
#include <stdarg.h>

int
doit()
{
  char buffer[32];
  va_list args;
  int r;

  r = snprintf(buffer, 5, "1234567");

  if (r != 7)
    exit(1);

  r = snprintf(buffer, 0, "1234567");

  if (r != 7)
    exit(1);

  r = snprintf(NULL, 0, "1234567");

  if (r != 7)
    exit(1);

  exit(0);
}
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1014

1015 1016 1017 1018 1019 1020 1021
int
main(void)
{
  doit();
  exit(1);
}'''

1022 1023 1024 1025 1026 1027 1028 1029 1030
  if cc_can_run
    rres = cc.run(snprintf_c99_test_code, name : 'C99 snprintf')
    if rres.compiled() and rres.returncode() == 0
      glib_conf.set('HAVE_C99_SNPRINTF', 1)
      have_good_snprintf = true
    endif
  else
      have_good_snprintf = meson.get_cross_property('have_c99_snprintf', false)
      glib_conf.set('HAVE_C99_SNPRINTF', have_good_snprintf)
1031
  endif
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

  printf_unix98_test_code = '''
#include <stdio.h>

int
main (void)
{
  char buffer[128];

  sprintf (buffer, "%2\$d %3\$d %1\$d", 1, 2, 3);
  if (strcmp ("2 3 1", buffer) == 0)
    exit (0);
  exit (1);
}'''

  if cc_can_run
    rres = cc.run(printf_unix98_test_code, name : 'Unix98 printf positional parameters')
    if rres.compiled() and rres.returncode() == 0
      glib_conf.set('HAVE_UNIX98_PRINTF', 1)
      have_good_printf = true
    endif
  else
      have_good_printf = meson.get_cross_property('have_unix98_printf', false)
      glib_conf.set('HAVE_UNIX98_PRINTF', have_good_printf)
  endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1057 1058
endif

1059
if host_system == 'windows'
1060
  glib_conf.set_quoted('EXEEXT', '.exe')
1061
else
1062
  glib_conf.set('EXEEXT', '')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1063 1064
endif

1065 1066 1067 1068 1069 1070 1071
# Our printf is 'good' only if vsnpintf()/snprintf()/printf() supports C99 well enough
use_system_printf = have_good_vsnprintf and have_good_snprintf and have_good_printf
glib_conf.set('USE_SYSTEM_PRINTF', use_system_printf)
glibconfig_conf.set('GLIB_USING_SYSTEM_PRINTF', use_system_printf)

if not use_system_printf
  # gnulib has vasprintf so override the previous check
1072 1073 1074
  glib_conf.set('HAVE_VASPRINTF', 1)
endif

1075 1076 1077 1078 1079
# Check for nl_langinfo and CODESET
if cc.links('''#include <langinfo.h>
               int main (int argc, char ** argv) {
                 char *codeset = nl_langinfo (CODESET);
                 return 0;
1080 1081
               }''', name : 'nl_langinfo and CODESET')
  glib_conf.set('HAVE_LANGINFO_CODESET', 1)
1082
  glib_conf.set('HAVE_CODESET', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1083 1084
endif

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
# Check for nl_langinfo and LC_TIME parts that are needed in gdatetime.c
if cc.links('''#include <langinfo.h>
               int main (int argc, char ** argv) {
                 char *str;
                 str = nl_langinfo (PM_STR);
                 str = nl_langinfo (D_T_FMT);
                 str = nl_langinfo (D_FMT);
                 str = nl_langinfo (T_FMT);
                 str = nl_langinfo (T_FMT_AMPM);
                 str = nl_langinfo (MON_1);
                 str = nl_langinfo (ABMON_12);
                 str = nl_langinfo (DAY_1);
                 str = nl_langinfo (ABDAY_7);
                 return 0;
               }''', name : 'nl_langinfo (PM_STR)')
  glib_conf.set('HAVE_LANGINFO_TIME', 1)
endif
if cc.links('''#include <langinfo.h>
               int main (int argc, char ** argv) {
                 char *str;
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT1_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT2_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT3_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT4_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT5_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT6_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT7_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT8_MB);
                 str = nl_langinfo (_NL_CTYPE_OUTDIGIT9_MB);
                 return 0;
               }''', name : 'nl_langinfo (_NL_CTYPE_OUTDIGITn_MB)')
  glib_conf.set('HAVE_LANGINFO_OUTDIGIT', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1118 1119
endif

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
# Check for nl_langinfo and alternative month names
if cc.links('''#ifndef _GNU_SOURCE
              # define _GNU_SOURCE
              #endif
              #include <langinfo.h>
               int main (int argc, char ** argv) {
                 char *str;
                 str = nl_langinfo (ALTMON_1);
                 str = nl_langinfo (ALTMON_2);
                 str = nl_langinfo (ALTMON_3);
                 str = nl_langinfo (ALTMON_4);
                 str = nl_langinfo (ALTMON_5);
                 str = nl_langinfo (ALTMON_6);
                 str = nl_langinfo (ALTMON_7);
                 str = nl_langinfo (ALTMON_8);
                 str = nl_langinfo (ALTMON_9);
                 str = nl_langinfo (ALTMON_10);
                 str = nl_langinfo (ALTMON_11);
                 str = nl_langinfo (ALTMON_12);
                 return 0;
               }''', name : 'nl_langinfo (ALTMON_n)')
  glib_conf.set('HAVE_LANGINFO_ALTMON', 1)
endif

# Check for nl_langinfo and abbreviated alternative month names
if cc.links('''#ifndef _GNU_SOURCE
              # define _GNU_SOURCE
              #endif
              #include <langinfo.h>
               int main (int argc, char ** argv) {
                 char *str;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
                 str = nl_langinfo (_NL_ABALTMON_1);
                 str = nl_langinfo (_NL_ABALTMON_2);
                 str = nl_langinfo (_NL_ABALTMON_3);
                 str = nl_langinfo (_NL_ABALTMON_4);
                 str = nl_langinfo (_NL_ABALTMON_5);
                 str = nl_langinfo (_NL_ABALTMON_6);
                 str = nl_langinfo (_NL_ABALTMON_7);
                 str = nl_langinfo (_NL_ABALTMON_8);
                 str = nl_langinfo (_NL_ABALTMON_9);
                 str = nl_langinfo (_NL_ABALTMON_10);
                 str = nl_langinfo (_NL_ABALTMON_11);
                 str = nl_langinfo (_NL_ABALTMON_12);
1163
                 return 0;
1164
               }''', name : 'nl_langinfo (_NL_ABALTMON_n)')
1165 1166 1167
  glib_conf.set('HAVE_LANGINFO_ABALTMON', 1)
endif

1168 1169 1170
# Check if C compiler supports the 'signed' keyword
if not cc.compiles('''signed char x;''', name : 'signed')
  glib_conf.set('signed', '/* NOOP */')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1171 1172
endif

1173
# Check if the ptrdiff_t type exists
1174
if cc.has_header_symbol('stddef.h', 'ptrdiff_t')
1175
  glib_conf.set('HAVE_PTRDIFF_T', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1176 1177
endif

1178 1179 1180 1181 1182 1183 1184 1185
# Check for sig_atomic_t type
if cc.links('''#include <signal.h>
               #include <sys/types.h>
               sig_atomic_t val = 42;
               int main (int argc, char ** argv) {
                 return val == 42 ? 0 : 1;
               }''', name : 'sig_atomic_t')
  glib_conf.set('HAVE_SIG_ATOMIC_T', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1186 1187
endif

1188
# Check if 'long long' works
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
# jm_AC_TYPE_LONG_LONG
if cc.compiles('''long long ll = 1LL;
                  int i = 63;
                  int some_func (void) {
                    long long llmax = (long long) -1;
                    return ll << i | ll >> i | llmax / ll | llmax % ll;
                  }''', name : 'long long')
  glib_conf.set('HAVE_LONG_LONG', 1)
  have_long_long = true
else
  have_long_long = false
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1200 1201
endif

1202 1203 1204 1205 1206 1207 1208
# Test whether the compiler supports the 'long double' type.
if cc.compiles('''/* The Stardent Vistra knows sizeof(long double), but does not support it.  */
                  long double foo = 0.0;
                  /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
                  int array [2*(sizeof(long double) >= sizeof(double)) - 1];''',
               name : 'long double')
  glib_conf.set('HAVE_LONG_DOUBLE', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1209 1210
endif

1211
# Test whether <stddef.h> has the 'wchar_t' type.
1212
if cc.has_header_symbol('stddef.h', 'wchar_t')
1213
  glib_conf.set('HAVE_WCHAR_T', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1214 1215
endif

1216
# Test whether <wchar.h> has the 'wint_t' type.
1217
if cc.has_header_symbol('wchar.h', 'wint_t')
1218
  glib_conf.set('HAVE_WINT_T', 1)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1219 1220
endif

1221
found_uintmax_t = false
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
# Define HAVE_INTTYPES_H_WITH_UINTMAX if <inttypes.h> exists,
# doesn't clash with <sys/types.h>, and declares uintmax_t.
# jm_AC_HEADER_INTTYPES_H
if cc.compiles('''#include <sys/types.h>
                  #include <inttypes.h>
                  void some_func (void) {
                    uintmax_t i = (uintmax_t) -1;
                  }''', name : 'uintmax_t in inttypes.h')
  glib_conf.set('HAVE_INTTYPES_H_WITH_UINTMAX', 1)
  found_uintmax_t = true
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1233
endif
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244

# Define HAVE_STDINT_H_WITH_UINTMAX if <stdint.h> exists,
# doesn't clash with <sys/types.h>, and declares uintmax_t.
# jm_AC_HEADER_STDINT_H
if cc.compiles('''#include <sys/types.h>
                  #include <stdint.h>
                  void some_func (void) {
                    uintmax_t i = (uintmax_t) -1;
                  }''', name : 'uintmax_t in stdint.h')
  glib_conf.set('HAVE_STDINT_H_WITH_UINTMAX', 1)
  found_uintmax_t = true
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1245 1246
endif

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
# Define intmax_t to 'long' or 'long long'
# if it is not already defined in <stdint.h> or <inttypes.h>.
# For simplicity, we assume that a header file defines 'intmax_t' if and
# only if it defines 'uintmax_t'.
if found_uintmax_t
  glib_conf.set('HAVE_INTMAX_T', 1)
elif have_long_long
  glib_conf.set('intmax_t', 'long long')
else
  glib_conf.set('intmax_t', 'long')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1257 1258 1259 1260 1261 1262 1263
endif

char_size = cc.sizeof('char')
short_size = cc.sizeof('short')
int_size = cc.sizeof('int')
voidp_size = cc.sizeof('void*')
long_size = cc.sizeof('long')
1264 1265 1266 1267 1268
if have_long_long
  long_long_size = cc.sizeof('long long')
else
  long_long_size = 0
endif
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1269
sizet_size = cc.sizeof('size_t')
1270
if cc.get_id() == 'msvc' or cc.get_id() == 'clang-cl'
1271 1272 1273 1274 1275
  ssizet_size = cc.sizeof('SSIZE_T', prefix : '#include <BaseTsd.h>')
else
  ssizet_size = cc.sizeof('ssize_t')
endif

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
# Some platforms (Apple) hard-code int64_t to long long instead of
# using long on 64-bit architectures. This can cause type mismatch
# warnings when trying to interface with code using the standard
# library type. Test for the warnings and set gint64 to whichever
# works.
if long_long_size == long_size
  if cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
                    #pragma options langlvl=stdc99
                    #endif
                    #pragma GCC diagnostic error "-Wincompatible-pointer-types"
                    #include <stdint.h>
                    #include <stdio.h>
                    int main () {
                      int64_t i1 = 1;
                      long *i2 = &i1;
                      return 1;
                    }''', name : 'int64_t is long')
    int64_t_typedef = 'long'
  elif cc.compiles('''#if defined(_AIX) && !defined(__GNUC__)
                      #pragma options langlvl=stdc99
                      #endif
                      #pragma GCC diagnostic error "-Wincompatible-pointer-types"
                      #include <stdint.h>
                      #include <stdio.h>
                      int main () {
                        int64_t i1 = 1;
                        long long *i2 = &i1;
                        return 1;
                      }''', name : 'int64_t is long long')
    int64_t_typedef = 'long long'
  endif
endif

1309
int64_m = 'll'
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1310 1311 1312 1313 1314
char_align = cc.alignment('char')
short_align = cc.alignment('short')
int_align = cc.alignment('int')
voidp_align = cc.alignment('void*')
long_align = cc.alignment('long')
1315 1316 1317
long_long_align = cc.alignment('long long')
# NOTE: We don't check for size of __int64 because long long is guaranteed to
# be 64-bit in C99, and it is available on all supported compilers
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
sizet_align = cc.alignment('size_t')

glib_conf.set('ALIGNOF_UNSIGNED_LONG', long_align)

glib_conf.set('SIZEOF_CHAR', char_size)
glib_conf.set('SIZEOF_INT', int_size)
glib_conf.set('SIZEOF_SHORT', short_size)
glib_conf.set('SIZEOF_LONG', long_size)
glib_conf.set('SIZEOF_LONG_LONG', long_long_size)
glib_conf.set('SIZEOF_SIZE_T', sizet_size)
1328
glib_conf.set('SIZEOF_SSIZE_T', ssizet_size)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1329
glib_conf.set('SIZEOF_VOID_P', voidp_size)
1330
glib_conf.set('SIZEOF_WCHAR_T', cc.sizeof('wchar_t', prefix: '#include <stddef.h>'))
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1331 1332 1333

if short_size == 2
  gint16 = 'short'
1334 1335 1336
  gint16_modifier='h'
  gint16_format='hi'
  guint16_format='hu'
1337 1338
elif int_size == 2
  gint16 = 'int'
1339 1340 1341
  gint16_modifier=''
  gint16_format='i'
  guint16_format='u'
1342 1343
else
  error('Compiler provides no native 16-bit integer type')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1344 1345
endif
glibconfig_conf.set('gint16', gint16)
1346 1347 1348
glibconfig_conf.set_quoted('gint16_modifier', gint16_modifier)
glibconfig_conf.set_quoted('gint16_format', gint16_format)
glibconfig_conf.set_quoted('guint16_format', guint16_format)
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1349 1350 1351

if short_size == 4
  gint32 = 'short'
1352 1353 1354
  gint32_modifier='h'
  gint32_format='hi'
  guint32_format='hu'
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1355
  guint32_align = short_align
1356 1357
elif int_size == 4
  gint32 = 'int'
1358 1359 1360
  gint32_modifier=''
  gint32_format='i'
  guint32_format='u'
1361 1362 1363
  guint32_align = int_align
elif long_size == 4
  gint32 = 'long'
1364 1365 1366
  gint32_modifier='l'
  gint32_format='li'
  guint32_format='lu'
1367 1368 1369
  guint32_align = long_align
else
  error('Compiler provides no native 32-bit integer type')
Jussi Pakkanen's avatar
Jussi Pakkanen committed
1370 1371
endif
glibconfig_conf.set('gint32', gint32)
1372