1 # Copyright (c) 2022-2024, PostgreSQL Global Development Group
3 # Entry point for building PostgreSQL with meson
5 # Good starting points for writing meson.build files are:
6 # - https://mesonbuild.com/Syntax.html
7 # - https://mesonbuild.com/Reference-manual.html
12 license: 'PostgreSQL',
14 # We want < 0.56 for python 3.5 compatibility on old platforms. EPEL for
15 # RHEL 7 has 0.55. < 0.54 would require replacing some uses of the fs
16 # module, < 0.53 all uses of fs. So far there's no need to go to >=0.56.
17 meson_version: '>=0.54',
19 'warning_level=1', #-Wall equivalent
21 'buildtype=debugoptimized', # -O2 + debug
22 # For compatibility with the autoconf build, set a default prefix. This
23 # works even on windows, where it's a drive-relative path (i.e. when on
24 # d:/somepath it'll install to d:/usr/local/pgsql)
25 'prefix=/usr/local/pgsql',
31 ###############################################################
33 ###############################################################
36 pkgconfig = import('pkgconfig')
38 host_system = host_machine.system()
39 build_system = build_machine.system()
40 host_cpu = host_machine.cpu_family()
42 cc = meson.get_compiler('c')
44 not_found_dep = dependency('', required: false)
45 thread_dep = dependency('threads')
46 auto_features = get_option('auto_features')
50 ###############################################################
52 ###############################################################
54 # It's very easy to get into confusing states when the source directory
55 # contains an in-place build. E.g. the wrong pg_config.h will be used. So just
56 # refuse to build in that case.
58 # There's a more elaborate check later, that checks for conflicts around all
59 # generated files. But we can only do that much further down the line, so this
60 # quick check seems worth it. Adhering to this advice should clean up the
61 # conflict, but won't protect against somebody doing make distclean or just
62 # removing pg_config.h
63 errmsg_nonclean_base = '''
65 Non-clean source code directory detected.
67 To build with meson the source tree may not have an in-place, ./configure
68 style, build configured. You can have both meson and ./configure style builds
69 for the same source tree by building out-of-source / VPATH with
70 configure. Alternatively use a separate check out for meson based builds.
74 if fs.exists(meson.current_source_dir() / 'src' / 'include' / 'pg_config.h')
75 errmsg_cleanup = 'To clean up, run make distclean in the source tree.'
76 error(errmsg_nonclean_base.format(errmsg_cleanup))
81 ###############################################################
82 # Variables to be determined
83 ###############################################################
85 postgres_inc_d = ['src/include']
86 postgres_inc_d += get_option('extra_include_dirs')
88 postgres_lib_d = get_option('extra_lib_dirs')
107 backend_both_deps = []
113 # source of data for pg_config.h etc
114 cdata = configuration_data()
118 ###############################################################
119 # Version and other metadata
120 ###############################################################
122 pg_version = meson.project_version()
124 if pg_version.endswith('devel')
125 pg_version_arr = [pg_version.split('devel')[0], '0']
126 elif pg_version.contains('beta')
127 pg_version_arr = [pg_version.split('beta')[0], '0']
128 elif pg_version.contains('rc')
129 pg_version_arr = [pg_version.split('rc')[0], '0']
131 pg_version_arr = pg_version.split('.')
134 pg_version_major = pg_version_arr[0].to_int()
135 pg_version_minor = pg_version_arr[1].to_int()
136 pg_version_num = (pg_version_major * 10000) + pg_version_minor
138 pg_url = 'https://www.postgresql.org/'
140 cdata.set_quoted('PACKAGE_NAME', 'PostgreSQL')
141 cdata.set_quoted('PACKAGE_BUGREPORT', 'pgsql-bugs@lists.postgresql.org')
142 cdata.set_quoted('PACKAGE_URL', pg_url)
143 cdata.set_quoted('PACKAGE_VERSION', pg_version)
144 cdata.set_quoted('PACKAGE_STRING', 'PostgreSQL @0@'.format(pg_version))
145 cdata.set_quoted('PACKAGE_TARNAME', 'postgresql')
147 pg_version += get_option('extra_version')
148 cdata.set_quoted('PG_VERSION', pg_version)
149 cdata.set_quoted('PG_MAJORVERSION', pg_version_major.to_string())
150 cdata.set('PG_MAJORVERSION_NUM', pg_version_major)
151 cdata.set('PG_MINORVERSION_NUM', pg_version_minor)
152 cdata.set('PG_VERSION_NUM', pg_version_num)
153 # PG_VERSION_STR is built later, it depends on compiler test results
154 cdata.set_quoted('CONFIGURE_ARGS', '')
158 ###############################################################
159 # Basic platform specific configuration
160 ###############################################################
162 exesuffix = '' # overridden below where necessary
163 dlsuffix = '.so' # overridden below where necessary
164 library_path_var = 'LD_LIBRARY_PATH'
166 # Format of file to control exports from libraries, and how to pass them to
167 # the compiler. For export_fmt @0@ is the path to the file export file.
168 export_file_format = 'gnu'
169 export_file_suffix = 'list'
170 export_fmt = '-Wl,--version-script=@0@'
172 # Flags to add when linking a postgres extension, @0@ is path to
173 # the relevant object on the platform.
174 mod_link_args_fmt = []
176 memset_loop_limit = 1024
178 # Choice of shared memory and semaphore implementation
182 # We implement support for some operating systems by pretending they're
183 # another. Map here, before determining system properties below
184 if host_system == 'dragonfly'
185 # apparently the most similar
186 host_system = 'netbsd'
187 elif host_system == 'android'
188 # while android isn't quite a normal linux, it seems close enough
189 # for our purposes so far
190 host_system = 'linux'
193 # meson's system names don't quite map to our "traditional" names. In some
194 # places we need the "traditional" name, e.g., for mapping
195 # src/include/port/$os.h to src/include/pg_config_os.h. Define portname for
197 portname = host_system
199 if host_system == 'cygwin'
200 sema_kind = 'unnamed_posix'
201 cppflags += '-D_GNU_SOURCE'
203 mod_link_args_fmt = ['@0@']
204 mod_link_with_name = 'lib@0@.a'
205 mod_link_with_dir = 'libdir'
207 elif host_system == 'darwin'
209 library_path_var = 'DYLD_LIBRARY_PATH'
211 export_file_format = 'darwin'
212 export_fmt = '-Wl,-exported_symbols_list,@0@'
214 mod_link_args_fmt = ['-bundle_loader', '@0@']
215 mod_link_with_dir = 'bindir'
216 mod_link_with_name = '@0@'
218 sysroot_args = [files('src/tools/darwin_sysroot'), get_option('darwin_sysroot')]
219 pg_sysroot = run_command(sysroot_args, check:true).stdout().strip()
220 message('darwin sysroot: @0@'.format(pg_sysroot))
222 cflags += ['-isysroot', pg_sysroot]
223 ldflags += ['-isysroot', pg_sysroot]
226 # meson defaults to -Wl,-undefined,dynamic_lookup for modules, which we
227 # don't want because a) it's different from what we do for autoconf, b) it
228 # causes warnings in macOS Ventura. But using -Wl,-undefined,error causes a
229 # warning starting in Sonoma. So only add -Wl,-undefined,error if it does
230 # not cause a warning.
231 if cc.has_multi_link_arguments('-Wl,-undefined,error', '-Werror')
232 ldflags_mod += '-Wl,-undefined,error'
235 # Starting in Sonoma, the linker warns about the same library being
236 # linked twice. Which can easily happen when multiple dependencies
237 # depend on the same library. Quiesce the ill considered warning.
238 ldflags += cc.get_supported_link_arguments('-Wl,-no_warn_duplicate_libraries')
240 elif host_system == 'freebsd'
241 sema_kind = 'unnamed_posix'
243 elif host_system == 'linux'
244 sema_kind = 'unnamed_posix'
245 cppflags += '-D_GNU_SOURCE'
247 elif host_system == 'netbsd'
248 # We must resolve all dynamic linking in the core server at program start.
249 # Otherwise the postmaster can self-deadlock due to signals interrupting
250 # resolution of calls, since NetBSD's linker takes a lock while doing that
251 # and some postmaster signal handlers do things that will also acquire that
252 # lock. As long as we need "-z now", might as well specify "-z relro" too.
253 # While there's not a hard reason to adopt these settings for our other
254 # executables, there's also little reason not to, so just add them to
256 ldflags += ['-Wl,-z,now', '-Wl,-z,relro']
258 elif host_system == 'openbsd'
261 elif host_system == 'sunos'
263 export_fmt = '-Wl,-M@0@'
264 cppflags += '-D_POSIX_PTHREAD_SEMANTICS'
266 elif host_system == 'windows'
270 library_path_var = ''
271 if cc.get_id() != 'msvc'
272 # define before including <time.h> for getting localtime_r() etc. on MinGW
273 cppflags += '-D_POSIX_C_SOURCE'
276 export_file_format = 'win'
277 export_file_suffix = 'def'
278 if cc.get_id() == 'msvc'
279 export_fmt = '/DEF:@0@'
280 mod_link_with_name = '@0@.lib'
283 mod_link_with_name = 'lib@0@.a'
285 mod_link_args_fmt = ['@0@']
286 mod_link_with_dir = 'libdir'
291 cdata.set('WIN32_STACK_RLIMIT', 4194304)
292 if cc.get_id() == 'msvc'
293 ldflags += '/INCREMENTAL:NO'
294 ldflags += '/STACK:@0@'.format(cdata.get('WIN32_STACK_RLIMIT'))
295 # ldflags += '/nxcompat' # generated by msbuild, should have it for ninja?
297 ldflags += '-Wl,--stack,@0@'.format(cdata.get('WIN32_STACK_RLIMIT'))
298 # Need to allow multiple definitions, we e.g. want to override getopt.
299 ldflags += '-Wl,--allow-multiple-definition'
300 # Ensure we get MSVC-like linking behavior.
301 ldflags += '-Wl,--disable-auto-import'
304 os_deps += cc.find_library('ws2_32', required: true)
305 secur32_dep = cc.find_library('secur32', required: true)
306 backend_deps += secur32_dep
307 libpq_deps += secur32_dep
309 postgres_inc_d += 'src/include/port/win32'
310 if cc.get_id() == 'msvc'
311 postgres_inc_d += 'src/include/port/win32_msvc'
314 windows = import('windows')
317 # XXX: Should we add an option to override the host_system as an escape
319 error('unknown host system: @0@'.format(host_system))
324 ###############################################################
326 ###############################################################
329 perl = find_program(get_option('PERL'), required: true, native: true)
330 python = find_program(get_option('PYTHON'), required: true, native: true)
331 flex = find_program(get_option('FLEX'), native: true, version: '>= 2.5.35')
332 bison = find_program(get_option('BISON'), native: true, version: '>= 2.3')
333 sed = find_program(get_option('SED'), 'sed', native: true, required: false)
334 prove = find_program(get_option('PROVE'), native: true, required: false)
335 tar = find_program(get_option('TAR'), native: true, required: false)
336 gzip = find_program(get_option('GZIP'), native: true, required: false)
337 program_lz4 = find_program(get_option('LZ4'), native: true, required: false)
338 openssl = find_program(get_option('OPENSSL'), native: true, required: false)
339 program_zstd = find_program(get_option('ZSTD'), native: true, required: false)
340 dtrace = find_program(get_option('DTRACE'), native: true, required: get_option('dtrace'))
341 missing = find_program('config/missing', native: true)
342 cp = find_program('cp', required: false, native: true)
343 xmllint_bin = find_program(get_option('XMLLINT'), native: true, required: false)
344 xsltproc_bin = find_program(get_option('XSLTPROC'), native: true, required: false)
348 bison_version_c = run_command(bison, '--version', check: true)
349 # bison version string helpfully is something like
350 # >>bison (GNU bison) 3.8.1<<
351 bison_version = bison_version_c.stdout().split(' ')[3].split('\n')[0]
352 if bison_version.version_compare('>=3.0')
353 bison_flags += ['-Wno-deprecated']
356 bison_cmd = [bison, bison_flags, '-o', '@OUTPUT0@', '-d', '@INPUT@']
358 'output': ['@BASENAME@.c', '@BASENAME@.h'],
359 'command': bison_cmd,
364 flex_version_c = run_command(flex, '--version', check: true)
365 flex_version = flex_version_c.stdout().split(' ')[1].split('\n')[0]
367 flex_wrapper = files('src/tools/pgflex')
368 flex_cmd = [python, flex_wrapper,
369 '--builddir', '@BUILD_ROOT@',
370 '--srcdir', '@SOURCE_ROOT@',
371 '--privatedir', '@PRIVATE_DIR@',
372 '--flex', flex, '--perl', perl,
373 '-i', '@INPUT@', '-o', '@OUTPUT0@',
376 wget = find_program('wget', required: false, native: true)
377 wget_flags = ['-O', '@OUTPUT0@', '--no-use-server-timestamps']
379 install_files = files('src/tools/install_files')
383 ###############################################################
384 # Path to meson (for tests etc)
385 ###############################################################
387 # NB: this should really be part of meson, see
388 # https://github.com/mesonbuild/meson/issues/8511
389 meson_binpath_r = run_command(python, 'src/tools/find_meson', check: true)
391 if meson_binpath_r.stdout() == ''
392 error('huh, could not run find_meson.\nerrcode: @0@\nstdout: @1@\nstderr: @2@'.format(
393 meson_binpath_r.returncode(),
394 meson_binpath_r.stdout(),
395 meson_binpath_r.stderr()))
398 meson_binpath_s = meson_binpath_r.stdout().split('\n')
399 meson_binpath_len = meson_binpath_s.length()
401 if meson_binpath_len < 1
402 error('unexpected introspect line @0@'.format(meson_binpath_r.stdout()))
409 foreach e : meson_binpath_s
420 if meson_impl not in ['muon', 'meson']
421 error('unknown meson implementation "@0@"'.format(meson_impl))
424 meson_bin = find_program(meson_binpath, native: true)
428 ###############################################################
430 ###############################################################
432 cdata.set('USE_ASSERT_CHECKING', get_option('cassert') ? 1 : false)
433 cdata.set('USE_INJECTION_POINTS', get_option('injection_points') ? 1 : false)
435 blocksize = get_option('blocksize').to_int() * 1024
437 if get_option('segsize_blocks') != 0
438 if get_option('segsize') != 1
439 warning('both segsize and segsize_blocks specified, segsize_blocks wins')
442 segsize = get_option('segsize_blocks')
444 segsize = (get_option('segsize') * 1024 * 1024 * 1024) / blocksize
447 cdata.set('BLCKSZ', blocksize, description:
448 '''Size of a disk block --- this also limits the size of a tuple. You can set
449 it bigger if you need bigger tuples (although TOAST should reduce the need
450 to have large tuples, since fields can be spread across multiple tuples).
451 BLCKSZ must be a power of 2. The maximum possible value of BLCKSZ is
452 currently 2^15 (32768). This is determined by the 15-bit widths of the
453 lp_off and lp_len fields in ItemIdData (see include/storage/itemid.h).
454 Changing BLCKSZ requires an initdb.''')
456 cdata.set('XLOG_BLCKSZ', get_option('wal_blocksize').to_int() * 1024)
457 cdata.set('RELSEG_SIZE', segsize)
458 cdata.set('DEF_PGPORT', get_option('pgport'))
459 cdata.set_quoted('DEF_PGPORT_STR', get_option('pgport').to_string())
460 cdata.set_quoted('PG_KRB_SRVNAM', get_option('krb_srvnam'))
461 if get_option('system_tzdata') != ''
462 cdata.set_quoted('SYSTEMTZDIR', get_option('system_tzdata'))
467 ###############################################################
469 ###############################################################
471 # These are set by the equivalent --xxxdir configure options. We
472 # append "postgresql" to some of them, if the string does not already
473 # contain "pgsql" or "postgres", in order to avoid directory clutter.
477 dir_prefix = get_option('prefix')
479 dir_prefix_contains_pg = (dir_prefix.contains('pgsql') or dir_prefix.contains('postgres'))
481 dir_bin = get_option('bindir')
483 dir_data = get_option('datadir')
484 if not (dir_prefix_contains_pg or dir_data.contains('pgsql') or dir_data.contains('postgres'))
485 dir_data = dir_data / pkg
488 dir_sysconf = get_option('sysconfdir')
489 if not (dir_prefix_contains_pg or dir_sysconf.contains('pgsql') or dir_sysconf.contains('postgres'))
490 dir_sysconf = dir_sysconf / pkg
493 dir_lib = get_option('libdir')
495 dir_lib_pkg = dir_lib
496 if not (dir_prefix_contains_pg or dir_lib_pkg.contains('pgsql') or dir_lib_pkg.contains('postgres'))
497 dir_lib_pkg = dir_lib_pkg / pkg
500 dir_pgxs = dir_lib_pkg / 'pgxs'
502 dir_include = get_option('includedir')
504 dir_include_pkg = dir_include
505 dir_include_pkg_rel = ''
506 if not (dir_prefix_contains_pg or dir_include_pkg.contains('pgsql') or dir_include_pkg.contains('postgres'))
507 dir_include_pkg = dir_include_pkg / pkg
508 dir_include_pkg_rel = pkg
511 dir_man = get_option('mandir')
513 # FIXME: These used to be separately configurable - worth adding?
514 dir_doc = get_option('datadir') / 'doc'
515 if not (dir_prefix_contains_pg or dir_doc.contains('pgsql') or dir_doc.contains('postgres'))
516 dir_doc = dir_doc / pkg
518 dir_doc_html = dir_doc / 'html'
520 dir_locale = get_option('localedir')
524 dir_bitcode = dir_lib_pkg / 'bitcode'
525 dir_include_internal = dir_include_pkg / 'internal'
526 dir_include_server = dir_include_pkg / 'server'
527 dir_include_extension = dir_include_server / 'extension'
528 dir_data_extension = dir_data / 'extension'
529 dir_doc_extension = dir_doc / 'extension'
533 ###############################################################
534 # Search paths, preparation for compiler tests
536 # NB: Arguments added later are not automatically used for subsequent
537 # configuration-time checks (so they are more isolated). If they should be
538 # used, they need to be added to test_c_args as well.
539 ###############################################################
541 postgres_inc = [include_directories(postgres_inc_d)]
542 test_lib_d = postgres_lib_d
543 test_c_args = cppflags + cflags
547 ###############################################################
549 ###############################################################
551 bsd_authopt = get_option('bsd_auth')
552 bsd_auth = not_found_dep
553 if cc.check_header('bsd_auth.h', required: bsd_authopt,
554 args: test_c_args, include_directories: postgres_inc)
555 cdata.set('USE_BSD_AUTH', 1)
556 bsd_auth = declare_dependency()
561 ###############################################################
564 # For now don't search for DNSServiceRegister in a library - only Apple's
565 # Bonjour implementation, which is always linked, works.
566 ###############################################################
568 bonjouropt = get_option('bonjour')
569 bonjour = not_found_dep
570 if cc.check_header('dns_sd.h', required: bonjouropt,
571 args: test_c_args, include_directories: postgres_inc) and \
572 cc.has_function('DNSServiceRegister',
573 args: test_c_args, include_directories: postgres_inc)
574 cdata.set('USE_BONJOUR', 1)
575 bonjour = declare_dependency()
580 ###############################################################
581 # Option: docs in HTML and man page format
582 ###############################################################
584 docs_opt = get_option('docs')
585 docs_dep = not_found_dep
586 if not docs_opt.disabled()
587 if xmllint_bin.found() and xsltproc_bin.found()
588 docs_dep = declare_dependency()
589 elif docs_opt.enabled()
590 error('missing required tools (xmllint and xsltproc needed) for docs in HTML / man page format')
596 ###############################################################
597 # Option: docs in PDF format
598 ###############################################################
600 docs_pdf_opt = get_option('docs_pdf')
601 docs_pdf_dep = not_found_dep
602 if not docs_pdf_opt.disabled()
603 fop = find_program(get_option('FOP'), native: true, required: docs_pdf_opt)
604 if xmllint_bin.found() and xsltproc_bin.found() and fop.found()
605 docs_pdf_dep = declare_dependency()
606 elif docs_pdf_opt.enabled()
607 error('missing required tools for docs in PDF format')
613 ###############################################################
615 ###############################################################
617 gssapiopt = get_option('gssapi')
620 if not gssapiopt.disabled()
621 gssapi = dependency('krb5-gssapi', required: false)
622 have_gssapi = gssapi.found()
625 gssapi_deps = [gssapi]
627 # Hardcoded lookup for gssapi. This is necessary as gssapi on windows does
628 # not install neither pkg-config nor cmake dependency information.
629 if host_system == 'windows'
630 is_64 = cc.sizeof('void *', args: test_c_args) == 8
632 gssapi_search_libs = ['gssapi64', 'krb5_64', 'comerr64']
634 gssapi_search_libs = ['gssapi32', 'krb5_32', 'comerr32']
637 gssapi_search_libs = ['gssapi_krb5']
641 foreach libname : gssapi_search_libs
642 lib = cc.find_library(libname, dirs: test_lib_d, required: false)
650 # Meson before 0.57.0 did not support using check_header() etc with
651 # declare_dependency(). Thus the tests below use the library looked up
652 # above. Once we require a newer meson version, we can simplify.
653 gssapi = declare_dependency(dependencies: gssapi_deps)
658 elif cc.check_header('gssapi/gssapi.h', dependencies: gssapi_deps, required: false,
659 args: test_c_args, include_directories: postgres_inc)
660 cdata.set('HAVE_GSSAPI_GSSAPI_H', 1)
661 elif cc.check_header('gssapi.h', dependencies: gssapi_deps, required: gssapiopt,
662 args: test_c_args, include_directories: postgres_inc)
663 cdata.set('HAVE_GSSAPI_H', 1)
669 elif cc.check_header('gssapi/gssapi_ext.h', dependencies: gssapi_deps, required: false,
670 args: test_c_args, include_directories: postgres_inc)
671 cdata.set('HAVE_GSSAPI_GSSAPI_EXT_H', 1)
672 elif cc.check_header('gssapi_ext.h', dependencies: gssapi_deps, required: gssapiopt,
673 args: test_c_args, include_directories: postgres_inc)
674 cdata.set('HAVE_GSSAPI_EXT_H', 1)
680 elif cc.has_function('gss_store_cred_into', dependencies: gssapi_deps,
681 args: test_c_args, include_directories: postgres_inc)
682 cdata.set('ENABLE_GSS', 1)
684 krb_srvtab = 'FILE:/@0@/krb5.keytab)'.format(get_option('sysconfdir'))
685 cdata.set_quoted('PG_KRB_SRVTAB', krb_srvtab)
686 elif gssapiopt.enabled()
687 error('''could not find function 'gss_store_cred_into' required for GSSAPI''')
692 if not have_gssapi and gssapiopt.enabled()
693 error('dependency lookup for gssapi failed')
698 gssapi = not_found_dep
703 ###############################################################
705 ###############################################################
707 ldapopt = get_option('ldap')
708 if ldapopt.disabled()
710 ldap_r = not_found_dep
711 elif host_system == 'windows'
712 ldap = cc.find_library('wldap32', required: ldapopt)
715 # macos framework dependency is buggy for ldap (one can argue whether it's
716 # Apple's or meson's fault), leading to an endless recursion with ldap.h
717 # including itself. See https://github.com/mesonbuild/meson/issues/10002
718 # Luckily we only need pkg-config support, so the workaround isn't
720 ldap = dependency('ldap', method: 'pkg-config', required: false)
723 # Before 2.5 openldap didn't have a pkg-config file, and it might not be
726 ldap = cc.find_library('ldap', required: ldapopt, dirs: test_lib_d,
727 has_headers: 'ldap.h', header_include_directories: postgres_inc)
729 # The separate ldap_r library only exists in OpenLDAP < 2.5, and if we
730 # have 2.5 or later, we shouldn't even probe for ldap_r (we might find a
731 # library from a separate OpenLDAP installation). The most reliable
732 # way to check that is to check for a function introduced in 2.5.
734 # don't have ldap, we shouldn't check for ldap_r
735 elif cc.has_function('ldap_verify_credentials',
736 dependencies: ldap, args: test_c_args)
737 ldap_r = ldap # ldap >= 2.5, no need for ldap_r
740 # Use ldap_r for FE if available, else assume ldap is thread-safe.
741 ldap_r = cc.find_library('ldap_r', required: false, dirs: test_lib_d,
742 has_headers: 'ldap.h', header_include_directories: postgres_inc)
743 if not ldap_r.found()
746 # On some platforms ldap_r fails to link without PTHREAD_LIBS.
747 ldap_r = declare_dependency(dependencies: [ldap_r, thread_dep])
750 # PostgreSQL sometimes loads libldap_r and plain libldap into the same
751 # process. Check for OpenLDAP versions known not to tolerate doing so;
752 # assume non-OpenLDAP implementations are safe. The dblink test suite
753 # exercises the hazardous interaction directly.
754 compat_test_code = '''
756 #if !defined(LDAP_VENDOR_VERSION) || \
757 (defined(LDAP_API_FEATURE_X_OPENLDAP) && \
758 LDAP_VENDOR_VERSION >= 20424 && LDAP_VENDOR_VERSION <= 20431)
762 if not cc.compiles(compat_test_code,
763 name: 'LDAP implementation compatible',
764 dependencies: ldap, args: test_c_args)
766 *** With OpenLDAP versions 2.4.24 through 2.4.31, inclusive, each backend
767 *** process that loads libpq (via WAL receiver, dblink, or postgres_fdw) and
768 *** also uses LDAP will crash on exit.''')
773 if ldap.found() and cc.has_function('ldap_initialize',
774 dependencies: ldap, args: test_c_args)
775 cdata.set('HAVE_LDAP_INITIALIZE', 1)
780 assert(ldap_r.found())
781 cdata.set('USE_LDAP', 1)
783 assert(not ldap_r.found())
788 ###############################################################
790 ###############################################################
792 llvmopt = get_option('llvm')
794 if add_languages('cpp', required: llvmopt, native: false)
795 llvm = dependency('llvm', version: '>=14', method: 'config-tool', required: llvmopt)
799 cdata.set('USE_LLVM', 1)
801 cpp = meson.get_compiler('cpp')
803 llvm_binpath = llvm.get_variable(configtool: 'bindir')
805 ccache = find_program('ccache', native: true, required: false)
807 # Some distros put LLVM and clang in different paths, so fallback to
808 # find via PATH, too.
809 clang = find_program(llvm_binpath / 'clang', 'clang', required: true)
812 message('llvm requires a C++ compiler')
817 ###############################################################
819 ###############################################################
821 icuopt = get_option('icu')
822 if not icuopt.disabled()
823 icu = dependency('icu-uc', required: false)
825 icu_i18n = dependency('icu-i18n', required: true)
828 # Unfortunately the dependency is named differently with cmake
829 if not icu.found() # combine with above once meson 0.60.0 is required
830 icu = dependency('ICU', required: icuopt,
831 components: ['uc'], modules: ['ICU::uc'], method: 'cmake')
833 icu_i18n = dependency('ICU', required: true,
834 components: ['i18n'], modules: ['ICU::i18n'])
839 cdata.set('USE_ICU', 1)
841 icu_i18n = not_found_dep
846 icu_i18n = not_found_dep
851 ###############################################################
853 ###############################################################
855 libxmlopt = get_option('libxml')
856 if not libxmlopt.disabled()
857 libxml = dependency('libxml-2.0', required: false, version: '>= 2.6.23')
858 # Unfortunately the dependency is named differently with cmake
859 if not libxml.found() # combine with above once meson 0.60.0 is required
860 libxml = dependency('LibXml2', required: libxmlopt, version: '>= 2.6.23',
865 cdata.set('USE_LIBXML', 1)
868 libxml = not_found_dep
873 ###############################################################
875 ###############################################################
877 libxsltopt = get_option('libxslt')
878 if not libxsltopt.disabled()
879 libxslt = dependency('libxslt', required: false)
880 # Unfortunately the dependency is named differently with cmake
881 if not libxslt.found() # combine with above once meson 0.60.0 is required
882 libxslt = dependency('LibXslt', required: libxsltopt, method: 'cmake')
886 cdata.set('USE_LIBXSLT', 1)
889 libxslt = not_found_dep
894 ###############################################################
896 ###############################################################
898 lz4opt = get_option('lz4')
899 if not lz4opt.disabled()
900 lz4 = dependency('liblz4', required: false)
901 # Unfortunately the dependency is named differently with cmake
902 if not lz4.found() # combine with above once meson 0.60.0 is required
903 lz4 = dependency('lz4', required: lz4opt,
904 method: 'cmake', modules: ['LZ4::lz4_shared'],
909 cdata.set('USE_LZ4', 1)
910 cdata.set('HAVE_LIBLZ4', 1)
919 ###############################################################
920 # Library: Tcl (for pltcl)
922 # NB: tclConfig.sh is used in autoconf build for getting
923 # TCL_SHARED_BUILD, TCL_INCLUDE_SPEC, TCL_LIBS and TCL_LIB_SPEC
924 # variables. For now we have not seen a need to copy
925 # that behaviour to the meson build.
926 ###############################################################
928 tclopt = get_option('pltcl')
929 tcl_version = get_option('tcl_version')
930 tcl_dep = not_found_dep
931 if not tclopt.disabled()
934 tcl_dep = dependency(tcl_version, required: false)
936 if not tcl_dep.found()
937 tcl_dep = cc.find_library(tcl_version,
942 if not cc.has_header('tcl.h', dependencies: tcl_dep, required: tclopt)
943 tcl_dep = not_found_dep
949 ###############################################################
951 ###############################################################
953 pamopt = get_option('pam')
954 if not pamopt.disabled()
955 pam = dependency('pam', required: false)
958 pam = cc.find_library('pam', required: pamopt, dirs: test_lib_d)
962 pam_header_found = false
964 # header file <security/pam_appl.h> or <pam/pam_appl.h> is required for PAM.
965 if cc.check_header('security/pam_appl.h', dependencies: pam, required: false,
966 args: test_c_args, include_directories: postgres_inc)
967 cdata.set('HAVE_SECURITY_PAM_APPL_H', 1)
968 pam_header_found = true
969 elif cc.check_header('pam/pam_appl.h', dependencies: pam, required: pamopt,
970 args: test_c_args, include_directories: postgres_inc)
971 cdata.set('HAVE_PAM_PAM_APPL_H', 1)
972 pam_header_found = true
976 cdata.set('USE_PAM', 1)
987 ###############################################################
988 # Library: Perl (for plperl)
989 ###############################################################
991 perlopt = get_option('plperl')
992 perl_dep = not_found_dep
993 if not perlopt.disabled()
996 # First verify that perl has the necessary dependencies installed
997 perl_mods = run_command(
999 '-MConfig', '-MOpcode', '-MExtUtils::Embed', '-MExtUtils::ParseXS',
1002 if perl_mods.returncode() != 0
1003 perl_may_work = false
1004 perl_msg = 'perl installation does not have the required modules'
1007 # Then inquire perl about its configuration
1009 perl_conf_cmd = [perl, '-MConfig', '-e', 'print $Config{$ARGV[0]}']
1010 perlversion = run_command(perl_conf_cmd, 'api_versionstring', check: true).stdout()
1011 archlibexp = run_command(perl_conf_cmd, 'archlibexp', check: true).stdout()
1012 privlibexp = run_command(perl_conf_cmd, 'privlibexp', check: true).stdout()
1013 useshrplib = run_command(perl_conf_cmd, 'useshrplib', check: true).stdout()
1015 perl_inc_dir = '@0@/CORE'.format(archlibexp)
1017 if perlversion.version_compare('< 5.14')
1018 perl_may_work = false
1019 perl_msg = 'Perl version 5.14 or later is required, but this is @0@'.format(perlversion)
1020 elif useshrplib != 'true'
1021 perl_may_work = false
1022 perl_msg = 'need a shared perl'
1027 # On most platforms, archlibexp is also where the Perl include files live ...
1028 perl_ccflags = ['-I@0@'.format(perl_inc_dir)]
1029 # ... but on newer macOS versions, we must use -iwithsysroot to look
1031 if not fs.is_file('@0@/perl.h'.format(perl_inc_dir)) and \
1032 fs.is_file('@0@@1@/perl.h'.format(pg_sysroot, perl_inc_dir))
1033 perl_ccflags = ['-iwithsysroot', perl_inc_dir]
1036 # check compiler finds header
1037 if not cc.has_header('perl.h', required: false,
1038 args: test_c_args + perl_ccflags, include_directories: postgres_inc)
1039 perl_may_work = false
1040 perl_msg = 'missing perl.h'
1045 perl_ccflags_r = run_command(perl_conf_cmd, 'ccflags', check: true).stdout()
1047 # See comments for PGAC_CHECK_PERL_EMBED_CCFLAGS in perl.m4
1048 foreach flag : perl_ccflags_r.split(' ')
1049 if flag.startswith('-D') and \
1050 (not flag.startswith('-D_') or flag == '_USE_32BIT_TIME_T')
1051 perl_ccflags += flag
1055 if host_system == 'windows'
1056 perl_ccflags += ['-DPLPERL_HAVE_UID_GID']
1058 if cc.get_id() == 'msvc'
1059 # prevent binary mismatch between MSVC built plperl and Strawberry or
1060 # msys ucrt perl libraries
1061 perl_v = run_command(perl, '-V').stdout()
1062 if not perl_v.contains('USE_THREAD_SAFE_LOCALE')
1063 perl_ccflags += ['-DNO_THREAD_SAFE_LOCALE']
1068 message('CCFLAGS recommended by perl: @0@'.format(perl_ccflags_r))
1069 message('CCFLAGS for embedding perl: @0@'.format(' '.join(perl_ccflags)))
1071 # We are after Embed's ldopts, but without the subset mentioned in
1072 # Config's ccdlflags and ldflags. (Those are the choices of those who
1073 # built the Perl installation, which are not necessarily appropriate
1074 # for building PostgreSQL.)
1075 perl_ldopts = run_command(perl, '-e', '''
1076 use ExtUtils::Embed;
1077 use Text::ParseWords;
1078 # tell perl to suppress including these in ldopts
1079 *ExtUtils::Embed::_ldflags =*ExtUtils::Embed::_ccdlflags = sub { return ""; };
1080 # adding an argument to ldopts makes it return a value instead of printing
1081 # print one of these per line so splitting will preserve spaces in file names.
1082 # shellwords eats backslashes, so we need to escape them.
1083 (my $opts = ldopts(undef)) =~ s!\\!\\\\!g;
1084 print "$_\n" foreach shellwords($opts);
1086 check: true).stdout().strip().split('\n')
1088 message('LDFLAGS for embedding perl: "@0@"'.format(' '.join(perl_ldopts)))
1090 perl_dep_int = declare_dependency(
1091 compile_args: perl_ccflags,
1092 link_args: perl_ldopts,
1093 version: perlversion,
1096 # While we're at it, check that we can link to libperl.
1097 # On most platforms, if perl.h is there then libperl.so will be too, but
1098 # at this writing Debian packages them separately.
1099 perl_link_test = '''
1102 #define __inline__ inline
1110 if not cc.links(perl_link_test, name: 'libperl',
1111 args: test_c_args + perl_ccflags + perl_ldopts,
1112 include_directories: postgres_inc)
1113 perl_may_work = false
1114 perl_msg = 'missing libperl'
1117 endif # perl_may_work
1120 perl_dep = perl_dep_int
1122 if perlopt.enabled()
1123 error('dependency plperl failed: @0@'.format(perl_msg))
1125 message('disabling optional dependency plperl: @0@'.format(perl_msg))
1132 ###############################################################
1133 # Library: Python (for plpython)
1134 ###############################################################
1136 pyopt = get_option('plpython')
1137 python3_dep = not_found_dep
1138 if not pyopt.disabled()
1139 pm = import('python')
1140 python3_inst = pm.find_installation(python.path(), required: pyopt)
1141 if python3_inst.found()
1142 python3_dep = python3_inst.dependency(embed: true, required: pyopt)
1143 # Remove this check after we depend on Meson >= 1.1.0
1144 if not cc.check_header('Python.h', dependencies: python3_dep, required: pyopt, include_directories: postgres_inc)
1145 python3_dep = not_found_dep
1152 ###############################################################
1154 ###############################################################
1156 if not get_option('readline').disabled()
1157 libedit_preferred = get_option('libedit_preferred')
1158 # Set the order of readline dependencies.
1159 # cc.find_library breaks and throws on the first dependency which
1160 # is marked as required=true and can't be found. Thus, we only mark
1161 # the last dependency to look up as required, to not throw too early.
1162 check_readline_deps = [
1164 'name': libedit_preferred ? 'libedit' : 'readline',
1168 'name': libedit_preferred ? 'readline' : 'libedit',
1169 'required': get_option('readline')
1173 foreach readline_dep : check_readline_deps
1174 readline = dependency(readline_dep['name'], required: false)
1175 if not readline.found()
1176 readline = cc.find_library(readline_dep['name'],
1177 required: readline_dep['required'],
1186 cdata.set('HAVE_LIBREADLINE', 1)
1189 'header_prefix': 'editline/',
1190 'flag_prefix': 'EDITLINE_',
1193 'header_prefix': 'readline/',
1194 'flag_prefix': 'READLINE_',
1197 'header_prefix': '',
1201 # Set the order of prefixes
1202 prefixes = libedit_preferred ? \
1203 [editline_prefix, default_prefix, readline_prefix] : \
1204 [readline_prefix, default_prefix, editline_prefix]
1206 at_least_one_header_found = false
1207 foreach header : ['history', 'readline']
1209 foreach prefix : prefixes
1210 header_file = '@0@@1@.h'.format(prefix['header_prefix'], header)
1211 # Check history.h and readline.h
1212 if not is_found and cc.has_header(header_file,
1213 args: test_c_args, include_directories: postgres_inc,
1214 dependencies: [readline], required: false)
1215 if header == 'readline'
1216 readline_h = header_file
1218 cdata.set('HAVE_@0@@1@_H'.format(prefix['flag_prefix'], header).to_upper(), 1)
1220 at_least_one_header_found = true
1225 if not at_least_one_header_found
1226 error('''readline header not found
1227 If you have @0@ already installed, see meson-logs/meson-log.txt for details on the
1228 failure. It is possible the compiler isn't looking in the proper directory.
1229 Use -Dreadline=disabled to disable readline support.'''.format(readline_dep))
1234 'history_truncate_file',
1235 'rl_completion_matches',
1236 'rl_filename_completion_function',
1237 'rl_reset_screen_size',
1241 foreach func : check_funcs
1242 found = cc.has_function(func, dependencies: [readline],
1243 args: test_c_args, include_directories: postgres_inc)
1244 cdata.set('HAVE_' + func.to_upper(), found ? 1 : false)
1248 'rl_completion_suppress_quote',
1249 'rl_filename_quote_characters',
1250 'rl_filename_quoting_function',
1253 foreach var : check_vars
1254 cdata.set('HAVE_' + var.to_upper(),
1255 cc.has_header_symbol(readline_h, var,
1256 args: test_c_args, include_directories: postgres_inc,
1257 prefix: '#include <stdio.h>',
1258 dependencies: [readline]) ? 1 : false)
1261 # If found via cc.find_library() ensure headers are found when using the
1262 # dependency. On meson < 0.57 one cannot do compiler checks using the
1263 # dependency returned by declare_dependency(), so we can't do this above.
1264 if readline.type_name() == 'library'
1265 readline = declare_dependency(dependencies: readline,
1266 include_directories: postgres_inc)
1269 # On windows with mingw readline requires auto-import to successfully
1270 # link, as the headers don't use declspec(dllimport)
1271 if host_system == 'windows' and cc.get_id() != 'msvc'
1272 readline = declare_dependency(dependencies: readline,
1273 link_args: '-Wl,--enable-auto-import')
1277 # XXX: Figure out whether to implement mingw warning equivalent
1279 readline = not_found_dep
1284 ###############################################################
1286 ###############################################################
1288 selinux = not_found_dep
1289 selinuxopt = get_option('selinux')
1290 if meson.version().version_compare('>=0.59')
1291 selinuxopt = selinuxopt.disable_auto_if(host_system != 'linux')
1293 selinux = dependency('libselinux', required: selinuxopt, version: '>= 2.1.10')
1294 cdata.set('HAVE_LIBSELINUX',
1295 selinux.found() ? 1 : false)
1299 ###############################################################
1301 ###############################################################
1303 systemd = not_found_dep
1304 systemdopt = get_option('systemd')
1305 if meson.version().version_compare('>=0.59')
1306 systemdopt = systemdopt.disable_auto_if(host_system != 'linux')
1308 systemd = dependency('libsystemd', required: systemdopt)
1309 cdata.set('USE_SYSTEMD', systemd.found() ? 1 : false)
1313 ###############################################################
1315 ###############################################################
1318 ssl_library = 'none'
1319 sslopt = get_option('ssl')
1321 if sslopt == 'auto' and auto_features.disabled()
1325 if sslopt in ['auto', 'openssl']
1326 openssl_required = (sslopt == 'openssl')
1328 # Try to find openssl via pkg-config et al, if that doesn't work
1329 # (e.g. because it's provided as part of the OS, like on FreeBSD), look for
1330 # the library names that we know about.
1332 # via pkg-config et al
1333 ssl = dependency('openssl', required: false)
1334 # only meson >= 0.57 supports declare_dependency() in cc.has_function(), so
1335 # we pass cc.find_library() results if necessary
1338 # via library + headers
1340 ssl_lib = cc.find_library('ssl',
1342 header_include_directories: postgres_inc,
1343 has_headers: ['openssl/ssl.h', 'openssl/err.h'],
1344 required: openssl_required)
1345 crypto_lib = cc.find_library('crypto',
1347 required: openssl_required)
1348 if ssl_lib.found() and crypto_lib.found()
1349 ssl_int = [ssl_lib, crypto_lib]
1350 ssl = declare_dependency(dependencies: ssl_int, include_directories: postgres_inc)
1352 elif cc.has_header('openssl/ssl.h', args: test_c_args, dependencies: ssl, required: openssl_required) and \
1353 cc.has_header('openssl/err.h', args: test_c_args, dependencies: ssl, required: openssl_required)
1361 ['CRYPTO_new_ex_data', {'required': true}],
1362 ['SSL_new', {'required': true}],
1364 # Functions introduced in OpenSSL 1.1.1.
1365 ['SSL_CTX_set_ciphersuites', {'required': true}],
1367 # Function introduced in OpenSSL 1.0.2, not in LibreSSL.
1368 ['SSL_CTX_set_cert_cb'],
1370 # Function introduced in OpenSSL 1.1.1, not in LibreSSL.
1371 ['X509_get_signature_info'],
1372 ['SSL_CTX_set_num_tickets'],
1375 are_openssl_funcs_complete = true
1376 foreach c : check_funcs
1378 val = cc.has_function(func, args: test_c_args, dependencies: ssl_int)
1379 required = c.get(1, {}).get('required', false)
1380 if required and not val
1381 are_openssl_funcs_complete = false
1383 error('openssl function @0@ is required'.format(func))
1387 cdata.set('HAVE_' + func.to_upper(), val ? 1 : false)
1391 if are_openssl_funcs_complete
1392 cdata.set('USE_OPENSSL', 1,
1393 description: 'Define to 1 to build with OpenSSL support. (-Dssl=openssl)')
1394 cdata.set('OPENSSL_API_COMPAT', '0x10101000L',
1395 description: 'Define to the OpenSSL API version in use. This avoids deprecation warnings from newer OpenSSL versions.')
1396 ssl_library = 'openssl'
1403 if sslopt == 'auto' and auto_features.enabled() and not ssl.found()
1404 error('no SSL library found')
1409 ###############################################################
1411 ###############################################################
1413 uuidopt = get_option('uuid')
1414 if uuidopt != 'none'
1415 uuidname = uuidopt.to_upper()
1416 if uuidopt == 'e2fs'
1417 uuid = dependency('uuid', required: true)
1418 uuidfunc = 'uuid_generate'
1419 uuidheader = 'uuid/uuid.h'
1420 elif uuidopt == 'bsd'
1421 # libc should have uuid function
1422 uuid = declare_dependency()
1423 uuidfunc = 'uuid_to_string'
1424 uuidheader = 'uuid.h'
1425 elif uuidopt == 'ossp'
1426 # In upstream, the package and library is called just 'uuid', but many
1427 # distros change it to 'ossp-uuid'.
1428 uuid = dependency('ossp-uuid', 'uuid', required: false)
1429 uuidfunc = 'uuid_export'
1430 uuidheader = 'uuid.h'
1432 # Hardcoded lookup for ossp-uuid. This is necessary as ossp-uuid on
1433 # windows installs neither a pkg-config nor a cmake dependency
1434 # information. Nor is there another supported uuid implementation
1435 # available on windows.
1437 uuid = cc.find_library('ossp-uuid',
1438 required: false, dirs: test_lib_d,
1439 has_headers: uuidheader, header_include_directories: postgres_inc)
1442 uuid = cc.find_library('uuid',
1443 required: true, dirs: test_lib_d,
1444 has_headers: uuidheader, header_include_directories: postgres_inc)
1447 error('unknown uuid build option value: @0@'.format(uuidopt))
1450 if not cc.has_header_symbol(uuidheader, uuidfunc,
1452 include_directories: postgres_inc,
1454 error('uuid library @0@ missing required function @1@'.format(uuidopt, uuidfunc))
1456 cdata.set('HAVE_@0@'.format(uuidheader.underscorify().to_upper()), 1)
1458 cdata.set('HAVE_UUID_@0@'.format(uuidname), 1,
1459 description: 'Define to 1 if you have @0@ UUID support.'.format(uuidname))
1461 uuid = not_found_dep
1466 ###############################################################
1468 ###############################################################
1470 zlibopt = get_option('zlib')
1471 zlib = not_found_dep
1472 if not zlibopt.disabled()
1473 zlib_t = dependency('zlib', required: zlibopt)
1475 if zlib_t.type_name() == 'internal'
1476 # if fallback was used, we don't need to test if headers are present (they
1477 # aren't built yet, so we can't test)
1479 elif not zlib_t.found()
1480 warning('did not find zlib')
1481 elif not cc.has_header('zlib.h',
1482 args: test_c_args, include_directories: postgres_inc,
1483 dependencies: [zlib_t], required: zlibopt)
1484 warning('zlib header not found')
1490 cdata.set('HAVE_LIBZ', 1)
1496 ###############################################################
1497 # Library: tap test dependencies
1498 ###############################################################
1500 # Check whether tap tests are enabled or not
1501 tap_tests_enabled = false
1502 tapopt = get_option('tap_tests')
1503 if not tapopt.disabled()
1504 # Checking for perl modules for tap tests
1505 perl_ipc_run_check = run_command(perl, 'config/check_modules.pl', check: false)
1506 if perl_ipc_run_check.returncode() != 0
1507 message(perl_ipc_run_check.stderr().strip())
1509 error('Additional Perl modules are required to run TAP tests.')
1511 warning('Additional Perl modules are required to run TAP tests.')
1514 tap_tests_enabled = true
1520 ###############################################################
1522 ###############################################################
1524 zstdopt = get_option('zstd')
1525 if not zstdopt.disabled()
1526 zstd = dependency('libzstd', required: false, version: '>=1.4.0')
1527 # Unfortunately the dependency is named differently with cmake
1528 if not zstd.found() # combine with above once meson 0.60.0 is required
1529 zstd = dependency('zstd', required: zstdopt, version: '>=1.4.0',
1530 method: 'cmake', modules: ['zstd::libzstd_shared'])
1534 cdata.set('USE_ZSTD', 1)
1535 cdata.set('HAVE_LIBZSTD', 1)
1539 zstd = not_found_dep
1544 ###############################################################
1546 ###############################################################
1548 # Do we need -std=c99 to compile C99 code? We don't want to add -std=c99
1549 # unnecessarily, because we optionally rely on newer features.
1551 #include <stdbool.h>
1552 #include <complex.h>
1554 #include <inttypes.h>
1556 struct named_init_test {
1561 extern void structfunc(struct named_init_test);
1563 int main(int argc, char **argv)
1565 struct named_init_test nit = {
1570 for (int loop_var = 0; loop_var < 3; loop_var++)
1575 structfunc((struct named_init_test){1, 0});
1581 if not cc.compiles(c99_test, name: 'c99', args: test_c_args)
1582 if cc.compiles(c99_test, name: 'c99 with -std=c99',
1583 args: test_c_args + ['-std=c99'])
1584 test_c_args += '-std=c99'
1585 cflags += '-std=c99'
1587 error('C compiler does not support C99')
1591 sizeof_long = cc.sizeof('long', args: test_c_args)
1592 cdata.set('SIZEOF_LONG', sizeof_long)
1594 cdata.set('HAVE_LONG_INT_64', 1)
1595 pg_int64_type = 'long int'
1596 cdata.set_quoted('INT64_MODIFIER', 'l')
1597 elif sizeof_long == 4 and cc.sizeof('long long', args: test_c_args) == 8
1598 cdata.set('HAVE_LONG_LONG_INT_64', 1)
1599 pg_int64_type = 'long long int'
1600 cdata.set_quoted('INT64_MODIFIER', 'll')
1602 error('do not know how to get a 64bit int')
1604 cdata.set('PG_INT64_TYPE', pg_int64_type)
1606 if host_machine.endian() == 'big'
1607 cdata.set('WORDS_BIGENDIAN', 1)
1610 # Determine memory alignment requirements for the basic C data types.
1612 alignof_types = ['short', 'int', 'long', 'double']
1613 foreach t : alignof_types
1614 align = cc.alignment(t, args: test_c_args)
1615 cdata.set('ALIGNOF_@0@'.format(t.to_upper()), align)
1618 # Compute maximum alignment of any basic type.
1620 # We require 'double' to have the strictest alignment among the basic types,
1621 # because otherwise the C ABI might impose 8-byte alignment on some of the
1622 # other C types that correspond to TYPALIGN_DOUBLE SQL types. That could
1623 # cause a mismatch between the tuple layout and the C struct layout of a
1624 # catalog tuple. We used to carefully order catalog columns such that any
1625 # fixed-width, attalign=4 columns were at offsets divisible by 8 regardless
1626 # of MAXIMUM_ALIGNOF to avoid that, but we no longer support any platforms
1627 # where TYPALIGN_DOUBLE != MAXIMUM_ALIGNOF.
1629 # We assume without checking that int64's alignment is at least as strong
1630 # as long, char, short, or int. Note that we intentionally do not consider
1631 # any types wider than 64 bits, as allowing MAXIMUM_ALIGNOF to exceed 8
1632 # would be too much of a penalty for disk and memory space.
1633 alignof_double = cdata.get('ALIGNOF_DOUBLE')
1634 if cc.alignment(pg_int64_type, args: test_c_args) > alignof_double
1635 error('alignment of int64 is greater than the alignment of double')
1637 cdata.set('MAXIMUM_ALIGNOF', alignof_double)
1639 cdata.set('SIZEOF_VOID_P', cc.sizeof('void *', args: test_c_args))
1640 cdata.set('SIZEOF_SIZE_T', cc.sizeof('size_t', args: test_c_args))
1643 # Check if __int128 is a working 128 bit integer type, and if so
1644 # define PG_INT128_TYPE to that typename.
1646 # This currently only detects a GCC/clang extension, but support for other
1647 # environments may be added in the future.
1649 # For the moment we only test for support for 128bit math; support for
1650 # 128bit literals and snprintf is not required.
1653 * We don't actually run this test, just link it to verify that any support
1654 * functions needed for __int128 are present.
1656 * These are globals to discourage the compiler from folding all the
1657 * arithmetic tests down to compile-time constants. We do not have
1658 * convenient support for 128bit literals at this point...
1660 __int128 a = 48828125;
1661 __int128 b = 97656250;
1666 a = (a << 12) + 1; /* 200000000001 */
1667 b = (b << 12) + 5; /* 400000000005 */
1668 /* try the most relevant arithmetic ops */
1671 /* must use the results, else compiler may optimize arithmetic away */
1677 buggy_int128 = false
1679 # Use of non-default alignment with __int128 tickles bugs in some compilers.
1680 # If not cross-compiling, we can test for bugs and disable use of __int128
1681 # with buggy compilers. If cross-compiling, hope for the best.
1682 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83925
1683 if not meson.is_cross_build()
1685 /* This must match the corresponding code in c.h: */
1686 #if defined(__GNUC__) || defined(__SUNPRO_C)
1687 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
1688 #elif defined(_MSC_VER)
1689 #define pg_attribute_aligned(a) __declspec(align(a))
1691 typedef __int128 int128a
1692 #if defined(pg_attribute_aligned)
1693 pg_attribute_aligned(8)
1698 void pass_by_val(void *buffer, int128a par) { holder = par; }
1702 long int i64 = 97656225L << 12;
1704 pass_by_val(main, (int128a) i64);
1708 name: '__int128 alignment bug',
1710 assert(r.compiled())
1711 if r.returncode() != 0
1713 message('__int128 support present but buggy and thus disabled')
1718 cdata.set('PG_INT128_TYPE', '__int128')
1719 cdata.set('ALIGNOF_PG_INT128_TYPE', cc.alignment('__int128', args: test_c_args))
1724 # Check if the C compiler knows computed gotos (gcc extension, also
1725 # available in at least clang). If so, define HAVE_COMPUTED_GOTO.
1727 # Checking whether computed gotos are supported syntax-wise ought to
1728 # be enough, as the syntax is otherwise illegal.
1730 static inline int foo(void)
1732 void *labeladdrs[] = {&&my_label};
1733 goto *labeladdrs[0];
1738 cdata.set('HAVE_COMPUTED_GOTO', 1)
1742 # Check if the C compiler understands _Static_assert(),
1743 # and define HAVE__STATIC_ASSERT if so.
1745 # We actually check the syntax ({ _Static_assert(...) }), because we need
1746 # gcc-style compound expressions to be able to wrap the thing into macros.
1748 int main(int arg, char **argv)
1750 ({ _Static_assert(1, "foo"); });
1754 cdata.set('HAVE__STATIC_ASSERT', 1)
1758 # We use <stdbool.h> if we have it and it declares type bool as having
1759 # size 1. Otherwise, c.h will fall back to declaring bool as unsigned char.
1760 if cc.has_type('_Bool', args: test_c_args) \
1761 and cc.has_type('bool', prefix: '#include <stdbool.h>', args: test_c_args) \
1762 and cc.sizeof('bool', prefix: '#include <stdbool.h>', args: test_c_args) == 1
1763 cdata.set('HAVE__BOOL', 1)
1764 cdata.set('PG_USE_STDBOOL', 1)
1768 # Need to check a call with %m because netbsd supports gnu_printf but emits a
1769 # warning for each use of %m.
1770 printf_attributes = ['gnu_printf', '__syslog__', 'printf']
1772 extern void emit_log(int ignore, const char *fmt,...) __attribute__((format(@0@, 2,3)));
1773 static void call_log(void)
1775 emit_log(0, "error: %s: %m", "foo");
1778 attrib_error_args = cc.get_supported_arguments('-Werror=format', '-Werror=ignored-attributes')
1779 foreach a : printf_attributes
1780 if cc.compiles(testsrc.format(a),
1781 args: test_c_args + attrib_error_args, name: 'format ' + a)
1782 cdata.set('PG_PRINTF_ATTRIBUTE', a)
1788 if cc.has_function_attribute('visibility:default') and \
1789 cc.has_function_attribute('visibility:hidden')
1790 cdata.set('HAVE_VISIBILITY_ATTRIBUTE', 1)
1792 # Only newer versions of meson know not to apply gnu_symbol_visibility =
1793 # inlineshidden to C code as well... And either way, we want to put these
1794 # flags into exported files (pgxs, .pc files).
1795 cflags_mod += '-fvisibility=hidden'
1796 cxxflags_mod += ['-fvisibility=hidden', '-fvisibility-inlines-hidden']
1797 ldflags_mod += '-fvisibility=hidden'
1801 # Check if various builtins exist. Some builtins are tested separately,
1802 # because we want to test something more complicated than the generic case.
1815 foreach builtin : builtins
1816 fname = '__builtin_@0@'.format(builtin)
1817 if cc.has_function(fname, args: test_c_args)
1818 cdata.set('HAVE@0@'.format(fname.to_upper()), 1)
1823 # Check if the C compiler understands __builtin_types_compatible_p,
1824 # and define HAVE__BUILTIN_TYPES_COMPATIBLE_P if so.
1826 # We check usage with __typeof__, though it's unlikely any compiler would
1827 # have the former and not the latter.
1830 static int y[__builtin_types_compatible_p(__typeof__(x), int)];
1832 name: '__builtin_types_compatible_p',
1834 cdata.set('HAVE__BUILTIN_TYPES_COMPATIBLE_P', 1)
1838 # Check if the C compiler understands __builtin_$op_overflow(),
1839 # and define HAVE__BUILTIN_OP_OVERFLOW if so.
1841 # Check for the most complicated case, 64 bit multiplication, as a
1842 # proxy for all of the operations. To detect the case where the compiler
1843 # knows the function but library support is missing, we must link not just
1844 # compile, and store the results in global variables so the compiler doesn't
1845 # optimize away the call.
1853 return __builtin_mul_overflow(a, b, &result);
1855 name: '__builtin_mul_overflow',
1856 args: test_c_args + ['-DINT64=@0@'.format(cdata.get('PG_INT64_TYPE'))],
1858 cdata.set('HAVE__BUILTIN_OP_OVERFLOW', 1)
1862 # XXX: The configure.ac check for __cpuid() is broken, we don't copy that
1863 # here. To prevent problems due to two detection methods working, stop
1864 # checking after one.
1867 int main(int arg, char **argv)
1869 unsigned int exx[4] = {0, 0, 0, 0};
1870 __get_cpuid(1, &exx[0], &exx[1], &exx[2], &exx[3]);
1872 ''', name: '__get_cpuid',
1874 cdata.set('HAVE__GET_CPUID', 1)
1877 int main(int arg, char **argv)
1879 unsigned int exx[4] = {0, 0, 0, 0};
1882 ''', name: '__cpuid',
1884 cdata.set('HAVE__CPUID', 1)
1888 # Check for __get_cpuid_count() and __cpuidex() in a similar fashion.
1891 int main(int arg, char **argv)
1893 unsigned int exx[4] = {0, 0, 0, 0};
1894 __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]);
1896 ''', name: '__get_cpuid_count',
1898 cdata.set('HAVE__GET_CPUID_COUNT', 1)
1901 int main(int arg, char **argv)
1903 unsigned int exx[4] = {0, 0, 0, 0};
1904 __cpuidex(exx, 7, 0);
1906 ''', name: '__cpuidex',
1908 cdata.set('HAVE__CPUIDEX', 1)
1912 # Defend against clang being used on x86-32 without SSE2 enabled. As current
1913 # versions of clang do not understand -fexcess-precision=standard, the use of
1914 # x87 floating point operations leads to problems like isinf possibly returning
1915 # false for a value that is infinite when converted from the 80bit register to
1916 # the 8byte memory representation.
1918 # Only perform the test if the compiler doesn't understand
1919 # -fexcess-precision=standard, that way a potentially fixed compiler will work
1921 if '-fexcess-precision=standard' not in cflags
1922 if not cc.compiles('''
1923 #if defined(__clang__) && defined(__i386__) && !defined(__SSE2_MATH__)
1926 name: '', args: test_c_args)
1927 error('Compiling PostgreSQL with clang, on 32bit x86, requires SSE2 support. Use -msse2 or use gcc.')
1933 ###############################################################
1935 ###############################################################
1937 common_functional_flags = [
1938 # Disable strict-aliasing rules; needed for gcc 3.3+
1939 '-fno-strict-aliasing',
1940 # Disable optimizations that assume no overflow; needed for gcc 4.3+
1942 '-fexcess-precision=standard',
1945 cflags += cc.get_supported_arguments(common_functional_flags)
1947 cxxflags += cpp.get_supported_arguments(common_functional_flags)
1950 vectorize_cflags = cc.get_supported_arguments(['-ftree-vectorize'])
1951 unroll_loops_cflags = cc.get_supported_arguments(['-funroll-loops'])
1953 common_warning_flags = [
1954 '-Wmissing-prototypes',
1956 # Really don't want VLAs to be used in our dialect of C
1958 # On macOS, complain about usage of symbols newer than the deployment target
1959 '-Werror=unguarded-availability-new',
1961 '-Wmissing-format-attribute',
1962 '-Wimplicit-fallthrough=3',
1963 '-Wcast-function-type',
1964 '-Wshadow=compatible-local',
1965 # This was included in -Wall/-Wformat in older GCC versions
1966 '-Wformat-security',
1969 cflags_warn += cc.get_supported_arguments(common_warning_flags)
1971 cxxflags_warn += cpp.get_supported_arguments(common_warning_flags)
1974 # A few places with imported code get a pass on -Wdeclaration-after-statement, remember
1975 # the result for them
1976 cflags_no_decl_after_statement = []
1977 if cc.has_argument('-Wdeclaration-after-statement')
1978 cflags_warn += '-Wdeclaration-after-statement'
1979 cflags_no_decl_after_statement += '-Wno-declaration-after-statement'
1982 # Some code is not clean for -Wmissing-variable-declarations, so we
1983 # make the "no" option available. Also, while clang supports this
1984 # option for C++, gcc does not, so for consistency, leave it off for
1986 cflags_no_missing_var_decls = []
1987 if cc.has_argument('-Wmissing-variable-declarations')
1988 cflags_warn += '-Wmissing-variable-declarations'
1989 cflags_no_missing_var_decls += '-Wno-missing-variable-declarations'
1993 # The following tests want to suppress various unhelpful warnings by adding
1994 # -Wno-foo switches. But gcc won't complain about unrecognized -Wno-foo
1995 # switches, so we have to test for the positive form and if that works,
1996 # add the negative form.
1998 negative_warning_flags = [
1999 # Suppress clang's unhelpful unused-command-line-argument warnings.
2000 'unused-command-line-argument',
2002 # Remove clang 12+'s compound-token-split-by-macro, as this causes a lot
2003 # of warnings when building plperl because of usages in the Perl headers.
2004 'compound-token-split-by-macro',
2006 # Similarly disable useless truncation warnings from gcc 8+
2007 'format-truncation',
2008 'stringop-truncation',
2010 # Suppress clang 16's strict warnings about function casts
2011 'cast-function-type-strict',
2013 # To make warning_level=2 / -Wextra work, we'd need at least the following
2015 # 'missing-field-initializers',
2017 # 'unused-parameter',
2020 foreach w : negative_warning_flags
2021 if cc.has_argument('-W' + w)
2022 cflags_warn += '-Wno-' + w
2024 if llvm.found() and cpp.has_argument('-W' + w)
2025 cxxflags_warn += '-Wno-' + w
2030 if cc.get_id() == 'msvc'
2032 '/wd4018', # signed/unsigned mismatch
2033 '/wd4244', # conversion from 'type1' to 'type2', possible loss of data
2034 '/wd4273', # inconsistent DLL linkage
2035 '/wd4101', # unreferenced local variable
2036 '/wd4102', # unreferenced label
2037 '/wd4090', # different 'modifier' qualifiers
2038 '/wd4267', # conversion from 'size_t' to 'type', possible loss of data
2046 '/D_CRT_SECURE_NO_DEPRECATE',
2047 '/D_CRT_NONSTDC_NO_DEPRECATE',
2050 # We never need export libraries. As link.exe reports their creation, they
2051 # are unnecessarily noisy. Similarly, we don't need import library for
2052 # modules, we only import them dynamically, and they're also noisy.
2054 ldflags_mod += '/NOIMPLIB'
2058 # Compute flags that are built into Meson. We need these to
2059 # substitute into Makefile.global and for pg_config. We only compute
2060 # the flags for Unix-style compilers, since that's the only style that
2061 # would use Makefile.global or pg_config.
2063 # We don't use get_option('warning_level') here, because the other
2064 # warning levels are not useful with PostgreSQL source code.
2065 common_builtin_flags = ['-Wall']
2067 if get_option('debug')
2068 common_builtin_flags += ['-g']
2071 optimization = get_option('optimization')
2072 if optimization == '0'
2073 common_builtin_flags += ['-O0']
2074 elif optimization == '1'
2075 common_builtin_flags += ['-O1']
2076 elif optimization == '2'
2077 common_builtin_flags += ['-O2']
2078 elif optimization == '3'
2079 common_builtin_flags += ['-O3']
2080 elif optimization == 's'
2081 common_builtin_flags += ['-Os']
2084 cflags_builtin = cc.get_supported_arguments(common_builtin_flags)
2086 cxxflags_builtin = cpp.get_supported_arguments(common_builtin_flags)
2091 ###############################################################
2093 ###############################################################
2096 {'name': 'HAVE_GCC__SYNC_CHAR_TAS',
2097 'desc': '__sync_lock_test_and_set(char)',
2100 __sync_lock_test_and_set(&lock, 1);
2101 __sync_lock_release(&lock);'''},
2103 {'name': 'HAVE_GCC__SYNC_INT32_TAS',
2104 'desc': '__sync_lock_test_and_set(int32)',
2107 __sync_lock_test_and_set(&lock, 1);
2108 __sync_lock_release(&lock);'''},
2110 {'name': 'HAVE_GCC__SYNC_INT32_CAS',
2111 'desc': '__sync_val_compare_and_swap(int32)',
2114 __sync_val_compare_and_swap(&val, 0, 37);'''},
2116 {'name': 'HAVE_GCC__SYNC_INT64_CAS',
2117 'desc': '__sync_val_compare_and_swap(int64)',
2120 __sync_val_compare_and_swap(&val, 0, 37);'''},
2122 {'name': 'HAVE_GCC__ATOMIC_INT32_CAS',
2123 'desc': ' __atomic_compare_exchange_n(int32)',
2127 __atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);'''},
2129 {'name': 'HAVE_GCC__ATOMIC_INT64_CAS',
2130 'desc': ' __atomic_compare_exchange_n(int64)',
2134 __atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);'''},
2137 foreach check : atomic_checks
2142 }'''.format(check['test'])
2144 cdata.set(check['name'],
2146 name: check['desc'],
2147 args: test_c_args + ['-DINT64=@0@'.format(cdata.get('PG_INT64_TYPE'))]) ? 1 : false
2152 ###############################################################
2153 # Check for the availability of XSAVE intrinsics.
2154 ###############################################################
2156 if host_cpu == 'x86' or host_cpu == 'x86_64'
2159 #include <immintrin.h>
2161 #if defined(__has_attribute) && __has_attribute (target)
2162 __attribute__((target("xsave")))
2166 return _xgetbv(0) & 0xe0;
2170 if cc.links(prog, name: 'XSAVE intrinsics', args: test_c_args)
2171 cdata.set('HAVE_XSAVE_INTRINSICS', 1)
2177 ###############################################################
2178 # Check for the availability of AVX-512 popcount intrinsics.
2179 ###############################################################
2181 if host_cpu == 'x86_64'
2184 #include <immintrin.h>
2186 #if defined(__has_attribute) && __has_attribute (target)
2187 __attribute__((target("avx512vpopcntdq,avx512bw")))
2191 const char buf[sizeof(__m512i)];
2193 __m512i accum = _mm512_setzero_si512();
2194 const __m512i val = _mm512_maskz_loadu_epi8((__mmask64) 0xf0f0f0f0f0f0f0f0, (const __m512i *) buf);
2195 const __m512i cnt = _mm512_popcnt_epi64(val);
2196 accum = _mm512_add_epi64(accum, cnt);
2197 popcnt = _mm512_reduce_add_epi64(accum);
2198 /* return computed value, to prevent the above being optimized away */
2203 if cc.links(prog, name: 'AVX-512 popcount',
2204 args: test_c_args + ['-DINT64=@0@'.format(cdata.get('PG_INT64_TYPE'))])
2205 cdata.set('USE_AVX512_POPCNT_WITH_RUNTIME_CHECK', 1)
2211 ###############################################################
2212 # Select CRC-32C implementation.
2214 # If we are targeting a processor that has Intel SSE 4.2 instructions, we can
2215 # use the special CRC instructions for calculating CRC-32C. If we're not
2216 # targeting such a processor, but we can nevertheless produce code that uses
2217 # the SSE intrinsics, perhaps with some extra CFLAGS, compile both
2218 # implementations and select which one to use at runtime, depending on whether
2219 # SSE 4.2 is supported by the processor we're running on.
2221 # Similarly, if we are targeting an ARM processor that has the CRC
2222 # instructions that are part of the ARMv8 CRC Extension, use them. And if
2223 # we're not targeting such a processor, but can nevertheless produce code that
2224 # uses the CRC instructions, compile both, and select at runtime.
2225 ###############################################################
2227 have_optimized_crc = false
2229 if host_cpu == 'x86' or host_cpu == 'x86_64'
2231 if cc.get_id() == 'msvc'
2232 cdata.set('USE_SSE42_CRC32C', false)
2233 cdata.set('USE_SSE42_CRC32C_WITH_RUNTIME_CHECK', 1)
2234 have_optimized_crc = true
2238 #include <nmmintrin.h>
2242 unsigned int crc = 0;
2243 crc = _mm_crc32_u8(crc, 0);
2244 crc = _mm_crc32_u32(crc, 0);
2245 /* return computed value, to prevent the above being optimized away */
2250 if cc.links(prog, name: '_mm_crc32_u8 and _mm_crc32_u32 without -msse4.2',
2252 # Use Intel SSE 4.2 unconditionally.
2253 cdata.set('USE_SSE42_CRC32C', 1)
2254 have_optimized_crc = true
2255 elif cc.links(prog, name: '_mm_crc32_u8 and _mm_crc32_u32 with -msse4.2',
2256 args: test_c_args + ['-msse4.2'])
2257 # Use Intel SSE 4.2, with runtime check. The CPUID instruction is needed for
2258 # the runtime check.
2259 cflags_crc += '-msse4.2'
2260 cdata.set('USE_SSE42_CRC32C', false)
2261 cdata.set('USE_SSE42_CRC32C_WITH_RUNTIME_CHECK', 1)
2262 have_optimized_crc = true
2267 elif host_cpu == 'arm' or host_cpu == 'aarch64'
2270 #include <arm_acle.h>
2274 unsigned int crc = 0;
2275 crc = __crc32cb(crc, 0);
2276 crc = __crc32ch(crc, 0);
2277 crc = __crc32cw(crc, 0);
2278 crc = __crc32cd(crc, 0);
2280 /* return computed value, to prevent the above being optimized away */
2285 if cc.links(prog, name: '__crc32cb, __crc32ch, __crc32cw, and __crc32cd without -march=armv8-a+crc',
2287 # Use ARM CRC Extension unconditionally
2288 cdata.set('USE_ARMV8_CRC32C', 1)
2289 have_optimized_crc = true
2290 elif cc.links(prog, name: '__crc32cb, __crc32ch, __crc32cw, and __crc32cd with -march=armv8-a+crc',
2291 args: test_c_args + ['-march=armv8-a+crc'])
2292 # Use ARM CRC Extension, with runtime check
2293 cflags_crc += '-march=armv8-a+crc'
2294 cdata.set('USE_ARMV8_CRC32C', false)
2295 cdata.set('USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK', 1)
2296 have_optimized_crc = true
2299 elif host_cpu == 'loongarch64'
2304 unsigned int crc = 0;
2305 crc = __builtin_loongarch_crcc_w_b_w(0, crc);
2306 crc = __builtin_loongarch_crcc_w_h_w(0, crc);
2307 crc = __builtin_loongarch_crcc_w_w_w(0, crc);
2308 crc = __builtin_loongarch_crcc_w_d_w(0, crc);
2310 /* return computed value, to prevent the above being optimized away */
2315 if cc.links(prog, name: '__builtin_loongarch_crcc_w_b_w, __builtin_loongarch_crcc_w_h_w, __builtin_loongarch_crcc_w_w_w, and __builtin_loongarch_crcc_w_d_w',
2317 # Use LoongArch CRC instruction unconditionally
2318 cdata.set('USE_LOONGARCH_CRC32C', 1)
2319 have_optimized_crc = true
2324 if not have_optimized_crc
2325 # fall back to slicing-by-8 algorithm, which doesn't require any special CPU
2327 cdata.set('USE_SLICING_BY_8_CRC32C', 1)
2332 ###############################################################
2333 # Other CPU specific stuff
2334 ###############################################################
2336 if host_cpu == 'x86_64'
2341 long long x = 1; long long r;
2342 __asm__ __volatile__ (" popcntq %1,%0\n" : "=q"(r) : "rm"(x));
2344 name: '@0@: popcntq instruction'.format(host_cpu),
2346 cdata.set('HAVE_X86_64_POPCNTQ', 1)
2349 elif host_cpu == 'ppc' or host_cpu == 'ppc64'
2350 # Check if compiler accepts "i"(x) when __builtin_constant_p(x).
2351 if cdata.has('HAVE__BUILTIN_CONSTANT_P')
2354 addi(int ra, int si)
2357 if (__builtin_constant_p(si))
2358 __asm__ __volatile__(
2359 " addi %0,%1,%2\n" : "=r"(res) : "b"(ra), "i"(si));
2362 int test_adds(int x) { return addi(3, x) + addi(x, 5); }
2365 cdata.set('HAVE_I_CONSTRAINT__BUILTIN_CONSTANT_P', 1)
2372 ###############################################################
2373 # Library / OS tests
2374 ###############################################################
2376 # XXX: Might be worth conditioning some checks on the OS, to avoid doing
2377 # unnecessary checks over and over, particularly on windows.
2390 'sys/personality.h',
2400 foreach header : header_checks
2401 varname = 'HAVE_' + header.underscorify().to_upper()
2403 # Emulate autoconf behaviour of not-found->undef, found->1
2404 found = cc.has_header(header,
2405 include_directories: postgres_inc, args: test_c_args)
2406 cdata.set(varname, found ? 1 : false,
2407 description: 'Define to 1 if you have the <@0@> header file.'.format(header))
2412 ['F_FULLFSYNC', 'fcntl.h'],
2413 ['fdatasync', 'unistd.h'],
2414 ['posix_fadvise', 'fcntl.h'],
2415 ['strlcat', 'string.h'],
2416 ['strlcpy', 'string.h'],
2417 ['strnlen', 'string.h'],
2418 ['strsep', 'string.h'],
2421 # Need to check for function declarations for these functions, because
2422 # checking for library symbols wouldn't handle deployment target
2423 # restrictions on macOS
2425 ['preadv', 'sys/uio.h'],
2426 ['pwritev', 'sys/uio.h'],
2429 # Check presence of some optional LLVM functions.
2432 ['LLVMCreateGDBRegistrationListener', 'llvm-c/ExecutionEngine.h'],
2433 ['LLVMCreatePerfJITEventListener', 'llvm-c/ExecutionEngine.h'],
2437 foreach c : decl_checks
2441 varname = 'HAVE_DECL_' + func.underscorify().to_upper()
2443 found = cc.has_header_symbol(header, func,
2444 args: test_c_args, include_directories: postgres_inc,
2446 cdata.set10(varname, found, description:
2447 '''Define to 1 if you have the declaration of `@0@', and to 0 if you
2448 don't.'''.format(func))
2452 if cc.has_type('struct option',
2453 args: test_c_args, include_directories: postgres_inc,
2454 prefix: '@0@'.format(cdata.get('HAVE_GETOPT_H')) == '1' ? '#include <getopt.h>' : '')
2455 cdata.set('HAVE_STRUCT_OPTION', 1)
2459 foreach c : ['opterr', 'optreset']
2460 varname = 'HAVE_INT_' + c.underscorify().to_upper()
2469 '''.format(c), name: c, args: test_c_args)
2470 cdata.set(varname, 1)
2472 cdata.set(varname, false)
2476 if cc.has_type('socklen_t',
2477 args: test_c_args, include_directories: postgres_inc,
2479 #include <sys/socket.h>''')
2480 cdata.set('HAVE_SOCKLEN_T', 1)
2483 if cc.has_member('struct sockaddr', 'sa_len',
2484 args: test_c_args, include_directories: postgres_inc,
2486 #include <sys/types.h>
2487 #include <sys/socket.h>''')
2488 cdata.set('HAVE_STRUCT_SOCKADDR_SA_LEN', 1)
2491 if cc.has_member('struct tm', 'tm_zone',
2492 args: test_c_args, include_directories: postgres_inc,
2494 #include <sys/types.h>
2497 cdata.set('HAVE_STRUCT_TM_TM_ZONE', 1)
2502 extern int foo(void);
2505 return timezone / 60;
2508 name: 'global variable `timezone\' exists',
2509 args: test_c_args, include_directories: postgres_inc)
2510 cdata.set('HAVE_INT_TIMEZONE', 1)
2512 cdata.set('HAVE_INT_TIMEZONE', false)
2515 if cc.has_type('union semun',
2517 include_directories: postgres_inc,
2519 #include <sys/types.h>
2520 #include <sys/ipc.h>
2521 #include <sys/sem.h>
2523 cdata.set('HAVE_UNION_SEMUN', 1)
2531 switch (strerror_r(1, buf, sizeof(buf)))
2532 { case 0: break; default: break; }
2535 args: test_c_args, include_directories: postgres_inc)
2536 cdata.set('STRERROR_R_INT', 1)
2538 cdata.set('STRERROR_R_INT', false)
2541 # Check if the C compiler understands typeof or a variant. Define
2542 # HAVE_TYPEOF if so, and define 'typeof' to the actual key word.
2543 foreach kw : ['typeof', '__typeof__', 'decltype']
2554 args: test_c_args, include_directories: postgres_inc)
2556 cdata.set('HAVE_TYPEOF', 1)
2558 cdata.set('typeof', kw)
2566 # MSVC doesn't cope well with defining restrict to __restrict, the spelling it
2567 # understands, because it conflicts with __declspec(restrict). Therefore we
2568 # define pg_restrict to the appropriate definition, which presumably won't
2571 # We assume C99 support, so we don't need to make this conditional.
2572 cdata.set('pg_restrict', '__restrict')
2575 # Most libraries are included only if they demonstrably provide a function we
2576 # need, but libm is an exception: always include it, because there are too
2577 # many compilers that play cute optimization games that will break probes for
2578 # standard functions such as pow().
2579 os_deps += cc.find_library('m', required: false)
2581 rt_dep = cc.find_library('rt', required: false)
2583 dl_dep = cc.find_library('dl', required: false)
2585 util_dep = cc.find_library('util', required: false)
2587 getopt_dep = cc.find_library('getopt', required: false)
2588 gnugetopt_dep = cc.find_library('gnugetopt', required: false)
2589 # Check if we want to replace getopt/getopt_long even if provided by the system
2590 # - Mingw has adopted a GNU-centric interpretation of optind/optreset,
2591 # so always use our version on Windows
2592 # - On OpenBSD and Solaris, getopt() doesn't do what we want for long options
2593 # (i.e., allow '-' as a flag character), so use our version on those platforms
2594 # - We want to use system's getopt_long() only if the system provides struct
2596 always_replace_getopt = host_system in ['windows', 'cygwin', 'openbsd', 'solaris']
2597 always_replace_getopt_long = host_system in ['windows', 'cygwin'] or not cdata.has('HAVE_STRUCT_OPTION')
2600 execinfo_dep = cc.find_library('execinfo', required: false)
2602 if host_system == 'cygwin'
2603 cygipc_dep = cc.find_library('cygipc', required: false)
2605 cygipc_dep = not_found_dep
2608 if host_system == 'sunos'
2609 socket_dep = cc.find_library('socket', required: false)
2611 socket_dep = not_found_dep
2614 # XXX: Might be worth conditioning some checks on the OS, to avoid doing
2615 # unnecessary checks over and over, particularly on windows.
2617 ['_configthreadlocale', {'skip': host_system != 'windows'}],
2618 ['backtrace_symbols', {'dependencies': [execinfo_dep]}],
2619 ['clock_gettime', {'dependencies': [rt_dep], 'define': false}],
2621 ['copy_file_range'],
2622 # gcc/clang's sanitizer helper library provides dlopen but not dlsym, thus
2623 # when enabling asan the dlopen check doesn't notice that -ldl is actually
2624 # required. Just checking for dlsym() ought to suffice.
2625 ['dlsym', {'dependencies': [dl_dep], 'define': false}],
2628 ['getopt', {'dependencies': [getopt_dep, gnugetopt_dep], 'skip': always_replace_getopt}],
2629 ['getopt_long', {'dependencies': [getopt_dep, gnugetopt_dep], 'skip': always_replace_getopt_long}],
2639 ['posix_fallocate'],
2641 ['pthread_barrier_wait', {'dependencies': [thread_dep]}],
2642 ['pthread_is_threaded_np', {'dependencies': [thread_dep]}],
2643 ['sem_init', {'dependencies': [rt_dep, thread_dep], 'skip': sema_kind != 'unnamed_posix', 'define': false}],
2644 ['setproctitle', {'dependencies': [util_dep]}],
2645 ['setproctitle_fast'],
2646 ['shm_open', {'dependencies': [rt_dep], 'define': false}],
2647 ['shm_unlink', {'dependencies': [rt_dep], 'define': false}],
2648 ['shmget', {'dependencies': [cygipc_dep], 'define': false}],
2649 ['socket', {'dependencies': [socket_dep], 'define': false}],
2651 ['strerror_r', {'dependencies': [thread_dep]}],
2657 ['sync_file_range'],
2663 func_check_results = {}
2664 foreach c : func_checks
2666 kwargs = c.get(1, {})
2667 deps = kwargs.get('dependencies', [])
2669 if kwargs.get('skip', false)
2673 found = cc.has_function(func, args: test_c_args)
2680 found = cc.has_function(func, args: test_c_args,
2681 dependencies: [dep])
2689 func_check_results += {func: found}
2691 if kwargs.get('define', true)
2692 # Emulate autoconf behaviour of not-found->undef, found->1
2693 cdata.set('HAVE_' + func.underscorify().to_upper(),
2695 description: 'Define to 1 if you have the `@0@\' function.'.format(func))
2700 if cc.has_function('syslog', args: test_c_args) and \
2701 cc.check_header('syslog.h', args: test_c_args)
2702 cdata.set('HAVE_SYSLOG', 1)
2706 # if prerequisites for unnamed posix semas aren't fulfilled, fall back to sysv
2708 if sema_kind == 'unnamed_posix' and \
2709 not func_check_results.get('sem_init', false)
2713 cdata.set('USE_@0@_SHARED_MEMORY'.format(shmem_kind.to_upper()), 1)
2714 cdata.set('USE_@0@_SEMAPHORES'.format(sema_kind.to_upper()), 1)
2716 cdata.set('MEMSET_LOOP_LIMIT', memset_loop_limit)
2717 cdata.set_quoted('DLSUFFIX', dlsuffix)
2720 # built later than the rest of the version metadata, we need SIZEOF_VOID_P
2721 cdata.set_quoted('PG_VERSION_STR',
2722 'PostgreSQL @0@ on @1@-@2@, compiled by @3@-@4@, @5@-bit'.format(
2723 pg_version, host_machine.cpu_family(), host_system,
2724 cc.get_id(), cc.version(), cdata.get('SIZEOF_VOID_P') * 8,
2729 ###############################################################
2731 ###############################################################
2733 nlsopt = get_option('nls')
2734 libintl = not_found_dep
2736 if not nlsopt.disabled()
2737 # otherwise there'd be lots of
2738 # "Gettext not found, all translation (po) targets will be ignored."
2739 # warnings if not found.
2740 msgfmt = find_program('msgfmt', required: nlsopt, native: true)
2742 # meson 0.59 has this wrapped in dependency('intl')
2743 if (msgfmt.found() and
2744 cc.check_header('libintl.h', required: nlsopt,
2745 args: test_c_args, include_directories: postgres_inc))
2748 if cc.has_function('ngettext')
2749 libintl = declare_dependency()
2751 libintl = cc.find_library('intl',
2752 has_headers: ['libintl.h'], required: nlsopt,
2753 header_include_directories: postgres_inc,
2759 i18n = import('i18n')
2760 cdata.set('ENABLE_NLS', 1)
2766 ###############################################################
2768 ###############################################################
2770 # Set up compiler / linker arguments to be used everywhere, individual targets
2771 # can add further args directly, or indirectly via dependencies
2772 add_project_arguments(cflags, language: ['c'])
2773 add_project_arguments(cppflags, language: ['c'])
2774 add_project_arguments(cflags_warn, language: ['c'])
2775 add_project_arguments(cxxflags, language: ['cpp'])
2776 add_project_arguments(cppflags, language: ['cpp'])
2777 add_project_arguments(cxxflags_warn, language: ['cpp'])
2778 add_project_link_arguments(ldflags, language: ['c', 'cpp'])
2781 # Collect a number of lists of things while recursing through the source
2782 # tree. Later steps then can use those.
2784 # list of targets for various alias targets
2785 backend_targets = []
2788 contrib_targets = []
2789 testprep_targets = []
2793 # Define the tests to distribute them to the correct test styles later
2798 # Default options for targets
2800 # First identify rpaths
2801 bin_install_rpaths = []
2802 lib_install_rpaths = []
2803 mod_install_rpaths = []
2806 # Don't add rpaths on darwin for now - as long as only absolute references to
2807 # libraries are needed, absolute LC_ID_DYLIB ensures libraries can be found in
2808 # their final destination.
2809 if host_system != 'darwin'
2810 # Add absolute path to libdir to rpath. This ensures installed binaries /
2811 # libraries find our libraries (mainly libpq).
2812 bin_install_rpaths += dir_prefix / dir_lib
2813 lib_install_rpaths += dir_prefix / dir_lib
2814 mod_install_rpaths += dir_prefix / dir_lib
2816 # Add extra_lib_dirs to rpath. This ensures we find libraries we depend on.
2818 # Not needed on darwin even if we use relative rpaths for our own libraries,
2819 # as the install_name of libraries in extra_lib_dirs will point to their
2821 bin_install_rpaths += postgres_lib_d
2822 lib_install_rpaths += postgres_lib_d
2823 mod_install_rpaths += postgres_lib_d
2827 # Define arguments for default targets
2829 default_target_args = {
2830 'implicit_include_directories': false,
2834 default_lib_args = default_target_args + {
2838 internal_lib_args = default_lib_args + {
2839 'build_by_default': false,
2843 default_mod_args = default_lib_args + {
2845 'install_dir': dir_lib_pkg,
2848 default_bin_args = default_target_args + {
2849 'install_dir': dir_bin,
2852 if get_option('rpath')
2853 default_lib_args += {
2854 'install_rpath': ':'.join(lib_install_rpaths),
2857 default_mod_args += {
2858 'install_rpath': ':'.join(mod_install_rpaths),
2861 default_bin_args += {
2862 'install_rpath': ':'.join(bin_install_rpaths),
2867 # Helper for exporting a limited number of symbols
2868 gen_export_kwargs = {
2869 'input': 'exports.txt',
2870 'output': '@BASENAME@.'+export_file_suffix,
2871 'command': [perl, files('src/tools/gen_export.pl'),
2872 '--format', export_file_format,
2873 '--input', '@INPUT0@', '--output', '@OUTPUT0@'],
2874 'build_by_default': false,
2881 ### Helpers for custom targets used across the tree
2884 catalog_pm = files('src/backend/catalog/Catalog.pm')
2885 perfect_hash_pm = files('src/tools/PerfectHash.pm')
2886 gen_kwlist_deps = [perfect_hash_pm]
2888 perl, '-I', '@SOURCE_ROOT@/src/tools',
2889 files('src/tools/gen_keywordlist.pl'),
2890 '--output', '@OUTDIR@', '@INPUT@']
2895 ### windows resources related stuff
2898 if host_system == 'windows'
2899 pg_ico = meson.source_root() / 'src' / 'port' / 'win32.ico'
2900 win32ver_rc = files('src/port/win32ver.rc')
2901 rcgen = find_program('src/tools/rcgen', native: true)
2904 '--srcdir', '@SOURCE_DIR@',
2905 '--builddir', meson.build_root(),
2906 '--rcout', '@OUTPUT0@',
2907 '--out', '@OUTPUT1@',
2908 '--input', '@INPUT@',
2912 if cc.get_argument_syntax() == 'msvc'
2913 rc = find_program('rc', required: true)
2914 rcgen_base_args += ['--rc', rc.path()]
2915 rcgen_outputs = ['@BASENAME@.rc', '@BASENAME@.res']
2917 windres = find_program('windres', required: true)
2918 rcgen_base_args += ['--windres', windres.path()]
2919 rcgen_outputs = ['@BASENAME@.rc', '@BASENAME@.obj']
2922 # msbuild backend doesn't support this atm
2923 if meson.backend() == 'ninja'
2924 rcgen_base_args += ['--depfile', '@DEPFILE@']
2927 rcgen_bin_args = rcgen_base_args + [
2928 '--VFT_TYPE', 'VFT_APP',
2929 '--FILEENDING', 'exe',
2933 rcgen_lib_args = rcgen_base_args + [
2934 '--VFT_TYPE', 'VFT_DLL',
2935 '--FILEENDING', 'dll',
2938 rc_bin_gen = generator(rcgen,
2939 depfile: '@BASENAME@.d',
2940 arguments: rcgen_bin_args,
2941 output: rcgen_outputs,
2944 rc_lib_gen = generator(rcgen,
2945 depfile: '@BASENAME@.d',
2946 arguments: rcgen_lib_args,
2947 output: rcgen_outputs,
2953 # headers that the whole build tree depends on
2954 generated_headers = []
2955 # headers that the backend build depends on
2956 generated_backend_headers = []
2957 # configure_files() output, needs a way of converting to file names
2958 configure_files = []
2960 # generated files that might conflict with a partial in-tree autoconf build
2961 generated_sources = []
2962 # same, for paths that differ between autoconf / meson builds
2963 # elements are [dir, [files]]
2964 generated_sources_ac = {}
2967 # First visit src/include - all targets creating headers are defined
2968 # within. That makes it easy to add the necessary dependencies for the
2969 # subsequent build steps.
2971 subdir('src/include')
2975 # Then through src/port and src/common, as most other things depend on them
2977 frontend_port_code = declare_dependency(
2978 compile_args: ['-DFRONTEND'],
2979 include_directories: [postgres_inc],
2980 dependencies: os_deps,
2983 backend_port_code = declare_dependency(
2984 compile_args: ['-DBUILDING_DLL'],
2985 include_directories: [postgres_inc],
2986 sources: [errcodes], # errcodes.h is needed due to use of ereport
2987 dependencies: os_deps,
2992 frontend_common_code = declare_dependency(
2993 compile_args: ['-DFRONTEND'],
2994 include_directories: [postgres_inc],
2995 sources: generated_headers,
2996 dependencies: [os_deps, zlib, zstd, lz4],
2999 backend_common_code = declare_dependency(
3000 compile_args: ['-DBUILDING_DLL'],
3001 include_directories: [postgres_inc],
3002 sources: generated_headers,
3003 dependencies: [os_deps, zlib, zstd],
3006 subdir('src/common')
3008 # all shared libraries should depend on shlib_code
3009 shlib_code = declare_dependency(
3010 link_args: ldflags_sl,
3013 # all static libraries not part of the backend should depend on this
3014 frontend_stlib_code = declare_dependency(
3015 include_directories: [postgres_inc],
3016 link_with: [common_static, pgport_static],
3017 sources: generated_headers,
3018 dependencies: [os_deps, libintl],
3021 # all shared libraries not part of the backend should depend on this
3022 frontend_shlib_code = declare_dependency(
3023 include_directories: [postgres_inc],
3024 link_with: [common_shlib, pgport_shlib],
3025 sources: generated_headers,
3026 dependencies: [shlib_code, os_deps, libintl],
3029 # Dependencies both for static and shared libpq
3039 subdir('src/interfaces/libpq')
3040 # fe_utils depends on libpq
3041 subdir('src/fe_utils')
3043 # for frontend binaries
3044 frontend_code = declare_dependency(
3045 include_directories: [postgres_inc],
3046 link_with: [fe_utils, common_static, pgport_static],
3047 sources: generated_headers,
3048 dependencies: [os_deps, libintl],
3051 backend_both_deps += [
3068 backend_mod_deps = backend_both_deps + os_deps
3070 backend_code = declare_dependency(
3071 compile_args: ['-DBUILDING_DLL'],
3072 include_directories: [postgres_inc],
3073 link_args: ldflags_be,
3075 sources: generated_headers + generated_backend_headers,
3076 dependencies: os_deps + backend_both_deps + backend_deps,
3079 # install these files only during test, not main install
3080 test_install_data = []
3081 test_install_libs = []
3083 # src/backend/meson.build defines backend_mod_code used for extension
3087 # Then through the main sources. That way contrib can have dependencies on
3088 # main sources. Note that this explicitly doesn't enter src/test, right now a
3089 # few regression tests depend on contrib files.
3096 subdir('src/interfaces/libpq/test')
3097 subdir('src/interfaces/ecpg/test')
3099 subdir('doc/src/sgml')
3101 generated_sources_ac += {'': ['GNUmakefile']}
3103 # After processing src/test, add test_install_libs to the testprep_targets
3105 testprep_targets += test_install_libs
3108 # If there are any files in the source directory that we also generate in the
3109 # build directory, they might get preferred over the newly generated files,
3110 # e.g. because of a #include "file", which always will search in the current
3112 message('checking for file conflicts between source and build directory')
3113 conflicting_files = []
3114 potentially_conflicting_files_t = []
3115 potentially_conflicting_files_t += generated_headers
3116 potentially_conflicting_files_t += generated_backend_headers
3117 potentially_conflicting_files_t += generated_backend_sources
3118 potentially_conflicting_files_t += generated_sources
3120 potentially_conflicting_files = []
3122 # convert all sources of potentially conflicting files into uniform shape
3123 foreach t : potentially_conflicting_files_t
3124 potentially_conflicting_files += t.full_path()
3126 foreach t1 : configure_files
3127 if meson.version().version_compare('>=0.59')
3128 t = fs.parent(t1) / fs.name(t1)
3130 t = '@0@'.format(t1)
3132 potentially_conflicting_files += meson.current_build_dir() / t
3134 foreach sub, fnames : generated_sources_ac
3135 sub = meson.build_root() / sub
3136 foreach fname : fnames
3137 potentially_conflicting_files += sub / fname
3141 # find and report conflicting files
3142 foreach build_path : potentially_conflicting_files
3143 build_path = host_system == 'windows' ? fs.as_posix(build_path) : build_path
3144 # str.replace is in 0.56
3145 src_path = meson.current_source_dir() / build_path.split(meson.current_build_dir() / '')[1]
3146 if fs.exists(src_path) or fs.is_symlink(src_path)
3147 conflicting_files += src_path
3150 # XXX: Perhaps we should generate a file that would clean these up? The list
3152 if conflicting_files.length() > 0
3153 errmsg_cleanup = '''
3154 Conflicting files in source directory:
3157 The conflicting files need to be removed, either by removing the files listed
3158 above, or by running configure and then make maintainer-clean.
3160 errmsg_cleanup = errmsg_cleanup.format(' '.join(conflicting_files))
3161 error(errmsg_nonclean_base.format(errmsg_cleanup))
3166 ###############################################################
3168 ###############################################################
3171 # We want to define additional install targets beyond what meson provides. For
3172 # that we need to define targets depending on nearly everything. We collected
3173 # the results of i18n.gettext() invocations into nls_targets, that also
3174 # includes maintainer targets though. Collect the ones we want as a dependency.
3176 # i18n.gettext() doesn't return the dependencies before 0.60 - but the gettext
3177 # generation happens during install, so that's not a real issue.
3179 if libintl.found() and meson.version().version_compare('>=0.60')
3180 # use range() to avoid the flattening of the list that foreach() would do
3181 foreach off : range(0, nls_targets.length())
3182 # i18n.gettext() list containing 1) list of built .mo files 2) maintainer
3183 # -pot target 3) maintainer -pot target
3184 nls_mo_targets += nls_targets[off][0]
3186 alias_target('nls', nls_mo_targets)
3201 # Meson's default install target is quite verbose. Provide one that is quiet.
3202 install_quiet = custom_target('install-quiet',
3203 output: 'install-quiet',
3204 build_always_stale: true,
3205 build_by_default: false,
3206 command: [meson_bin, meson_args, 'install', '--quiet', '--no-rebuild'],
3210 # Target to install files used for tests, which aren't installed by default
3211 install_test_files_args = [
3213 '--prefix', dir_prefix,
3214 '--install', contrib_data_dir, test_install_data,
3215 '--install', dir_lib_pkg, test_install_libs,
3217 run_target('install-test-files',
3218 command: [python] + install_test_files_args,
3219 depends: testprep_targets,
3224 ###############################################################
3226 ###############################################################
3228 # DESTDIR for the installation we'll run tests in
3229 test_install_destdir = meson.build_root() / 'tmp_install/'
3231 # DESTDIR + prefix appropriately munged
3232 if build_system != 'windows'
3233 # On unixoid systems this is trivial, we just prepend the destdir
3234 assert(dir_prefix.startswith('/')) # enforced by meson
3235 temp_install_bindir = '@0@@1@'.format(test_install_destdir, dir_prefix / dir_bin)
3236 temp_install_libdir = '@0@@1@'.format(test_install_destdir, dir_prefix / dir_lib)
3238 # drives, drive-relative paths, etc make this complicated on windows, call
3239 # into a copy of meson's logic for it
3242 'import sys; from pathlib import PurePath; d1=sys.argv[1]; d2=sys.argv[2]; print(str(PurePath(d1, *PurePath(d2).parts[1:])))',
3243 test_install_destdir]
3244 temp_install_bindir = run_command(command, dir_prefix / dir_bin, check: true).stdout().strip()
3245 temp_install_libdir = run_command(command, dir_prefix / dir_lib, check: true).stdout().strip()
3248 meson_install_args = meson_args + ['install'] + {
3249 'meson': ['--quiet', '--only-changed', '--no-rebuild'],
3253 # setup tests should be run first,
3254 # so define priority for these
3255 setup_tests_priority = 100
3257 meson_bin, args: meson_install_args ,
3258 env: {'DESTDIR':test_install_destdir},
3259 priority: setup_tests_priority,
3264 test('install_test_files',
3266 args: install_test_files_args + ['--destdir', test_install_destdir],
3267 priority: setup_tests_priority,
3271 test_result_dir = meson.build_root() / 'testrun'
3274 # XXX: pg_regress doesn't assign unique ports on windows. To avoid the
3275 # inevitable conflicts from running tests in parallel, hackishly assign
3276 # different ports for different tests.
3280 test_env = environment()
3282 test_initdb_template = meson.build_root() / 'tmp_install' / 'initdb-template'
3283 test_env.set('PG_REGRESS', pg_regress.full_path())
3284 test_env.set('REGRESS_SHLIB', regress_module.full_path())
3285 test_env.set('INITDB_TEMPLATE', test_initdb_template)
3287 # Add the temporary installation to the library search path on platforms where
3288 # that works (everything but windows, basically). On windows everything
3289 # library-like gets installed into bindir, solving that issue.
3290 if library_path_var != ''
3291 test_env.prepend(library_path_var, temp_install_libdir)
3295 # Create (and remove old) initdb template directory. Tests use that, where
3296 # possible, to make it cheaper to run tests.
3298 # Use python to remove the old cached initdb, as we cannot rely on a working
3299 # 'rm' binary on windows.
3300 test('initdb_cache',
3308 shutil.rmtree(sys.argv[1], ignore_errors=True)
3309 sp = subprocess.run(sys.argv[2:] + [sys.argv[1]])
3310 sys.exit(sp.returncode)
3312 test_initdb_template,
3313 temp_install_bindir / 'initdb',
3314 '--auth', 'trust', '--no-sync', '--no-instructions', '--lc-messages=C',
3317 priority: setup_tests_priority - 1,
3325 ###############################################################
3327 ###############################################################
3329 # When using a meson version understanding exclude_suites, define a
3330 # 'tmp_install' test setup (the default) that excludes tests running against a
3331 # pre-existing install and a 'running' setup that conflicts with creation of
3332 # the temporary installation and tap tests (which don't support running
3333 # against a running server).
3337 if meson.version().version_compare('>=0.57')
3340 runningcheck = false
3343 testwrap = files('src/tools/testwrap')
3345 foreach test_dir : tests
3348 '--basedir', meson.build_root(),
3349 '--srcdir', test_dir['sd'],
3350 # Some test suites are not run by default but can be run if selected by the
3351 # user via variable PG_TEST_EXTRA. Pass configuration time value of
3352 # PG_TEST_EXTRA as an argument to testwrap so that it can be overridden by
3353 # run time value, if any.
3354 '--pg-test-extra', get_option('PG_TEST_EXTRA'),
3357 foreach kind, v : test_dir
3358 if kind in ['sd', 'bd', 'name']
3364 if kind in ['regress', 'isolation', 'ecpg']
3365 if kind == 'regress'
3367 fallback_dbname = 'regression_@0@'
3368 elif kind == 'isolation'
3369 runner = pg_isolation_regress
3370 fallback_dbname = 'isolation_regression_@0@'
3372 runner = pg_regress_ecpg
3373 fallback_dbname = 'ecpg_regression_@0@'
3376 test_group = test_dir['name']
3377 test_group_running = test_dir['name'] + '-running'
3379 test_output = test_result_dir / test_group / kind
3380 test_output_running = test_result_dir / test_group_running/ kind
3382 # Unless specified by the test, choose a non-conflicting database name,
3383 # to avoid conflicts when running against existing server.
3384 dbname = t.get('dbname',
3385 fallback_dbname.format(test_dir['name']))
3387 test_command_base = [
3389 '--inputdir', t.get('inputdir', test_dir['sd']),
3390 '--expecteddir', t.get('expecteddir', test_dir['sd']),
3392 '--dlpath', test_dir['bd'],
3393 '--max-concurrent-tests=20',
3395 ] + t.get('regress_args', [])
3398 if t.has_key('schedule')
3399 test_selection += ['--schedule', t['schedule'],]
3402 if kind == 'isolation'
3403 test_selection += t.get('specs', [])
3405 test_selection += t.get('sql', [])
3409 env.prepend('PATH', temp_install_bindir, test_dir['bd'])
3415 'depends': test_deps + t.get('deps', []),
3417 } + t.get('test_kwargs', {})
3419 test(test_group / kind,
3423 '--testgroup', test_group,
3427 '--outputdir', test_output,
3428 '--temp-instance', test_output / 'tmp_check',
3429 '--port', testport.to_string(),
3433 kwargs: test_kwargs,
3435 install_suites += test_group
3437 # some tests can't support running against running DB
3438 if runningcheck and t.get('runningcheck', true)
3439 test(test_group_running / kind,
3443 '--testgroup', test_group_running,
3447 '--outputdir', test_output_running,
3450 is_parallel: t.get('runningcheck-parallel', true),
3451 suite: test_group_running,
3452 kwargs: test_kwargs,
3454 running_suites += test_group_running
3459 testwrap_tap = testwrap_base
3460 if not tap_tests_enabled
3461 testwrap_tap += ['--skip', 'TAP tests not enabled']
3466 '-I', meson.source_root() / 'src/test/perl',
3467 '-I', test_dir['sd'],
3470 # Add temporary install, the build directory for non-installed binaries and
3471 # also test/ for non-installed test binaries built separately.
3473 env.prepend('PATH', temp_install_bindir, test_dir['bd'], test_dir['bd'] / 'test')
3475 foreach name, value : t.get('env', {})
3476 env.set(name, value)
3479 test_group = test_dir['name']
3482 'suite': test_group,
3484 'depends': test_deps + t.get('deps', []),
3486 } + t.get('test_kwargs', {})
3488 foreach onetap : t['tests']
3489 # Make tap test names prettier, remove t/ and .pl
3491 if onetap_p.startswith('t/')
3492 onetap_p = onetap.split('t/')[1]
3494 if onetap_p.endswith('.pl')
3495 onetap_p = fs.stem(onetap_p)
3498 test(test_dir['name'] / onetap_p,
3500 kwargs: test_kwargs,
3501 args: testwrap_tap + [
3502 '--testgroup', test_dir['name'],
3503 '--testname', onetap_p,
3505 test_dir['sd'] / onetap,
3509 install_suites += test_group
3511 error('unknown kind @0@ of test in @1@'.format(kind, test_dir['sd']))
3514 endforeach # kinds of tests
3516 endforeach # directories with tests
3518 # repeat condition so meson realizes version dependency
3519 if meson.version().version_compare('>=0.57')
3520 add_test_setup('tmp_install',
3522 exclude_suites: running_suites)
3523 add_test_setup('running',
3524 exclude_suites: ['setup'] + install_suites)
3529 ###############################################################
3531 ###############################################################
3533 alias_target('backend', backend_targets)
3534 alias_target('bin', bin_targets + [libpq_st])
3535 alias_target('pl', pl_targets)
3536 alias_target('contrib', contrib_targets)
3537 alias_target('testprep', testprep_targets)
3539 alias_target('world', all_built, docs)
3540 alias_target('install-world', install_quiet, installdocs)
3544 perl, '-ne', 'next if /^#/; print',
3545 files('doc/src/sgml/targets-meson.txt'),
3551 ###############################################################
3552 # Distribution archive
3553 ###############################################################
3555 # Meson has its own distribution building command (meson dist), but we
3556 # are not using that at this point. The main problem is that, the way
3557 # they have implemented it, it is not deterministic. Also, we want it
3558 # to be equivalent to the "make" version for the time being. But the
3559 # target name "dist" in meson is reserved for that reason, so we call
3560 # the custom target "pgdist".
3562 git = find_program('git', required: false, native: true, disabler: true)
3563 bzip2 = find_program('bzip2', required: false, native: true)
3565 distdir = meson.project_name() + '-' + meson.project_version()
3567 pg_git_revision = get_option('PG_GIT_REVISION')
3569 # Note: core.autocrlf=false is needed to avoid line-ending conversion
3570 # in case the environment has a different setting. Without this, a
3571 # tarball created on Windows might be different than on, and unusable
3572 # on, Unix machines.
3574 tar_gz = custom_target('tar.gz',
3575 build_always_stale: true,
3576 command: [git, '-C', '@SOURCE_ROOT@',
3577 '-c', 'core.autocrlf=false',
3579 '--format', 'tar.gz',
3581 '--prefix', distdir + '/',
3582 '-o', join_paths(meson.build_root(), '@OUTPUT@'),
3584 output: distdir + '.tar.gz',
3588 tar_bz2 = custom_target('tar.bz2',
3589 build_always_stale: true,
3590 command: [git, '-C', '@SOURCE_ROOT@',
3591 '-c', 'core.autocrlf=false',
3592 '-c', 'tar.tar.bz2.command="@0@" -c'.format(bzip2.path()),
3594 '--format', 'tar.bz2',
3595 '--prefix', distdir + '/',
3596 '-o', join_paths(meson.build_root(), '@OUTPUT@'),
3598 output: distdir + '.tar.bz2',
3601 tar_bz2 = custom_target('tar.bz2',
3602 command: [perl, '-e', 'exit 1'],
3603 output: distdir + '.tar.bz2',
3607 alias_target('pgdist', [tar_gz, tar_bz2])
3609 # Make the standard "dist" command fail, to prevent accidental use.
3610 # But not if we are in a subproject, in case the parent project wants to
3611 # create a dist using the standard Meson command.
3612 if not meson.is_subproject()
3613 # We can only pass the identifier perl here when we depend on >= 0.55
3614 if meson.version().version_compare('>=0.55')
3615 meson.add_dist_script(perl, '-e', 'exit 1')
3621 ###############################################################
3622 # The End, The End, My Friend
3623 ###############################################################
3625 if meson.version().version_compare('>=0.57')
3629 'data block size': '@0@ kB'.format(cdata.get('BLCKSZ') / 1024),
3630 'WAL block size': '@0@ kB'.format(cdata.get('XLOG_BLCKSZ') / 1024),
3631 'segment size': get_option('segsize_blocks') != 0 ?
3632 '@0@ blocks'.format(cdata.get('RELSEG_SIZE')) :
3633 '@0@ GB'.format(get_option('segsize')),
3635 section: 'Data layout',
3640 'host system': '@0@ @1@'.format(host_system, host_cpu),
3641 'build system': '@0@ @1@'.format(build_machine.system(),
3642 build_machine.cpu_family()),
3649 'linker': '@0@'.format(cc.get_linker_id()),
3650 'C compiler': '@0@ @1@'.format(cc.get_id(), cc.version()),
3652 section: 'Compiler',
3657 'CPP FLAGS': ' '.join(cppflags),
3658 'C FLAGS, functional': ' '.join(cflags),
3659 'C FLAGS, warnings': ' '.join(cflags_warn),
3660 'C FLAGS, modules': ' '.join(cflags_mod),
3661 'C FLAGS, user specified': ' '.join(get_option('c_args')),
3662 'LD FLAGS': ' '.join(ldflags + get_option('c_link_args')),
3664 section: 'Compiler Flags',
3670 'C++ compiler': '@0@ @1@'.format(cpp.get_id(), cpp.version()),
3672 section: 'Compiler',
3677 'C++ FLAGS, functional': ' '.join(cxxflags),
3678 'C++ FLAGS, warnings': ' '.join(cxxflags_warn),
3679 'C++ FLAGS, user specified': ' '.join(get_option('cpp_args')),
3681 section: 'Compiler Flags',
3687 'bison': '@0@ @1@'.format(bison.full_path(), bison_version),
3689 'flex': '@0@ @1@'.format(flex.full_path(), flex_version),
3691 section: 'Programs',
3697 'bsd_auth': bsd_auth,
3699 'docs_pdf': docs_pdf_dep,
3711 'plpython': python3_dep,
3713 'readline': readline,
3720 section: 'External libraries',