# Makefile for uClibc
#
# Copyright (C) 2000-2008 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

locale_DIR := $(top_srcdir)extra/locale
locale_OUT := $(top_builddir)extra/locale

# command used to download source code
WGET := wget --passive-ftp

LOCALE_DATA_FILENAME := uClibc-locale-20081111-$(ARCH_NATIVE_BIT)-$(if $(ARCH_LITTLE_ENDIAN),el,eb).tgz

BUILD_CFLAGS-locale-common := \
	-D__UCLIBC_GEN_LOCALE \
	-I$(top_builddir)

BUILD_CFLAGS-gen_wc8bit := $(BUILD_CFLAGS-locale-common) -DCTYPE_PACKED=1
BUILD_CFLAGS-gen_wctype := $(BUILD_CFLAGS-locale-common)
BUILD_CFLAGS-gen_ldc := -I$(locale_OUT)
ifeq ($(UCLIBC_HAS_WCHAR),y)
BUILD_CFLAGS-gen_wc8bit += -DDO_WIDE_CHAR=1
BUILD_CFLAGS-gen_ldc += -D__WCHAR_ENABLED=1
endif
BUILD_CFLAGS-gen_locale := -D_GNU_SOURCE -I$(locale_OUT)
BUILD_CFLAGS-gen_collate := -D_GNU_SOURCE

locale_headers-$(UCLIBC_HAS_LOCALE) := $(top_builddir)include/bits/uClibc_locale_data.h

#DEPH-locale := $(top_builddir)include/bits/sysnum.h
DEPH-locale := $(top_builddir)include/bits/uClibc_config.h
DEPH-gen_collate := $(DEPH-locale)
DEPH-gen_ldc := $(addprefix $(locale_OUT)/,c8tables.h wctables.h locale_tables.h locale_collate.h) $(DEPH-locale)
DEPH-gen_locale := $(addprefix $(locale_OUT)/,c8tables.h) $(DEPH-locale)
DEPH-gen_wc8bit := $(top_builddir)include/bits/uClibc_ctype.h $(DEPH-locale)
DEPH-gen_wctype := $(top_builddir)include/bits/uClibc_ctype.h $(DEPH-locale)

locale_HOBJ := gen_collate gen_ldc gen_locale gen_wc8bit gen_wctype
locale_HOBJ := $(addprefix $(locale_OUT)/,$(locale_HOBJ))
$(locale_HOBJ): | $(locale_OUT)

locale_SRC := $(locale_OUT)/locale_data.c
locale_OBJ := $(locale_OUT)/locale_data.o

CFLAGS-locale_data.c := -D__WCHAR_ENABLED -I$(locale_OUT) -I$(locale_DIR)

#headers-$(UCLIBC_HAS_LOCALE) += $(locale_headers-y)
headers: $(locale_headers-y)

libc-$(UCLIBC_HAS_LOCALE) += $(locale_OBJ)
libc-nomulti-$(UCLIBC_HAS_LOCALE) += $(locale_OBJ)

$(locale_OUT)%.o:  $(locale_OUT)%.c FORCE ; $(compile.c)
$(locale_OUT)%.os: $(locale_OUT)%.c FORCE ; $(compile.c)
$(locale_OUT)%.oS: $(locale_OUT)%.c FORCE ; $(compile.c)
$(locale_OUT)%.o:  $(locale_OUT)%.S FORCE ; $(compile.S)
$(locale_OUT)%.os: $(locale_OUT)%.S FORCE ; $(compile.S)
$(locale_OUT)%.oS: $(locale_OUT)%.S FORCE ; $(compile.S)
$(locale_OUT)%.o:  $(locale_OUT)%.s FORCE ; $(compile.S)
$(locale_OUT)%.os: $(locale_OUT)%.s FORCE ; $(compile.S)
$(locale_OUT)%.oS: $(locale_OUT)%.s FORCE ; $(compile.S)
$(locale_OUT)%.i:  $(locale_OUT)%.c FORCE ; $(compile.i)
$(locale_OUT)%.i:  $(locale_OUT)%.S FORCE ; $(compile.i)
$(locale_OUT)%.s:  $(locale_OUT)%.c FORCE ; $(compile.s)
$(locale_OUT)%.s:  $(locale_OUT)%.S FORCE ; $(compile.s)
$(locale_OUT)%.dep:


