# Make file for obpager


# Set the dirs for object code and dependency files
OBJDIR = ./objs
DEPDIR = ./deps


# Set the install directory for the executable
INSTALLDIR = /usr/local/bin


# Set the compilation flags and such
CXX = g++
CDEFS = -D_REENTRANT
CPPFLAGS = -ggdb -Wall
#CPPFLAGS = -ggdb -Wall -O2
INCLUDES = -I/usr/X11R6/include/X11 -I/usr/X11R6/include/X11/extensions -I./src
COMPILE = $(CXX) $(CDEFS) $(INCLUDES) $(CPPFLAGS)
LINK = $(CXX) $(LDFLAGS) $(LDLIBS)
LDLIBS = -L/usr/X11R6/lib  -lX11 -lXext



# Specify the source code files

SRCS =  src/main.cc \
		src/OBPager.cc


# Generate the object filenames and dependency filenames

OBJS = $(patsubst %.cc,$(OBJDIR)/%.o,$(SRCS))

DEPS = $(patsubst %.cc,$(DEPDIR)/%.d,$(SRCS))

EXECUTABLE = obpager


# Default build target

all: $(EXECUTABLE)


# This is the main target, really

$(EXECUTABLE): $(OBJS)
	@echo "Linking "$@ ;\
	$(LINK) -o $(EXECUTABLE) $(OBJS)

install:
	@echo "Installing obpager...." ;\
	install $(EXECUTABLE) $(INSTALLDIR)


# For deps, copy gcc's dependency output to a ".d" file in the deps dir, then append additional deps to this file.
#
# The appended deps are extracted from the base dep file by using sed to do the following:
# - delete targets ("XXXX.Y : " at start of lines)
# - delete line continuations (" \" at end of lines)
# - delete leading spaces
# - convert embedded spaces in lines to a " :\n" sequence
# - append a ':' to the end of each line
#
# Thus, lines are converted like the following:
#
# Base dep file:
#  obj/AC3DFont.o: AC3DFont.cc include/AC3DFont.h include/VerboseException.h \
#    include/AC3DTexture.h include/AC3DGLArray.h /usr/include/GL/gl.h
#
# Output of sed (appended to base dep file):
#  AC3DFont.cc :
#  include/AC3DFont.h :
#  include/VerboseException.h :
#  include/AC3DTexture.h :
#  include/AC3DGLArray.h :
#  /usr/include/GL/gl.h :
#
# The point of all this extra effort is to deal elegantly with the removal/renaming of included header files,
# which is admittedly not a big deal in general (a "make clean" fixes it); however, it was interesting to implement.
# By including these additional deps, when a header file is removed/renamed (i.e. the target file cannot
# be found), make assumes that the "missing" target has been updated, so anything depending on the target
# must also be updated as usual (see the make info section on "Force Targets" for info about rules without
# commands or prerequisites).

$(OBJDIR)/%.o : %.cc
	@echo "Compiling "$< ;\
	mkdir $(OBJDIR) 2>/dev/null ; mkdir $(OBJDIR)/src 2>/dev/null ; \
	mkdir $(DEPDIR) 2>/dev/null ; mkdir $(DEPDIR)/src 2>/dev/null ; \
	$(COMPILE) -MMD -o $@ -c $< ; \
	cp $(OBJDIR)/$*.d $(DEPDIR)/$*.d ; \
	sed -s -e 's/^[^:]\+: *//' -e 's/ *\\$$//' -e 's/^ *//' -e 's/ \+/ :\n/g' -e 's/$$/ :/' < $(OBJDIR)/$*.d >> $(DEPDIR)/$*.d ; \
	rm -f $(OBJDIR)/$*.d
	

ifneq ($(MAKECMDGOALS),clean)
-include $(SRCS:%.cc=$(DEPDIR)/%.d)
endif


# Clean up the object code and deps and executable

clean:
	@for i in $(DEPS) $(OBJS) $(EXECUTABLE) ; \
		do \
			if [ -e $$i ] ; then \
				echo "Deleting "$$i ; \
				rm -f $$i ; \
			fi ; \
		done



# Should have an install target, I guess....

