Skip to content

Commit 9ab76c4

Browse files
committed
Merge branch 'ps/zlib-ng' into seen
The code paths to interact with zlib has been cleaned up in preparation for building with zlib-ng. * ps/zlib-ng: ci: make "linux-musl" job use zlib-ng ci: switch linux-musl to use Meson compat/zlib: allow use of zlib-ng as backend git-zlib: cast away potential constness of `next_in` pointer compat/zlib: provide stubs for `deflateSetHeader()` compat/zlib: provide `deflateBound()` shim centrally git-compat-util: move include of "compat/zlib.h" into "git-zlib.h" compat: introduce new "zlib.h" header git-compat-util: drop `z_const` define compat: drop `uncompress2()` compatibility shim
2 parents 8103f63 + 6e8952b commit 9ab76c4

20 files changed

+100
-140
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ jobs:
390390
- jobname: linux-meson
391391
image: ubuntu:rolling
392392
cc: gcc
393-
- jobname: linux-musl
393+
- jobname: linux-musl-meson
394394
image: alpine:latest
395395
# Supported until 2025-04-02.
396396
- jobname: linux32

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test:linux:
6060
CC: clang
6161
- jobname: pedantic
6262
image: fedora:latest
63-
- jobname: linux-musl
63+
- jobname: linux-musl-meson
6464
image: alpine:latest
6565
- jobname: linux32
6666
image: i386/ubuntu:20.04

Makefile

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ include shared.mak
183183
# byte-order mark (BOM) when writing UTF-16 or UTF-32 and always writes in
184184
# big-endian format.
185185
#
186-
# Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
186+
# Define NO_DEFLATE_BOUND if your zlib does not have deflateBound. Define
187+
# ZLIB_NG if you want to use zlib-ng instead of zlib.
187188
#
188189
# Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
189190
# as the compiler can crash (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
@@ -986,7 +987,6 @@ LIB_OBJS += commit.o
986987
LIB_OBJS += compat/nonblock.o
987988
LIB_OBJS += compat/obstack.o
988989
LIB_OBJS += compat/terminal.o
989-
LIB_OBJS += compat/zlib-uncompress2.o
990990
LIB_OBJS += config.o
991991
LIB_OBJS += connect.o
992992
LIB_OBJS += connected.o
@@ -1695,11 +1695,20 @@ else
16951695
endif
16961696
IMAP_SEND_LDFLAGS += $(OPENSSL_LINK) $(OPENSSL_LIBSSL) $(LIB_4_CRYPTO)
16971697

1698-
ifdef ZLIB_PATH
1699-
BASIC_CFLAGS += -I$(ZLIB_PATH)/include
1700-
EXTLIBS += $(call libpath_template,$(ZLIB_PATH)/$(lib))
1698+
ifdef ZLIB_NG
1699+
BASIC_CFLAGS += -DHAVE_ZLIB_NG
1700+
ifdef ZLIB_NG_PATH
1701+
BASIC_CFLAGS += -I$(ZLIB_NG_PATH)/include
1702+
EXTLIBS += $(call libpath_template,$(ZLIB_NG_PATH)/$(lib))
1703+
endif
1704+
EXTLIBS += -lz-ng
1705+
else
1706+
ifdef ZLIB_PATH
1707+
BASIC_CFLAGS += -I$(ZLIB_PATH)/include
1708+
EXTLIBS += $(call libpath_template,$(ZLIB_PATH)/$(lib))
1709+
endif
1710+
EXTLIBS += -lz
17011711
endif
1702-
EXTLIBS += -lz
17031712

17041713
ifndef NO_OPENSSL
17051714
OPENSSL_LIBSSL = -lssl

