Skip to content

Commit f1f1ae3

Browse files
committed
Provide variant pkg-config file for multi-threaded static lib
Multi-threaded static library require -pthread to correctly link and works. The pkg-config we provide tho only works with dynamic multi-threaded library and won't provide the correct libs and cflags values if lib-mt is used. To handle this, introduce an env variable MT to permit advanced user to install and generate a correct pkg-config file for lib-mt or detect if lib-mt target is called. With MT env set on calling make install-pc, libzstd.pc.in is a pkg-config file for a multi-threaded static library. On calling make lib-mt, a libzstd.pc is generated for a multi-threaded static library as it's what asked by the user by forcing it. libzstd.pc is changed to PHONY to force regeneration of it on calling lib targets or install-pc to handle case where the same directory is used for mixed compilation. This was notice while migrating from meson to make build system where meson generates a correct .pc file while make doesn't. Signed-off-by: Christian Marangi <[email protected]>
1 parent 72c16b1 commit f1f1ae3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/Makefile

+19-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ CPPFLAGS_DYNLIB += -DZSTD_MULTITHREAD # dynamic library build defaults to multi
6363
LDFLAGS_DYNLIB += -pthread
6464
CPPFLAGS_STATICLIB += # static library build defaults to single-threaded
6565

66+
# pkg-config Libs.private points to LDFLAGS_DYNLIB
67+
PCLIB := $(LDFLAGS_DYNLIB)
6668

6769
ifeq ($(findstring GCC,$(CCVER)),GCC)
6870
decompress/zstd_decompress_block.o : CFLAGS+=-fno-tree-vectorize
@@ -186,12 +188,15 @@ lib : libzstd.a libzstd
186188
%-mt : CPPFLAGS_DYNLIB := -DZSTD_MULTITHREAD
187189
%-mt : CPPFLAGS_STATICLIB := -DZSTD_MULTITHREAD
188190
%-mt : LDFLAGS_DYNLIB := -pthread
191+
%-mt : PCLIB :=
192+
%-mt : PCMTLIB := $(LDFLAGS_DYNLIB)
189193
%-mt : %
190194
@echo multi-threaded build completed
191195

192196
%-nomt : CPPFLAGS_DYNLIB :=
193197
%-nomt : LDFLAGS_DYNLIB :=
194198
%-nomt : CPPFLAGS_STATICLIB :=
199+
%-nomt : PCLIB :=
195200
%-nomt : %
196201
@echo single-threaded build completed
197202

@@ -292,6 +297,14 @@ PCLIBPREFIX := $(if $(findstring $(LIBDIR),$(PCLIBDIR)),,$${exec_prefix})
292297
# to PREFIX, rather than as a resolved value.
293298
PCEXEC_PREFIX := $(if $(HAS_EXPLICIT_EXEC_PREFIX),$(EXEC_PREFIX),$${prefix})
294299

300+
301+
ifneq ($(MT),)
302+
PCLIB :=
303+
PCMTLIB := $(LDFLAGS_DYNLIB)
304+
else
305+
PCLIB := $(LDFLAGS_DYNLIB)
306+
endif
307+
295308
ifneq (,$(filter $(UNAME),FreeBSD NetBSD DragonFly))
296309
PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
297310
else
@@ -308,6 +321,10 @@ INSTALL_PROGRAM ?= $(INSTALL)
308321
INSTALL_DATA ?= $(INSTALL) -m 644
309322

310323

324+
# pkg-config library define.
325+
# For static single-threaded library declare -pthread in Libs.private
326+
# For static multi-threaded library declare -pthread in Libs and Cflags
327+
.PHONY: libzstd.pc
311328
libzstd.pc: libzstd.pc.in
312329
@echo creating pkgconfig
313330
@sed \
@@ -316,7 +333,8 @@ libzstd.pc: libzstd.pc.in
316333
-e 's|@INCLUDEDIR@|$(PCINCPREFIX)$(PCINCDIR)|' \
317334
-e 's|@LIBDIR@|$(PCLIBPREFIX)$(PCLIBDIR)|' \
318335
-e 's|@VERSION@|$(VERSION)|' \
319-
-e 's|@LIBS_PRIVATE@|$(LDFLAGS_DYNLIB)|' \
336+
-e 's|@LIBS_MT@|$(PCMTLIB)|' \
337+
-e 's|@LIBS_PRIVATE@|$(PCLIB)|' \
320338
$< >$@
321339

322340
.PHONY: install

lib/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ Enabling multithreading requires 2 conditions :
2727

2828
For convenience, we provide a build target to generate multi and single threaded libraries:
2929
- Force enable multithreading on both dynamic and static libraries by appending `-mt` to the target, e.g. `make lib-mt`.
30+
Note that the `.pc` generated on calling `make lib-mt` will already include the require Libs and Cflags.
3031
- Force disable multithreading on both dynamic and static libraries by appending `-nomt` to the target, e.g. `make lib-nomt`.
3132
- By default, as mentioned before, dynamic library is multithreaded, and static library is single-threaded, e.g. `make lib`.
3233

3334
When linking a POSIX program with a multithreaded version of `libzstd`,
3435
note that it's necessary to invoke the `-pthread` flag during link stage.
3536

37+
The `.pc` generated from `make install` or `make install-pc` always assume a single-threaded static library
38+
is compiled. To correctly generate a `.pc` for the multi-threaded static library, set `MT=1` as ENV variable.
39+
3640
Multithreading capabilities are exposed
3741
via the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.4.3/lib/zstd.h#L351).
3842

lib/libzstd.pc.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Name: zstd
1111
Description: fast lossless compression algorithm library
1212
URL: https://facebook.github.io/zstd/
1313
Version: @VERSION@
14-
Libs: -L${libdir} -lzstd
14+
Libs: -L${libdir} -lzstd @LIBS_MT@
1515
Libs.private: @LIBS_PRIVATE@
16-
Cflags: -I${includedir}
16+
Cflags: -I${includedir} @LIBS_MT@

0 commit comments

Comments
 (0)