
######################################################################
# Makefile template for simple projects.  Just fill in the blanks and
# add or adjust variables as needed.  Remove unnecessary lines if
# desired.
#
# To override any variables conditionally set (with ?=), run
#
#       make VAR=value
# e.g.
#       make PREFIX=/opt/local CC=gcc CFLAGS=-O2 LFLAGS1="-L/usr/X11R6 -lX11"
#
# Author:
#    Jason Bacon
#         
######################################################################

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

LIB1    = libroboctl.a
LIBS    = ${LIB1}

HEADERS = rct_machdep.h rct_nxt.h rct_nxt_output.h \
	rct_protos.h rct_rcx.h rct_pic.h roboctl.h

MAN3    = roboctl.3

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

OBJS1   = rct.o nxt.o nxt_direct_cmd.o nxt_system_cmd.o \
	    rcx.o usb.o brick.o get_home_dir.o debug.o pic.o vex.o
OBJS    = ${OBJS1}

#####################################
# Compile, link, and install options

PREFIX          ?= /usr/local
LOCALBASE       ?= /usr/local
MANPREFIX       ?= /usr/local

CC      ?= cc
INCLUDES?= -I. -I${LOCALBASE}/include
#CFLAGS  ?= -O -pipe -Wall
CFLAGS  = -g -pipe -Wall
CFLAGS  += ${INCLUDES}

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


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

all:    ${BINS} ${LIBS}

# Link rules
${LIB1}:        ${OBJS1}
	${AR} r ${LIB1} ${OBJS1}
	${RANLIB} ${LIB1}

############################################################################
# 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} -MM ${INCLUDES} $${file} >> Makefile.depend; \
		${PRINTF} "\t\$${CC} -c \$${CFLAGS} $${file}\n\n" >> Makefile.depend; \
	done


############################################################################
# Generate a header containing prototypes for C files

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

# Remove generated files (objs and nroff output from man pages)
clean:
	rm -f ${OBJS} ${BINS} ${LIBS} *.nr

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

dox:
	doxygen

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

uninstall:
	${RM} -rf ${PREFIX}/include/roboctl
	@for file in ${LIBS}; do \
		${RM} ${PREFIX}/lib/$${file}; \
	done
	@for file in ${MAN3}; do \
		${RM} ${MANPREFIX}/man/man3/$${file}; \
	done