archive-tar.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,7 @@ static const char internal_gzip_command[] = "git archive gzip";
473473
static int write_tar_filter_archive(const struct archiver *ar,
474474
struct archiver_args *args)
475475
{
476-
#if ZLIB_VERNUM >= 0x1221
477476
struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
478-
#endif
479477
struct strbuf cmd = STRBUF_INIT;
480478
struct child_process filter = CHILD_PROCESS_INIT;
481479
int r;
@@ -486,10 +484,8 @@ static int write_tar_filter_archive(const struct archiver *ar,
486484
if (!strcmp(ar->filter_command, internal_gzip_command)) {
487485
write_block = tgz_write_block;
488486
git_deflate_init_gzip(&gzstream, args->compression_level);
489-
#if ZLIB_VERNUM >= 0x1221
490487
if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
491488
BUG("deflateSetHeader() called too late");
492-
#endif
493489
gzstream.next_out = outbuf;
494490
gzstream.avail_out = sizeof(outbuf);
495491

archive.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "convert.h"
88
#include "environment.h"
99
#include "gettext.h"
10+
#include "git-zlib.h"
1011
#include "hex.h"
1112
#include "object-name.h"
1213
#include "path.h"

ci/install-dependencies.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fi
2424

2525
case "$distro" in
2626
alpine-*)
27-
apk add --update shadow sudo build-base curl-dev openssl-dev expat-dev gettext \
28-
pcre2-dev python3 musl-libintl perl-utils ncurses \
27+
apk add --update shadow sudo meson ninja-build gcc libc-dev curl-dev openssl-dev expat-dev gettext \
28+
zlib-ng-dev pcre2-dev python3 musl-libintl perl-utils ncurses \
2929
apache2 apache2-http2 apache2-proxy apache2-ssl apache2-webdav apr-util-dbd_sqlite3 \
3030
bash cvs gnupg perl-cgi perl-dbd-sqlite perl-io-tty >/dev/null
3131
;;

ci/lib.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ linux32)
349349
CC=gcc
350350
;;
351351
linux-musl)
352-
CC=gcc
353-
MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/python3 USE_LIBPCRE2=Yes"
354-
MAKEFLAGS="$MAKEFLAGS NO_REGEX=Yes ICONV_OMITS_BOM=Yes"
355-
MAKEFLAGS="$MAKEFLAGS GIT_TEST_UTF8_LOCALE=C.UTF-8"
352+
MESONFLAGS="$MESONFLAGS -DGIT_TEST_UTF8_LOCALE=C.UTF-8"
356353
;;
357354
linux-leaks|linux-reftable-leaks)
358355
export SANITIZE=leak

ci/run-build-and-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ case "$jobname" in
5656
--fatal-meson-warnings \
5757
--warnlevel 2 --werror \
5858
--wrap-mode nofallback \
59-
-Dfuzzers=true
59+
-Dfuzzers=true \
60+
$MESONFLAGS
6061
group "Build" meson compile -C build --
6162
if test -n "$run_tests"
6263
then

compat/zlib-compat.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef COMPAT_ZLIB_H
2+
#define COMPAT_ZLIB_H
3+
4+
#ifdef HAVE_ZLIB_NG
5+
# include <zlib-ng.h>
6+
7+
# define z_stream zng_stream
8+
#define gz_header_s zng_gz_header_s
9+
10+
# define crc32(crc, buf, len) zng_crc32(crc, buf, len)
11+
12+
# define inflate(strm, bits) zng_inflate(strm, bits)
13+
# define inflateEnd(strm) zng_inflateEnd(strm)
14+
# define inflateInit(strm) zng_inflateInit(strm)
15+
# define inflateInit2(strm, bits) zng_inflateInit2(strm, bits)
16+
# define inflateReset(strm) zng_inflateReset(strm)
17+
18+
# define deflate(strm, flush) zng_deflate(strm, flush)
19+
# define deflateBound(strm, source_len) zng_deflateBound(strm, source_len)
20+
# define deflateEnd(strm) zng_deflateEnd(strm)
21+
# define deflateInit(strm, level) zng_deflateInit(strm, level)
22+
# define deflateInit2(stream, level, method, window_bits, mem_level, strategy) zng_deflateInit2(stream, level, method, window_bits, mem_level, strategy)
23+
# define deflateReset(strm) zng_deflateReset(strm)
24+
# define deflateSetHeader(strm, head) zng_deflateSetHeader(strm, head)
25+
26+
#else
27+
# include <zlib.h>
28+
29+
# if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
30+
# define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
31+
# endif
32+
33+
# if ZLIB_VERNUM < 0x1221
34+
struct gz_header_s {
35+
int os;
36+
};
37+
38+
static int deflateSetHeader(z_streamp strm, struct gz_header_s *head)
39+
{
40+
(void)(strm);
41+
(void)(head);
42+
return Z_OK;
43+
}
44+
# endif
45+
#endif /* HAVE_ZLIB_NG */
46+
47+
#endif /* COMPAT_ZLIB_H */

compat/zlib-uncompress2.c

Lines changed: 0 additions & 96 deletions
This file was deleted.

config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "convert.h"
2020
#include "environment.h"
2121
#include "gettext.h"
22+
#include "git-zlib.h"
2223
#include "ident.h"
2324
#include "repository.h"
2425
#include "lockfile.h"

csum-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#define USE_THE_REPOSITORY_VARIABLE
1212

1313
#include "git-compat-util.h"
14-
#include "progress.h"
1514
#include "csum-file.h"
15+
#include "git-zlib.h"
1616
#include "hash.h"
17+
#include "progress.h"
1718

