# **********************************************************************
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.net
# *
# * PostGIS is free software: you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation, either version 2 of the License, or
# * (at your option) any later version.
# *
# * PostGIS is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
# *
# **********************************************************************
# *
# * Copyright 2020 Regina Obe
# * Copyright 2008 Mark Cave-Ayland
# *
# **********************************************************************

POSTGIS_PGSQL_VERSION=@POSTGIS_PGSQL_VERSION@
PROTOC_VERSION=@PROTOC_VERSION@
MODULE_big=postgis-@POSTGIS_MAJOR_VERSION@
MODULEDIR=contrib/postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULEPATH    = $$libdir/postgis-@POSTGIS_MAJOR_VERSION@

ifeq (@LIBINCLUDEMINORVERSION@,yes)
MODULEPATH=$$libdir/postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULE_big=postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
endif

# Files to be copied to the contrib/ directory
SQL_built=postgis.sql uninstall_postgis.sql postgis_upgrade.sql legacy.sql uninstall_legacy.sql legacy_minimal.sql legacy_gist.sql
DATA=../spatial_ref_sys.sql

# SQL objects (files requiring pre-processing)
SQL_OBJS = \
	postgis.sql \
	legacy.sql \
	legacy_minimal.sql

DATA_built=$(SQL_built)

ifeq (@HAVE_SPGIST@,yes)
SPGIST_OBJ= gserialized_spgist_2d.o gserialized_spgist_3d.o gserialized_spgist_nd.o
endif

WAGYU_LIBPATH = ../deps/wagyu/@WAGYU_LIB@

ifeq (@HAVE_PROTOBUF@,yes)
PROTOBUF_OBJ = vector_tile.pb-c.o geobuf.pb-c.o
UTHASH_INCLUDE = -I@top_srcdir@/deps/uthash/include

WAYGU_INCLUDE = -I@top_srcdir@/deps/wagyu
WAYGU_LIB = $(WAGYU_LIBPATH) @WAGYU_LDFLAGS@
WAGYU_DEP = $(WAGYU_LIBPATH)
endif

FLATGEOBUF_LIBPATH = ../deps/flatgeobuf/@FLATGEOBUF_LIB@
FLATGEOBUF_INCLUDE = -I@top_srcdir@/deps/flatgeobuf
FLATGEOBUF_LIB = $(FLATGEOBUF_LIBPATH) @FLATGEOBUF_LDFLAGS@
FLATGEOBUF_DEP = $(FLATGEOBUF_LIBPATH)

# SQL preprocessor
SQLPP = @SQLPP@

# PostgreSQL objects
PG_OBJS= \
	postgis_module.o \
	lwgeom_accum.o \
	lwgeom_union.o \
	lwgeom_spheroid.o \
	lwgeom_ogc.o \
	lwgeom_functions_analytic.o \
	lwgeom_functions_basic.o \
	lwgeom_inout.o \
	lwgeom_btree.o \
	lwgeom_box.o \
	lwgeom_box3d.o \
	lwgeom_geos.o \
	lwgeom_geos_predicates.o \
	lwgeom_geos_prepared.o \
	lwgeom_geos_clean.o \
	lwgeom_geos_relatematch.o \
	lwgeom_generate_grid.o \
	lwgeom_export.o \
	lwgeom_in_gml.o \
	lwgeom_in_kml.o \
	lwgeom_in_marc21.o \
	lwgeom_out_marc21.o \
	lwgeom_in_geohash.o \
	lwgeom_in_geojson.o \
	lwgeom_in_encoded_polyline.o \
	lwgeom_triggers.o \
	lwgeom_dump.o \
	lwgeom_dumppoints.o \
	lwgeom_functions_lrs.o \
	lwgeom_functions_temporal.o \
	lwgeom_rectree.o \
	lwgeom_itree.o \
	lwgeom_sqlmm.o \
	lwgeom_transform.o \
	lwgeom_window.o \
	gserialized_typmod.o \
	gserialized_gist_2d.o \
	gserialized_gist_nd.o \
	gserialized_supportfn.o \
	$(SPGIST_OBJ) \
	brin_2d.o \
	brin_nd.o \
	brin_common.o \
	gserialized_estimate.o \
	geography_inout.o \
	geography_btree.o \
	geography_centroid.o \
	geography_measurement.o \
	geography_measurement_trees.o \
	geometry_inout.o \
	postgis_libprotobuf.o \
	$(PROTOBUF_OBJ) \
	mvt.o \
	lwgeom_out_mvt.o \
	geobuf.o \
	lwgeom_out_geobuf.o \
	lwgeom_out_geojson.o \
	flatgeobuf.o \
	lwgeom_in_flatgeobuf.o \
	lwgeom_out_flatgeobuf.o \
	lwgeom_remove_irrelevant_points_for_view.o \
	lwgeom_remove_small_parts.o \
	postgis_legacy.o

# Objects to build using PGXS
OBJS=$(PG_OBJS)

# Libraries to link into the module (proj, geos)
#
# Note: we specify liblwgeom.a directly in SHLIB_LINK rather than using
# -L... -l options to prevent issues with some platforms trying to link
# to an existing liblwgeom.so in the PostgreSQL $libdir supplied by an
# older version of PostGIS, rather than with the static liblwgeom.a
# supplied with newer versions of PostGIS
PG_CPPFLAGS += -I@top_srcdir@/liblwgeom -I@top_builddir@/liblwgeom @CFLAGS@ -I@top_srcdir@/libpgcommon $(FLATGEOBUF_INCLUDE) $(WAYGU_INCLUDE) $(UTHASH_INCLUDE) @CPPFLAGS@ @PICFLAGS@
SHLIB_LINK_F = $(WAYGU_LIB) $(FLATGEOBUF_LIB) ../libpgcommon/libpgcommon.a ../liblwgeom/.libs/liblwgeom.a @SHLIB_LINK@

