Skip to content

Commit 0756477

Browse files
avargitster
authored andcommitted
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 <[email protected]> Helped-by: Beat Bolli <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 297ca89 commit 0756477

File tree

8 files changed

+21
-46
lines changed

8 files changed

+21
-46
lines changed

Makefile

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ all::
256256
#
257257
# Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
258258
#
259-
# Define NO_UNCOMPRESS2 if your zlib does not have uncompress2.
260-
#
261259
# Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
262260
# as the compiler can crash (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
263261
#
@@ -862,6 +860,7 @@ LIB_OBJS += commit-reach.o
862860
LIB_OBJS += commit.o
863861
LIB_OBJS += compat/obstack.o
864862
LIB_OBJS += compat/terminal.o
863+
LIB_OBJS += compat/zlib-uncompress2.o
865864
LIB_OBJS += config.o
866865
LIB_OBJS += connect.o
867866
LIB_OBJS += connected.o
@@ -1194,7 +1193,8 @@ THIRD_PARTY_SOURCES += compat/regex/%
11941193
THIRD_PARTY_SOURCES += sha1collisiondetection/%
11951194
THIRD_PARTY_SOURCES += sha1dc/%
11961195

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)
11981198
EXTLIBS =
11991199

12001200
GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -1726,11 +1726,6 @@ ifdef NO_DEFLATE_BOUND
17261726
BASIC_CFLAGS += -DNO_DEFLATE_BOUND
17271727
endif
17281728

1729-
ifdef NO_UNCOMPRESS2
1730-
BASIC_CFLAGS += -DNO_UNCOMPRESS2
1731-
REFTABLE_OBJS += compat/zlib-uncompress2.o
1732-
endif
1733-
17341729
ifdef NO_POSIX_GOODIES
17351730
BASIC_CFLAGS += -DNO_POSIX_GOODIES
17361731
endif

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "repository.h"
1919
#include "mem-pool.h"
2020

21-
#include <zlib.h>
2221
typedef struct git_zstream {
2322
z_stream z;
2423
unsigned long avail_in;

ci/lib.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ esac
197197
case "$jobname" in
198198
linux32)
199199
CC=gcc
200-
MAKEFLAGS="$MAKEFLAGS NO_UNCOMPRESS2=1"
201200
;;
202201
linux-musl)
203202
CC=gcc

compat/zlib-uncompress2.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include "git-compat-util.h"
2+
3+
#if ZLIB_VERNUM < 0x1290
14
/* taken from zlib's uncompr.c
25
36
commit cacf7f1d4e3d44d871b605da3b647f07d718623f
@@ -8,16 +11,11 @@
811
912
*/
1013

11-
#include "../reftable/system.h"
12-
#define z_const
13-
1414
/*
1515
* Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
1616
* For conditions of distribution and use, see copyright notice in zlib.h
1717
*/
1818

19-
#include <zlib.h>
20-
2119
/* clang-format off */
2220

2321
/* ===========================================================================
@@ -93,3 +91,6 @@ int ZEXPORT uncompress2 (
9391
err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
9492
err;
9593
}
94+
#else
95+
static void *dummy_variable = &dummy_variable;
96+
#endif

config.mak.uname

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ ifeq ($(uname_S),Linux)
6666
# centos7/rhel7 provides gcc 4.8.5 and zlib 1.2.7.
6767
ifneq ($(findstring .el7.,$(uname_R)),)
6868
BASIC_CFLAGS += -std=c99
69-
NO_UNCOMPRESS2 = YesPlease
7069
endif
7170
endif
7271
ifeq ($(uname_S),GNU/kFreeBSD)
@@ -266,10 +265,6 @@ ifeq ($(uname_S),FreeBSD)
266265
FILENO_IS_A_MACRO = UnfortunatelyYes
267266
endif
268267
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
273268
NO_STRCASESTR = YesPlease
274269
NO_MEMMEM = YesPlease
275270
USE_ST_TIMESPEC = YesPlease
@@ -525,7 +520,6 @@ ifeq ($(uname_S),Interix)
525520
endif
526521
endif
527522
ifeq ($(uname_S),Minix)
528-
NO_UNCOMPRESS2 = YesPlease
529523
NO_IPV6 = YesPlease
530524
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
531525
NO_NSEC = YesPlease
@@ -581,7 +575,6 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
581575
NO_SETENV = YesPlease
582576
NO_UNSETENV = YesPlease
583577
NO_MKDTEMP = YesPlease
584-
NO_UNCOMPRESS2 = YesPlease
585578
# Currently libiconv-1.9.1.
586579
OLD_ICONV = UnfortunatelyYes
587580
NO_REGEX = NeedsStartEnd

configure.ac

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -664,22 +664,9 @@ AC_LINK_IFELSE([ZLIBTEST_SRC],
664664
NO_DEFLATE_BOUND=yes])
665665
LIBS="$old_LIBS"
666666

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-
679667
GIT_UNSTASH_FLAGS($ZLIB_PATH)
680668

681669
GIT_CONF_SUBST([NO_DEFLATE_BOUND])
682-
GIT_CONF_SUBST([NO_UNCOMPRESS2])
683670

684671
#
685672
# Define NEEDS_SOCKET if linking with libc is not enough (SunOS,

git-compat-util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,18 @@ void unleak_memory(const void *ptr, size_t len);
13861386
#define UNLEAK(var) do {} while (0)
13871387
#endif
13881388

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+
13891401
/*
13901402
* This include must come after system headers, since it introduces macros that
13911403
* replace system names.

reftable/system.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ license that can be found in the LICENSE file or at
1616
#include "hash.h" /* hash ID, sizes.*/
1717
#include "dir.h" /* remove_dir_recursively, for tests.*/
1818

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-
3019
int hash_size(uint32_t id);
3120

3221
#endif

0 commit comments

Comments
 (0)