1819
static void verify_buffer_or_die(struct hashfile *f,
1920
const void *buf,

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "convert.h"
1717
#include "environment.h"
1818
#include "gettext.h"
19+
#include "git-zlib.h"
1920
#include "repository.h"
2021
#include "config.h"
2122
#include "refs.h"

git-compat-util.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,18 +1559,6 @@ int cmd_main(int, const char **);
15591559
int common_exit(const char *file, int line, int code);
15601560
#define exit(code) exit(common_exit(__FILE__, __LINE__, (code)))
15611561

1562-
#define z_const
1563-
#include <zlib.h>
1564-
1565-
#if ZLIB_VERNUM < 0x1290
1566-
/*
1567-
* This is uncompress2, which is only available in zlib >= 1.2.9
1568-
* (released as of early 2017). See compat/zlib-uncompress2.c.
1569-
*/
1570-
int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
1571-
uLong *sourceLen);
1572-
#endif
1573-
15741562
/*
15751563
* This include must come after system headers, since it introduces macros that
15761564
* replace system names.

git-zlib.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void zlib_post_call(git_zstream *s)
5959

6060
s->total_out = s->z.total_out;
6161
s->total_in = s->z.total_in;
62-
s->next_in = s->z.next_in;
62+
s->next_in = (unsigned char *) s->z.next_in;
6363
s->next_out = s->z.next_out;
6464
s->avail_in -= bytes_consumed;
6565
s->avail_out -= bytes_produced;
@@ -147,10 +147,6 @@ int git_inflate(git_zstream *strm, int flush)
147147
return status;
148148
}
149149

150-
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
151-
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
152-
#endif
153-
154150
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
155151
{
156152
return deflateBound(&strm->z, size);

git-zlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef GIT_ZLIB_H
22
#define GIT_ZLIB_H
33

4+
#include "compat/zlib-compat.h"
5+
46
typedef struct git_zstream {
57
z_stream z;
68
unsigned long avail_in;

meson.build

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ libgit_sources = [
263263
'compat/nonblock.c',
264264
'compat/obstack.c',
265265
'compat/terminal.c',
266-
'compat/zlib-uncompress2.c',
267266
'config.c',
268267
'connect.c',
269268
'connected.c',
@@ -668,7 +667,7 @@ build_options_config.set('GIT_TEST_CMP_USE_COPIED_CONTEXT', '')
668667
build_options_config.set('GIT_TEST_INDEX_VERSION', '')
669668
build_options_config.set('GIT_TEST_OPTS', '')
670669
build_options_config.set('GIT_TEST_PERL_FATAL_WARNINGS', '')
671-
build_options_config.set('GIT_TEST_UTF8_LOCALE', '')
670+
build_options_config.set_quoted('GIT_TEST_UTF8_LOCALE', get_option('test_utf8_locale'))
672671
build_options_config.set_quoted('LOCALEDIR', fs.as_posix(get_option('prefix') / get_option('localedir')))
673672
build_options_config.set('GITWEBDIR', fs.as_posix(get_option('prefix') / get_option('datadir') / 'gitweb'))
674673

@@ -801,11 +800,23 @@ else
801800
build_options_config.set('NO_PERL_CPAN_FALLBACKS', '')
802801
endif
803802

804-
zlib = dependency('zlib', default_options: ['default_library=static', 'tests=disabled'])
805-
if zlib.version().version_compare('<1.2.0')
806-
libgit_c_args += '-DNO_DEFLATE_BOUND'
803+
zlib_backend = get_option('zlib_backend')
804+
if zlib_backend in ['auto', 'zlib-ng']
805+
zlib_ng = dependency('zlib-ng', required: zlib_backend == 'zlib-ng')
806+
if zlib_ng.found()
807+
zlib_backend = 'zlib-ng'
808+
libgit_c_args += '-DHAVE_ZLIB_NG'
809+
libgit_dependencies += zlib_ng
810+
endif
811+
endif
812+
if zlib_backend in ['auto', 'zlib']
813+
zlib = dependency('zlib', default_options: ['default_library=static', 'tests=disabled'])
814+
if zlib.version().version_compare('<1.2.0')
815+
libgit_c_args += '-DNO_DEFLATE_BOUND'
816+
endif
817+
zlib_backend = 'zlib'
818+
libgit_dependencies += zlib
807819
endif
808-
libgit_dependencies += zlib
809820

810821
threads = dependency('threads', required: false)
811822
if threads.found()
@@ -2014,4 +2025,5 @@ summary({
20142025
'sha1': sha1_backend,
20152026
'sha1_unsafe': sha1_unsafe_backend,
20162027
'sha256': sha256_backend,
2028+
'zlib': zlib_backend,
20172029
}, section: 'Backends')

0 commit comments

Comments
 (0)