# Extra files to remove during 'make clean'
EXTRA_CLEAN=$(SQL_OBJS) \
	uninstall_postgis.sql \
	uninstall_legacy.sql \
	uninstall_sfcgal.sql \
	postgis_upgrade.sql.in \
	postgis_upgrade.sql \
	vector_tile.pb-c.c \
	vector_tile.pb-c.h \
	geobuf.pb-c.c \
	geobuf.pb-c.h

# PGXS information
PG_CONFIG := @PG_CONFIG@
PGXS := @PGXS@
# NO_TEMP_INSTALL is a workaround for a 9.5dev bug. See:
# http://www.postgresql.org/message-id/CAB7nPqTsR5o3g-fBi6jbsVdhfPiLFWQ_0cGU5=94Rv_8W3qvFA@mail.gmail.com
NO_TEMP_INSTALL=yes
include $(PGXS)

srcdir := @srcdir@
VPATH := $(srcdir)

# Set PERL _after_ the include of PGXS
PERL=@PERL@

# This is to workaround a bug in PGXS 8.4 win32 link line,
# see http://trac.osgeo.org/postgis/ticket/1158#comment:57
SHLIB_LINK := $(SHLIB_LINK_F) $(SHLIB_LINK)

# NOTE: setting LDFLAGS explicitly here avoids getting the
# link line polluted by unrelated PostgreSQL switches that
# may be confusing and out-of-control for the builder of
# PostGIS
LDFLAGS = @LDFLAGS@

# If REGRESS=1 passed as a parameter, change the default install paths
# so that no prefix is included. This allows us to relocate to a temporary
# directory for regression testing.
ifeq ($(REGRESS),1)
	bindir=/bin
	pkglibdir=/lib
	datadir=/share
	datamoduledir=contrib/postgis
endif

# Make all PostGIS objects depend upon liblwgeom, so that if an underlying
# change is made, a PostGIS rebuild is triggered.
#
# Also they are all dependent on postgis_config.h
# and thus postgis_revision.h
#
$(PG_OBJS): ../liblwgeom/.libs/liblwgeom.a ../libpgcommon/libpgcommon.a ../postgis_config.h ../postgis_revision.h $(WAGYU_DEP) $(FLATGEOBUF_DEP)

../postgis_revision.h:
	$(MAKE) -C .. postgis_revision.h

../liblwgeom/.libs/liblwgeom.a:
	$(MAKE) -C ../liblwgeom liblwgeom.la

../libpgcommon/libpgcommon.a:
	$(MAKE) -C ../libpgcommon libpgcommon.a

$(WAGYU_LIBPATH):
	$(MAKE) -C ../deps/wagyu all

$(FLATGEOBUF_LIBPATH):
	$(MAKE) -C ../deps/flatgeobuf all

ifeq (@HAVE_PROTOBUF@,yes)

# Get protobuf-c compiler command
PROTOCC=@PROTOCC@
lwgeom_out_mvt.o mvt.o: vector_tile.pb-c.h
lwgeom_out_geobuf.o geobuf.o: geobuf.pb-c.h
vector_tile.pb-c.h: vector_tile.pb-c.c
geobuf.pb-c.h: geobuf.pb-c.c

# Generate Mapbox Vector Tile and Geobuf encoder/decoders using protobuf-c compiler
%.pb-c.c: %.proto
	$(PROTOCC) -I@srcdir@ --c_out=. $<

endif

# Borrow the $libdir substitution from PGXS but customise by running the preprocessor
# and adding the version number
# replace @extschema@. with nothing, this is only used as placeholder for extension install
#
# TODO: create an intermediate product with @extschema@ in place, to reduce rules duplications
#       in the extension/ folder
#
%.sql: %.sql.in
	$(SQLPP) -I@top_srcdir@/libpgcommon -I@builddir@ $< > $@.tmp
	grep -v '^#' $@.tmp | $(PERL) -lpe "s'MODULE_PATHNAME'\$(MODULEPATH)'g;s'@extschema@\.''g" > $@
	rm -f $@.tmp

#this is redundant but trying to fold in with extension just hangs
postgis_upgrade.sql.in: postgis.sql ../utils/create_upgrade.pl
	$(PERL) @top_srcdir@/utils/create_upgrade.pl $< > $@


postgis_upgrade.sql: common_before_upgrade.sql postgis_before_upgrade.sql postgis_upgrade.sql.in postgis_after_upgrade.sql common_after_upgrade.sql
	echo "BEGIN;" > $@
	cat $^ >> $@
	echo "COMMIT;" >> $@

# SQL objects are also dependent on postgis_config.h for PostgreSQL version
$(SQL_OBJS): ../postgis_config.h ../postgis_revision.h

postgis.sql: sqldefines.h geography.sql.in postgis_brin.sql.in postgis_spgist.sql.in postgis_letters.sql

uninstall_postgis.sql: postgis.sql ../utils/create_uninstall.pl
	$(PERL) @top_srcdir@/utils/create_uninstall.pl $< $(POSTGIS_PGSQL_VERSION) > $@

uninstall_legacy.sql: legacy.sql ../utils/create_uninstall.pl
	$(PERL) @top_srcdir@/utils/create_uninstall.pl $< $(POSTGIS_PGSQL_VERSION) > $@

distclean: clean
	rm -f Makefile
	rm -f sqldefines.h

check-unit:

check-regress:

maintainer-clean: distclean
