## makefile for LangIdent

INCDIR=../framepac
DESTDIR=/usr/bin
DBDIR=/usr/share/langident

SHAREDLIB=

OBJS = langid.o scan_langid.o mtrie.o prepfile.o pstrie.o ptrie.o roman.o smooth.o \
	trie.o trigram.o wildcard.o

DISTFILES = COPYING README makefile manual.txt *.C *.h \
	mklangid romanize whatlang

LIBRARY=langident.a

#########################################################################
## define the compiler

CC=g++
CCLINK=$(CC)

#########################################################################
## define compilation options

ifndef BUILD_DBG
### compile fully optimized for distribution
BUILD_DBG=0
### compile with debugging info
#BUILD_DBG=1
### compile with debugging info and all optimizations off
#BUILD_DBG=2
endif

# build statically-linked executable (1=yes, 0=no)
#STATIC?=1
STATIC?=0

# enable multi-threading? (1=yes, 0=no)
#THREADS?=1
THREADS?=0

ifndef NODEBUG
#NODEBUG=-DNDEBUG
endif

ifndef GDB
#GDB = -ggdb3
endif

ifeq ($(DO_PROFILE),1)
PROFILE=-pg
#PROFILE=-DPURIFY
else
ifndef PROFILE
PROFILE=
endif
endif

ifeq ($(SANE),1)
SANITIZE=-fsanitize=thread -fPIE -DHELGRIND
LINKSAN=-pie
else ifeq ($(SANE),2)
SANITIZE=-fsanitize=address -fno-omit-framepointer -DPURIFY
else ifeq ($(SANE),3)
SANITIZE=-fsanitize=leak -DPURIFY
else ifeq ($(SANE),4)
SANITIZE=-fsanitize=memory -fno-omit-framepointer
else ifeq ($(SANE),5)
SANITIZE=-fsanitize=undefined
endif

ifndef CPU
## Uncomment the appropriate CPU type
### 486
#CPU=4
### Pentium
#CPU=5
### PentiumPro or higher
#CPU=6
### AMD Athlon; not supported by GCC 2.x
#CPU=7
### AMD64/x86_64 CPUs in 64-bit mode; not supported by GCC 2.x
###    (AMD K8 [Opteron/Athlon64], newest PentiumIV with EM64t)
#CPU=8
### AMD64 "PhenomII" (K10) or newer
#CPU=10
### Let GCC auto-determine CPU type, but assume at least CPU=8 capabilities
CPU=99
endif

ifndef BITS
#BITS=32
BITS=64
endif

ifeq ($(THREADS),1)
  PTHREAD=-pthread -DFrMULTITHREAD 
#-fopenmp
else
  PTHREAD=
endif

ifeq ($(STATIC),1)
  LINKTYPE=-static -z muldefs
else
  LINKTYPE=
endif

ifdef MAKE_SHAREDLIB
SHAREDLIB=-fPIC -DSHARED
endif

ifeq ($(NOICONV),1)
ICONV=-DNO_ICONV
else
ICONV=
endif

ifndef RELEASE
RELPATH=LangIdent
ZIPNAME=langident.zip
else
RELPATH=LangIdent-$(RELEASE)
ZIPNAME=langident-$(RELEASE).zip
endif

WARN=-Wall -Wextra -Wno-deprecated -Wshadow -Wcast-align -Wmissing-noreturn -Wmissing-format-attribute
#WARN += -Wunused-result (not on Doha)
#WARN += -Wno-multichar -Wpacked -Wdisabled-optimization -Wpadded

# explicitly force includes to check here first, to fix an incompatibility
#   with the templated iostreams (they don't have many of the functions
#   present in the old iostream)
#EXTRAINC=-I/usr/include/g++-3/

LINKBITS=-m$(BITS)
ifeq ($(CPU),99)
  # auto-detection, assuming at least AMD "K8" level of features (any
  #  x64 processor qualifies); required GCC 4.2+
  CPUDEF=-march=native -D__886__ -D__BITS__=$(BITS)
else ifeq ($(CPU),10)
  # newest AMD chips: "Barcelona", PhenomII
  CPUDEF=-march=amdfam10 -D__886__ -D__BITS__=$(BITS)
else ifeq ($(CPU),8)
  CPUDEF=-march=k8 -msse -D__BITS__=$(BITS)
else ifeq ($(CPU),7)
  CPUDEF=-march=athlon-xp -mmmx
else ifeq ($(CPU),6)
  CPUDEF=-march=i$(CPU)86 -mtune=i$(CPU)86 -mmmx
else
  CPUDEF=-march=i$(CPU)86 -mtune=i$(CPU)86
endif
ifneq ($(CPU),99)
CPUDEF += -D__$(CPU)86__
endif

CC = g++ -std=c++11
CCLINK = $(CC)
CFLAGS = $(WARN)
CFLAGS +=$(CPUDEF)

