#-------------------------------------------------------------------------
#
# Makefile for backend/catalog
#
# src/backend/catalog/Makefile
#
#-------------------------------------------------------------------------

subdir = src/backend/catalog
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global

OBJS = catalog.o dependency.o heap.o index.o indexing.o namespace.o aclchk.o \
       gp_global_sequence.o gp_persistent.o \
       pg_aggregate.o pg_appendonly.o pg_constraint.o pg_conversion.o pg_depend.o \
       pg_exttable.o pg_extprotocol.o pg_filesystem.o pg_largeobject.o pg_namespace.o \
       pg_operator.o pg_proc.o pg_proc_callback.o pg_shdepend.o \
       pg_type.o toasting.o aoseg.o \
       pg_attribute_encoding.o pg_compression.o quicklz_compression.o

SUBDIRS = caql core external

BKIFILES = postgres.bki postgres.description postgres.shdescription

OTHER_TASK = cdb_init.sql cdb_util.sql cdb_schema.sql gp_toolkit.sql gp_toolkit_test.sql $(BKIFILES)

include $(top_srcdir)/src/backend/common.mk


cdb_init.sql: cdb_schema.sql filesystem.sql
	cat $(call vpathsearch,cdb_schema.sql) $(call vpathsearch,cdb_external_extensions.sql) $(call vpathsearch,filesystem.sql) > cdb_init.sql

cdb_schema.sql: cdb_schema.in
			sed -e 's,@pkglibdir@,$(pkglibdir),g' \
                -e 's,@dlsuffix@,$(DLSUFFIX),g' \
             $< > cdb_schema.sql

cdb_util.sql: cdb_util.in
			sed -e 's,@pkglibdir@,$(pkglibdir),g' \
                -e 's,@dlsuffix@,$(DLSUFFIX),g' \
             $< > cdb_util.sql

JETPACK_NAME = toolkit
JETPACK_SCHEMA = hawq_$(JETPACK_NAME)
JETPACK_OBJECT_PREFIX = hawq_
JETPACK_PREFIX = $(JETPACK_SCHEMA).$(JETPACK_OBJECT_PREFIX)
JETPACK_PREFIX_AUX = $(JETPACK_SCHEMA).__$(JETPACK_OBJECT_PREFIX)
WORKFILE_PATHNAME = $$libdir\/gp_workfile_mgr
SESSION_STATE_PATHNAME = $$libdir\/gp_session_state
OPTUTILS_PATHNAME = $$libdir\/gpoptutils

gp_toolkit.sql: gp_toolkit.sql.in
	sed \
	-e 's/%%JETPACK_NAME%%/$(JETPACK_NAME)/g' \
	-e 's/%%JETPACK_SCHEMA%%/$(JETPACK_SCHEMA)/g' \
	-e 's/%%JETPACK_PREFIX%%/$(JETPACK_PREFIX)/g' \
	-e 's/%%JETPACK_PREFIX_AUX%%/$(JETPACK_PREFIX_AUX)/g' \
	-e 's/%%WORKFILE_MODULE%%/$(WORKFILE_PATHNAME)/g' \
	-e 's/%%SESSION_STATE_MODULE%%/$(SESSION_STATE_PATHNAME)/g' \
	-e 's/%%OPTUTILS_MODULE%%/$(OPTUTILS_PATHNAME)/g' \
	-e 's/	/    /g' \
	$< > $@

gp_toolkit_test.sql: gp_toolkit_test.sql.in
	sed \
	-e 's/%%JETPACK_NAME%%/$(JETPACK_NAME)/g' \
	-e 's/%%JETPACK_SCHEMA%%/$(JETPACK_SCHEMA)/g' \
	-e 's/%%JETPACK_PREFIX%%/$(JETPACK_PREFIX)/g' \
	-e 's/%%JETPACK_PREFIX_AUX%%/$(JETPACK_PREFIX_AUX)/g' \
	-e 's/	/    /g' \
	$< > $@

# TIDYCAT_BEGIN_CODEGEN 
# WARNING: DO NOT MODIFY THE FOLLOWING SECTION: 
# Generated by ./tidycat.pl version 34
# on Wed Feb  5 22:00:04 2014


TIDYCAT_BKI_SRCS := \
	pg_attribute_encoding.h \
	pg_auth_time_constraint.h \
	pg_compression.h \
	pg_filesystem.h \
	pg_foreign_data_wrapper.h \
	pg_foreign_server.h \
	pg_foreign_table.h \
	pg_partition_encoding.h \
	pg_proc_callback.h \
	pg_remote_credentials.h \
	pg_type_encoding.h

