# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#	glibc for Makronix/Linux (ext/AGENTS.md: a tree without pkgsrc recipe)
#
# The recipe of LFS's cross-toolchain chapter "Glibc" (configure in an
# object directory, --prefix=/usr, the kernel headers given, installed with
# DESTDIR), compiled with Makronix's own gcc 14.4.0 of src/ (the tooldir,
# mobile/tools/mktooldir.sh) into Makronix's own sysroot.  Run by
# mobile/tools/mkglibc.sh, which passes:
#   MX_TOOLDIR  the tooldir (bin/aarch64-linux-gnu-gcc ...)
#   MX_SYSROOT  where the kernel headers are and glibc goes (/usr/include,
#               /usr/lib: usr-merged, as LFS and the phone's layout)
#   MX_OBJ      the object directory
#   MX_JOBS     parallel jobs
#
# Host tools glibc needs to build, from the host for now: GNU make 4.0 or
# newer (INSTALL:473; the tooldir's nbgmake is 3.81), python3, bison, gawk.

TARGET=		aarch64-linux-gnu
GLIBC=		${.CURDIR:H}
GMAKE?=		make

.for v in MX_TOOLDIR MX_SYSROOT MX_OBJ
.if !defined(${v})
.error "${v} is not set (run mobile/tools/mkglibc.sh)"
.endif
.endfor
MX_JOBS?=	8

T=		${MX_TOOLDIR}/bin/${TARGET}

all: install

${MX_OBJ}/config.status:
	mkdir -p ${MX_OBJ}
	# LFS: programs like ldconfig and sln go to /usr/sbin
	echo "rootsbindir=/usr/sbin" > ${MX_OBJ}/configparms
	cd ${MX_OBJ} && \
	    CC="${T}-gcc" CXX="${T}-g++" AR="${T}-ar" RANLIB="${T}-ranlib" \
	    NM="${T}-nm" OBJCOPY="${T}-objcopy" OBJDUMP="${T}-objdump" \
	    READELF="${T}-readelf" STRIP="${T}-strip" \
	    ${GLIBC}/configure \
		--prefix=/usr \
		--host=${TARGET} \
		--build=$$(${GLIBC}/scripts/config.guess) \
		--enable-kernel=5.4 \
		--with-headers=${MX_SYSROOT}/usr/include \
		--disable-nscd \
		--disable-werror \
		libc_cv_slibdir=/usr/lib

build: ${MX_OBJ}/config.status
	cd ${MX_OBJ} && ${GMAKE} -j${MX_JOBS}

install: build
	cd ${MX_OBJ} && ${GMAKE} DESTDIR=${MX_SYSROOT} install

.PHONY: all build install