CFLAGS +=$(PTHREAD)
CFLAGS +=$(PROFILE)
CFLAGS +=$(ICONV)
CFLAGS +=$(NODEBUG)
CFLAGS +=$(LINKBITS) -pipe
CFLAGS +=$(EXTRAINC)
CFLAGS +=$(SANITIZE)
CFLAGS +=$(INCLUDEDIRS)
CFLAGS +=$(SHAREDLIB)
CFLAGS +=$(COMPILE_OPTS)
CFLAGS +=-I$(INCDIR)
CFLAGEXE = -L$(LIBINSTDIR) $(PROFILE) -o $@
LINKFLAGS =$(LINKBITS)
LINKFLAGS +=$(LINKTYPE)
LINKFLAGS +=$(PTHREAD)
LINKFLAGS +=$(SANITIZE)
LINKFLAGS +=$(LINKSAN)
LINKFLAGS +=-L$(INCDIR)

ifeq ($(BUILD_DBG),2)
  CFLAGS += -ggdb3 -O0 -fno-inline -g3
else ifeq ($(BUILD_DBG),1)
  CFLAGS += -ggdb3 -O -g3
else
  CFLAGS += -O3 -fexpensive-optimizations -g$(DBGLVL) $(GDB)
# CFLAGS += -fweb -ftracer -fgcse-sm -fgcse-las -fno-math-errno
endif

ifeq ($(BUILD_DBG),1)
CFLAGS +=-O0 -fno-inline -ggdb3 '-DDBDIR="$(DBDIR)"'
CFLAGSLOOP=
LINKFLAGS += -ggdb3
else ifeq ($(BUILD_DBG),-1)
CFLAGS +=-O3 -fexpensive-optimizations -ggdb3 '-DDBDIR="$(DBDIR)"'
CFLAGSLOOP=-fno-align-labels -fno-align-loops
LINKFLAGS += -ggdb3
else
CFLAGS +=-O3 -fexpensive-optimizations -fmerge-constants '-DDBDIR="$(DBDIR)"'
CFLAGSLOOP=-fno-align-labels -fno-align-loops
endif

CFLAGSLOOP += $(CFLAGS)

CFLAGEXE=-o $@ -m$(BITS) $(PROFILE)

RM=rm
CP=cp -p

#########################################################################
## define the programs needed to create the target library

LIBRARIAN=ar
LIBFLAGS=rucl
LIBINDEXER = ranlib
LIBIDXFLAGS = $(LIBRARY)

#########################################################################
## standard targets

all:  mklangid romanize whatlang subsample

clean:
	-$(RM) $(LIBRARY) *.o mklangid whatlang romanize subsample

allclean:
	-$(RM) $(LIBRARY) *.o mklangid whatlang romanize subsample

tags:
	etags --c++ *.h *.C

install:
	$(CP) mklangid romanize whatlang $(DESTDIR)

zip: 	mklangid romanize whatlang
	-strip mklangid romanize whatlang
	-$(RM) $(ZIPNAME)
	mkdir $(RELPATH)
	-$(CP) -i --parents $(DISTFILES) $(RELPATH)
	-( cd $(RELPATH) ; md5sum ${DISTFILES} >MD5SUM )
	-( cd $(RELPATH) ; sha1sum ${DISTFILES} >SHA1SUM )
	zip -mro9 $(ZIPNAME) $(RELPATH)/

lib:	$(LIBRARY)

#########################################################################
## executables

mklangid: mklangid.o $(LIBRARY) $(INCDIR)/framepac.a
	$(CCLINK) $(LINKFLAGS) $(CFLAGEXE) mklangid.o $(LIBRARY) $(INCDIR)/framepac.a

romanize: romanize.o $(LIBRARY) $(INCDIR)/framepac.a
	$(CCLINK) $(LINKFLAGS) $(CFLAGEXE) romanize.o $(LIBRARY) $(INCDIR)/framepac.a

whatlang: whatlang.o $(LIBRARY) $(INCDIR)/framepac.a
	$(CCLINK) $(LINKFLAGS) $(CFLAGEXE) whatlang.o $(LIBRARY) $(INCDIR)/framepac.a

subsample: subsample.o
	$(CCLINK) $(LINKFLAGS) $(CFLAGEXE) subsample.o

#########################################################################
## object modules

langid.o: langid.C langid.h
	$(CC) $(CFLAGSLOOP) -c $<

mklangid.o: mklangid.C langid.h prepfile.h trie.h mtrie.h ptrie.h

whatlang.o: whatlang.C langid.h

scan_langid.o: scan_langid.C langid.h

mtrie.o: mtrie.C mtrie.h

prepfile.o: prepfile.C prepfile.h

pstrie.o: pstrie.C pstrie.h mtrie.h wildcard.h

ptrie.o: ptrie.C ptrie.h mtrie.h

roman.o: roman.C roman.h

smooth.o: smooth.C langid.h

subsample.o: subsample.C

trie.o: trie.C trie.h wildcard.h

trigram.o: trigram.C langid.h trie.h

wildcard.o: wildcard.C wildcard.h

#########################################################################
## header files -- touching to ensure proper recompilation

langid.h:	mtrie.h
	touch langid.h

pstrie.h:	ptrie.h trie.h
	touch pstrie.h

#########################################################################
## library

$(LIBRARY): $(OBJS)
	-$(RM) $(LIBRARY)
	$(LIBRARIAN) $(LIBFLAGS) $(LIBRARY) $(OBJS)
	$(LIBINDEXER) $(LIBIDXFLAGS)

$(INCDIR)/framepac.a:
	( cd $(INCDIR) ; make lib )

#########################################################################
## default compilation rule

.C.o: ; $(CC) $(CFLAGS) -c $<
