# Makefile
#
#      Makefile for src directory of Veil
#
#      Copyright (c) 2005 - 2008 Marc Munro
#      Author:  Marc Munro
#      License: BSD
#
# $Id: Makefile,v 1.3 2008/08/01 00:29:25 bloodnok Exp $
# 
# Do not attempt to use this makefile directly: its targets are available
# and should be built from the main GNUmakefile in the parent directory.
# The GNUmakefile in this directory will build using the parent GNUmakefile
# so using make <target> in this directory will work as long as you don't
# try to specify this makefile.  It even works with emacs compile and 
# next-error functions though the number of makefiles involved seems a 
# little alarming at first.
# The whole strangeness of this makefile hierarchy derives from a, 
# possibly misguided, attempt to avoid recursive make (see the article
# "Recursive make considered harmful" for a rationale).
# 

SRC_DIR = src
SRC_SOURCES = $(wildcard $(SRC_DIR)/*.c)
SRC_HEADERS = $(wildcard $(SRC_DIR)/*.h)

SRC_OBJECTS = $(SRC_SOURCES:%.c=%.o)
SRC_DEPS = $(SRC_SOURCES:%.c=%.d)
SRC_TRIALDEPS = $(SRC_SOURCES:%.c=%_trial.d)
SRC_GARBAGE = $(garbage:%=$(SRC_DIR)/%)
SRC_SQL = veil_interface.sql

TRIAL_OBJECTS = $(subst .o,_trial.o,$(SRC_OBJECTS))

SQL_SRC = $(wildcard $(SRC_DIR)/*.sqs)
SQL_INSTALLTARG = $(SQL_SRC:%.sqs=%.sql) 
SQL_TARG = $(SQL_SRC:$(SRC_DIR)/%.sqs=%.sql)
SQL_TRIALTARG = $(SQL_SRC:$(SRC_DIR)/%.sqs=%_trial.sql)

VEIL_DEBUG = 0
VEIL_DEBUG_FLAG = "-D VEIL_DEBUG=$(VEIL_DEBUG)"
CFLAGS_VEIL = $(VEIL_DEBUG_FLAG)

.PHONY: src_all src_clean veil

LIB = veil$(DLSUFFIX)
TRIAL_LIB = veil_trial$(DLSUFFIX)

# Include dependency files for each source file
include $(SRC_DEPS) $(SRC_TRIALDEPS)

# Build veil shared library from sources.
src_all:  $(LIB) $(TRIAL_LIB) $(SQL_TARG) $(SQL_INSTALLTARG) $(SQL_TRIALTARG)

$(SQL_TARG): $(SQL_SRC)
	echo "Making $@ (sqltarg)"
	@sed -e 's!@LIBDIR@!$(top_builddir)!g' <$(SQL_SRC) >$@

$(SQL_INSTALLTARG): $(SQL_SRC)
	echo "Making $@ (installtarg)"
	@sed -e 's!@LIBDIR@!$$libdir!g' <$(SQL_SRC) >$@

# veil_interfaces_trial.sql   This defines veil interfaces for the trial
# version of the veil shared library (not fully linked in with postgres)
$(SQL_TRIALTARG): $(SQL_TARG)
	@echo "Making $@ (trialtarg)"
	@sed -e 's!$(LIB)!$(TRIAL_LIB)!g' <$(SQL_TARG) >$@

# Standard veil shared library
$(LIB): $(SRC_OBJECTS)

# Same as standard objects but compiled with VEIL_TRIAL defined, for inclusion
# in TRIAL_LIB (below)

# Trial version of veil shared library (in case you can't define
# shared_preload_libraries in postgresql.conf
$(TRIAL_LIB): $(TRIAL_OBJECTS)

src_install: src_all
	$(INSTALL_SCRIPT) $(LIB) $(pgpkglibdir)
	$(INSTALL_SCRIPT) $(SQL_INSTALLTARG) $(pgsharedir)

# Remove the shared library and all generated/intermediate files
src_clean:
	@echo Cleaning src...
	@rm -f $(SRC_OBJECTS) $(TRIAL_OBJECTS) $(SRC_DEPS) $(LIB) \
	       $(SRC_SQL) $(SRC_GARBAGE) $(SQL_TARG) $(SQL_INSTALLTARG) \
	       $(SQL_TRIALTARG) $(TRIAL_LIB) $(SRC_DEPS) $(SRC_TRIALDEPS)

src_distclean: src_clean
	@rm -f src/veil_version.h src/veil_mainpage.c 
