# !/bin/sh
#  BasicMathEval makefile
#  Copyright (C)  2015, 2016  Ivano Primi  <ivprimi@libero.it>    
#
#   Copying and distribution of this file, with or without modification,
#   are permitted in any medium without royalty provided the copyright
#   notice and this notice are preserved.
#

# Current version of BasicMathEval
VERSION=1.0

# Tools needed by this makefile.
CXX=g++
MAKE=make
AR=./shtool arx -C ar
RANLIB=ranlib
CP= ./shtool install -c -m 644
MKDIR= ./shtool install -d
INSTALL_BIN= ./shtool install -m 755
INSTALL_LIB= ./shtool install -m 644
INSTALL_DOC= ./shtool install -m 644
ECHO= ./shtool echo 
RM= rm -f
RMDIR= rmdir

# Uncomment the wished setting of DEBUG_MACRO.
# Leave it undefined if you do not want to built
# the library with debugging symbols.
#
#DEBUG_MACRO= -g 
#DEBUG_MACRO= -g -D_DEBUG_

# To enable code optimization at building time
# uncomment one of the settings below (both work with g++, the first one
# should be understood by other compilers as well).
#OPT_MACRO= -O
#OPT_MACRO= -O2

# Uncomment the wished setting of PREC_MACRO.
# If you leave it undefined, BasicMathEval will
# be compiled with support to double precision arithmetic.
#
PREC_MACRO= -D_DOUBLE_PRECISION_
#PREC_MACRO= -D_SINGLE_PRECISION_

# Comment out the definition of READLINE_MACRO
# if you do not want to build and install 
# the simple scientific calculator, or if the
# GNU Readline library is not available on your system.
READLINE_MACRO= -D_USE_READLINE_

# Comment out the definition of READLINE_LIBS
# if you do not want to build and install 
# the simple scientific calculator, or if the
# GNU Readline library is not available on your system.
READLINE_LIBS=-lreadline -lhistory

# Uncomment the following macro definition if you
# want to build the simple scientific calculator
# with enabled support for colors, and your terminal
# application does know about ASCII escape sequences
# (xterm, rxvt, urxvt, evilvte, eterm, terminology, aterm,
# and Linux console do all support color specification via ASCII
# escape sequences).
#ASCII_COLORS_MACRO= -D_USE_ASCII_ESCAPES_

COMPILER_FLAGS=-c -Wall $(DEBUG_MACRO) $(OPT_MACRO) $(PREC_MACRO)
#COMPILER_FLAGS=-c $(OPT_MACRO) $(PREC_MACRO) -D_BUILD_WITH_LESS_FUNCTIONALITY_

# Root directory of the installation. The library will be installed
# in $(PREFIX)/lib, its header files in $(PREFIX)/include/BasicMathEval,
# the documentation files in $(PREFIX)/doc/BasicMathEval, the program
# bmEvalConf in $(PREFIX)/bin. 
PREFIX=/usr/local
#PREFIX=/opt
#PREFIX=/usr
#PREFIX=$(HOME)

# BELOW THIS LINE YOU SHOULD NOT CHANGE ANYTHING
SHELL = /bin/sh

HEADER_FILES=basicCalculator.h evalError.h evaluator.h Faddeeva.h functionList.h mathToken.h numTypes.h parser.h translator.h Utils.h variablesTable.h

OBJECTS=basicCalculator.o evalError.o evaluator.o Faddeeva.o mathToken.o parser.o translator.o Utils.o variablesTable.o
TST_OBJ_1=test_program.o
TST_OBJ_2=testVartable.o

TARGET=./libbmEval.a
BIN_TARGET= ./bmEvalConf
SSC_TARGET= ./ssc
TST_TARGET_1=./tst/test_program
TST_TARGET_2=./tst/testVartable
RUN_TST=./runtest.sh
TST_OUTPUT=./tst/test_output ./tst/diff_output

# INCDIR defines the (sub)directory of the system where the
# HEADER_FILES of the BasicMathEval library will be installed; 
# you should change this value only if you know what you are doing.
INCDIR= $(PREFIX)/include/BasicMathEval

# LIBDIR defines the (sub)directory of the system where TARGET
# will be installed; you should change
# this value only if you know what you are doing.
LIBDIR= $(PREFIX)/lib

# DOCDIR defines the (sub)directory where the documentation 
# files will be copied to (text of the license, help files and so on). 
# Also this location can be redefined, but you should do it only
# if you know what you are doing
DOCDIR= $(PREFIX)/doc/BasicMathEval

# BINDIR defines the (sub)directory of the system where BIN_TARGET
# will be installed; you should change
# this value only if you know what you are doing.
BINDIR= $(PREFIX)/bin

#Documents
DOCS            = ./INSTALL ./LICENSE ./MANUAL ./MANUAL.html ./README

#GTK_CFLAGS=`pkg-config --cflags gtk+-2.0`
#CAIRO_CFLAGS=`pkg-config --cflags cairo gtk+-2.0`
#CAIRO_LIBS=`pkg-config --libs cairo gtk+-2.0`

