]> git.ipfire.org Git - thirdparty/git.git/commitdiff
compat: auto-detect if zlib has uncompress2()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Mon, 24 Jan 2022 18:27:59 +0000 (10:27 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 26 Jan 2022 17:05:55 +0000 (09:05 -0800)
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>
Makefile
cache.h
ci/lib.sh
compat/zlib-uncompress2.c
config.mak.uname
configure.ac
git-compat-util.h
reftable/system.h

index 5580859afdb45b44459798fa490f9b53e426079b..f194b2afc63c0b4d88a9256f8787f9779bf455b3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -256,8 +256,6 @@ all::
 #
 # Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
 #
-# Define NO_UNCOMPRESS2 if your zlib does not have uncompress2.
-#
 # Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
 # as the compiler can crash (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
 #
@@ -862,6 +860,7 @@ LIB_OBJS += commit-reach.o
 LIB_OBJS += commit.o
 LIB_OBJS += compat/obstack.o
 LIB_OBJS += compat/terminal.o
+LIB_OBJS += compat/zlib-uncompress2.o
 LIB_OBJS += config.o
 LIB_OBJS += connect.o
 LIB_OBJS += connected.o
@@ -1194,7 +1193,8 @@ THIRD_PARTY_SOURCES += compat/regex/%
 THIRD_PARTY_SOURCES += sha1collisiondetection/%
 THIRD_PARTY_SOURCES += sha1dc/%
 
-GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
+# xdiff and reftable libs may in turn depend on what is in libgit.a
+GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
 EXTLIBS =
 
 GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -1726,11 +1726,6 @@ ifdef NO_DEFLATE_BOUND
        BASIC_CFLAGS += -DNO_DEFLATE_BOUND
 endif
 
-ifdef NO_UNCOMPRESS2
-       BASIC_CFLAGS += -DNO_UNCOMPRESS2
-       REFTABLE_OBJS += compat/zlib-uncompress2.o
-endif
-
 ifdef NO_POSIX_GOODIES
        BASIC_CFLAGS += -DNO_POSIX_GOODIES
 endif
diff --git a/cache.h b/cache.h
index 281f00ab1b161dc71d0bcdae91222e9429ba0342..3a0142aa56f38a23ff89dba3975bff569ffa7625 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -18,7 +18,6 @@
 #include "repository.h"
 #include "mem-pool.h"
 
-#include <zlib.h>
 typedef struct git_zstream {
        z_stream z;
        unsigned long avail_in;
index 9d28ab50fb4462a1b064e8c89cd5f13518fd86cd..cbc2f8f1caa6c0ddf69824b03e9394aecbcac250 100755 (executable)
--- a/ci/lib.sh
+++ b/ci/lib.sh
@@ -197,7 +197,6 @@ esac
 case "$jobname" in
 linux32)
        CC=gcc
-       MAKEFLAGS="$MAKEFLAGS NO_UNCOMPRESS2=1"
        ;;
 linux-musl)
        CC=gcc
index 722610b971804abf47130d07bbd6c0b084c5036f..77a1b08048463da25ba8d6b36031ccb7e8cce7b5 100644 (file)
@@ -1,3 +1,6 @@
+#include "git-compat-util.h"
+
+#if ZLIB_VERNUM < 0x1290
 /* taken from zlib's uncompr.c
 
    commit cacf7f1d4e3d44d871b605da3b647f07d718623f
 
 */
 
