# LVB (c) Copyright 2003-2006 by Daniel Barker.
# Permission is granted to copy and use this program provided that no fee is
# charged for it and provided that this copyright notice is not removed.

# Makefile - makefile for LVB

# $Id: Makefile,v 1.65 2006/02/06 19:55:46 db60 Exp $

# Requires GNU make
# Requires pod2html (this will usually be present if Perl is installed)
#
# It is always safer to make from scratch after changing the source code
# or Makefile
#
# For UNIX-like systems (including Windows with MinGW and MSYS, MacOS X
# with Developer Tools, and Linux) compile with
#
# make
#
# AFTER BUILDING LVB, RUN TESTS IMMEDIATELY: for UNIX-like systems, do
#
# make tests
#
# for Windows, the tests must be run by hand.

# Directories

LVB_DIR = .
TEST_DIR = $(LVB_DIR)/tests
DOCS_PROG_DIR = $(LVB_DIR)/docs_programmer
PHYLIP_SRC_DIR = ../PHYLIP_FOR_LVB/src

# System-independent compiler options

CFLAGS = -DLVB	 	# Must be present

# Compiler-dependent options
#CFLAGS += -O -v	# Solaris with Sun C
CFLAGS += -g		# for interactive debugging under UNIX or Linux
#CFLAGS += -O4 -Wall	# for more warnings with GNU C
#CFLAGS += -O		# try for UNIX, if you don't want to read the cc manual
#CFLAGS += -pg		# for profiling with gprof
#CFLAGS += -fprofile-arcs -ftest-coverage	# for profiling with gcov

CFLAGS += -O2 -fomit-frame-pointer -Zexe	# For any system with GNU C

# System-dependent macros

# UNIX-like systems
NON_LVB_LIBS = -lm	# UNIX
RANLIB = ranlib		# UNIX
EXE =			# UNIX
OBJ = o			# UNIX
LIB_EXT = a		# UNIX

%.obj : %.c
	$(CC) $(CFLAGS) /c $<

#endif

# LVB library

LVB_LIB = $(LVB_DIR)/liblvb.$(LIB_EXT)
LIBS += $(LVB_LIB)

LVB_PROG = lvb$(EXE)

LIBS += $(NON_LVB_LIBS)

ifeq ($(CC), gcc)
    # Ideally, we would always build in ANSI mode with warnings enabled.
    # But the names of the relevant options vary with compiler. At least
    # we can get it right for the case where the compiler is named "gcc".
    CFLAGS += -Wall -ansi
endif

# Object files that will go into the LVB library

LVB_LIB_OBJS = $(LVB_DIR)/admin.$(OBJ) \
               $(LVB_DIR)/treestack.$(OBJ) \
               $(LVB_DIR)/cleanup.$(OBJ) \
               $(LVB_DIR)/datops.$(OBJ) \
               $(LVB_DIR)/err.$(OBJ) \
               $(LVB_DIR)/fops.$(OBJ) \
               $(LVB_DIR)/getparam.$(OBJ) \
               $(LVB_DIR)/mops.$(OBJ) \
               $(LVB_DIR)/mymaths.$(OBJ) \
               $(LVB_DIR)/myuni.$(OBJ) \
               $(LVB_DIR)/parsim.$(OBJ) \
               $(LVB_DIR)/randpint.$(OBJ) \
               $(LVB_DIR)/selfconf.$(OBJ) \
               $(LVB_DIR)/solve.$(OBJ) \
               $(LVB_DIR)/sops.$(OBJ) \
               $(LVB_DIR)/trops.$(OBJ) \
               $(LVB_DIR)/wrapper.$(OBJ) \
               $(PHYLIP_SRC_DIR)/cons.$(OBJ) \
               $(PHYLIP_SRC_DIR)/dnapars.$(OBJ) \
               $(PHYLIP_SRC_DIR)/phylip.$(OBJ) \
               $(PHYLIP_SRC_DIR)/seq.$(OBJ)

LVB_LIB_OBJS_OUTPUT = $(LVB_LIB_OBJS)

# Object files that are used directly and will not go into the library

LVB_PROG_OBJS = main.$(OBJ)

# Programmer's documentation files

DOCS_PROGRAMMER = $(TEST_MANUAL) \
                  $(DOCS_PROG_DIR)/main.html \
                  $(DOCS_PROG_DIR)/treestack.html \
		  $(DOCS_PROG_DIR)/cleanup.html \
		  $(DOCS_PROG_DIR)/datops.html \
		  $(DOCS_PROG_DIR)/err.html \
		  $(DOCS_PROG_DIR)/fops.html \
		  $(DOCS_PROG_DIR)/getparam.html \
		  $(DOCS_PROG_DIR)/admin.html \
		  $(DOCS_PROG_DIR)/mops.html \
		  $(DOCS_PROG_DIR)/mymaths.html \
		  $(DOCS_PROG_DIR)/myuni.html \
		  $(DOCS_PROG_DIR)/parsim.html \
		  $(DOCS_PROG_DIR)/randpint.html \
		  $(DOCS_PROG_DIR)/selfconf.html \
		  $(DOCS_PROG_DIR)/solve.html \
		  $(DOCS_PROG_DIR)/sops.html \
		  $(DOCS_PROG_DIR)/trops.html \
		  $(DOCS_PROG_DIR)/wrapper.html

TEST_MANUAL = $(DOCS_PROG_DIR)/go.html

# All documentation files

DOCS = $(DOCS_PROGRAMMER) \
       $(TEST_MANUAL) \

# Default rule for documentation files (not suitable for user's
# documentation files, which are derived from *.pod not *.c, or for
# go.html, which is derived from a different directory)

%.html : $(LVB_LIB)
	pod2html $(LVB_DIR)/$(notdir $*).c > $@

all : lvb	# allow 'gmake all' as synonym for 'gmake lvb'

lvb : LVB_PROG $(DOCS)

LVB_PROG : $(LVB_LIB) $(LVB_PROG_OBJS)
	$(CC) -o $(LVB_PROG) $(CFLAGS) $(LVB_PROG_OBJS) $(LIBS)

$(LVB_LIB) : $(LVB_LIB_OBJS)
	ar rv $@ $(LVB_LIB_OBJS_OUTPUT)
	$(RANLIB) $(LVB_LIB)

# If the main test script has changed, we should run the tests
$(TEST_MANUAL) : $(TEST_DIR)/go
	pod2html $< >$@

tests : FORCE
	cd tests ; env LVB_EXECUTABLE="`pwd`/../$(LVB_PROG)" LVB_LIBRARY="`pwd`/../$(LVB_DIR)/$(LVB_LIB)" LVB_OTHERLIBS="$(NON_LVB_LIBS)" LVB_HEADER_PATH=".." CC="$(CC)" CFLAGS="$(CFLAGS)" ./go ; cd ..

# make clean to remove files generated by a previous make, leaving
# source files
clean : FORCE
	rm -f $(LVB_DIR)/$(LVB_PROG) $(LVB_DIR)/$(LVB_PROG).exe \
	$(LVB_LIB) \
	$(LVB_LIB_OBJS) \
	$(LVB_PROG_OBJS) \
	$(DOCS)

FORCE:
