# Makefile for zlib
# Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler
# For conditions of distribution and use, see copyright notice in zlib.h

# To compile and test, type:
#    ./configure; make test
# Normally configure builds both a static and a shared library.
# If you want to build just a static library, use: ./configure --static

# To install /usr/local/lib/libz.* and /usr/local/include/zlib.h, type:
#    make install
# To install in $HOME instead of /usr/local, use:
#    make install prefix=$HOME

CC=cc

CFLAGS=-O
#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
#CFLAGS=-g -DZLIB_DEBUG
#CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \
#           -Wstrict-prototypes -Wmissing-prototypes

SFLAGS=-O
LDFLAGS=-L.
LIBNAME1=libz-ng
LIBNAME2=zlib-ng
SUFFIX=-ng
TEST_LIBS=$(LIBNAME1).a
LDSHARED=$(CC)
LDSHAREDFLAGS=-shared

VER=2.0.0-RC2
VER1=2

STATICLIB=$(LIBNAME1).a
SHAREDLIB=$(LIBNAME1).so
SHAREDLIBV=$(LIBNAME1).so.$(VER)
SHAREDLIBM=$(LIBNAME1).so.$(VER1)
IMPORTLIB=
SHAREDTARGET=$(LIBNAME1).so.$(VER)
PKGFILE=$(LIBNAME2).pc

LIBS=$(STATICLIB) $(SHAREDTARGET)

AR=ar
ARFLAGS=rc
DEFFILE=
RC=
RCFLAGS=
RCOBJS=
STRIP=
RANLIB=ranlib
LDCONFIG=ldconfig
LDSHAREDLIBC=
EXE=

SRCDIR=.
INCLUDES=-I$(SRCDIR)

ARCHDIR=arch/generic
ARCH_STATIC_OBJS=
ARCH_SHARED_OBJS=

prefix = /usr/local
exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
libdir = ${exec_prefix}/lib
sharedlibdir = ${libdir}
includedir = ${prefix}/include
mandir = ${prefix}/share/man
man3dir = ${mandir}/man3
pkgconfigdir = ${libdir}/pkgconfig

OBJZ = \
	adler32.o \
	chunkset.o \
	compare258.o \
	compress.o \
	crc32.o \
	crc32_comb.o \
	deflate.o \
	deflate_fast.o \
	deflate_medium.o \
	deflate_quick.o \
	deflate_slow.o \
	functable.o \
	infback.o \
	inffast.o \
	inflate.o \
	inftrees.o \
	insert_string.o \
	trees.o \
	uncompr.o \
	zutil.o \
	$(ARCH_STATIC_OBJS)

OBJG = \
	gzlib.o \
	gzread.o \
	gzwrite.o

OBJC = $(OBJZ) $(OBJG)

PIC_OBJZ = \
	adler32.lo \
	chunkset.lo \
	compare258.lo \
	compress.lo \
	crc32.lo \
	crc32_comb.lo \
	deflate.lo \
	deflate_fast.lo \
	deflate_medium.lo \
	deflate_quick.lo \
	deflate_slow.lo \
	functable.lo \
	infback.lo \
	inffast.lo \
	inflate.lo \
	inftrees.lo \
	insert_string.lo \
	trees.lo \
	uncompr.lo \
	zutil.lo \
	$(ARCH_SHARED_OBJS)

PIC_OBJG = \
	gzlib.lo \
	gzread.lo \
	gzwrite.lo

PIC_OBJC = $(PIC_OBJZ) $(PIC_OBJG)

OBJS = $(OBJC)

PIC_OBJS = $(PIC_OBJC)

all: static shared

static: adler32_test$(EXE) example$(EXE) minigzip$(EXE) fuzzers makefixed$(EXE) maketrees$(EXE) makecrct$(EXE)

shared: adler32_testsh$(EXE) examplesh$(EXE) minigzipsh$(EXE)

check: test

.SECONDARY:

$(ARCHDIR)/%.o: $(SRCDIR)/$(ARCHDIR)/%.c
	$(MAKE) -C $(ARCHDIR) $(notdir $@)