# TIDYCAT_END_CODEGEN 

# Note: there are some undocumented dependencies on the ordering in which
# the catalog header files are assembled into postgres.bki.  In particular,
# indexing.h had better be last, and toasting.h just before it.

POSTGRES_BKI_SRCS := $(addprefix $(top_srcdir)/src/include/catalog/,\
	pg_proc.h pg_type.h pg_attribute.h pg_class.h pg_autovacuum.h \
	pg_attrdef.h pg_constraint.h pg_inherits.h pg_index.h \
	pg_operator.h pg_opclass.h pg_am.h pg_amop.h pg_amproc.h \
	pg_language.h pg_largeobject.h pg_aggregate.h pg_statistic.h \
	pg_rewrite.h pg_trigger.h pg_listener.h pg_description.h pg_cast.h \
	pg_namespace.h pg_conversion.h pg_depend.h \
	pg_database.h pg_tablespace.h pg_pltemplate.h \
	pg_authid.h pg_auth_members.h pg_shdepend.h pg_shdescription.h pg_resqueue.h \
	gp_configuration.h gp_policy.h gp_version.h \
	gp_segment_config.h gp_san_config.h \
	gp_verification_history.h \
	pg_window.h \
	pg_exttable.h pg_appendonly.h pg_appendonly_alter_column.h gp_master_mirroring.h aoseg.h \
	pg_extprotocol.h \
	pg_partition.h pg_partition_rule.h pg_filespace.h pg_filespace_entry.h \
	gp_global_sequence.h gp_persistent.h \
	$(TIDYCAT_BKI_SRCS) \
	pg_user_mapping.h toasting.h indexing.h \
    )

pg_includes = $(sort -I$(top_srcdir)/src/include -I$(top_builddir)/src/include)

# see explanation in ../parser/Makefile
postgres.description: postgres.bki ;

postgres.shdescription: postgres.bki ;

postgres.bki: genbki.sh $(POSTGRES_BKI_SRCS) \
    $(top_srcdir)/src/include/postgres_ext.h $(top_builddir)/src/include/pg_config_manual.h
	AWK='$(AWK)' $(SHELL) $< $(pg_includes) --set-version=$(VERSION) -o postgres $(POSTGRES_BKI_SRCS)

.PHONY: install-data
install-data: $(BKIFILES) installdirs
	$(INSTALL_DATA) $(call vpathsearch,postgres.bki) '$(DESTDIR)$(datadir)/postgres.bki'
	$(INSTALL_DATA) $(call vpathsearch,postgres.description) '$(DESTDIR)$(datadir)/postgres.description'
	$(INSTALL_DATA) $(call vpathsearch,postgres.shdescription) '$(DESTDIR)$(datadir)/postgres.shdescription'
	$(INSTALL_DATA) $(srcdir)/system_views.sql '$(DESTDIR)$(datadir)/system_views.sql'
	$(INSTALL_DATA) $(srcdir)/information_schema.sql '$(DESTDIR)$(datadir)/information_schema.sql'
	$(INSTALL_DATA) $(call vpathsearch,cdb_schema.sql) '$(DESTDIR)$(datadir)/cdb_schema.sql'
	$(INSTALL_DATA) $(call vpathsearch,cdb_util.sql) '$(DESTDIR)$(datadir)/cdb_util.sql'
	$(INSTALL_DATA) $(call vpathsearch,cdb_init.sql) '$(DESTDIR)$(datadir)/cdb_init.sql'
	$(INSTALL_DATA) $(call vpathsearch,cdb_external_extensions.sql) '$(DESTDIR)$(datadir)/cdb_external_extensions.sql'
	$(INSTALL_DATA) $(call vpathsearch,filesystem.sql) '$(DESTDIR)$(datadir)/filesystem.sql'
	$(INSTALL_DATA) $(srcdir)/sql_features.txt '$(DESTDIR)$(datadir)/sql_features.txt'
	$(INSTALL_DATA) $(call vpathsearch,gp_toolkit.sql) '$(DESTDIR)$(datadir)/gp_toolkit.sql'

installdirs:
	$(MKDIR_P) '$(DESTDIR)$(datadir)'

.PHONY: uninstall-data
uninstall-data:
	rm -f $(addprefix '$(DESTDIR)$(datadir)'/, $(BKIFILES) system_views.sql information_schema.sql cdb_schema.sql sql_features.txt)

clean:
	rm -f SUBSYS.o $(OBJS) $(BKIFILES) cdb_schema.sql gp_toolkit_test.sql gp_toolkit.sql cdb_util.sql cdb_init.sql
