# vim: ft=make ts=4 sw=4 noet
.DEFAULT_GOAL := all

NAME    = libuv
VERSION = 1.52.1
GARTYPE = v2

DESCRIPTION = Async I/O library
define BLURB
  libuv is a multi-platform asynchronous I/O library.
  Used by Node.js, BIND 9, and others. On Solaris it uses
  event ports (port_create/port_get) for async I/O.
endef

MASTER_SITES = https://dist.libuv.org/dist/v$(VERSION)/
DISTFILES   += $(NAME)-v$(VERSION)-dist.tar.gz
DISTNAME     = $(NAME)-$(VERSION)

LICENSE = LICENSE

# -----------------------------------------------------------------------
# OZSWlibuv — shared library + headers
# -----------------------------------------------------------------------
PACKAGES             += OZSWlibuv
SPKG_DESC_OZSWlibuv   = Async I/O library (libuv $(VERSION))
CATALOGNAME_OZSWlibuv = libuv

# -----------------------------------------------------------------------
# Build configuration
# -----------------------------------------------------------------------

EXTENSIONS = 1

CONFIGURE_ARGS  = --prefix=$(prefix)
CONFIGURE_ARGS += --enable-shared
CONFIGURE_ARGS += --disable-static
# Solaris: socket/nsl/rt/pthread/sendfile are separate libraries.
# SUNOS_NO_IFADDRS: libuv 1.52 already has #ifdef SUNOS_NO_IFADDRS stub in
# sunos.c; defining it disables the getifaddrs path (absent on snv_111b).
CONFIGURE_ARGS += CFLAGS="-D_XOPEN_SOURCE=600 -D__EXTENSIONS__ -DSUNOS_NO_IFADDRS"
CONFIGURE_ARGS += LDFLAGS="-L$(BUILD_PREFIX)/lib -Wl,-R,$(BUILD_PREFIX)/lib -lsocket -lnsl -lrt -lpthread -lsendfile"

post-extract:
	@echo "==> Patching src/unix/fs.c: futimens/utimensat absent on OpenSolaris snv_111b"
	@# The 4-space-indented '|| defined(__sun)' lines appear only in the futimens/utimensat
	@# #if chains (uv__fs_to_timespec, uv__fs_futime, uv__fs_utime, uv__fs_lutime).
	@# The fdatasync __sun check on a '#if' line (no indent) is left untouched.
	@gsed -i '/^    || defined(__sun)/d' $(WORKSRC)/src/unix/fs.c
	@# Guard the unconditional futimens() call in uv__fs_copyfile
	@/opt/ozsw/bin/python3 -c "\
path='$(WORKSRC)/src/unix/fs.c'; \
c=open(path).read(); \
old='  if (futimens(dstfd, times) == -1) {\n    err = UV__ERR(errno);\n    goto out;\n  }'; \
new='#if !defined(__sun)  /* futimens absent on OpenSolaris snv_111b */\n  if (futimens(dstfd, times) == -1) {\n    err = UV__ERR(errno);\n    goto out;\n  }\n#endif'; \
open(path,'w').write(c.replace(old,new))"
	@echo "==> Patching src/unix/tcp.c: no ifaddrs.h on Solaris; use AIX-style fallback"
	@gsed -i 's/#if !defined(_AIX)/#if !defined(_AIX) \&\& !defined(__sun)/' \
	    $(WORKSRC)/src/unix/tcp.c
	@gsed -i 's/#if defined(_AIX)/#if defined(_AIX) || defined(__sun)/' \
	    $(WORKSRC)/src/unix/tcp.c
	@echo "==> Patching src/unix/thread.c: pthread_setname_np/getname_np absent on OpenSolaris snv_111b"
	@# Setname guard is single-line #if; add __sun to get ENOSYS stub
	@gsed -i 's/^#if defined(_AIX) || defined(__MVS__) || defined(__PASE__)$$/#if defined(_AIX) || defined(__MVS__) || defined(__PASE__) || defined(__sun)/' \
	    $(WORKSRC)/src/unix/thread.c
	@# Getname guard last continuation line (indented, only in getname block); add __sun
	@gsed -i 's/^    defined(__PASE__)$$/    defined(__PASE__) || \\\n    defined(__sun)/' \
	    $(WORKSRC)/src/unix/thread.c
	@$(MAKECOOKIE)

# Fix libtool ECHO="print -r --" (ksh93 builtin, fails under bash)
post-configure:
	@if [ -f $(WORKSRC)/libtool ]; then \
	    find $(WORKSRC) -name "libtool" -type f -exec \
	        gsed -i 's/ECHO="print -r --"/ECHO="printf %s\\n"/g' {} \; ; \
	fi
	@$(MAKECOOKIE)

include gar/category.mk
