#-----------------------------------------------------------------------------
# Makefile for FINAL CUT
#-----------------------------------------------------------------------------

# This is where make install will install the library
VERSION = "0.7.2"
MAJOR := $(shell echo ${VERSION} | cut -d. -f1)
LIBDIR = /usr/local/lib
INCLUDEDIR1 = include/final
INCLUDEDIR2 = /usr/local/include/final
INCLUDE_HEADERS = \
	fapplication.h \
	fbuttongroup.h \
	fbutton.h \
	fcallback.h \
	fdata.h \
	fcolorpair.h \
	fstyle.h \
	ftogglebutton.h \
	fcheckbox.h \
	fswitch.h \
	fdialog.h \
	fwindow.h \
	ffiledialog.h \
	final.h \
	flabel.h \
	flineedit.h \
	flistbox.h \
	flistview.h \
	flog.h \
	flogger.h \
	fmenu.h \
	fdialoglistmenu.h \
	fmenubar.h \
	fradiomenuitem.h \
	fcheckmenuitem.h \
	fmessagebox.h \
	ftooltip.h \
	fbusyindicator.h \
	sgr_optimizer.h \
	foptiattr.h \
	foptimove.h \
	ftermbuffer.h \
	fpoint.h \
	fsize.h \
	fprogressbar.h \
	fradiobutton.h \
	frect.h \
	fsystem.h \
	fsystemimpl.h \
	fscrollbar.h \
	fscrollview.h \
	fspinbox.h \
	fcombobox.h \
	fstatusbar.h \
	fstring.h \
	fstringstream.h \
	fmouse.h \
	fkeyboard.h \
	fstartoptions.h \
	ftermcap.h \
	fterm.h \
	ftermdata.h \
	ftermdebugdata.h \
	ftermios.h \
	ftermdetection.h \
	ftermcapquirks.h \
	ftermxterminal.h \
	ftermfreebsd.h \
	ftermopenbsd.h \
	ftermlinux.h \
	fvterm.h \
	ftextview.h \
	fcolorpalette.h \
	fwidgetcolors.h \
	fwidget.h \
	fevent.h \
	fobject.h \

# compiler parameter
CXX = clang++
CCXFLAGS = $(OPTIMIZE) $(PROFILE) -DCOMPILE_FINAL_CUT $(DEBUG) $(VER) $(GPM) -fexceptions -std=c++11
MAKEFILE = -f Makefile.clang
LDFLAGS = $(TERMCAP) -lgpm
INCLUDES = -Iinclude
GPM = -D F_HAVE_LIBGPM
VER = -D F_VERSION=$(VERSION)
RM = rm -f
LIB = libfinal.so
OBJS = \
	fstring.o \
	fstringstream.o \
	fpoint.o \
	fsize.o \
	frect.o \
	fcallback.o \
	fdata.o \
	fscrollbar.o \
	fprogressbar.o \
	flineedit.o \
	fbutton.o \
	fbuttongroup.o \
	ftogglebutton.o \
	fradiobutton.o \
	fcheckbox.o \
	fswitch.o \
	flabel.o \
	flistbox.o \
	flistview.o \
	flog.o \
	flogger.o \
	fmenu.o \
	fdialoglistmenu.o \
	fmenubar.o \
	fmenuitem.o \
	fradiomenuitem.o \
	fcheckmenuitem.o \
	fmenulist.o \
	fdialog.o \
	fwindow.o \
	fscrollview.o \
	fspinbox.o \
	fcombobox.o \
	fmessagebox.o \
	ftooltip.o \
	fbusyindicator.o \
	ffiledialog.o \
	fkey_map.o \
	fcharmap.o \
	ftextview.o \
	fstatusbar.o \
	fmouse.o \
	fsystem.o \
	fsystemimpl.o \
	fkeyboard.o \
	fstartoptions.o \
	ftermcap.o \
	fterm.o \
	fterm_functions.o \
	ftermdebugdata.o \
	ftermios.o \
	ftermdetection.o \
	ftermcapquirks.o \
	ftermxterminal.o \
	ftermfreebsd.o \
	ftermopenbsd.o \
	ftermlinux.o \
	fvterm.o \
	sgr_optimizer.o \
	foptiattr.o \
	foptimove.o \
	ftermbuffer.o \
	fapplication.o \
	fcolorpalette.o \
	fwidgetcolors.o \
	fwidget.o \
	fwidget_functions.o \
	fevent.o \
	fobject.o

TERMCAP := $(shell test -n "$$(ldd {/usr,}/lib64/libncursesw.so.5 2>/dev/null | grep libtinfo)" && echo "-ltinfo" || echo "-lncurses")

ifdef DEBUG
  OPTIMIZE = -O0 -fsanitize=undefined
else
  OPTIMIZE = -O2
endif

.SUFFIXES: .cpp

# $@ = name of the targets
# $< = the first dependency
.cpp.o:
	$(CXX) -c $(CCXFLAGS) $(INCLUDES) -fpic -o $@ $<

all: dep $(OBJS)
	$(CXX) $(OBJS) -o $(LIB).$(VERSION) $(CCXFLAGS) $(INCLUDES) $(LDFLAGS) -shared -Wl,-soname,$(LIB).$(MAJOR)
	ln -s -f $(LIB).$(VERSION) libfinal.so.$(MAJOR)
	ln -s -f $(LIB).$(MAJOR) libfinal.so

$(LIB): all

debug:
	$(MAKE) $(MAKEFILE) DEBUG="-g -D DEBUG -Wall -Wextra -Wpedantic -Weverything -Wno-padded -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-implicit-fallthrough -Wno-reserved-id-macro -Wno-format-nonliteral"

unittest:
	$(MAKE) $(MAKEFILE) DEBUG="-g -D DEBUG -DUNIT_TEST -Wall -Wextra -Wpedantic -Weverything -Wno-padded -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-implicit-fallthrough -Wno-reserved-id-macro -Wno-format-nonliteral"

profile:
	$(MAKE) $(MAKEFILE) PROFILE="-pg"

install: $(LIB)
	mkdir -p $(LIBDIR)
	install -c $(LIB).$(VERSION) $(LIBDIR)/$(LIB).$(VERSION)
	ln -s -f $(LIB).$(VERSION) $(LIBDIR)/libfinal.so.$(MAJOR)
	ln -s -f $(LIB).$(MAJOR) $(LIBDIR)/libfinal.so
	ldconfig
	mkdir -p $(INCLUDEDIR2)
	@list='$(INCLUDE_HEADERS)'; for h in $$list; \
	do \
	  install -m 644 $(INCLUDEDIR1)/$$h $(INCLUDEDIR2)/$$h; \
	done

uninstall: $(OBJS)
	$(RM) $(LIBDIR)/$(LIB).$(VERSION) $(LIBDIR)/libfinal.so.$(MAJOR) $(LIBDIR)/libfinal.so
	@list='$(INCLUDE_HEADERS)'; for h in $$list; \
	do \
	  $(RM) $(INCLUDEDIR2)/$$h; \
	done
	rmdir $(INCLUDEDIR2)

.PHONY: clean dep
clean:
	$(RM) $(LIB)* $(OBJS) .depend *.gcno *.gcda *.gch *.plist *~

dep:
	$(CXX) $(INCLUDES) -std=c++11 -DCOMPILE_FINAL_CUT -MM *.cpp >.depend

#
# include .depend if it exists
#

-include .depend
