# Copyright (C) 2011-2013 G.P. Halkes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
.POSIX:

# C-compiler flags
CFLAGS=-O2

# Configuration flags
# - If strdup is not provided by the library, remove -DHAS_STRDUP.
# - If your platform does not support the nl_langinfo function, remove
#    -DHAS_NL_LANGINFO
# - If your platform provides dlopen/dlsym/dlclose add -DHAS_DLFCN and replace
#    -lltdl with the appropriate libraries to CONFIGLIBS
CONFIGFLAGS=-DHAS_STRDUP -DHAS_NL_LANGINFO
CONFIGLIBS=-lltdl

# Flags and libraries required for compilation with Pthreads
PTHREADFLAGS=-pthread
PTHREADLIBS=-pthread -lpthread

# The libtool executable
LIBTOOL=libtool

# Installation prefix (also defined in Makefile)
prefix=/usr/local

# Gettext configuration
# GETTEXTFLAGS should contain -DUSE_GETTEXT to enable gettext translations
# GETTEXTLIBS should contain all link flags to allow linking with gettext, if
# it has been enabled. The GNU libc already contains the gettext library, so
# there is no need to add any flags. Otherwise, -lintl is usually required, and
# sometimes -liconv as well.
# LOCALEDIR: the directory where the locale dependant files should be installed.
# LINGUAS: translations to be installed. Look in po directory for available
#  translations.
GETTEXTFLAGS=
GETTEXTLIBS=
LOCALEDIR=$(prefix)/share/locale
LINGUAS=

SILENCELT=--silent
SILENTCCLT=@echo '[CCLT]' $< ;
SILENTLDLT=@echo '[LDLT]' $@ ;

# Miscelaneous install paths
libdir=$(prefix)/lib

all: src/libtranscript.la tables modules

tables: src/tables/cns116435.la src/tables/iso2022sbcs.la src/tables/ibm878p1001996.la src/tables/eucjp.la src/tables/ibm37p1001995.la src/tables/cns116437.la src/tables/ibm1168p1002002.la src/tables/shiftjis.la src/tables/viscii.la src/tables/isoir165.la src/tables/iso2022jisx213.la src/tables/koi8ru.la src/tables/iso2022jisx208.la src/tables/cns116436.la src/tables/iso2022ksc5601.la src/tables/cns116432004e.la src/tables/big5.la src/tables/cns116432004b.la src/tables/eucjis2004.la src/tables/euckr.la src/tables/cns116431.la src/tables/cp949.la src/tables/cns116432004c.la src/tables/cns116432004d.la src/tables/cns11643f.la src/tables/windows932.la src/tables/cns116432.la src/tables/ibm437p1001995.la src/tables/cns116433.la src/tables/windows9362000.la src/tables/gb18030table.la src/tables/iso2022gb2312.la src/tables/shiftjis2004.la src/tables/windows125x.la src/tables/euccn.la src/tables/jisx2011969.la src/tables/cns116434.la src/tables/iso2022jisx2121990.la src/tables/big5hkscs.la src/tables/iso8859.la src/tables/ibm1047p1001995.la
modules: src/modules/ascii.la src/modules/unicode.la src/modules/iso2022.la src/modules/euctw.la

.PHONY: all clean dist-clean
.SUFFIXES: .c .o .lo .la .mo .po
.SECONDARY: # Tell GNU make not to delete intermediate files

OBJECTS_LIBTRANSCRIPT=src/sbcs_table_converter.lo src/transcript_io.lo src/state_table_converter.lo src/utf.lo src/transcript_iconv.lo src/transcript.lo src/aliases.lo src/generic_fallbacks.lo

clean:
	rm -rf src/*.lo src/tables/*.lo src/modules/*.lo po/*.mo src/.libs src/libtranscript.la src/tables/*.la src/tables/.libs \
		src/modules/.libs

dist-clean: clean

.c.lo:
	$(SILENTCCLT) $(LIBTOOL) $(SILENCELT) --mode=compile --tag=CC $(CC) -shared $(CFLAGS) -Isrc \
		$(CONFIGFLAGS) $(GETTEXTFLAGS) $(PTHREADFLAGS) -DTRANSCRIPT_BUILD_DSO \
		-DLOCALEDIR=\"$(LOCALEDIR)\" -DDB_DIRECTORY=\"$(libdir)/transcript1\" -c -o $@ $<

.lo.la:
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -module -avoid-version \
		-shrext .ltc $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS) -rpath $(libdir)/transcript1

src/libtranscript.la: $(OBJECTS_LIBTRANSCRIPT)
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -version-info 1:0:0 \
		$(CFLAGS) $(LDFLAGS) -o $@ $(OBJECTS_LIBTRANSCRIPT) $(LDLIBS) $(CONFIGLIBS) $(PTHREADLIBS) $(GETTEXTLIBS) \
		-rpath $(libdir)

src/modules/ascii.la:src/modules/ascii.lo src/libtranscript.la
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -module -avoid-version -shrext .ltc $(CFLAGS) $(LDFLAGS) -o $@ src/modules/ascii.lo $(LDLIBS) -rpath $(libdir)/transcript1
src/modules/unicode.la:src/modules/unicode_gb18030.lo src/modules/unicode_utf7.lo src/modules/unicode.lo src/libtranscript.la
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -module -avoid-version -shrext .ltc $(CFLAGS) $(LDFLAGS) -o $@ src/modules/unicode_gb18030.lo src/modules/unicode_utf7.lo src/modules/unicode.lo $(LDLIBS) -rpath $(libdir)/transcript1
src/modules/iso2022.la:src/modules/iso2022.lo src/libtranscript.la
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -module -avoid-version -shrext .ltc $(CFLAGS) $(LDFLAGS) -o $@ src/modules/iso2022.lo $(LDLIBS) -rpath $(libdir)/transcript1
src/modules/euctw.la:src/modules/euctw.lo src/libtranscript.la
	$(SILENTLDLT) $(LIBTOOL) $(SILENCELT) --mode=link --tag=CC $(CC) -shared -module -avoid-version -shrext .ltc $(CFLAGS) $(LDFLAGS) -o $@ src/modules/euctw.lo $(LDLIBS) -rpath $(libdir)/transcript1