-#include "../reftable/system.h"
-#define z_const
-
 /*
  * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-#include <zlib.h>
-
 /* clang-format off */
 
 /* ===========================================================================
@@ -93,3 +91,6 @@ int ZEXPORT uncompress2 (
           err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
           err;
 }
+#else
+static void *dummy_variable = &dummy_variable;
+#endif
index c48db45106c8dc0e4138ccb8ecff016f3f7d0faa..92ea00c219d172b41620b47e09fa539e57bb7c64 100644 (file)
@@ -66,7 +66,6 @@ ifeq ($(uname_S),Linux)
        # centos7/rhel7 provides gcc 4.8.5 and zlib 1.2.7.
        ifneq ($(findstring .el7.,$(uname_R)),)
                BASIC_CFLAGS += -std=c99
-               NO_UNCOMPRESS2 = YesPlease
        endif
 endif
 ifeq ($(uname_S),GNU/kFreeBSD)
@@ -266,10 +265,6 @@ ifeq ($(uname_S),FreeBSD)
        FILENO_IS_A_MACRO = UnfortunatelyYes
 endif
 ifeq ($(uname_S),OpenBSD)
-       # Versions < 7.0 need compatibility layer
-       ifeq ($(shell expr "$(uname_R)" : "[1-6]\."),2)
-               NO_UNCOMPRESS2 = UnfortunatelyYes
-       endif
        NO_STRCASESTR = YesPlease
        NO_MEMMEM = YesPlease
        USE_ST_TIMESPEC = YesPlease
@@ -525,7 +520,6 @@ ifeq ($(uname_S),Interix)
        endif
 endif
 ifeq ($(uname_S),Minix)
-       NO_UNCOMPRESS2 = YesPlease
        NO_IPV6 = YesPlease
        NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
        NO_NSEC = YesPlease
@@ -581,7 +575,6 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
        NO_SETENV = YesPlease
        NO_UNSETENV = YesPlease
        NO_MKDTEMP = YesPlease
-       NO_UNCOMPRESS2 = YesPlease
        # Currently libiconv-1.9.1.
        OLD_ICONV = UnfortunatelyYes
        NO_REGEX = NeedsStartEnd
index d60d494ee4c81b7ccbe3a1e36285a41125e56107..5ee25ec95c898847adaaef258774df3da218f004 100644 (file)
@@ -664,22 +664,9 @@ AC_LINK_IFELSE([ZLIBTEST_SRC],
        NO_DEFLATE_BOUND=yes])
 LIBS="$old_LIBS"
 
-AC_DEFUN([ZLIBTEST_UNCOMPRESS2_SRC], [
-AC_LANG_PROGRAM([#include <zlib.h>],
- [uncompress2(NULL,NULL,NULL,NULL);])])
-AC_MSG_CHECKING([for uncompress2 in -lz])
-old_LIBS="$LIBS"
-LIBS="$LIBS -lz"
-AC_LINK_IFELSE([ZLIBTEST_UNCOMPRESS2_SRC],
-       [AC_MSG_RESULT([yes])],
-       [AC_MSG_RESULT([no])
-       NO_UNCOMPRESS2=yes])
-LIBS="$old_LIBS"
-
 GIT_UNSTASH_FLAGS($ZLIB_PATH)
 
 GIT_CONF_SUBST([NO_DEFLATE_BOUND])
-GIT_CONF_SUBST([NO_UNCOMPRESS2])
 
 #
 # Define NEEDS_SOCKET if linking with libc is not enough (SunOS,
index 1229c8296b92547b89bc1cc29e6f2415fa1370af..ea111a7b481c9d920082394c74cd7011efbff590 100644 (file)
@@ -1386,6 +1386,18 @@ void unleak_memory(const void *ptr, size_t len);
 #define UNLEAK(var) do {} while (0)
 #endif
 
+#define z_const
+#include <zlib.h>
+
+#if ZLIB_VERNUM < 0x1290
+/*
+ * This is uncompress2, which is only available in zlib >= 1.2.9
+ * (released as of early 2017). See compat/zlib-uncompress2.c.
+ */
+int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
+               uLong *sourceLen);
+#endif
+
 /*
  * This include must come after system headers, since it introduces macros that
  * replace system names.
index 4907306c0c5d4e13ef13ea9f89c9529a6b7a3cda..18f9207dfee16accdaac46a77d13b9960aad3ca2 100644 (file)
@@ -16,17 +16,6 @@ https://developers.google.com/open-source/licenses/bsd
 #include "hash.h" /* hash ID, sizes.*/
 #include "dir.h" /* remove_dir_recursively, for tests.*/
 
-#include <zlib.h>
-
-#ifdef NO_UNCOMPRESS2
-/*
- * This is uncompress2, which is only available in zlib >= 1.2.9
- * (released as of early 2017)
- */
-int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
-               uLong *sourceLen);
-#endif
-
 int hash_size(uint32_t id);
 
 #endif