# GNUmakefile
#
#      Makefile for src directory of Veil
#
#      Copyright (c) 2005, 2006 Marc Munro
#      Author:  Marc Munro
#      License: BSD
#
# $Id: GNUmakefile,v 1.3 2007/07/31 22:18:27 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.
# 

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_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)

# 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_OBJECTS): $(SRC_SOURCES)
	$(CC) $(CFLAGS) $(CPPFLAGS) -D VEIL_TRIAL -c -o \
		$@ $(subst _trial.o,.c,$@)

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

src_install:
	$(INSTALL_SCRIPT) $(LIB) $(pglibdir)
	$(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_distclean: src_clean
	@rm -f src/veil_version.h src/veil_mainpage.c 