blob: 1bbcaf4c8a10a3f4477c4b46ebf0ca3c56294ac1 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From f1f1ae369a4cefd3474b3528e8d1847b18750605 Mon Sep 17 00:00:00 2001
2From: Christian Marangi <ansuelsmth@gmail.com>
3Date: Sat, 6 Apr 2024 14:41:54 +0200
4Subject: [PATCH] Provide variant pkg-config file for multi-threaded static lib
5
6Multi-threaded static library require -pthread to correctly link and works.
7The pkg-config we provide tho only works with dynamic multi-threaded library
8and won't provide the correct libs and cflags values if lib-mt is used.
9
10To handle this, introduce an env variable MT to permit advanced user to
11install and generate a correct pkg-config file for lib-mt or detect if
12lib-mt target is called.
13
14With MT env set on calling make install-pc, libzstd.pc.in is a
15pkg-config file for a multi-threaded static library.
16
17On calling make lib-mt, a libzstd.pc is generated for a multi-threaded
18static library as it's what asked by the user by forcing it.
19
20libzstd.pc is changed to PHONY to force regeneration of it on calling
21lib targets or install-pc to handle case where the same directory is
22used for mixed compilation.
23
24This was notice while migrating from meson to make build system where
25meson generates a correct .pc file while make doesn't.
26
27Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
28---
29 lib/Makefile | 20 +++++++++++++++++++-
30 lib/README.md | 4 ++++
31 lib/libzstd.pc.in | 4 ++--
32 3 files changed, 25 insertions(+), 3 deletions(-)
33
34--- a/lib/Makefile
35+++ b/lib/Makefile
36@@ -63,6 +63,8 @@ CPPFLAGS_DYNLIB += -DZSTD_MULTITHREAD #
37 LDFLAGS_DYNLIB += -pthread
38 CPPFLAGS_STATICLIB += # static library build defaults to single-threaded
39
40+# pkg-config Libs.private points to LDFLAGS_DYNLIB
41+PCLIB := $(LDFLAGS_DYNLIB)
42
43 ifeq ($(findstring GCC,$(CCVER)),GCC)
44 decompress/zstd_decompress_block.o : CFLAGS+=-fno-tree-vectorize
45@@ -186,12 +188,15 @@ lib : libzstd.a libzstd
46 %-mt : CPPFLAGS_DYNLIB := -DZSTD_MULTITHREAD
47 %-mt : CPPFLAGS_STATICLIB := -DZSTD_MULTITHREAD
48 %-mt : LDFLAGS_DYNLIB := -pthread
49+%-mt : PCLIB :=
50+%-mt : PCMTLIB := $(LDFLAGS_DYNLIB)
51 %-mt : %
52 @echo multi-threaded build completed
53
54 %-nomt : CPPFLAGS_DYNLIB :=
55 %-nomt : LDFLAGS_DYNLIB :=
56 %-nomt : CPPFLAGS_STATICLIB :=
57+%-nomt : PCLIB :=
58 %-nomt : %
59 @echo single-threaded build completed
60
61@@ -292,6 +297,14 @@ PCLIBPREFIX := $(if $(findstring $(LIBDI
62 # to PREFIX, rather than as a resolved value.
63 PCEXEC_PREFIX := $(if $(HAS_EXPLICIT_EXEC_PREFIX),$(EXEC_PREFIX),$${prefix})
64
65+
66+ifneq ($(MT),)
67+ PCLIB :=
68+ PCMTLIB := $(LDFLAGS_DYNLIB)
69+else
70+ PCLIB := $(LDFLAGS_DYNLIB)
71+endif
72+
73 ifneq (,$(filter $(UNAME),FreeBSD NetBSD DragonFly))
74 PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
75 else
76@@ -308,6 +321,10 @@ INSTALL_PROGRAM ?= $(INSTALL)
77 INSTALL_DATA ?= $(INSTALL) -m 644
78
79
80+# pkg-config library define.
81+# For static single-threaded library declare -pthread in Libs.private
82+# For static multi-threaded library declare -pthread in Libs and Cflags
83+.PHONY: libzstd.pc
84 libzstd.pc: libzstd.pc.in
85 @echo creating pkgconfig
86 @sed \
87@@ -316,7 +333,8 @@ libzstd.pc: libzstd.pc.in
88 -e 's|@INCLUDEDIR@|$(PCINCPREFIX)$(PCINCDIR)|' \
89 -e 's|@LIBDIR@|$(PCLIBPREFIX)$(PCLIBDIR)|' \
90 -e 's|@VERSION@|$(VERSION)|' \
91- -e 's|@LIBS_PRIVATE@|$(LDFLAGS_DYNLIB)|' \
92+ -e 's|@LIBS_MT@|$(PCMTLIB)|' \
93+ -e 's|@LIBS_PRIVATE@|$(PCLIB)|' \
94 $< >$@
95
96 .PHONY: install
97--- a/lib/README.md
98+++ b/lib/README.md
99@@ -27,12 +27,16 @@ Enabling multithreading requires 2 condi
100
101 For convenience, we provide a build target to generate multi and single threaded libraries:
102 - Force enable multithreading on both dynamic and static libraries by appending `-mt` to the target, e.g. `make lib-mt`.
103+ Note that the `.pc` generated on calling `make lib-mt` will already include the require Libs and Cflags.
104 - Force disable multithreading on both dynamic and static libraries by appending `-nomt` to the target, e.g. `make lib-nomt`.
105 - By default, as mentioned before, dynamic library is multithreaded, and static library is single-threaded, e.g. `make lib`.
106
107 When linking a POSIX program with a multithreaded version of `libzstd`,
108 note that it's necessary to invoke the `-pthread` flag during link stage.
109
110+The `.pc` generated from `make install` or `make install-pc` always assume a single-threaded static library
111+is compiled. To correctly generate a `.pc` for the multi-threaded static library, set `MT=1` as ENV variable.
112+
113 Multithreading capabilities are exposed
114 via the [advanced API defined in `lib/zstd.h`](https://github.com/facebook/zstd/blob/v1.4.3/lib/zstd.h#L351).
115
116--- a/lib/libzstd.pc.in
117+++ b/lib/libzstd.pc.in
118@@ -11,6 +11,6 @@ Name: zstd
119 Description: fast lossless compression algorithm library
120 URL: https://facebook.github.io/zstd/
121 Version: @VERSION@
122-Libs: -L${libdir} -lzstd
123+Libs: -L${libdir} -lzstd @LIBS_MT@
124 Libs.private: @LIBS_PRIVATE@
125-Cflags: -I${includedir}
126+Cflags: -I${includedir} @LIBS_MT@