# !/bin/sh
#  XXCalc makefile
#  Copyright (C)  2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
#                 2011, 2012, 2013  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.

# Check for the presence of these programs on the system where you are going
# to build the library. If you can not find the GNU C compiler (gcc) you can
# use in place of it any other ANSI-compliant C compiler supporting the 
# options -c, -D, -g, -I, -O, -o, -L and -l.
# Remark: 'shtool' is shipped together with the source code of XXCalc. 
CC= gcc
AR= ar rcs
CP= ./shtool install -c -m 644
MKDIR= ./shtool install -d
INSTALL_BIN= ./shtool install -m 755
INSTALL_LIB= ./shtool install -m 644
ECHO= ./shtool echo 
RM= rm -f
RMDIR= rmdir

# If you want that the compiler puts the debug symbols into the object code,
# then set the following variable to -g.
DEBUG_MACRO=-Wall
#DEBUG_MACRO= -g -Wall

# If you want that the compiler produces an optimized object code, then
# set the following variable to something like -O (-O2 suggested for gcc).
OPT_MACRO=
#OPT_MACRO= -O

# If you have vsnprintf() in the header file <stdio.h> of your system,
# then define the following macro as -DHAVE_VSNPRINTF. 
# VSNPRINTF_OPT=
VSNPRINTF_OPT=-DHAVE_VSNPRINTF

# If you want that XXCalc routines return an error code whenever
# a variable turns out to be undefined, then define the following macro as
# -DBE_STRICT. If you leave this macro empty, then, whenever a variable
# will result to be undefined, XXCalc will attribute to it the value
# zero (0). For more information about this subject see the
# document README.xxcalc.
# STRICT_OPT=
STRICT_OPT=-DBE_STRICT 

# If you want to define
# r_eal		as an alias of long double,
# i_nteger	as an alias of long long,
# ui_nteger	as an alias of unsigned long long,
# in place of having
# r_eal		as an alias of double,
# i_nteger	as an alias of long,
# ui_nteger	as an alias of unsigned long,
# then define the following macro as -DUSE_LONG_DOUBLE. 
# Before defining it however, ckecks for the presence, on the system 
# where you are going to install the library, of the header file <tgmath.h>.
# If this file is not present, or the header file <stdlib.h> does not declare
# the function strtold(), or your compiler does not support the numeric
# types 'long long' and 'unsigned long long', then leave the following macro
# undefined, otherwise you will not be able to build the library.
# Mind that all recent versions of the GNU C library
# ( >= 2.2.x) and GNU C Compiler ( >= 3.0.x) fully support long double,
# long long and unsigned long long.
# 
# What are the reasons to define LDBL_OPT as -DUSE_LONG_DOUBLE ?
# Well, a good reason is given by the higher precision which
# XXCalc will perform its computations with (see README.xxcalc).
LDBL_OPT=
#LDBL_OPT=-DUSE_LONG_DOUBLE

# Root directory of the installation. XXCalc library will be installed
# in $(PREFIX)/lib, its header files in $(PREFIX)/include/xxcalc. 
PREFIX=/usr/local
#PREFIX=/opt
#PREFIX=/usr
#PREFIX=$(HOME)

# BELOW THIS LINE YOU SHOULD NOT CHANGE ANYTHING

# If you want to enable the code necessary for memory debugging with
# the dmalloc library, then define the following variable to -DDMALLOC
# (not adviced, used by me for debugging purposes).
DMALLOC_MACRO=
#DMALLOC_MACRO= -DDMALLOC

# If you want that the functions for the complex arithmetic
# cause the termination of the current program, after printing an error
# message to stderr, whenever a mathematical error occurs within one
# of them, then define the following macro as
# -DSTOP_AT_ERROR (Strongly discouraged, is used only by me for debugging
# purposes).
# The default behaviour consists in suitably setting an internal error
# indicator rather than causing the termination of the current program.
ERRFLAG_OPT=
#ERRFLAG_OPT=-DSTOP_AT_ERROR

# BINDIR defines the (sub)directory of the system where the 'xxcconf'
# program (see README.xxcalc) will be installed; of course, you can change
# this value but you must know what you are doing.
# Mind that, if $(PREFIX)/bin is not present in the searching path of
# the executable files (i.e. it is not present in the list 
# specified by the PATH environment variable), then you should
# add $(PREFIX)/bin to the list of directories of PATH.
BINDIR= $(PREFIX)/bin

# INCLUDEDIR defines the (sub)directory of the system where the include files
# of the XXCalc library will be installed; of course, you can change
# this value but you must know what you are doing.
INCLUDEDIR= $(PREFIX)/include/xxcalc

# LIBDIR defines the (sub)directory where the file containing the object code
# of the library will be placed. The library will be created in a static
# form. You can change the location defined by LIBDIR but you must 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.
DOCDIR= $(PREFIX)/doc/xxcalc