CFLAGS=$(COMPILER_FLAGS) 

LIBS=-lm


all: $(TST_TARGET_1) $(TST_TARGET_2) $(BIN_TARGET)

basicCalculator.o: basicCalculator.cpp basicCalculator.h evalError.h Utils.h mathToken.h numTypes.h variablesTable.h evaluator.h parser.h translator.h Faddeeva.h functionList.h
	$(CXX) $(CFLAGS) $< -o $@

evalError.o: evalError.cpp evalError.h Utils.h mathToken.h numTypes.h
	$(CXX) $(CFLAGS) $< -o $@

evaluator.o: evaluator.cpp evaluator.h evalError.h Utils.h mathToken.h numTypes.h parser.h translator.h variablesTable.h basicCalculator.h
	$(CXX) $(CFLAGS) $< -o $@

Faddeeva.o: Faddeeva.cpp Faddeeva.h
	$(CXX) $(CFLAGS) $< -o $@

mathToken.o: mathToken.cpp mathToken.h numTypes.h Utils.h
	$(CXX) $(CFLAGS) $< -o $@

parser.o: parser.cpp parser.h mathToken.h numTypes.h evalError.h Utils.h
	$(CXX) $(CFLAGS) $< -o $@

test_program.o: test_program.cpp evalError.h evaluator.h parser.h mathToken.h numTypes.h translator.h variablesTable.h basicCalculator.h Utils.h
	$(CXX) $(CFLAGS) $< -o $@

translator.o: translator.cpp translator.h mathToken.h numTypes.h evalError.h Utils.h
	$(CXX) $(CFLAGS) $< -o $@

Utils.o: Utils.cpp Utils.h mathToken.h numTypes.h
	$(CXX) $(CFLAGS) $< -o $@

variablesTable.o: variablesTable.cpp variablesTable.h numTypes.h
	$(CXX) $(CFLAGS) $< -o $@

$(TARGET): $(OBJECTS)
	$(AR) rc $(TARGET) $(OBJECTS) 
	$(RANLIB) $(TARGET)

$(TST_TARGET_1): $(TARGET) $(TST_OBJ_1)
	$(CXX) $(TST_OBJ_1) $(TARGET) -o $@ $(LDFLAGS) $(LIBS) 

$(TST_TARGET_2): $(TARGET) $(TST_OBJ_2)
	$(CXX) $(TST_OBJ_2) $(TARGET) -o $@ $(LDFLAGS) $(LIBS) 

$(BIN_TARGET): bmEvalConf.cpp
	$(CXX) -DVERSION="\"$(VERSION)\"" -DPREC="\"$(PREC_MACRO)\"" -DIPATH="\"-I$(PREFIX)/include\"" -DLPATH="\"-L$(LIBDIR)\"" -DLIBS="\"$(LIBS)\"" bmEvalConf.cpp -o $@

$(SSC_TARGET): simple_calculator.cpp
	 $(CXX) -DVERSION="\"$(VERSION)\"" $(PREC_MACRO) $(ASCII_COLORS_MACRO) $(READLINE_MACRO) $(DEBUG_MACRO) simple_calculator.cpp -o $@ -L./ -lbmEval $(READLINE_LIBS) $(LIBS)

install : all
	$(MKDIR) $(BINDIR)
	$(MKDIR) $(LIBDIR)
	$(MKDIR) $(INCDIR)
	$(MKDIR) $(DOCDIR)
	$(CP) $(DOCS)    $(DOCDIR)
	$(CP) $(HEADER_FILES) $(INCDIR)
	$(INSTALL_LIB)   $(TARGET) $(LIBDIR)
	$(INSTALL_BIN)   $(BIN_TARGET) $(BINDIR)

installssc : all $(SSC_TARGET) install
	$(INSTALL_BIN)   $(SSC_TARGET) $(BINDIR)

test :
	make CFLAGS="$(CFLAGS) $(DEBUG_MACRO)" all
	cd ./tst && $(RUN_TST)

clean:
	$(RM) $(OBJECTS) $(TST_OBJ_1) $(TST_OBJ_2) $(TARGET) $(TST_TARGET_1) $(TST_TARGET_2) $(BIN_TARGET) $(SSC_TARGET) $(TST_OUTPUT)

distclean: clean
	$(RM) *~ cscope.out

uninstall :
	$(RM)    $(DOCDIR)/*
	$(RM)    $(INCDIR)/*
	$(RM)    $(LIBDIR)/$(TARGET)
	$(RM)    $(BINDIR)/$(BIN_TARGET)
	$(RM)    $(BINDIR)/$(SSC_TARGET)
	$(RMDIR) $(DOCDIR)
	$(RMDIR) $(INCDIR)
	@$(ECHO) "I have not removed the directories $(PREFIX),"
	@$(ECHO) "$(BINDIR) and $(LIBDIR)."
	@$(ECHO) "If you may and want to do it, please do it yourself!"

