#! /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
	echo 'libc_cv_forced_unwind=yes' >>libcbuild/config.cache
	echo 'libc_cv_c_cleanup=yes'     >>libcbuild/config.cache
	echo 'libc_cv_gnu89_inline=yes'  >>libcbuild/config.cache
	echo 'libc_cv_ssp=no'            >>libcbuild/config.cache
	echo 'libc_cv_slibdir=/lib/'"$${OPK_HOST_ARCH}" \
		>>libcbuild/config.cache
	if [ -n "$${TOOLS_PREFIX}" ]; then \
		BINUTILS="--with-binutils=$${TOOLS_PREFIX}/bin"; \
	else \
		BINUTILS=; \
	fi && \
	if [ -n "$${HEADERS_PREFIX}" ]; 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
	cp ../ld.so.conf dest/etc
	oh-autoinstall -s "$${PWD}/src/libc" -B libcbuild
	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
	rm -f dest/etc/ld.so.cache

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-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