Skip to content

Commit 74aeebb

Browse files
committed
meson: for internal linkage, link to both libzstd and a static copy of it
Partial, Meson-only implementation of facebook#2976 for non-MSVC builds. Due to the prevalence of private symbol reuse, linking to a shared library is simply utterly unreliable, but we still want to defer to the shared library for installable applications. By linking to both, we can share symbols where possible, and statically link where needed. This means we no longer need to manually track every file that needs to be extracted and reused. The flip side is that MSVC completely does not support this, so for MSVC builds we just link to a full static copy even where -Ddefault_library=shared. As a side benefit, by using library inclusion rather than including extra explicit object files, the zstd program shrinks in size slightly (~4kb).
1 parent 9e10939 commit 74aeebb

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

build/meson/lib/meson.build

+29
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,35 @@ libzstd = library('zstd',
126126
libzstd_dep = declare_dependency(link_with: libzstd,
127127
include_directories: libzstd_includes)
128128

129+
# we link to both:
130+
# - the shared library (for public symbols)
131+
# - the static library (for private symbols)
132+
#
133+
# this is needed because internally private symbols are used all the time, and
134+
# -fvisibility=hidden means those cannot be found
135+
if get_option('default_library') == 'static'
136+
libzstd_static = libzstd
137+
libzstd_internal_dep = libzstd_dep
138+
else
139+
if get_option('default_library') == 'shared'
140+
libzstd_static = static_library('zstd_objlib',
141+
objects: libzstd.extract_all_objects(recursive: true),
142+
build_by_default: false)
143+
else
144+
libzstd_static = libzstd.get_static_lib()
145+
endif
146+
147+
if cc_id == compiler_msvc
148+
# msvc does not actually support linking to both, but errors out with:
149+
# error LNK2005: ZSTD_<foo> already defined in zstd.lib(zstd-1.dll)
150+
libzstd_internal_dep = declare_dependency(link_with: libzstd_static)
151+
else
152+
libzstd_internal_dep = declare_dependency(link_with: libzstd,
153+
# the static library must be linked after the shared one
154+
dependencies: declare_dependency(link_with: libzstd_static))
155+
endif
156+
endif
157+
129158
pkgconfig.generate(libzstd,
130159
name: 'libzstd',
131160
filebase: 'libzstd',

build/meson/programs/meson.build

+2-13
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ zstd_programs_sources = [join_paths(zstd_rootdir, 'programs/zstdcli.c'),
2121
join_paths(zstd_rootdir, 'programs/dibio.c'),
2222
join_paths(zstd_rootdir, 'programs/zstdcli_trace.c')]
2323

24-
zstd_deps = [ libzstd_dep ]
24+
zstd_deps = [ libzstd_internal_dep ]
2525
zstd_c_args = libzstd_debug_cflags
2626

27-
zstd_frugal_deps = [ libzstd_dep ]
27+
zstd_frugal_deps = [ libzstd_internal_dep ]
2828
zstd_frugal_c_args = [ '-DZSTD_NOBENCH', '-DZSTD_NODICT', '-DZSTD_NOTRACE' ]
2929

3030
if use_multi_thread
@@ -70,12 +70,6 @@ zstd = executable('zstd',
7070
zstd_programs_sources,
7171
c_args: zstd_c_args,
7272
dependencies: zstd_deps,
73-
# needed due to use of private symbol + -fvisibility=hidden
74-
objects: libzstd.extract_objects(
75-
join_paths(zstd_rootdir, 'lib/common/xxhash.c'),
76-
join_paths(zstd_rootdir, 'lib/common/pool.c'),
77-
join_paths(zstd_rootdir, 'lib/common/zstd_common.c'),
78-
join_paths(zstd_rootdir, 'lib/common/error_private.c')),
7973
export_dynamic: export_dynamic_on_windows, # Since Meson 0.45.0
8074
install: true)
8175

@@ -90,11 +84,6 @@ zstd_frugal_sources = [join_paths(zstd_rootdir, 'programs/zstdcli.c'),
9084
executable('zstd-frugal',
9185
zstd_frugal_sources,
9286
dependencies: zstd_frugal_deps,
93-
# needed due to use of private symbol + -fvisibility=hidden
94-
objects: libzstd.extract_objects(
95-
join_paths(zstd_rootdir, 'lib/common/pool.c'),
96-
join_paths(zstd_rootdir, 'lib/common/zstd_common.c'),
97-
join_paths(zstd_rootdir, 'lib/common/error_private.c')),
9887
c_args: zstd_frugal_c_args,
9988
install: true)
10089

build/meson/tests/meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ testcommon_sources = [join_paths(zstd_rootdir, 'programs/datagen.c'),
3838
testcommon = static_library('testcommon',
3939
testcommon_sources,
4040
# needed due to use of private symbol + -fvisibility=hidden
41-
objects: libzstd.extract_all_objects(recursive: false))
41+
link_with: libzstd_static)
4242

4343
testcommon_dep = declare_dependency(link_with: testcommon,
4444
dependencies: libzstd_deps,

0 commit comments

Comments
 (0)