# SPDX-License-Identifier: Apache-2.0
# ncurses -- terminal capability database and screen library, MIT:
# https://invisible-island.net/ncurses/
#
# Here because zsh's line editor is the reason to want zsh, and the line editor
# drives the terminal through tigetstr/tputs. zsh's configure will happily build
# without a terminal library -- AC_SEARCH_LIBS is soft -- and produce a shell
# whose cursor cannot move and whose arrow keys do nothing. So this comes first.
#
# Unlike every other port, nothing here reaches sysroot/. ncurses is built
# static and consumed at link time by zsh; `install` means "into $(PREFIX)".
#
# The awkward part of ncurses is that it is a library whose data is a database.
# Normally that database is ~4000 compiled terminal descriptions under
# /usr/share/terminfo, which SchOS has no business shipping. --disable-database
# plus --with-fallbacks compiles a chosen handful directly into the library, so
# the resulting .a is self-contained and the filesystem stays clean.

include ../mk/port.mk

NC_VERSION := 6.4
NC_URL     := https://ftp.gnu.org/gnu/ncurses/ncurses-$(NC_VERSION).tar.gz
NC_SHA256  := 6931283d9ac87c5073f30b6290c4c75f21632bb4fc3603ac8100812bed248159

TARBALL := $(DISTDIR)/ncurses-$(NC_VERSION).tar.gz

# The terminal types compiled into the library. `linux` is what SchOS's own
# console is closest to and what TERM is set to; the rest are here so a serial
# console or an ssh client from a real machine still gets a working editor.
# Every name added is bytes in the binary, so this list is deliberately short.
NC_FALLBACKS := linux,vt100,vt102,xterm,xterm-256color,screen,dumb

# Cross-compiling ncurses needs tic and infocmp *on the host* to turn those
# names into C. Both exist here; the paths are looked up rather than written
# down because on NixOS they live in the store and move on every update.
HOST_TIC     ?= $(shell command -v tic)
HOST_INFOCMP ?= $(shell command -v infocmp)

# gcc, for the same reason fastfetch uses it: -print-libgcc-file-name.
HOSTCC_NC ?= gcc
GCC_INC   := $(shell $(HOSTCC_NC) -print-file-name=include)
LIBGCC    := $(shell $(HOSTCC_NC) -print-libgcc-file-name)

# Split exactly as autoconf consumes them: it links a test as
#   $CC $CFLAGS $LDFLAGS conftest.c $LIBS
# so crt1.o/crti.o must ride in LDFLAGS (before the object) and libc in LIBS
# (after it). libc twice -- libgcc reaches back into it, and a static link
# resolves left to right.
NC_CPPFLAGS := -nostdinc -isystem $(MUSL)/include -isystem $(GCC_INC) -D__SCHOS__
NC_CFLAGS   := -O2 $(CFLAGS_BT)
NC_LDFLAGS  := -static -nostdlib -L$(MUSL)/lib $(MUSL)/lib/crt1.o $(MUSL)/lib/crti.o
NC_LIBS     := $(MUSL)/lib/libc.a $(LIBGCC) $(MUSL)/lib/libc.a $(MUSL)/lib/crtn.o

# ncurses builds make_hash and make_keys to run during the build, not on the
# target. In a cross build those must be compiled for the host, with the host's
# own libc -- hence a second, completely separate set of flags.
NC_BUILD_CC := $(HOSTCC_NC)

NC_CONFIGURE := \
  --host=x86_64-linux-musl \
  --build=x86_64-pc-linux-gnu \
  --prefix=$(PREFIX) \
  --disable-database \
  --with-fallbacks=$(NC_FALLBACKS) \
  --with-tic-path=$(HOST_TIC) \
  --with-infocmp-path=$(HOST_INFOCMP) \
  --without-shared --with-normal \
  --enable-widec \
  --enable-overwrite \
  --without-cxx --without-cxx-binding \
  --without-ada --without-tests --without-progs --without-manpages \
  --without-debug --without-profile \
  --disable-stripping \
  --disable-home-terminfo \
  --disable-rpath-hack

$(TARBALL): | $(DISTDIR)
	curl -fsSL -o $@ $(NC_URL)
	sh $(SHA256CHECK) "$(NC_SHA256)" "$@"

download: $(TARBALL)

$(SRCDIR): $(TARBALL)
	mkdir -p $(PORTDIR)
	tar -xzf $< -C $(PORTDIR)
	mv $(PORTDIR)/ncurses-$(NC_VERSION) $@

extract: $(SRCDIR)

$(BUILDDIR)/config.status: | $(SRCDIR)
	@test -n "$(HOST_TIC)" -a -n "$(HOST_INFOCMP)" || { \
	  echo "ncurses: host tic/infocmp not found -- install ncurses on the build host" >&2; \
	  exit 1; }
	mkdir -p $(BUILDDIR)
	cd $(BUILDDIR) && \
	  CC="$(HOSTCC_NC)" \
	  CPPFLAGS="$(NC_CPPFLAGS)" \
	  CFLAGS="$(NC_CFLAGS)" \
	  LDFLAGS="$(NC_LDFLAGS)" \
	  LIBS="$(NC_LIBS)" \
	  BUILD_CC="$(NC_BUILD_CC)" \
	  BUILD_CPPFLAGS= BUILD_CFLAGS= BUILD_LDFLAGS= BUILD_LIBS= \
	  $(SRCDIR)/configure $(NC_CONFIGURE)

$(PREFIX)/lib/libncursesw.a: $(BUILDDIR)/config.status
	$(MAKE) -C $(BUILDDIR)
	$(MAKE) -C $(BUILDDIR) install

build install: $(PREFIX)/lib/libncursesw.a

clean:
	rm -rf $(PORTDIR)

distclean: clean
	rm -f $(TARBALL)
