# SPDX-License-Identifier: GPL-2.0-or-later
# Linux's userspace headers -- GPL-2.0 WITH Linux-syscall-note, which is the
# exception that makes them includable by anything: https://kernel.org/
#
# <linux/input.h>, <linux/fb.h>, <linux/kd.h>, <linux/vt.h> and the rest are
# how a program spells the ioctls SchOS answers in Linux's numbering, and
# Xorg, libevdev and libinput include them without asking whether the kernel
# underneath is Linux. Taken from sabotage-linux's kernel-headers: the
# headers alone, sanitised, a megabyte rather than a kernel tree.
#
# Installed into musl's prefix, next to libc's own, because that is where
# every port already looks. Not scsi/: musl ships its own scsi/*.h, and these
# would overwrite them. Nothing goes into sysroot/ -- headers are build input.

include ../mk/port.mk

LH_VERSION := 4.19.88-2
LH_URL     := https://github.com/sabotage-linux/kernel-headers/releases/download/v$(LH_VERSION)/linux-headers-$(LH_VERSION).tar.xz
# Recorded on first download; upstream publishes no checksum to compare with.
LH_SHA256  := dc7abf734487553644258a3822cfd429d74656749e309f2b25f09f4282e05588

TARBALL := $(DISTDIR)/linux-headers-$(LH_VERSION).tar.xz
STAMP   := $(MUSL)/include/linux/input.h

$(TARBALL): | $(DISTDIR)
	curl -fsSL -o $@ $(LH_URL)
	sh $(SHA256CHECK) "$(LH_SHA256)" "$@"

download: $(TARBALL)

$(SRCDIR): $(TARBALL)
	mkdir -p $(PORTDIR)
	rm -rf $@ $(PORTDIR)/linux-headers-$(LH_VERSION)
	tar -xJf $< -C $(PORTDIR)
	mv $(PORTDIR)/linux-headers-$(LH_VERSION) $@

# A file in the prefix as the target, not a stamp here: musl's prefix is
# rebuilt from scratch when musl is, and the headers have to come back with it.
$(STAMP): | $(SRCDIR)
	test -d $(MUSL)/include || { echo "linux-headers: build musl first"; exit 1; }
	cd $(SRCDIR)/x86/include && for d in *; do \
	    [ "$$d" = scsi ] && continue; \
	    mkdir -p $(MUSL)/include/$$d && cp -R $$d/. $(MUSL)/include/$$d/; \
	done

build install: $(STAMP)

clean:
	rm -rf $(PORTDIR)

distclean: clean
	rm -f $(TARBALL)
