compat: auto-detect if zlib has uncompress2()

We have a copy of uncompress2() implementation in compat/ so that we can build with an older version of zlib that lack the function, and the build procedure selects if it is used via the NO_UNCOMPRESS2 $(MAKE) variable. This is yet another "annoying" knob the porters need to tweak on platforms that are not common enough to have the default set in the config.mak.uname file. Attempt to instead ask the system header <zlib.h> to decide if we need the compatibility implementation. This is a deviation from the way we have been handling the "compatiblity" features so far, and if it can be done cleanly enough, it could work as a model for features that need compatibility definition we discover in the future. With that goal in mind, avoid expedient but ugly hacks, like shoving the code that is conditionally compiled into an unrelated .c file, which may not work in future cases---instead, take an approach that uses a file that is independently compiled and stands on its own. Compile and link compat/zlib-uncompress2.c file unconditionally, but conditionally hide the implementation behind #if/#endif when zlib version is 1.2.9 or newer, and unconditionally archive the resulting object file in the libgit.a to be picked up by the linker. There are a few things to note in the shape of the code base after this change: - We no longer use NO_UNCOMPRESS2 knob; if the system header <zlib.h> claims a version that is more cent than the library actually is, this would break, but it is easy to add it back when we find such a system. - The object file compat/zlib-uncompress2.o is always compiled and archived in libgit.a, just like a few other compat/ object files already are. - The inclusion of <zlib.h> is done in <git-compat-util.h>; we used to do so from <cache.h> which includes <git-compat-util.h> as the first thing it does, so from the *.c codes, there is no practical change. - Until objects in libgit.a that is already used gains a reference to the function, the reftable code will be the only one that wants it, so libgit.a on the linker command line needs to appear once more at the end to satisify the mutual dependency. - Beat found a trick used by OpenSSL to avoid making the conditionally-compiled object truly empty (apparently because they had to deal with compilers that do not want to see an effectively empty input file). Our compat/zlib-uncompress2.c file borrows the same trick for portabilty. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Helped-by: Beat Bolli <dev+git@drbeat.li> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 24, 2022 at 10:27 UTC 07564773c2569d012719ab9e26b9b27251f3d354
8 files changed +21 -46
Makefile
+3 -8
@@ -256,8 +256,6 @@ all::
256 #
257 # Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
258 #
259 -# Define NO_UNCOMPRESS2 if your zlib does not have uncompress2.
260 -#
259 # Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
260 # as the compiler can crash (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
261 #
@@ -862,6 +860,7 @@ LIB_OBJS += commit-reach.o
860 LIB_OBJS += commit.o
861 LIB_OBJS += compat/obstack.o
862 LIB_OBJS += compat/terminal.o
863 +LIB_OBJS += compat/zlib-uncompress2.o
864 LIB_OBJS += config.o
865 LIB_OBJS += connect.o
866 LIB_OBJS += connected.o
@@ -1194,7 +1193,8 @@ THIRD_PARTY_SOURCES += compat/regex/%
1193 THIRD_PARTY_SOURCES += sha1collisiondetection/%
1194 THIRD_PARTY_SOURCES += sha1dc/%
1195
1197 -GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
1196 +# xdiff and reftable libs may in turn depend on what is in libgit.a
1197 +GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
1198 EXTLIBS =
1199
1200 GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -1726,11 +1726,6 @@ ifdef NO_DEFLATE_BOUND
1726 BASIC_CFLAGS += -DNO_DEFLATE_BOUND
1727 endif
1728
1729 -ifdef NO_UNCOMPRESS2
1730 - BASIC_CFLAGS += -DNO_UNCOMPRESS2
1731 - REFTABLE_OBJS += compat/zlib-uncompress2.o
1732 -endif
1733 -
1729 ifdef NO_POSIX_GOODIES
1730 BASIC_CFLAGS += -DNO_POSIX_GOODIES
1731 endif
cache.h
-1
@@ -18,7 +18,6 @@
18 #include "repository.h"
19 #include "mem-pool.h"
20
21 -#include <zlib.h>
21 typedef struct git_zstream {
22 z_stream z;
23 unsigned long avail_in;
ci/lib.sh
-1
@@ -197,7 +197,6 @@ esac
197 case "$jobname" in
198 linux32)
199 CC=gcc
200 - MAKEFLAGS="$MAKEFLAGS NO_UNCOMPRESS2=1"
200 ;;
201 linux-musl)
202 CC=gcc
compat/zlib-uncompress2.c
+6 -5
@@ -1,3 +1,6 @@
1 +#include "git-compat-util.h"
2 +
3 +#if ZLIB_VERNUM < 0x1290
4 /* taken from zlib's uncompr.c
5
6 commit cacf7f1d4e3d44d871b605da3b647f07d718623f
@@ -8,16 +11,11 @@
11
12 */
13
11 -#include "../reftable/system.h"
12 -#define z_const
13 -
14 /*
15 * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
16 * For conditions of distribution and use, see copyright notice in zlib.h
17 */
18
19 -#include <zlib.h>
20 -
19 /* clang-format off */
20
21 /* ===========================================================================
@@ -93,3 +91,6 @@ int ZEXPORT uncompress2 (
91 err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
92 err;
93 }
94 +#else
95 +static void *dummy_variable = &dummy_variable;
96 +#endif
config.mak.uname
-7
@@ -66,7 +66,6 @@ ifeq ($(uname_S),Linux)
66 # centos7/rhel7 provides gcc 4.8.5 and zlib 1.2.7.
67 ifneq ($(findstring .el7.,$(uname_R)),)
68 BASIC_CFLAGS += -std=c99
69 - NO_UNCOMPRESS2 = YesPlease
69 endif
70 endif
71 ifeq ($(uname_S),GNU/kFreeBSD)
@@ -266,10 +265,6 @@ ifeq ($(uname_S),FreeBSD)
265 FILENO_IS_A_MACRO = UnfortunatelyYes
266 endif
267 ifeq ($(uname_S),OpenBSD)
269 - # Versions < 7.0 need compatibility layer
270 - ifeq ($(shell expr "$(uname_R)" : "[1-6]\."),2)
271 - NO_UNCOMPRESS2 = UnfortunatelyYes
272 - endif
268 NO_STRCASESTR = YesPlease
269 NO_MEMMEM = YesPlease
270 USE_ST_TIMESPEC = YesPlease
@@ -525,7 +520,6 @@ ifeq ($(uname_S),Interix)
520 endif
521 endif
522 ifeq ($(uname_S),Minix)
528 - NO_UNCOMPRESS2 = YesPlease
523 NO_IPV6 = YesPlease
524 NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
525 NO_NSEC = YesPlease
@@ -581,7 +575,6 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
575 NO_SETENV = YesPlease
576 NO_UNSETENV = YesPlease
577 NO_MKDTEMP = YesPlease
584 - NO_UNCOMPRESS2 = YesPlease
578 # Currently libiconv-1.9.1.
579 OLD_ICONV = UnfortunatelyYes
580 NO_REGEX = NeedsStartEnd
configure.ac
-13
@@ -664,22 +664,9 @@ AC_LINK_IFELSE([ZLIBTEST_SRC],
664 NO_DEFLATE_BOUND=yes])
665 LIBS="$old_LIBS"
666
667 -AC_DEFUN([ZLIBTEST_UNCOMPRESS2_SRC], [
668 -AC_LANG_PROGRAM([#include <zlib.h>],
669 - [uncompress2(NULL,NULL,NULL,NULL);])])
670 -AC_MSG_CHECKING([for uncompress2 in -lz])
671 -old_LIBS="$LIBS"
672 -LIBS="$LIBS -lz"
673 -AC_LINK_IFELSE([ZLIBTEST_UNCOMPRESS2_SRC],
674 - [AC_MSG_RESULT([yes])],
675 - [AC_MSG_RESULT([no])
676 - NO_UNCOMPRESS2=yes])
677 -LIBS="$old_LIBS"
678 -
667 GIT_UNSTASH_FLAGS($ZLIB_PATH)
668
669 GIT_CONF_SUBST([NO_DEFLATE_BOUND])
682 -GIT_CONF_SUBST([NO_UNCOMPRESS2])
670
671 #
672 # Define NEEDS_SOCKET if linking with libc is not enough (SunOS,
git-compat-util.h
+12
@@ -1386,6 +1386,18 @@ void unleak_memory(const void *ptr, size_t len);
1386 #define UNLEAK(var) do {} while (0)
1387 #endif
1388
1389 +#define z_const
1390 +#include <zlib.h>
1391 +
1392 +#if ZLIB_VERNUM < 0x1290
1393 +/*
1394 + * This is uncompress2, which is only available in zlib >= 1.2.9
1395 + * (released as of early 2017). See compat/zlib-uncompress2.c.
1396 + */
1397 +int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
1398 + uLong *sourceLen);
1399 +#endif
1400 +
1401 /*
1402 * This include must come after system headers, since it introduces macros that
1403 * replace system names.
reftable/system.h
-11
@@ -16,17 +16,6 @@ https://developers.google.com/open-source/licenses/bsd
16 #include "hash.h" /* hash ID, sizes.*/
17 #include "dir.h" /* remove_dir_recursively, for tests.*/
18
19 -#include <zlib.h>
20 -
21 -#ifdef NO_UNCOMPRESS2
22 -/*
23 - * This is uncompress2, which is only available in zlib >= 1.2.9
24 - * (released as of early 2017)
25 - */
26 -int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
27 - uLong *sourceLen);
28 -#endif
29 -
19 int hash_size(uint32_t id);
20
21 #endif