$(ARCHDIR)/%.lo: $(SRCDIR)/$(ARCHDIR)/%.c
	$(MAKE) -C $(ARCHDIR) $(notdir $@)

%.o: $(ARCHDIR)/%.o
	-cp $< $@

%.lo: $(ARCHDIR)/%.lo
	-cp $< $@

test: all
	$(MAKE) -C test

# This variable is set by configure.
WITH_FUZZERS=

# By default, use our own standalone_fuzz_target_runner.
# This runner does no fuzzing, but simply executes the inputs
# provided via parameters.
# Run e.g. "make all LIB_FUZZING_ENGINE=/path/to/libFuzzer.a"
# to link the fuzzer(s) against a real fuzzing engine.
ifeq (,$(LIB_FUZZING_ENGINE))
  LIB_FUZZING_ENGINE = standalone_fuzz_target_runner.o
else
  # OSS-Fuzz will define its own value for LIB_FUZZING_ENGINE.
  WITH_FUZZERS=1
endif

ifeq (1,$(WITH_FUZZERS))
fuzzers: checksum_fuzzer$(EXE) compress_fuzzer$(EXE) example_small_fuzzer$(EXE) example_large_fuzzer$(EXE) example_flush_fuzzer$(EXE) example_dict_fuzzer$(EXE) minigzip_fuzzer$(EXE)
else
fuzzers:
endif

# The standalone fuzz target runner.
standalone_fuzz_target_runner.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
checksum_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
compress_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
example_small_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
example_large_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
example_flush_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
example_dict_fuzzer.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
minigzip_fuzzer.o:
	$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<
checksum_fuzzer$(EXE): checksum_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) checksum_fuzzer.o $(STATICLIB) -lpthread
compress_fuzzer$(EXE): compress_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) compress_fuzzer.o $(STATICLIB) -lpthread
example_small_fuzzer$(EXE): example_small_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_small_fuzzer.o $(STATICLIB) -lpthread
example_large_fuzzer$(EXE): example_large_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_large_fuzzer.o $(STATICLIB) -lpthread
example_flush_fuzzer$(EXE): example_flush_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_flush_fuzzer.o $(STATICLIB) -lpthread
example_dict_fuzzer$(EXE): example_dict_fuzzer.o standalone_fuzz_target_runner.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) example_dict_fuzzer.o $(STATICLIB) -lpthread
minigzip_fuzzer$(EXE): minigzip_fuzzer.o standalone_fuzz_target_runner.o $(OBJG) $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ $(LIB_FUZZING_ENGINE) minigzip_fuzzer.o $(OBJG) $(STATICLIB) -lpthread

infcover.o: $(SRCDIR)/test/infcover.c $(SRCDIR)/zlib$(SUFFIX).h zconf$(SUFFIX).h
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/infcover.c

infcover$(EXE): infcover.o $(STATICLIB)
	$(CC) $(LDFLAGS) -o $@ infcover.o $(STATICLIB)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

cover: infcover$(EXE)
	rm -f *.gcda
	./infcover
	gcov inf*.c

$(STATICLIB): $(OBJS)
	$(AR) $(ARFLAGS) $@ $(OBJS)
	-@ ($(RANLIB) $@ || true) >/dev/null 2>&1

adler32_test.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/test/adler32_test.c

example.o:
	$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/example.c

minigzip.o:
	$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $(SRCDIR)/test/minigzip.c

makefixed.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/makefixed.c

maketrees.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/maketrees.c

makecrct.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/tools/makecrct.c

zlibrc.o: win32/zlib$(SUFFIX)1.rc
	$(RC) $(RCFLAGS) -o $@ win32/zlib$(SUFFIX)1.rc

.SUFFIXES: .lo

%.o: $(SRCDIR)/%.c
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<

%.lo: $(SRCDIR)/%.c
	$(CC) $(SFLAGS) -DPIC $(INCLUDES) -c -o $@ $<

$(OBJG): %.o: $(SRCDIR)/%.c
	$(CC) $(CFLAGS) -DWITH_GZFILEOP $(INCLUDES) -c -o $@ $<

