001-Provide-variant-pkg-config-file-for-multi-threaded-s.patch 4.8 KB

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