locale_headers: headers $(top_builddir)include/bits/uClibc_locale_data.h

# make sure that the host system has locales (this check is ok for uClibc/glibc)
# we do not know though which locales were really enabled for libc at build time
ifeq ($(wildcard /usr/include/iconv.h),)

$(locale_OUT)/codesets.txt:
	@if [ ! -f $@ ] ; then \
	    set -e; \
	    echo " "; \
	    echo "You do not have a codesets.txt file.  Please create this "; \
	    echo "file in the $(locale_OUT) directory by running something like: "; \
	    echo "  find $(CURDIR)/$(locale_DIR)/charmaps -name \"*.pairs\" > \\"; \
	    echo "        $@"; \
	    echo "and then edit that file to disable/enable the codesets you wish to support. "; \
	    echo " "; \
	    false; \
	fi

$(locale_OUT)/locales.txt:
	@if [ ! -f $@ ] ; then \
	    set -e; \
	    echo " "; \
	    echo "You do not have a locales.txt file in the $(locale_OUT) "; \
	    echo "directory, Please copy the LOCALES file to locales.txt by "; \
	    echo "running something like: "; \
	    echo "  cp $(locale_DIR)/LOCALES \\ "; \
	    echo "      $@ "; \
	    echo "then edit locales.txt to disable/enable the locales you wish "; \
	    echo "to support. "; \
	    echo " "; \
	    false; \
	fi

else

$(locale_OUT)/codesets.txt:
	@$(disp_gen)
ifeq ($(UCLIBC_BUILD_MINIMAL_LOCALE),y)
	$(Q)echo "$(CURDIR)/$(locale_DIR)/charmaps/ASCII.pairs" > $@
	$(Q)echo "$(CURDIR)/$(locale_DIR)/charmaps/ISO-8859-1.pairs" >> $@
else
	$(Q)set -e; \
		tmp=`mktemp $@.XXXXXX 2>/dev/null || true`; \
		[ -z "$$tmp" ] && tmp='$@.new'; \
		find $(CURDIR)/$(locale_DIR)/charmaps/ -name '*.pairs' | \
			sort > $$tmp; \
		[ -s $$tmp ]; \
		if cmp $@ $$tmp >/dev/null 2>&1; then \
			$(RM) $$tmp; \
		else \
			mv -f $$tmp $@; \
		fi
endif

# the lines beginning w/ '#-' are mandatory
# at least one conversion is needed (euro/cyrillic)
$(locale_OUT)/locales.txt: $(locale_DIR)/LOCALES
	@$(disp_gen)
ifeq ($(UCLIBC_BUILD_MINIMAL_LOCALE),y)
	$(Q)echo "@euro e" > $@
	$(Q)echo "#-" >> $@
	$(Q)echo "UTF-8 yes" >> $@
	$(Q)echo "8-BIT yes" >> $@
	$(Q)echo "#-" >> $@
	$(Q)for locale in $(call qstrip,$(UCLIBC_BUILD_MINIMAL_LOCALES)); do \
		echo "$$locale.UTF-8 UTF-8"; \
		echo "$$locale ISO-8859-1"; \
	done >> $@
else
	$(Q)cat $< > $@
endif

endif

$(locale_DIR)/gen_collate.c: $(DEPH-gen_collate)
$(locale_OUT)/gen_collate : $(locale_DIR)/gen_collate.c
	$(hcompile.u)
$(locale_DIR)/gen_ldc.c: $(DEPH-gen_ldc)
$(locale_OUT)/gen_ldc : $(locale_DIR)/gen_ldc.c
	$(hcompile.u)
$(locale_DIR)/gen_locale.c: $(DEPH-gen_locale)
$(locale_OUT)/gen_locale : $(locale_DIR)/gen_locale.c
	$(hcompile.u)
$(locale_DIR)/gen_wc8bit.c: $(DEPH-gen_wc8bit)
$(locale_OUT)/gen_wc8bit : $(locale_DIR)/gen_wc8bit.c
	$(hcompile.u)
$(locale_DIR)/gen_wctype.c: $(DEPH-gen_wctype)
$(locale_OUT)/gen_wctype : $(locale_DIR)/gen_wctype.c
	$(hcompile.u)