$(SHAREDTARGET): $(PIC_OBJS) $(DEFFILE) $(RCOBJS)
ifneq ($(SHAREDTARGET),)
	$(LDSHARED) $(CFLAGS) $(LDSHAREDFLAGS) $(LDFLAGS) -o $@ $(DEFFILE) $(PIC_OBJS) $(RCOBJS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif
ifneq ($(SHAREDLIB),$(SHAREDTARGET))
	rm -f $(SHAREDLIB) $(SHAREDLIBM)
	ln -s $@ $(SHAREDLIB)
	ln -s $@ $(SHAREDLIBM)
endif
endif

adler32_test$(EXE): adler32_test.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ adler32_test.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

example$(EXE): example.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ example.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

minigzip$(EXE): minigzip.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ minigzip.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

adler32_testsh$(EXE): adler32_test.o $(OBJG) $(SHAREDTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ adler32_test.o $(OBJG) $(SHAREDTARGET) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

examplesh$(EXE): example.o $(OBJG) $(SHAREDTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ example.o $(OBJG) $(SHAREDTARGET) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

minigzipsh$(EXE): minigzip.o $(OBJG) $(SHAREDTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ minigzip.o $(OBJG) $(SHAREDTARGET) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

makefixed$(EXE): makefixed.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ makefixed.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

maketrees$(EXE): maketrees.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ maketrees.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

makecrct$(EXE): makecrct.o $(OBJG) $(STATICLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ makecrct.o $(OBJG) $(TEST_LIBS) $(LDSHAREDLIBC)
ifneq ($(STRIP),)
	$(STRIP) $@
endif

install-shared: $(SHAREDTARGET)
ifneq ($(SHAREDTARGET),)
	-@if [ ! -d $(DESTDIR)$(sharedlibdir) ]; then mkdir -p $(DESTDIR)$(sharedlibdir); fi
	rm -f $(DESTDIR)$(sharedlibdir)/$(SHAREDTARGET)
	cp $(SHAREDTARGET) $(DESTDIR)$(sharedlibdir)
	chmod 755 $(DESTDIR)$(sharedlibdir)/$(SHAREDTARGET)
ifneq ($(SHAREDLIB),$(SHAREDTARGET))
	rm -f $(DESTDIR)$(sharedlibdir)/$(SHAREDLIB) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIBM)
	ln -s $(SHAREDLIBV) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIB)
	ln -s $(SHAREDLIBV) $(DESTDIR)$(sharedlibdir)/$(SHAREDLIBM)
	($(LDCONFIG) || true)  >/dev/null 2>&1
# ldconfig is for Linux
endif
ifneq ($(IMPORTLIB),)
	cp $(IMPORTLIB) $(DESTDIR)$(sharedlibdir)
	chmod 644 $(DESTDIR)$(sharedlibdir)/$(IMPORTLIB)
endif
endif

install-static: $(STATICLIB)
	-@if [ ! -d $(DESTDIR)$(libdir)       ]; then mkdir -p $(DESTDIR)$(libdir); fi
	rm -f $(DESTDIR)$(libdir)/$(STATICLIB)
	cp $(STATICLIB) $(DESTDIR)$(libdir)
	chmod 644 $(DESTDIR)$(libdir)/$(STATICLIB)
	-@($(RANLIB) $(DESTDIR)$(libdir)/$(STATICLIB) || true) >/dev/null 2>&1
# The ranlib in install-static is needed on NeXTSTEP which checks file times

install-libs: install-shared install-static
	-@if [ ! -d $(DESTDIR)$(man3dir)      ]; then mkdir -p $(DESTDIR)$(man3dir); fi
	-@if [ ! -d $(DESTDIR)$(pkgconfigdir) ]; then mkdir -p $(DESTDIR)$(pkgconfigdir); fi
	rm -f $(DESTDIR)$(man3dir)/zlib$(SUFFIX).3
	cp $(SRCDIR)/zlib.3 $(DESTDIR)$(man3dir)/zlib$(SUFFIX).3
	chmod 644 $(DESTDIR)$(man3dir)/zlib$(SUFFIX).3
	rm -f $(DESTDIR)$(pkgconfigdir)/$(PKGFILE)
	cp $(PKGFILE) $(DESTDIR)$(pkgconfigdir)
	chmod 644 $(DESTDIR)$(pkgconfigdir)/$(PKGFILE)

install: install-libs
	-@if [ ! -d $(DESTDIR)$(includedir)   ]; then mkdir -p $(DESTDIR)$(includedir); fi
	rm -f $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
	cp $(SRCDIR)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zlib$(SUFFIX).h
	cp zconf$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h
	chmod 644 $(DESTDIR)$(includedir)/zlib$(SUFFIX).h $(DESTDIR)$(includedir)/zconf$(SUFFIX).h

uninstall-static:
	cd $(DESTDIR)$(libdir) && rm -f $(STATICLIB)

uninstall-shared:
ifneq ($(SHAREDLIB),)
	cd $(DESTDIR)$(sharedlibdir) && rm -f $(SHAREDLIBV) $(SHAREDLIB) $(SHAREDLIBM)
endif
ifneq ($(IMPORTLIB),)
	cd $(DESTDIR)$(sharedlibdir) && rm -f $(IMPORTLIB)
endif

uninstall: uninstall-static uninstall-shared
	cd $(DESTDIR)$(includedir) && rm -f zlib$(SUFFIX).h zconf$(SUFFIX).h
	cd $(DESTDIR)$(man3dir) && rm -f zlib$(SUFFIX).3
	cd $(DESTDIR)$(pkgconfigdir) && rm -f $(PKGFILE)

docs: zlib.3.pdf

zlib.3.pdf: $(SRCDIR)/zlib.3
	groff -mandoc -f H -T ps $(SRCDIR)/zlib.3 | ps2pdf - zlib.3.pdf

mostlyclean: clean
clean:
	@if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) clean; fi
	@if [ -f test/Makefile ]; then $(MAKE) -C test clean; fi
	rm -f *.o *.lo *~ \
	   adler32_test$(EXE) example$(EXE) minigzip$(EXE) \
	   adler32_testsh$(EXE) examplesh$(EXE) minigzipsh$(EXE) \
	   checksum_fuzzer$(EXE) compress_fuzzer$(EXE) example_small_fuzzer$(EXE) example_large_fuzzer$(EXE) \
	   example_flush_fuzzer$(EXE) example_dict_fuzzer$(EXE) minigzip_fuzzer$(EXE) \
	   infcover makefixed$(EXE) maketrees$(EXE) makecrct$(EXE) \
	   $(STATICLIB) $(IMPORTLIB) $(SHAREDLIB) $(SHAREDLIBV) $(SHAREDLIBM) \
	   foo.gz so_locations \
	   _match.s maketree
	rm -rf objs
	rm -f *.gcda *.gcno *.gcov
	rm -f a.out a.exe
	rm -f *.pc
	rm -f *._h
	rm -rf btmp1 btmp2 pkgtmp1 pkgtmp2

maintainer-clean: distclean
distclean: clean
	@if [ -f $(ARCHDIR)/Makefile ]; then $(MAKE) -C $(ARCHDIR) distclean; fi
	@if [ -f test/Makefile ]; then $(MAKE) -C test distclean; fi
	rm -f $(PKGFILE) configure.log zconf.h zconf.h.cmakein
	-@rm -f .DS_Store
# Reset Makefile if building inside source tree
	@if [ -f Makefile.in ]; then \
	printf 'all:\n\t-@echo "Please use ./configure first.  Thank you."\n' > Makefile ; \
	printf '\ndistclean:\n\t$(MAKE) -f Makefile.in distclean\n' >> Makefile ; \
	touch -r $(SRCDIR)/Makefile.in Makefile ; fi
# Reset zconf.h and zconf.h.cmakein if building inside source tree
	@if [ -f zconf.h.in ]; then \
	cp -p $(SRCDIR)/zconf.h.in zconf.h ; \
	grep -v '^#cmakedefine' $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\
	touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein ; fi
# Cleanup these files if building outside source tree
	@if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf Makefile; fi
# Remove arch and test directory if building outside source tree
	@if [ ! -f $(ARCHDIR)/Makefile.in ]; then rm -rf arch; fi
	@if [ ! -f test/Makefile.in ]; then rm -rf test; fi

tags:
	etags $(SRCDIR)/*.[ch]
