
############################################################################
# This is a template Makefile for small to medium projects.
# It is meant to serve as a starting point for creating a portable
# Makefile, suitable for use under ports systems like *BSD ports,
# MacPorts, Portage, etc.  It contains examples of most
# Makefile components you will need.  
#
# For most small projects, you should be able to create a usable
# Makefile from this template by assigning the appropriate values
# to variables like BIN1, LIB1, etc., and removing everything you
# don't need.
#
# If you have a very complex project that rquires more configurability 
# than this, consider using a Makefile generator such as GNU autotools,
# or cmake.
#
# Variables that are conditionally assigned (with ?=) can be overridden
# on the command line as follows:
#
#       make VAR=value
#
# They can also inheret values from parent Makefiles (as in *BSD ports).
# This allows different systems to use the Makefile without modifications.
# For example, MacPorts installs to /opt/local instead of /usr/local,
# and hence might use the following:
# 
#       make PREFIX=/opt/local 
#
# Different systems may also use different compilers and keep libraries in
# different locations:
#
#       make CC=gcc CFLAGS=-O2 LFLAGS1=" -L/usr/X11R6 -lX11"
#
# Variables can also be appended in the Makefile (with +=), so that
# flags specified on the command line can be combined with those in
# the Makefile.
#
# Author: Jason W. Bacon
############################################################################

############################################################################
# Files to be installed by make.

BIN1    = vexctl
BINS    = ${BIN1}

MAN1    = vexctl.1
SCRIPTS =

############################################################################
# List object files that comprise BIN1, BIN2, LIB1, LIB2, etc.

OBJS1   = vexctl.o
OBJS    = ${OBJS1}

############################################################################
# Compile, link, and install options
# Override from the command line with "make VAR=value"
# or by setting in the parent Makefile.

# Install in /usr/local, unless defined by the parent Makefile, the
# environment, or a command line option such as PREFIX=/opt/local.
PREFIX  ?= /usr/local
MANPREFIX ?= ${PREFIX}

# Where to find local libraries and headers.  For MacPorts, override
# with LOCALBASE=/opt/local.
LOCALBASE ?= /usr/local

############################################################################
# Build flags
# Override with CC=gcc, CC=icc, etc.
CC      ?= cc
#CFLAGS  ?= -O -pipe
CFLAGS  = -g -pipe
INCLUDES+= -I../../Libs/C -I${LOCALBASE}/include
CFLAGS  += -Wall ${INCLUDES}

LFLAGS1 += -L../../Libs/C -L${LOCALBASE}/lib -lroboctl ${EXTRALIBS}

############################################################################
# Assume first command in PATH.  Override with full pathnames if necessary.
# E.g. make INSTALL=/usr/local/bin/ginstall

INSTALL ?= install
LN      ?= ln
RM      ?= rm
AR      ?= ar
PRINTF  ?= printf


############################################################################
# Standard targets required by ports

all:    ${BINS}

# Link rules
${BIN1}:        ${OBJS1} ../../Libs/C/libroboctl.a
	${CC} -o ${BIN1} ${OBJS1} ${LFLAGS1}

############################################################################
# Include dependencies generated by "make depend", if they exist.
# These rules explicitly list dependencies for each object file.
# See "depend" target below.  If Makefile.depend does not exist, use
# generic source compile rules.  These have some limitations, so you
# may prefer to create explicit rules for each target file.  This can
# be done automatically using "cpp -M" or "cpp -MM".  Run "man cpp"
# for more information, or see the "depend" target below.

include Makefile.depend

############################################################################
# Self-generate dependencies the old-fashioned way

depend:
	rm -f Makefile.depend
	for file in *.c; do \
		${CPP} ${INCLUDES} -MM $${file} >> Makefile.depend; \
		${PRINTF} "\t\$${CC} -c \$${CFLAGS} $${file}\n\n" >> Makefile.depend; \
	done


############################################################################
# Generate a header containing prototypes for C files.  Requires
# the cproto command, which is freely available on the WEB.

protos:
	(cproto ${INCLUDES} *.c > temp_protos.h && mv -f temp_protos.h protos.h)


############################################################################
# Remove generated files (objs and nroff output from man pages)

clean:
	rm -f ${OBJS} ${BINS} ${LIBS} *.nr *.exe *.EXE

# Keep backup files during normal clean, but provide an option to remove them
realclean: clean
	rm -f .*.bak *.bak *.BAK *.gmon


############################################################################
# Install all target files (binaries, libraries, docs, etc.)

install: all
	mkdir -p ${PREFIX}/bin ${PREFIX}/lib ${PREFIX}/include \
		${MANPREFIX}/man/man1
	@for file in ${BINS} ; do \
	    ${INSTALL} -s -c -m 0555 $${file} ${PREFIX}/bin; \
	done
	@for file in ${SCRIPTS} ; do \
	    ${INSTALL} -c -m 0555 $${file} ${PREFIX}/bin; \
	done
	@for file in ${HEADERS} ; do \
	    ${INSTALL} -c -m 0444 $${file} ${PREFIX}/include; \
	done
	@for file in ${LIBS} ; do \
	    ${INSTALL} -c -m 0444 $${file} ${PREFIX}/lib; \
	done
	@for file in ${MAN1} ; do \
	    ${INSTALL} -c -m 0444 $${file} ${MANPREFIX}/man/man1; \
	done
	@for file in ${MAN3} ; do \
	    ${INSTALL} -c -m 0444 $${file} ${MANPREFIX}/man/man3; \
	done


############################################################################
# Remove all installed files

uninstall:
	@for file in ${BINS} ; do \
	    ${RM} ${PREFIX}/bin/$${file}; \
	done
	@for file in ${SCRIPTS} ; do \
	    ${RM} ${PREFIX}/bin/$${file}; \
	done
	@for file in ${HEADERS} ; do \
	    ${RM} ${PREFIX}/include/$${file}; \
	done
	@for file in ${LIBS} ; do \
	    ${RM} ${PREFIX}/lib/$${file}; \
	done
	@for file in ${MAN1} ; do \
	    ${RM} ${MANPREFIX}/man/man1/$${file}; \
	done
	@for file in ${MAN3} ; do \
	    ${RM} ${MANPREFIX}/man/man3/$${file}; \
	done