ifneq ($(V),)
ifeq ($(V),1)
FLAG-locale-verbose := -v
endif
ifeq ($(V),2)
FLAG-locale-verbose := -v -v
endif
endif

# code needs to be modified to support top_builddir in almost all apps that write directly to a file
# grep fopen *.c
$(locale_OUT)/c8tables.h: $(locale_OUT)/gen_wc8bit $(locale_OUT)/codesets.txt
	@$(disp_gen)
	$(Q)$< `cat $(word 2,$^)` > $@

# Warning! Beware tr_TR toupper/tolower exceptions!
$(locale_OUT)/wctables.h: $(locale_OUT)/gen_wctype
	@$(disp_gen)
	$(Q)for locale in $(call qstrip,$(UCLIBC_BUILD_MINIMAL_LOCALES)) en_US en_GB; do \
		$< $(FLAG-locale-verbose) $$locale > $@ || \
		$< $(FLAG-locale-verbose) $$locale.UTF-8 > $@ || \
		$< $(FLAG-locale-verbose) $$locale.iso8859-1 > $@ && break; \
	done

$(locale_OUT)/locale_tables.h: $(locale_OUT)/gen_locale $(locale_OUT)/locales.txt
	@$(disp_gen)
	$(Q)$< $(FLAG-locale-verbose) -o $@ $(word 2,$^)

$(locale_OUT)/lt_defines.h: $(locale_OUT)/locale_tables.h $(locale_OUT)/locale_collate.h
	@$(disp_gen)
	$(Q)grep "^#define" $< > $@
	$(Q)grep "^#define __lc" $(word 2,$^) >> $@

$(locale_OUT)/locale_collate.h: $(locale_OUT)/gen_collate $(locale_OUT)/locale_tables.h
	@$(disp_gen)
	$(Q)grep COL_IDX_ $(word 2,$^) | \
		$(SED) -e "s/^.*COL_IDX_\([^, ]*\).*$$/\1/" | sort | uniq | \
		xargs $< $(locale_DIR)/collation $(FLAG-locale-verbose) -o $@

$(locale_OUT)/$(LOCALE_DATA_FILENAME):
ifeq ($(UCLIBC_DOWNLOAD_PREGENERATED_LOCALE_DATA),y)
	( cd $(@D); $(WGET) http://www.uclibc.org/downloads/$(@F) )
endif

ifeq ($(UCLIBC_PREGENERATED_LOCALE_DATA),y)

$(locale_SRC): $(locale_OUT)/$(LOCALE_DATA_FILENAME)
	zcat $< | $(TAR) -xv -C $(@D) -f -
	touch $@
	# we use the one in locale_DIR
	#$(RM) $(locale_OUT)/locale_mmap.h

# for arch specific versions we have to at least overwrite
# lt_defines.h/locale_data.c/uClibc_locale_data.h

$(locale_OUT)/uClibc_locale_data.h: $(locale_SRC)

else

$(locale_SRC): $(locale_OUT)/gen_ldc $(locale_OUT)/lt_defines.h
	@$(disp_gen)
	$(Q)$< $@

$(locale_OUT)/uClibc_locale_data.h: $(locale_OUT)/lt_defines.h $(locale_OUT)/c8tables.h $(locale_OUT)/wctables.h $(locale_DIR)/locale_mmap.h | $(locale_SRC)
	@$(disp_gen)
	$(Q)grep -v "define __LC" $< > $@
	$(Q)cat $(wordlist 2,4,$^) >> $@

endif

$(top_builddir)include/bits/uClibc_locale_data.h: $(locale_OUT)/uClibc_locale_data.h | $(top_builddir)include/bits/uClibc_config.h
	@$(disp_gen)
	$(Q)$(AWK) 'BEGIN{i=1}{if (/WANT_/) i=/endif/;else if (i) print $0}' \
		$< > $@

objclean-y += CLEAN_extra/locale

# lmmtolso.c/gen_mmap.c/tst-*.c not used
CLEAN_extra/locale:
	$(do_rm) $(locale_HOBJ) $(locale_SRC) $(addprefix $(locale_OUT)/*., o os txt) \
		$(addprefix $(locale_OUT)/,$(addsuffix .h,uClibc_locale_data lt_defines c8tables wctables locale_tables locale_collate) lmmtolso gen_mmap locale.mmap)
