Skip to content

Commit e555565

Browse files
committed
meson: Add support for building with precompiled headers
This substantially speeds up building for windows, due to the vast amount of headers included via windows.h. A cross build from linux targetting mingw goes from 994.11user 136.43system 0:31.58elapsed 3579%CPU to 422.41user 89.05system 0:14.35elapsed 3562%CPU The wins on windows are similar-ish (but I don't have a system at hand just now for actual numbers). Targetting other operating systems the wins are far smaller (tested linux, macOS, FreeBSD). For now precompiled headers are disabled by default, it's not clear how well they work on all platforms. E.g. on FreeBSD gcc doesn't seem to have working support, but clang does. When doing a full build precompiled headers are only beneficial for targets with multiple .c files, as meson builds a separate precompiled header for each target (so that different compilation options take effect). This commit therefore only changes target with at least two .c files to use precompiled headers. Because this commit adds b_pch=false to the default_options new build directories will have precompiled headers disabled by default, however existing build directories will continue use the default value of b_pch, which is true. Note that using precompiled headers with ccache requires setting CCACHE_SLOPPINESS=pch_defines,time_macros to get hits. Reviewed-by: Peter Eisentraut <[email protected]> Reviewed-by: Justin Pryzby <[email protected]> Discussion: https://postgr.es/m/CA+hUKG+50eOUbN++ocDc0Qnp9Pvmou23DSXu=ZA6fepOcftKqA@mail.gmail.com Discussion: https://postgr.es/m/[email protected] Discussion: https://postgr.es/m/20190826054000.GE7005%40paquier.xyz
1 parent e0b0142 commit e555565

File tree

32 files changed

+44
-1
lines changed

32 files changed

+44
-1
lines changed

.cirrus.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ task:
447447
# Use /DEBUG:FASTLINK to avoid high memory usage during linking
448448
configure_script: |
449449
vcvarsall x64
450-
meson setup --backend ninja --buildtype debug -Dc_link_args=/DEBUG:FASTLINK -Dcassert=true -Dssl=openssl -Dextra_lib_dirs=c:\openssl\1.1\lib -Dextra_include_dirs=c:\openssl\1.1\include -DTAR=c:/windows/system32/tar.exe -DPG_TEST_EXTRA="%PG_TEST_EXTRA%" build
450+
meson setup --backend ninja --buildtype debug -Dc_link_args=/DEBUG:FASTLINK -Dcassert=true -Db_pch=true -Dssl=openssl -Dextra_lib_dirs=c:\openssl\1.1\lib -Dextra_include_dirs=c:\openssl\1.1\include -DTAR=c:/windows/system32/tar.exe -DPG_TEST_EXTRA="%PG_TEST_EXTRA%" build
451451
452452
build_script: |
453453
vcvarsall x64

contrib/bloom/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endif
1515

1616
bloom = shared_module('bloom',
1717
bloom_sources,
18+
c_pch: pch_postgres_h,
1819
kwargs: contrib_mod_args,
1920
)
2021
contrib_targets += bloom

contrib/btree_gist/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ endif
3333

3434
btree_gist = shared_module('btree_gist',
3535
btree_gist_sources,
36+
c_pch: pch_postgres_h,
3637
kwargs: contrib_mod_args,
3738
)
3839
contrib_targets += btree_gist

contrib/hstore/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ endif
1818

1919
hstore = shared_module('hstore',
2020
hstore_sources,
21+
c_pch: pch_postgres_h,
2122
kwargs: contrib_mod_args,
2223
)
2324
contrib_targets += hstore

contrib/pg_trgm/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ endif
1313

1414
pg_trgm = shared_module('pg_trgm',
1515
pg_trgm_sources,
16+
c_pch: pch_postgres_h,
1617
kwargs: contrib_mod_args,
1718
)
1819
contrib_targets += pg_trgm

contrib/pgcrypto/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ endif
7878
pgcrypto = shared_module('pgcrypto',
7979
pgcrypto_sources,
8080
link_with: pgcrypto_link_with,
81+
c_pch: pch_postgres_h,
8182
kwargs: contrib_mod_args + {
8283
'dependencies': [pgcrypto_deps, contrib_mod_args['dependencies']]
8384
},

contrib/pgstattuple/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif
1212

1313
pgstattuple = shared_module('pgstattuple',
1414
pgstattuple_sources,
15+
c_pch: pch_postgres_h,
1516
kwargs: contrib_mod_args,
1617
)
1718
contrib_targets += pgstattuple

contrib/sepgsql/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endif
2222

2323
sepgsql = shared_module('sepgsql',
2424
sepgsql_sources,
25+
c_pch: pch_postgres_h,
2526
kwargs: contrib_mod_args + {
2627
'dependencies': [selinux, contrib_mod_args['dependencies']],
2728
}

contrib/xml2/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endif
1515

1616
xml2 = shared_module('pgxml',
1717
xml2_sources,
18+
c_pch: pch_postgres_h,
1819
kwargs: contrib_mod_args + {
1920
'dependencies': [libxml, libxslt, contrib_mod_args['dependencies']],
2021
},

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ project('postgresql',
1515
meson_version: '>=0.54',
1616
default_options: [
1717
'warning_level=1', #-Wall equivalent
18+
'b_pch=false',
1819
'buildtype=release',
1920
# For compatibility with the autoconf build, set a default prefix. This
2021
# works even on windows, where it's a drive-relative path (i.e. when on

src/backend/meson.build

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ postgres_lib = static_library('postgres_lib',
6262
backend_sources + timezone_sources + generated_backend_sources,
6363
link_whole: backend_link_with,
6464
dependencies: backend_build_deps,
65+
c_pch: pch_postgres_h,
6566
kwargs: internal_lib_args,
6667
)
6768

@@ -81,6 +82,10 @@ if cc.get_id() == 'msvc'
8182

8283
backend_link_args += '/DEF:@0@'.format(postgres_def.full_path())
8384
backend_link_depends += postgres_def
85+
# Due to the way msvc and meson's precompiled headers implementation
86+
# interact, we need to have symbols from the full library available. Could
87+
# be restricted to b_pch=true.
88+
backend_link_with += postgres_lib
8489

8590
elif host_system == 'aix'
8691
# The '.' argument leads mkldexport.sh to emit "#! .", which refers to the

src/backend/snowball/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ endif
6666

6767
dict_snowball = shared_module('dict_snowball',
6868
dict_snowball_sources,
69+
c_pch: pch_postgres_h,
6970
kwargs: pg_mod_args + {
7071
'include_directories': [stemmer_inc],
7172
}

src/bin/pg_dump/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pg_dump_common_sources = files(
1313

1414
pg_dump_common = static_library('libpgdump_common',
1515
pg_dump_common_sources,
16+
c_pch: pch_postgres_fe_h,
1617
dependencies: [frontend_code, libpq, zlib],
1718
kwargs: internal_lib_args,
1819
)

src/bin/pg_upgrade/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ endif
2424

2525
pg_upgrade = executable('pg_upgrade',
2626
pg_upgrade_sources,
27+
c_pch: pch_postgres_fe_h,
2728
dependencies: [frontend_code, libpq],
2829
kwargs: default_bin_args,
2930
)

src/bin/pgbench/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pgbench = executable('pgbench',
2727
pgbench_sources,
2828
dependencies: [frontend_code, libpq, thread_dep],
2929
include_directories: include_directories('.'),
30+
c_pch: pch_postgres_fe_h,
3031
c_args: host_system == 'windows' ? ['-DFD_SETSIZE=1024'] : [],
3132
kwargs: default_bin_args,
3233
)

src/bin/psql/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ endif
4444

4545
psql = executable('psql',
4646
psql_sources,
47+
c_pch: pch_postgres_fe_h,
4748
include_directories: include_directories('.'),
4849
dependencies: [frontend_code, libpq, readline],
4950
kwargs: default_bin_args,

src/common/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ foreach name, opts : pgcommon_variants
147147
endif
148148
c_args = opts.get('c_args', []) + common_cflags[cflagname]
149149
cflag_libs += static_library('libpgcommon@0@_@1@'.format(name, cflagname),
150+
c_pch: pch_c_h,
150151
include_directories: include_directories('.'),
151152
kwargs: opts + {
152153
'sources': sources,
@@ -159,6 +160,7 @@ foreach name, opts : pgcommon_variants
159160

160161
lib = static_library('libpgcommon@0@'.format(name),
161162
link_with: cflag_libs,
163+
c_pch: pch_c_h,
162164
include_directories: include_directories('.'),
163165
kwargs: opts + {
164166
'dependencies': opts['dependencies'] + [ssl],

src/fe_utils/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fe_utils_sources += psqlscan
2323

2424
fe_utils = static_library('libpgfeutils',
2525
fe_utils_sources + generated_headers,
26+
c_pch: pch_postgres_fe_h,
2627
include_directories: [postgres_inc, libpq_inc],
2728
c_args: host_system == 'windows' ? ['-DFD_SETSIZE=1024'] : [],
2829
dependencies: frontend_common_code,

src/include/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ install_headers(
114114

115115
subdir('catalog')
116116
subdir('nodes')
117+
subdir('pch')
117118
subdir('storage')
118119
subdir('utils')
119120

src/include/pch/c_pch.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "c.h"

src/include/pch/meson.build

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# See https://github.com/mesonbuild/meson/issues/10338
2+
pch_c_h = meson.source_root() / meson.current_source_dir() / 'c_pch.h'
3+
pch_postgres_h = meson.source_root() / meson.current_source_dir() / 'postgres_pch.h'
4+
pch_postgres_fe_h = meson.source_root() / meson.current_source_dir() / 'postgres_fe_pch.h'

src/include/pch/postgres_fe_pch.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "postgres_fe.h"

src/include/pch/postgres_pch.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "postgres.h"

src/interfaces/ecpg/ecpglib/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ecpglib_st = static_library('libecpg',
2727
ecpglib_sources,
2828
include_directories: ecpglib_inc,
2929
c_args: ecpglib_c_args,
30+
c_pch: pch_postgres_fe_h,
3031
dependencies: [frontend_stlib_code, thread_dep, libpq],
3132
link_with: [ecpg_pgtypes_st],
3233
kwargs: default_lib_args,
@@ -37,6 +38,7 @@ ecpglib_so = shared_library('libecpg',
3738
ecpglib_sources + ecpglib_so_sources,
3839
include_directories: ecpglib_inc,
3940
c_args: ecpglib_c_args,
41+
c_pch: pch_postgres_fe_h,
4042
dependencies: [frontend_shlib_code, libpq, thread_dep],
4143
link_with: ecpg_pgtypes_so,
4244
soversion: host_system != 'windows' ? '6' : '',

src/interfaces/ecpg/pgtypeslib/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ecpg_pgtypes_st = static_library('libpgtypes',
2323
ecpg_pgtypes_sources,
2424
include_directories: ecpg_pgtypes_inc,
2525
c_args: ecpg_pgtypes_c_args,
26+
c_pch: pch_postgres_fe_h,
2627
dependencies: frontend_stlib_code,
2728
kwargs: default_lib_args,
2829
)
@@ -32,6 +33,7 @@ ecpg_pgtypes_so = shared_library('libpgtypes',
3233
ecpg_pgtypes_sources + ecpg_pgtypes_so_sources,
3334
include_directories: ecpg_pgtypes_inc,
3435
c_args: ecpg_pgtypes_c_args,
36+
c_pch: pch_postgres_fe_h,
3537
dependencies: frontend_shlib_code,
3638
version: '3.' + pg_version_major.to_string(),
3739
soversion: host_system != 'windows' ? '3' : '',

src/interfaces/ecpg/preproc/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ endif
102102
ecpg_exe = executable('ecpg',
103103
ecpg_sources,
104104
include_directories: ['.', ecpg_inc, postgres_inc, libpq_inc],
105+
c_pch: pch_postgres_fe_h,
105106
dependencies: [frontend_code],
106107
kwargs: default_bin_args,
107108
)

src/interfaces/libpq/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ libpq_st = static_library('libpq',
5858
libpq_sources,
5959
include_directories: [libpq_inc],
6060
c_args: libpq_c_args,
61+
c_pch: pch_postgres_fe_h,
6162
dependencies: [frontend_stlib_code, libpq_deps],
6263
kwargs: default_lib_args,
6364
)
@@ -66,6 +67,7 @@ libpq_so = shared_library('libpq',
6667
libpq_sources + libpq_so_sources,
6768
include_directories: [libpq_inc, postgres_inc],
6869
c_args: libpq_c_args,
70+
c_pch: pch_postgres_fe_h,
6971
version: '5.' + pg_version_major.to_string(),
7072
soversion: host_system != 'windows' ? '5' : '',
7173
darwin_versions: ['5', '5.' + pg_version_major.to_string()],

src/pl/plperl/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ endif
4545

4646
plperl = shared_module('plperl',
4747
plperl_sources,
48+
c_pch: pch_postgres_h,
4849
include_directories: [plperl_inc, postgres_inc],
4950
kwargs: pg_mod_args + {
5051
'dependencies': [perl_dep, pg_mod_args['dependencies']],

src/pl/plpgsql/src/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ endif
4848

4949
plpgsql = shared_module('plpgsql',
5050
plpgsql_sources,
51+
c_pch: pch_postgres_h,
5152
include_directories: include_directories('.'),
5253
kwargs: pg_mod_args,
5354
)

src/pl/plpython/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ endif
3636

3737
plpython = shared_module('plpython3',
3838
plpython_sources,
39+
c_pch: pch_postgres_h,
3940
include_directories: [plpython_inc, postgres_inc],
4041
kwargs: pg_mod_args + {
4142
'dependencies': [python3_dep, pg_mod_args['dependencies']],

src/pl/tcl/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endif
2222

2323
pltcl = shared_module('pltcl',
2424
pltcl_sources,
25+
c_pch: pch_postgres_h,
2526
include_directories: [include_directories('.'), postgres_inc],
2627
kwargs: pg_mod_args + {
2728
'dependencies': [tcl_dep, pg_mod_args['dependencies']],

src/port/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ foreach name, opts : pgport_variants
161161
c_args = opts.get('c_args', []) + pgport_cflags[cflagname]
162162
cflag_libs += static_library('libpgport@0@_@1@'.format(name, cflagname),
163163
sources,
164+
c_pch: pch_c_h,
164165
kwargs: opts + {
165166
'c_args': c_args,
166167
'build_by_default': false,
@@ -172,6 +173,7 @@ foreach name, opts : pgport_variants
172173
lib = static_library('libpgport@0@'.format(name),
173174
pgport_sources,
174175
link_with: cflag_libs,
176+
c_pch: pch_c_h,
175177
kwargs: opts + {
176178
'dependencies': opts['dependencies'] + [ssl],
177179
}

0 commit comments

Comments
 (0)