1 From 7be7e35d61d8d499599623502a35460d410de114 Mon Sep 17 00:00:00 2001
2 From: Alexander Shpilkin <ashpilkin@gmail.com>
3 Date: Thu, 26 May 2022 16:03:27 +0300
4 Subject: [PATCH] Squashed commit of the following:
6 commit 9aacb9d5da65a64c3845937a6f9eede329d43989
7 Author: W. Felix Handte <w@felixhandte.com>
8 Date: Tue Dec 8 20:46:02 2020 -0500
10 Apply Same Strategy to CMake
12 (cherry picked from commit a75f9ce3e924564ab358c2c1aa95b6268383ec42)
14 commit e21b7ad0d98d1322ea92f99fcd1f85e2d6b6f6b7
15 Author: W. Felix Handte <w@felixhandte.com>
16 Date: Tue Dec 8 20:10:05 2020 -0500
18 Avoid Use of Regexes in Building Package-Config File
20 (cherry picked from commit b521183c74795bd9bdd9bdebe74af01cae4d3d43)
22 build/cmake/lib/CMakeLists.txt | 29 ++++++++++++++++++++++++----
23 lib/Makefile | 35 ++++++++++++++++------------------
24 lib/libzstd.pc.in | 6 +++---
25 3 files changed, 44 insertions(+), 26 deletions(-)
27 diff --git a/build/cmake/lib/CMakeLists.txt b/build/cmake/lib/CMakeLists.txt
28 index 32ae7525..466c2c7b 100644
29 --- a/build/cmake/lib/CMakeLists.txt
30 +++ b/build/cmake/lib/CMakeLists.txt
31 @@ -137,12 +137,33 @@ endif ()
34 set(PREFIX "${CMAKE_INSTALL_PREFIX}")
35 - set(LIBDIR "${CMAKE_INSTALL_LIBDIR}")
36 - set(INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
37 + set(EXEC_PREFIX "\\$$\{prefix}")
38 + set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
39 + set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
40 set(VERSION "${zstd_VERSION}")
42 + string(LENGTH "${PREFIX}" PREFIX_LENGTH)
43 + string(SUBSTRING "${LIBDIR}" 0 ${PREFIX_LENGTH} LIBDIR_PREFIX)
44 + string(SUBSTRING "${LIBDIR}" ${PREFIX_LENGTH} -1 LIBDIR_SUFFIX)
45 + string(SUBSTRING "${INCLUDEDIR}" 0 ${PREFIX_LENGTH} INCLUDEDIR_PREFIX)
46 + string(SUBSTRING "${INCLUDEDIR}" ${PREFIX_LENGTH} -1 INCLUDEDIR_SUFFIX)
48 + if ("${INCLUDEDIR_PREFIX}" STREQUAL "${PREFIX}")
49 + set(INCLUDEDIR_PREFIX "\\$$\{prefix}")
51 + if ("${LIBDIR_PREFIX}" STREQUAL "${PREFIX}")
52 + set(LIBDIR_PREFIX "\\$$\{exec_prefix}")
55 add_custom_target(libzstd.pc ALL
56 - ${CMAKE_COMMAND} -DIN="${LIBRARY_DIR}/libzstd.pc.in" -DOUT="libzstd.pc"
57 - -DPREFIX="${PREFIX}" -DLIBDIR="${LIBDIR}" -DINCLUDEDIR="${INCLUDEDIR}" -DVERSION="${VERSION}"
59 + -DIN="${LIBRARY_DIR}/libzstd.pc.in"
61 + -DPREFIX="${PREFIX}"
62 + -DEXEC_PREFIX="${EXEC_PREFIX}"
63 + -DINCLUDEDIR="${INCLUDEDIR_PREFIX}${INCLUDEDIR_SUFFIX}"
64 + -DLIBDIR="${LIBDIR_PREFIX}${LIBDIR_SUFFIX}"
65 + -DVERSION="${VERSION}"
66 -P "${CMAKE_CURRENT_SOURCE_DIR}/pkgconfig.cmake"
67 COMMENT "Creating pkg-config file")
69 diff --git a/lib/Makefile b/lib/Makefile
70 index 4a9ab799..2893ec21 100644
73 @@ -257,6 +257,8 @@ ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD Ne
77 +HAS_EXPLICIT_EXEC_PREFIX := $(if $(or $(EXEC_PREFIX),$(exec_prefix)),1,)
80 # directory variables : GNU conventions prefer lowercase
81 # see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
82 @@ -270,24 +272,17 @@ LIBDIR ?= $(libdir)
83 includedir ?= $(PREFIX)/include
84 INCLUDEDIR ?= $(includedir)
86 -PCLIBDIR ?= $(shell echo "$(LIBDIR)" | sed -n $(SED_ERE_OPT) -e "s@^$(EXEC_PREFIX)(/|$$)@@p")
87 -PCINCDIR ?= $(shell echo "$(INCLUDEDIR)" | sed -n $(SED_ERE_OPT) -e "s@^$(PREFIX)(/|$$)@@p")
88 +PCINCDIR := $(patsubst $(PREFIX)%,%,$(INCLUDEDIR))
89 +PCLIBDIR := $(patsubst $(EXEC_PREFIX)%,%,$(LIBDIR))
92 -# Additional prefix check is required, since the empty string is technically a
94 -ifeq (,$(shell echo "$(LIBDIR)" | sed -n $(SED_ERE_OPT) -e "\\@^$(EXEC_PREFIX)(/|$$)@ p"))
95 -$(error configured libdir ($(LIBDIR)) is outside of prefix ($(PREFIX)), can't generate pkg-config file)
98 +# If we successfully stripped off a prefix, we'll add a reference to the
99 +# relevant pc variable.
100 +PCINCPREFIX := $(if $(findstring $(INCLUDEDIR),$(PCINCDIR)),,$${prefix})
101 +PCLIBPREFIX := $(if $(findstring $(LIBDIR),$(PCLIBDIR)),,$${exec_prefix})
104 -# Additional prefix check is required, since the empty string is technically a
106 -ifeq (,$(shell echo "$(INCLUDEDIR)" | sed -n $(SED_ERE_OPT) -e "\\@^$(PREFIX)(/|$$)@ p"))
107 -$(error configured includedir ($(INCLUDEDIR)) is outside of exec_prefix ($(EXEC_PREFIX)), can't generate pkg-config file)
110 +# If no explicit EXEC_PREFIX was set by the caller, write it out as a reference
111 +# to PREFIX, rather than as a resolved value.
112 +PCEXEC_PREFIX := $(if $(HAS_EXPLICIT_EXEC_PREFIX),$(EXEC_PREFIX),$${prefix})
114 ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
115 PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
116 @@ -308,9 +303,11 @@ INSTALL_DATA ?= $(INSTALL) -m 644
118 libzstd.pc: libzstd.pc.in
119 @echo creating pkgconfig
120 - $(Q)@sed $(SED_ERE_OPT) -e 's|@PREFIX@|$(PREFIX)|' \
121 - -e 's|@LIBDIR@|$(PCLIBDIR)|' \
122 - -e 's|@INCLUDEDIR@|$(PCINCDIR)|' \
123 + $(Q)@sed $(SED_ERE_OPT) \
124 + -e 's|@PREFIX@|$(PREFIX)|' \
125 + -e 's|@EXEC_PREFIX@|$(PCEXEC_PREFIX)|' \
126 + -e 's|@INCLUDEDIR@|$(PCINCPREFIX)$(PCINCDIR)|' \
127 + -e 's|@LIBDIR@|$(PCLIBPREFIX)$(PCLIBDIR)|' \
128 -e 's|@VERSION@|$(VERSION)|' \
131 diff --git a/lib/libzstd.pc.in b/lib/libzstd.pc.in
132 index 8ec0235a..8465c977 100644
133 --- a/lib/libzstd.pc.in
134 +++ b/lib/libzstd.pc.in
136 # BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
139 -exec_prefix=${prefix}
140 -includedir=${prefix}/@INCLUDEDIR@
141 -libdir=${exec_prefix}/@LIBDIR@
142 +exec_prefix=@EXEC_PREFIX@
143 +includedir=@INCLUDEDIR@
147 Description: fast lossless compression algorithm library