# zlib for MontaukOS
#
# Upstream: https://github.com/madler/zlib (submodule at upstream/, pinned to
# v1.3.1; no local patches). Needed by NetSurf for gzip Content-Encoding, and
# by libpng.
#
# DYNAMIC_CRC_TABLE is deliberately NOT defined: it pulls in <stdatomic.h> and
# a runtime once() init, where the default path uses the checked-in static CRC
# tables in crc32.h. Deterministic and one less dependency.

UPSTREAM := upstream

PORT_NAME        := zlib
PORT_LIB         := 1
PORT_ARCHIVE     := libz.a
PORT_HEADER_ROOT := $(UPSTREAM)
PORT_HEADERS     := $(UPSTREAM)/zlib.h $(UPSTREAM)/zconf.h

include ../../mk/port.mk

CFLAGS := \
    -std=gnu11 \
    $(PORT_CFLAGS_COMMON) \
    -Wall \
    -Wno-implicit-fallthrough \
    -DHAVE_UNISTD_H \
    -DHAVE_STDARG_H \
    -I $(UPSTREAM) \
    -isystem $(shell $(CC) -print-file-name=include)

SRCS := \
    adler32.c compress.c crc32.c deflate.c gzclose.c gzlib.c gzread.c \
    gzwrite.c infback.c inffast.c inflate.c inftrees.c trees.c uncompr.c \
    zutil.c

OBJS := $(addprefix $(OBJDIR)/,$(SRCS:.c=.o))

.PHONY: all clean
all: install-lib

$(PORT_ARCHIVE): $(OBJS)
	$(AR) rcs $@ $(OBJS)
	@echo "Built: $@"

$(OBJDIR)/%.o: $(UPSTREAM)/%.c Makefile
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -MMD -MP -c $< -o $@

clean: clean-lib

-include $(OBJS:.o=.d)