CFLAGS = -c $(DEBUG_MACRO) $(OPT_MACRO) $(DMALLOC_MACRO) $(VSNPRINTF_OPT) $(ERRFLAG_OPT) $(STRICT_OPT) $(LDBL_OPT)

# Source files
SRCS		= calc.c compl.c evaluate.c expander.c heapsort.c ioctype.c mathtok.c parser.c preprocessor.c specfunc.c tokstack.c utils.c varlist.c

#Object files
OBJECTS		= calc.o compl.o evaluate.o expander.o heapsort.o ioctype.o mathtok.o parser.o preprocessor.o specfunc.o tokstack.o utils.o varlist.o

#Header files
HEADERS         = calc.h compl.h evaluate.h expander.h heapsort.h ioctype.h mathtok.h numtypes.h parser.h tokstack.h utils.h varlist.h

#library file
LIBNAME		= libxxcalc.a

#Documents
DOCS            = README.xxcalc REFMAN.xxcalc LICENSE

#xxcconf is the program that, once the library will have been installed,
#will help you to compile with and link against the XXCalc library (see 
#the file README.xxcalc for its use).
XXCCONF		= xxcconf

.PHONY :  all install clean clobber uninstall .depend

all : $(LIBNAME)

calc.o: calc.c compl.h numtypes.h mathtok.h parser.h heapsort.h varlist.h calc.h expander.h
	$(CC) $(CFLAGS) $< -o $@

compl.o: compl.c compl.h numtypes.h
	$(CC) $(CFLAGS) $< -o $@

evaluate.o: evaluate.c ioctype.h evaluate.h compl.h numtypes.h varlist.h tokstack.h mathtok.h parser.h heapsort.h calc.h utils.h expander.h
	$(CC) $(CFLAGS) $< -o $@

expander.o: expander.c parser.h mathtok.h compl.h numtypes.h utils.h expander.h varlist.h
	$(CC) $(CFLAGS) $< -o $@

heapsort.o: heapsort.c heapsort.h varlist.h compl.h numtypes.h calc.h
	$(CC) $(CFLAGS) $< -o $@

ioctype.o: ioctype.c ioctype.h
	$(CC) $(CFLAGS) $< -o $@

mathtok.o: mathtok.c mathtok.h compl.h numtypes.h
	$(CC) $(CFLAGS) $< -o $@

parser.o: parser.c ioctype.h compl.h numtypes.h varlist.h tokstack.h  mathtok.h utils.h parser.h
	$(CC) $(CFLAGS) $< -o $@

preprocessor.o: preprocessor.c ioctype.h varlist.h compl.h numtypes.h  mathtok.h utils.h parser.h
	$(CC) $(CFLAGS) $< -o $@

specfunc.o: specfunc.c compl.h numtypes.h
	$(CC) $(CFLAGS) $< -o $@

tokstack.o: tokstack.c tokstack.h mathtok.h compl.h numtypes.h
	$(CC) $(CFLAGS) $< -o $@

utils.o: utils.c ioctype.h compl.h numtypes.h mathtok.h utils.h
	$(CC) $(CFLAGS) $< -o $@

varlist.o: varlist.c varlist.h compl.h numtypes.h heapsort.h calc.h
	$(CC) $(CFLAGS) $< -o $@

$(LIBNAME): $(OBJECTS)
	$(AR) $(LIBNAME) $(OBJECTS)
	$(CC) -g $(VSNPRINTF_OPT) $(ERRFLAG_OPT) $(LDBL_OPT) $(STRICT_OPT) -DVERSION="\"3.2\"" -DIPATH="\"-I$(PREFIX)/include\"" -DLPATH="\"-L$(LIBDIR)\"" ./xxcconf.c -o $(XXCCONF)

install : all
	$(MKDIR) $(BINDIR)
	$(MKDIR) $(LIBDIR)
	$(MKDIR) $(INCLUDEDIR)
	$(MKDIR) $(DOCDIR)
	$(CP) $(DOCS)    $(DOCDIR)
	$(CP) $(HEADERS) $(INCLUDEDIR)
	$(INSTALL_LIB)   $(LIBNAME) $(LIBDIR)
	$(INSTALL_BIN)   $(XXCCONF) $(BINDIR)

clean :
	$(RM) $(OBJECTS)
	$(RM) $(LIBNAME)
	$(RM) $(XXCCONF)

uninstall :
	$(RM)    $(DOCDIR)/*
	$(RM)    $(INCLUDEDIR)/*
	$(RM)    $(LIBDIR)/$(LIBNAME)
	$(RM)    $(BINDIR)/$(XXCCONF)
	$(RMDIR) $(INCLUDEDIR)
	$(RMDIR) $(DOCDIR)
	@$(ECHO) "I have not removed the directories $(PREFIX), $(BINDIR),"
	@$(ECHO) "$(LIBDIR), $(PREFIX)/include and $(PREFIX)/doc."
	@$(ECHO) "If you may and want to do it, please do it yourself!"

.depend : 
	gcc $(CFLAGS) -MM $(SRCS) > .depend
