RGSRC=../../
include $(RGSRC)/envir.mak

A_TARGET=libz.a
O_OBJS_libz.a = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o \
  trees.o zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o \
  inffast.o

EXPORT_LIB_SRC = adler32.c zutil.h zutil.c infblock.h inflate.c inftrees.h \
  infcodes.h infutil.h infblock.c inffixed.h inftrees.c inffast.h infcodes.c \
  infutil.c inffast.c

CD_EXPORTED_FILES=$(EXPORT_LIB_SRC) uncompr.c zlib.h zconf.h deflate.c \
  trees.c deflate.h trees.h zlib_export.c zlib_export.h

CREATE_LOCAL=$(A_TARGET)
DONT_EXPORT=deflate.o local_deflate.o

EXPORT_HEADERS+=zlib.h zconf.h

include $(RGMK)

#use awk to remove all local includes: #include "*.?"
AWK_RM_INCLUDES='/\#include ["].*[.][hc]["]/ {print "/*",$$0,"*/"} \
  !/\#include ["].*[.][hc]["]/ {print}'
zlib_export.h:
	@cat zconf.h zlib.h > zlib_export_.h
	@awk $(AWK_RM_INCLUDES) zlib_export_.h > zlib_export.h
	@rm -f zlib_export_.h

zlib_export.c:
	@cat $(EXPORT_LIB_SRC) > zlib_export_.c
	@echo "#define _ZLIB_EXPORT_C" > zlib_export.c
	@echo "#include \"zlib.h\"" >> zlib_export.c  # won't remove by awk
	@awk $(AWK_RM_INCLUDES) zlib_export_.c >> zlib_export.c
	@rm -f zlib_export_.c

ifndef CONFIG_RG_OS_LINUX_24
  ifdef CONFIG_CRAMFS_FS
    CRAMFS_EDIR=$(RGSRC)/linux/fs/cramfs/inflate
    LINK_FILES+=$(CD_EXPORTED_FILES:%=$(CRAMFS_EDIR)/%)
  endif
endif

ifdef CONFIG_FREESWAN
  FREESWAN_EDIR=$(RGSRC)/pkg/freeswan/zlib
  LINK_FILES+=$(CD_EXPORTED_FILES:%=$(FREESWAN_EDIR)/%)
endif

ifdef CONFIG_RG_OS_LINUX
  LINK_EXPORT_DIRS+=pkg/boot
endif

LINK_EXPORT_FILES+=$(foreach d,$(LINK_EXPORT_DIRS),$(RGSRC)/$d/zlib.c $(RGSRC)/$d/zlib.h)

$(LINK_FILES):
	@$(call RG_LN_FUNC,$(notdir $@),$@)

$(LINK_EXPORT_FILES):
	@$(call RG_LN_FUNC,$(foreach f,$(notdir $@),$(f:zlib.%=zlib_export.%)),$@)
	
archconfig:: $(call DEPEND_F,libz.a,zlib_export.c zlib_export.h) \
	$(LINK_FILES) $(LINK_EXPORT_FILES)

DISTCLEAN+=$(LINK_EXPORT_FILES) $(LINK_FILES) zlib_export.c zlib_export.h

