# $PostgresPy: pl/Makefile,v 1.21 2004/07/10 15:53:06 flaw Exp $
#
# † Instrument:
#     Copyright 2004, rhid development. All Rights Reserved.
#     
#     Usage of the works is permitted provided that this
#     instrument is retained with the works, so that any entity
#     that uses the works is notified of this instrument.
#     
#     DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
#     
#     [2004, Fair License; rhid.com/fair]
#     
# Description:
#    A vanilla Makefile that should work with just about every
#    modern UNIX system.
##
HELP="help menu"

DLLSUFFIX=so

VERSION!=cat 2>&- VERSION || echo
PLPYLIB=plpy$(VERSION).$(DLLSUFFIX)

.ifndef PG
PG=pg_config
.endif

.ifndef PY
PY=python
.endif

PREFIX=/usr/local

PGINC!=$(PG) --includedir-server
PGLIB!=$(PG) --libdir
PGPKGLIB!=$(PG) --pkglibdir
PGVER!=$(PG) --version

VERPY="from sys import version_info as vi; print '%s.%s' % vi[0:2]"
INCPY="from distutils.sysconfig import get_python_inc as f; print f()"
LIBPY="from distutils.sysconfig import get_python_lib as f; print f()"

PYVER!=$(PY) -c $(VERPY)
PYINC!=$(PY) -c $(INCPY)
PYLIB!=$(PY) -c $(LIBPY)

CINCLUDED=\
-I/usr/local/include\
-I$(PYINC) -I$(PGINC)\
-Isrc/include -I$(PYINC)/postgres

.ifdef DEBUG
DFLAGS=-ggdb3 -DNDEBUG -O0
.endif

.ifdef ASSERT
ASSERT=-DUSE_ASSERT_CHECKING
.endif

# Fetch the Major and Minor version for importation of appropriate pg mod
PGV=$(PGVER:C/[^0-9]//g)
PGV_MM=$(PGV:C/([0-9]{2}).*/\1/1)

CFLAGS=-Wall -O2 $(CINCLUDED) $(DFLAGS) -DPGV_MM=$(PGV_MM)
LDFLAGS=-L$(PYLIB)/../.. -lpython$(PYVER) $(PYLIB)/postgres/_pg$(PGV_MM).so

OBJS=src/pl.c.o src/plpy.c.o

.MAIN: $(PLPYLIB)

src/pl.c.o: src/pl.c
	$(CC) $(CFLAGS) -c src/pl.c -o $(.TARGET)

src/plpy.c.o: src/plpy.c
	$(CC) $(CFLAGS) -c src/plpy.c -o $(.TARGET)

$(PLPYLIB): $(OBJS)
	ld -shared $(LDFLAGS) $(OBJS) -o $(.TARGET)

install: .MAIN
	install $(PLPYLIB) $(PGPKGLIB)

cs: .PHONY
	cscope -b -R -s src

clean:
	rm -f src/*.o $(PLPYLIB)

help: .PHONY
	@echo "$(HELP)"
