#! /usr/bin/make -f

prefix = /usr
datarootdir = $(prefix)/share
datadir = $(prefix)/share
localedir = $(prefix)/lib/locale
docdir = $(prefix)/share/doc

SUPPORTED_LOCALES = \
	de_DE.UTF-8/UTF-8 de_DE/ISO-8859-1 de_DE@euro/ISO-8859-15 \
	en_HK.UTF-8/UTF-8 en_HK/ISO-8859-1 \
	en_PH.UTF-8/UTF-8 en_PH/ISO-8859-1 \
	en_US.UTF-8/UTF-8 en_US/ISO-8859-1 \
	es_MX.UTF-8/UTF-8 es_MX/ISO-8859-1 \
	fa_IR/UTF-8 \
	fr_FR.UTF-8/UTF-8 fr_FR/ISO-8859-1 fr_FR@euro/ISO-8859-15 \
	it_IT.UTF-8/UTF-8 it_IT/ISO-8859-1 \
	ja_JP.EUC-JP/EUC-JP ja_JP.UTF-8/UTF-8

nop:
	@:

configure-libc:
	[ ! -d libcbuild ] && mkdir libcbuild || true
	rm -f libcbuild/config.cache
	if [ 'x$(OPK_HOST_PLAT)' = 'xbootstrap1' ]; then \
		printf '%s\n' \
			'libc_cv_forced_unwind=yes' \
			'libc_cv_c_cleanup=yes' \
			'libc_cv_gnu89_inline=yes' \
			'libc_cv_ssp=no' \
			>>libcbuild/config.cache; \
	fi
	printf '%s\n' \
		'libc_cv_slibdir=/lib/$(OPK_HOST_ARCH)' \
		>>libcbuild/config.cache; \
	if [ 'x$(TOOLS_PREFIX)' != 'x' ]; then \
		BINUTILS="--with-binutils=$(TOOLS_PREFIX)/bin"; \
	else \
		BINUTILS=; \
	fi && \
	if [ 'x$(HEADERS_PREFIX)' != 'x' ]; then \
		HEADERS="--with-headers=$(HEADERS_PREFIX)/usr/include"; \
	else \
		HEADERS=; \
	fi && \
	BUILD_CC=gcc BUILD_CXX=g++ MAKEINFO=: oh-autoconfigure \
		-s "$${PWD}/src/libc" -B libcbuild -- \
		--datarootdir=$(datarootdir) --datadir=$(datadir) \
		--localedir=$(localedir) --docdir=$(docdir) \
		--disable-profile \
		--enable-add-ons=ports,nptl,libidn \
		--enable-kernel=3.2.0 \
		--with-tls --with-__thread --without-cvs \
		--without-selinux --without-gd \
		$(BINUTILS) \
		$(HEADERS) \
		--cache-file=config.cache
	touch $@

configure-locales:
	[ ! -d localesbuild ] && mkdir localesbuild || true
	CPPFLAGS=-DNOT_IN_libc oh-autoconfigure \
		-s "$${PWD}/src/localedef" -B localesbuild -- \
		--libdir='$${prefix}/lib' \
		--with-glibc=../src/libc
	touch $@

configure: configure-libc configure-locales
	touch $@

build-libc: configure-libc
	oh-autobuild -s "$${PWD}/src/libc" -B libcbuild -- -j $${JOBS:-1}
	touch $@

build-locales: configure-locales
	oh-autobuild -s "$${PWD}/src/localedef" -B localesbuild -- \
		-j $${JOBS:-1}
	touch $@

build: build-libc build-locales
	touch $@

install-libc: build-libc
	# Install /etc/ld.so.conf early to silence a warning from ldconfig.
	mkdir -p dest/etc/ld.so.conf.d
	printf 'include /etc/ld.so.conf.d/*.conf\n' \
		>'dest/etc/ld.so.conf'
	printf '/usr/local/lib\n/opt/lib\n' \
		>'dest/etc/ld.so.conf.d/local'
	printf '/lib/$(OPK_HOST_ARCH)\n/usr/lib/$(OPK_HOST_ARCH)\n' \
		>'dest/etc/ld.so.conf.d/$(OPK_HOST_ARCH)'
	oh-autoinstall -s "$${PWD}/src/libc" -B libcbuild
	rm -f dest/etc/ld.so.cache
	# Make a /usr/lib/<host>/ld.so link for GCC.
	# No ELF binary should reference this link in its INTERP field.  This is
	# strictly a development file that points to the system's dynamic
	# linker.
	ln -sf $$(printf '%s' dest/lib/$(OPK_HOST_ARCH)/ld*so.* | \
		sed 's/dest//') dest/usr/lib/$(OPK_HOST_ARCH)/ld.so
	rm -f dest/var/db/Makefile \
		dest/sbin/sln \
		dest/usr/bin/makedb \
		dest/usr/bin/pcprofiledump \
		dest/usr/bin/xtrace
	# TODO: PIC package?
	rm -Rf dest/usr/lib/*/*_pic.*
	rm -Rf dest/usr/lib/*/libc_pic/
	# pt_chown isn't necessary with Linux's devptsfs.
	[ "x${OPK_HOST_ARCH_KERNEL}" = 'xlinux' ] && \
		rm -f dest/usr/lib/*/pt_chown
	cp ../nsswitch.conf dest/etc
	cp src/libc/posix/gai.conf dest/etc
	cp src/libc/nscd/nscd.conf dest/etc

install-locales: build-locales
	oh-autoinstall -s "$${PWD}/src/localedef" -B localesbuild \
		-T install-locales \
		SUPPORTED-LOCALES='$(SUPPORTED_LOCALES)'

install: install-libc install-locales
	oh-fixperms
	oh-strip -k
	# oh-fixperms changes the mode of the ELF interpreter, which is one
	# shared object that must be executable.
	chmod a+x 'dest/lib/$(OPK_HOST_ARCH)/ld-'*.so
	oh-installfiles

clean-libc:
	oh-autoclean -s "$${PWD}/src/libc" -B libcbuild
	rm -f configure-libc build-libc

clean-locales:
	oh-autoclean -s "$${PWD}/src/localedef" -B localesbuild
	rm -f configure-locales build-locales

clean: clean-libc clean-locales
	rm -f configure build
	rm -Rf *.data

# POSIX.1-2008:
#   If the pathname does not begin with a '/' it shall be treated as relative to
#   the current working directory of the process, not relative to the directory
#   containing the makefile.
include ../source.mk