# -*-makefile-*-
# src/Makefile.global.in
# @configure_input@

#------------------------------------------------------------------------------
# All PostgreSQL makefiles include this file and use the variables it sets,
# which in turn are put here by the configure script. There is no need for
# users to edit this file -- if it turns out to be necessary then that's a
# bug.
#
# A makefile that includes this file needs to set the variable `subdir' to
# the relative path from the top to itself and `top_builddir' to the relative
# path from itself to the top before including this file. (The "top" is the
# parent directory of the directory this file is in.)
#------------------------------------------------------------------------------


##########################################################################
#
# Meta configuration

standard_targets = all install installdirs uninstall distprep clean distclean maintainer-clean coverage check installcheck init-po update-po
# these targets should recurse even into subdirectories not being built:
standard_always_targets = distprep clean distclean maintainer-clean

.PHONY: $(standard_targets) install-strip html man installcheck-parallel maintainer-check

# make `all' the default target
all:

# Delete target files if the command fails after it has
# started to update the file.
.DELETE_ON_ERROR:

# Never delete any intermediate files automatically.
.SECONDARY:

# PostgreSQL version number
VERSION = @PACKAGE_VERSION@
MAJORVERSION = @PG_MAJORVERSION@
VERSION_NUM = @PG_VERSION_NUM@

# Set top_srcdir, srcdir, and VPATH.
ifdef PGXS
top_srcdir = $(top_builddir)

# If VPATH is set or Makefile is not in current directory we are building
# the extension with VPATH so we set the variable here.
ifdef VPATH
srcdir = $(VPATH)
else
ifeq ($(CURDIR),$(dir $(firstword $(MAKEFILE_LIST))))
srcdir = .
VPATH =
else
srcdir = $(dir $(firstword $(MAKEFILE_LIST)))
VPATH = $(srcdir)
endif
endif
else # not PGXS
vpath_build = @vpath_build@
abs_top_builddir = @abs_top_builddir@
abs_top_srcdir = @abs_top_srcdir@

ifneq ($(vpath_build),yes)
top_srcdir = $(top_builddir)
srcdir = .
else # vpath_build = yes
top_srcdir = $(abs_top_srcdir)
srcdir = $(top_srcdir)/$(subdir)
VPATH = $(srcdir)
endif
endif # not PGXS

vpathsearch = `for f in $(addsuffix /$(1),$(subst :, ,. $(VPATH))); do test -r $$f && echo $$f && break; done`

# Saved arguments from configure
configure_args = @configure_args@


##########################################################################
#
# Installation directories
#
# These are set by the equivalent --xxxdir configure options.  We
# append "postgresql" to some of them, if the string does not already
# contain "pgsql" or "postgres", in order to avoid directory clutter.
#
# In a PGXS build, we cannot use the values inserted into Makefile.global
# by configure, since the installation tree may have been relocated.
# Instead get the path values from pg_config.

ifndef PGXS

# Note that prefix, exec_prefix, and datarootdir aren't defined in a PGXS build;
# makefiles may only use the derived variables such as bindir.

prefix := @prefix@
exec_prefix := @exec_prefix@
datarootdir := @datarootdir@

bindir := @bindir@

datadir := @datadir@
ifeq "$(findstring pgsql, $(datadir))" ""
ifeq "$(findstring postgres, $(datadir))" ""
override datadir := $(datadir)/postgresql
endif
endif

sysconfdir := @sysconfdir@
ifeq "$(findstring pgsql, $(sysconfdir))" ""
ifeq "$(findstring postgres, $(sysconfdir))" ""
override sysconfdir := $(sysconfdir)/postgresql
endif
endif

libdir := @libdir@

pkglibdir = $(libdir)
ifeq "$(findstring pgsql, $(pkglibdir))" ""
ifeq "$(findstring postgres, $(pkglibdir))" ""
override pkglibdir := $(pkglibdir)/postgresql
endif
endif

includedir := @includedir@

pkgincludedir = $(includedir)
ifeq "$(findstring pgsql, $(pkgincludedir))" ""
ifeq "$(findstring postgres, $(pkgincludedir))" ""
override pkgincludedir := $(pkgincludedir)/postgresql
endif
endif

mandir := @mandir@

docdir := @docdir@
ifeq "$(findstring pgsql, $(docdir))" ""
ifeq "$(findstring postgres, $(docdir))" ""
override docdir := $(docdir)/postgresql
endif
endif

htmldir := @htmldir@

localedir := @localedir@

else # PGXS case

# Extension makefiles should set PG_CONFIG, but older ones might not
ifndef PG_CONFIG
PG_CONFIG = pg_config
endif

bindir := $(shell $(PG_CONFIG) --bindir)
datadir := $(shell $(PG_CONFIG) --sharedir)
sysconfdir := $(shell $(PG_CONFIG) --sysconfdir)
libdir := $(shell $(PG_CONFIG) --libdir)
pkglibdir := $(shell $(PG_CONFIG) --pkglibdir)
includedir := $(shell $(PG_CONFIG) --includedir)
pkgincludedir := $(shell $(PG_CONFIG) --pkgincludedir)
mandir := $(shell $(PG_CONFIG) --mandir)
docdir := $(shell $(PG_CONFIG) --docdir)
localedir := $(shell $(PG_CONFIG) --localedir)

endif # PGXS

# These derived path variables aren't separately configurable.

includedir_server = $(pkgincludedir)/server
includedir_internal = $(pkgincludedir)/internal
pgxsdir = $(pkglibdir)/pgxs


##########################################################################
#
# Features
#
# Records the choice of the various --enable-xxx and --with-xxx options.

with_perl	= @with_perl@
with_python	= @with_python@
with_tcl	= @with_tcl@
with_openssl	= @with_openssl@
with_selinux	= @with_selinux@
with_libxml	= @with_libxml@
with_libxslt	= @with_libxslt@
with_system_tzdata = @with_system_tzdata@
with_uuid	= @with_uuid@
with_zlib	= @with_zlib@
enable_rpath	= @enable_rpath@
enable_nls	= @enable_nls@
enable_debug	= @enable_debug@
enable_dtrace	= @enable_dtrace@
enable_coverage	= @enable_coverage@
enable_tap_tests	= @enable_tap_tests@
enable_thread_safety	= @enable_thread_safety@

python_includespec	= @python_includespec@
python_libdir		= @python_libdir@
python_libspec		= @python_libspec@
python_additional_libs	= @python_additional_libs@
python_majorversion	= @python_majorversion@
python_version		= @python_version@

krb_srvtab = @krb_srvtab@

TCLSH			= @TCLSH@
TCL_LIBS		= @TCL_LIBS@
TCL_LIB_SPEC		= @TCL_LIB_SPEC@
TCL_INCLUDE_SPEC	= @TCL_INCLUDE_SPEC@
TCL_SHARED_BUILD	= @TCL_SHARED_BUILD@
TCL_SHLIB_LD_LIBS	= @TCL_SHLIB_LD_LIBS@

PTHREAD_CFLAGS		= @PTHREAD_CFLAGS@
PTHREAD_LIBS		= @PTHREAD_LIBS@


##########################################################################
#
# Programs and flags

# Compilers

CPP = @CPP@
CPPFLAGS = @CPPFLAGS@

ifdef PGXS
override CPPFLAGS := -I$(includedir_server) -I$(includedir_internal) $(CPPFLAGS)
else # not PGXS
override CPPFLAGS := -I$(top_srcdir)/src/include $(CPPFLAGS)
ifdef VPATH
override CPPFLAGS := -I$(top_builddir)/src/include $(CPPFLAGS)
endif
endif # not PGXS

CC = @CC@
GCC = @GCC@
SUN_STUDIO_CC = @SUN_STUDIO_CC@
CFLAGS = @CFLAGS@
CFLAGS_VECTOR = @CFLAGS_VECTOR@
CFLAGS_SSE42 = @CFLAGS_SSE42@

# Kind-of compilers

BISON = @BISON@
BISONFLAGS = @BISONFLAGS@ $(YFLAGS)
FLEX = @FLEX@
FLEXFLAGS = @FLEXFLAGS@ $(LFLAGS)
DTRACE = @DTRACE@
DTRACEFLAGS = @DTRACEFLAGS@
ZIC = @ZIC@

# Linking

AR = @AR@
DLLTOOL = @DLLTOOL@
DLLWRAP = @DLLWRAP@
LIBS = @LIBS@
LDAP_LIBS_FE = @LDAP_LIBS_FE@
LDAP_LIBS_BE = @LDAP_LIBS_BE@
UUID_LIBS = @UUID_LIBS@
UUID_EXTRA_OBJS = @UUID_EXTRA_OBJS@
LD = @LD@
with_gnu_ld = @with_gnu_ld@
ld_R_works = @ld_R_works@

# We want -L for libpgport.a and libpgcommon.a to be first in LDFLAGS.  We
# also need LDFLAGS to be a "recursively expanded" variable, else adjustments
# to rpathdir don't work right.  So we must NOT do LDFLAGS := something,
# meaning this has to be done first and elsewhere we must only do LDFLAGS +=
# something.
ifdef PGXS
  LDFLAGS = -L$(libdir)
else
  LDFLAGS = -L$(top_builddir)/src/port -L$(top_builddir)/src/common
endif
LDFLAGS += @LDFLAGS@

LDFLAGS_EX = @LDFLAGS_EX@
# LDFLAGS_SL might have already been assigned by calling makefile
LDFLAGS_SL += @LDFLAGS_SL@
LDREL = -r
LDOUT = -o
RANLIB = @RANLIB@
WINDRES = @WINDRES@
X = @EXEEXT@

# Perl

ifneq (@PERL@,)
    # quoted to protect pathname with spaces
    PERL		= '@PERL@'
else
    PERL		= $(missing) perl
endif
perl_archlibexp		= @perl_archlibexp@
perl_privlibexp		= @perl_privlibexp@
perl_embed_ldflags	= @perl_embed_ldflags@

# Miscellaneous

AWK	= @AWK@
LN_S	= @LN_S@
MSGFMT  = @MSGFMT@
MSGFMT_FLAGS = @MSGFMT_FLAGS@
MSGMERGE = @MSGMERGE@
PYTHON	= @PYTHON@
TAR	= @TAR@
XGETTEXT = @XGETTEXT@

GZIP	= gzip
BZIP2	= bzip2

# Testing

check: temp-install

.PHONY: temp-install
temp-install:
ifndef NO_TEMP_INSTALL
ifeq ($(MAKELEVEL),0)
	rm -rf '$(abs_top_builddir)'/tmp_install
	$(MKDIR_P) '$(abs_top_builddir)'/tmp_install/log
	$(MAKE) -C '$(top_builddir)' DESTDIR='$(abs_top_builddir)'/tmp_install install >'$(abs_top_builddir)'/tmp_install/log/install.log 2>&1
endif
	$(if $(EXTRA_INSTALL),for extra in $(EXTRA_INSTALL); do $(MAKE) -C '$(top_builddir)'/$$extra DESTDIR='$(abs_top_builddir)'/tmp_install install >>'$(abs_top_builddir)'/tmp_install/log/install.log || exit; done)
endif

PROVE = @PROVE@
PG_PROVE_FLAGS = -I $(top_srcdir)/src/test/perl/
PROVE_FLAGS = --verbose

# prepend to path if already set, else just set it
define add_to_path
$(1)="$(if $($(1)),$(2):$$$(1),$(2))"
endef

# platform-specific environment variable to set shared library path
define ld_library_path_var
$(if $(filter $(PORTNAME),darwin),DYLD_LIBRARY_PATH,$(if $(filter $(PORTNAME),aix),LIBPATH,LD_LIBRARY_PATH))
endef

define with_temp_install
PATH="$(abs_top_builddir)/tmp_install$(bindir):$$PATH" $(call add_to_path,$(ld_library_path_var),$(abs_top_builddir)/tmp_install$(libdir))
endef

ifeq ($(enable_tap_tests),yes)

define prove_installcheck
cd $(srcdir) && TESTDIR='$(CURDIR)' PATH="$(bindir):$$PATH" PGPORT='6$(DEF_PGPORT)' top_builddir='$(CURDIR)/$(top_builddir)' PG_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) t/*.pl
endef

define prove_check
rm -rf $(CURDIR)/tmp_check/log
cd $(srcdir) && TESTDIR='$(CURDIR)' $(with_temp_install) PGPORT='6$(DEF_PGPORT)' PG_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) t/*.pl
endef

else
prove_installcheck = @echo "TAP tests not enabled"
prove_check = $(prove_installcheck)
endif

# Installation.

install_bin = @install_bin@
install_sh = $(SHELL) $(top_srcdir)/config/install-sh -c
INSTALL = $(if $(use_install_sh),$(install_sh),$(if $(install_bin),$(install_bin),$(install_sh)))

INSTALL_SCRIPT_MODE	= 755
INSTALL_DATA_MODE	= 644
INSTALL_PROGRAM	= $(INSTALL_PROGRAM_ENV) $(INSTALL) $(INSTALL_STRIP_FLAG)
INSTALL_SCRIPT	= $(INSTALL) -m $(INSTALL_SCRIPT_MODE)
INSTALL_DATA	= $(INSTALL) -m $(INSTALL_DATA_MODE)
INSTALL_STLIB	= $(INSTALL_STLIB_ENV) $(INSTALL_DATA) $(INSTALL_STRIP_FLAG)
INSTALL_SHLIB	= $(INSTALL_SHLIB_ENV) $(INSTALL) $(INSTALL_SHLIB_OPTS) $(INSTALL_STRIP_FLAG)
# Override in Makefile.port if necessary
INSTALL_SHLIB_OPTS = -m 755

MKDIR_P = @MKDIR_P@

missing		= $(SHELL) $(top_srcdir)/config/missing

STRIP		= @STRIP@
STRIP_STATIC_LIB = @STRIP_STATIC_LIB@
STRIP_SHARED_LIB = @STRIP_SHARED_LIB@

# Documentation

have_docbook	= @have_docbook@
COLLATEINDEX	= @COLLATEINDEX@
DBTOEPUB	= @DBTOEPUB@
DOCBOOKSTYLE	= @DOCBOOKSTYLE@
JADE			= @JADE@
NSGMLS			= @NSGMLS@
OSX				= @OSX@
XMLLINT			= @XMLLINT@
XSLTPROC		= @XSLTPROC@

# Code coverage

GCOV = @GCOV@
LCOV = @LCOV@
GENHTML = @GENHTML@

ifeq ($(enable_coverage),yes)
# ccache loses .gcno files
export CCACHE_DISABLE = 1
endif

# Feature settings

DEF_PGPORT = @default_port@
WANTED_LANGUAGES = @WANTED_LANGUAGES@


##########################################################################
#
# Additional platform-specific settings
#

# Name of the "template"
PORTNAME= @PORTNAME@

build_os = @build_os@

host_tuple = @host@
host_os = @host_os@
host_cpu = @host_cpu@

# Make HAVE_IPV6 available for initdb script creation
HAVE_IPV6= @HAVE_IPV6@

# This is mainly for use on FreeBSD, where we have both a.out and elf
# systems now.  May be applicable to other systems to?
ELF_SYSTEM= @ELF_SYS@

# Backend stack size limit has to be hard-wired on Windows (it's in bytes)
WIN32_STACK_RLIMIT=4194304

# Set if we have a working win32 crashdump header
have_win32_dbghelp = @have_win32_dbghelp@

# Pull in platform-specific magic
include $(top_builddir)/src/Makefile.port

# Set up rpath if enabled.  By default it will point to our libdir,
# but individual Makefiles can force other rpath paths if needed.
rpathdir = $(libdir)

ifeq ($(enable_rpath), yes)
LDFLAGS += $(rpath)
endif


##########################################################################
#
# Some variables needed to find some client interfaces

ifdef PGXS
# some contribs assumes headers and libs are in the source tree...
libpq_srcdir = $(includedir)
libpq_builddir = $(libdir)
else
libpq_srcdir = $(top_srcdir)/src/interfaces/libpq
libpq_builddir = $(top_builddir)/src/interfaces/libpq
endif

# This macro is for use by libraries linking to libpq.  (Because libpgport
# isn't created with the same link flags as libpq, it can't be used.)
libpq = -L$(libpq_builddir) -lpq

# This macro is for use by client executables (not libraries) that use libpq.
# We force clients to pull symbols from the non-shared libraries libpgport
# and libpgcommon rather than pulling some libpgport symbols from libpq just
# because libpq uses those functions too.  This makes applications less
# dependent on changes in libpq's usage of pgport.  To do this we link to
# pgport before libpq.  This does cause duplicate -lpgport's to appear
# on client link lines.
ifdef PGXS
libpq_pgport = -L$(libdir) -lpgcommon -lpgport $(libpq)
else
libpq_pgport = -L$(top_builddir)/src/common -lpgcommon -L$(top_builddir)/src/port -lpgport $(libpq)
endif

# Cygwin seems to need ldap libraries to be mentioned here, too
ifeq ($(PORTNAME),cygwin)
libpq_pgport += $(LDAP_LIBS_FE)
endif


submake-libpq:
	$(MAKE) -C $(libpq_builddir) all

submake-libpgport:
	$(MAKE) -C $(top_builddir)/src/port all
	$(MAKE) -C $(top_builddir)/src/common all

.PHONY: submake-libpq submake-libpgport


##########################################################################
#
# Testing support

PL_TESTDB = pl_regression
CONTRIB_TESTDB = contrib_regression
ifneq ($(MODULE_big),)
  CONTRIB_TESTDB_MODULE = contrib_regression_$(MODULE_big)
else
  ifneq ($(MODULES),)
    CONTRIB_TESTDB_MODULE = contrib_regression_$(MODULES)
  else
    CONTRIB_TESTDB_MODULE = contrib_regression
  endif
endif

ifdef NO_LOCALE
NOLOCALE += --no-locale
endif

pg_regress_locale_flags = $(if $(ENCODING),--encoding=$(ENCODING)) $(NOLOCALE)

pg_regress_check = $(with_temp_install) $(top_builddir)/src/test/regress/pg_regress --inputdir=$(srcdir) --temp-instance=./tmp_check --bindir= $(pg_regress_locale_flags) $(EXTRA_REGRESS_OPTS)
pg_regress_installcheck = $(top_builddir)/src/test/regress/pg_regress --inputdir=$(srcdir) --bindir='$(bindir)' $(pg_regress_locale_flags) $(EXTRA_REGRESS_OPTS)

pg_regress_clean_files = results/ regression.diffs regression.out tmp_check/ log/

pg_isolation_regress_check = $(with_temp_install) $(top_builddir)/src/test/isolation/pg_isolation_regress --inputdir=$(srcdir) --temp-instance=./tmp_check --bindir= $(pg_regress_locale_flags) $(EXTRA_REGRESS_OPTS)
pg_isolation_regress_installcheck = $(top_builddir)/src/test/isolation/pg_isolation_regress --inputdir=$(srcdir) $(pg_regress_locale_flags) $(EXTRA_REGRESS_OPTS)

##########################################################################
#
# Customization
#
# This includes your local customizations if Makefile.custom exists
# in the source directory.  This file doesn't exist in the original
# distribution so that it doesn't get overwritten when you upgrade.
#
# NOTE:  Makefile.custom is from the pre-Autoconf days of PostgreSQL.
# You are liable to shoot yourself in the foot if you use it without
# knowing exactly what you're doing.  The preferred (and more
# reliable) method is to communicate what you want to do to the
# configure script, and leave the makefiles alone.

-include $(top_srcdir)/src/Makefile.custom

ifneq ($(CUSTOM_INSTALL),)
INSTALL= $(CUSTOM_INSTALL)
endif

ifneq ($(CUSTOM_CC),)
  CC= $(CUSTOM_CC)
endif

ifneq ($(CUSTOM_COPT),)
  COPT= $(CUSTOM_COPT)
endif

ifdef COPT
   CFLAGS += $(COPT)
   LDFLAGS += $(COPT)
endif

ifdef PROFILE
   CFLAGS += $(PROFILE)
   LDFLAGS += $(PROFILE)
endif


##########################################################################
#
# substitute implementations of C library routines (see src/port/)
# note we already included -L.../src/port in LDFLAGS above

LIBOBJS = @LIBOBJS@

# files needed for the chosen CRC-32C implementation
PG_CRC32C_OBJS = @PG_CRC32C_OBJS@

LIBS := -lpgcommon -lpgport $(LIBS)

# to make ws2_32.lib the last library
ifeq ($(PORTNAME),win32)
LIBS += -lws2_32
endif

# Not really standard libc functions, used by the backend.
TAS         = @TAS@


##########################################################################
#
# Global targets and rules

%.c: %.l
ifdef FLEX
	$(FLEX) $(if $(FLEX_NO_BACKUP),-b) $(FLEXFLAGS) -o'$@' $<
	@$(if $(FLEX_NO_BACKUP),if [ `wc -l <lex.backup` -eq 1 ]; then rm lex.backup; else echo "Scanner requires backup; see lex.backup." 1>&2; exit 1; fi)
else
	@$(missing) flex $< '$@'
endif

%.c: %.y
	$(if $(BISON_CHECK_CMD),$(BISON_CHECK_CMD))
ifdef BISON
	$(BISON) $(BISONFLAGS) -o $@ $<
else
	@$(missing) bison $< $@
endif

%.i: %.c
	$(CPP) $(CPPFLAGS) -o $@ $<

%.gz: %
	$(GZIP) --best -c $< >$@

%.bz2: %
	$(BZIP2) -c $< >$@

# Direct builds of foo.c -> foo are disabled to avoid generating
# *.dSYM junk on Macs.  All builds should normally go through the
# foo.c -> foo.o -> foo steps.  This also ensures that dependency
# tracking (see below) is used.
%: %.c

ifndef PGXS

# Remake Makefile.global from Makefile.global.in if the latter
# changed. In order to trigger this rule, the including file must
# write `include $(top_builddir)/src/Makefile.global', not some
# shortcut thereof.
$(top_builddir)/src/Makefile.global: $(top_srcdir)/src/Makefile.global.in $(top_builddir)/config.status
	cd $(top_builddir) && ./config.status src/Makefile.global

# Remake pg_config.h from pg_config.h.in if the latter changed.
# config.status will not change the timestamp on pg_config.h if it
# doesn't change, so as to avoid recompiling the entire tree
# unnecessarily. Therefore we make config.status update a timestamp file
# stamp-h every time it runs, so that we don't trigger this rule every time.
# (We do trigger the null rule for stamp-h to pg_config.h every time; so it's
# important for that rule to be empty!)
#
# Of course you need to turn on dependency tracking to get any
# dependencies on pg_config.h.
$(top_builddir)/src/include/pg_config.h: $(top_builddir)/src/include/stamp-h ;

$(top_builddir)/src/include/stamp-h: $(top_srcdir)/src/include/pg_config.h.in $(top_builddir)/config.status
	cd $(top_builddir) && ./config.status src/include/pg_config.h

# Also remake pg_config_ext.h from pg_config_ext.h.in, same logic as above.
$(top_builddir)/src/include/pg_config_ext.h: $(top_builddir)/src/include/stamp-ext-h ;

$(top_builddir)/src/include/stamp-ext-h: $(top_srcdir)/src/include/pg_config_ext.h.in $(top_builddir)/config.status
	cd $(top_builddir) && ./config.status src/include/pg_config_ext.h

# Also remake ecpg_config.h from ecpg_config.h.in if the latter changed, same
# logic as above.
$(top_builddir)/src/interfaces/ecpg/include/ecpg_config.h: $(top_builddir)/src/interfaces/ecpg/include/stamp-h ;

 $(top_builddir)/src/interfaces/ecpg/include/stamp-h: $(top_builddir)/src/interfaces/ecpg/include/ecpg_config.h.in $(top_builddir)/config.status
	cd $(top_builddir) && ./config.status src/interfaces/ecpg/include/ecpg_config.h

# When configure changes, rerun configure with the same options as
# last time. To change configure, you need to run autoconf manually.
$(top_builddir)/config.status: $(top_srcdir)/configure
	cd $(top_builddir) && ./config.status --recheck

endif # not PGXS


install-strip:
# install-strip always uses install-sh, so that strip options can be
# passed.
	$(MAKE) use_install_sh=yes \
	    INSTALL_PROGRAM_ENV="STRIPPROG='$(STRIP)'" \
	    INSTALL_STLIB_ENV="STRIPPROG='$(STRIP_STATIC_LIB)'" \
	    INSTALL_SHLIB_ENV="STRIPPROG='$(STRIP_SHARED_LIB)'" \
	    INSTALL_STRIP_FLAG=-s \
	    install


##########################################################################
#
# Recursive make support
# ----------------------
# Instead of recursing through subdirectories with a for loop or
# repeated $(MAKE) -C whatever calls, this is a little smarter: it
# allows parallel make across directories and lets make -k and -q work
# correctly.

# We need the $(eval) function and order-only prerequisites, which are
# available in GNU make 3.80.  That also happens to be the version
# where the .VARIABLES variable was introduced, so this is a simple check.
ifndef .VARIABLES
$(error GNU make 3.80 or newer is required.  You are using version $(MAKE_VERSION))
endif

# This function is only for internal use below.  It should be called
# using $(eval).  It will set up a target so that it recurses into
# a given subdirectory.  Note that to avoid a nasty bug in make 3.80,
# this function has to avoid using any complicated constructs (like
# multiple targets on a line) and also not contain any lines that expand
# to more than about 200 bytes.  This is why we make it apply to just one
# subdirectory at a time, rather than to a list of subdirectories.
# $1: target name, e.g., all
# $2: subdir name
# $3: target to run in subdir, usually same as $1
define _create_recursive_target
.PHONY: $(1)-$(2)-recurse
$(1): $(1)-$(2)-recurse
$(1)-$(2)-recurse: $(if $(filter check, $(3)), temp-install)
	$$(MAKE) -C $(2) $(3)
endef
# Note that the use of $$ on the last line above is important; we want
# $(MAKE) to be evaluated when the rule is run, not when the $(eval) is run
# to create the rule.  This is necessary to get make -q working.

# Call this function in a makefile that needs to recurse into subdirectories.
# In the normal case all arguments can be defaulted.
# $1: targets to make recursive (defaults to list of standard targets)
# $2: list of subdirs (defaults to SUBDIRS variable)
# $3: target to run in subdir (defaults to current element of $1)
recurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))

# If a makefile's list of SUBDIRS varies depending on configuration, then
# any subdirectories excluded from SUBDIRS should instead be added to
# ALWAYS_SUBDIRS, and then it must call recurse_always as well as recurse.
# This ensures that distprep, distclean, etc will apply to all subdirectories.
# In the normal case all arguments will be defaulted.
# $1: targets to make recursive (defaults to standard_always_targets)
# $2: list of subdirs (defaults to ALWAYS_SUBDIRS variable)
# $3: target to run in subdir (defaults to current element of $1)
recurse_always = $(foreach target,$(if $1,$1,$(standard_always_targets)),$(foreach subdir,$(if $2,$2,$(ALWAYS_SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))


##########################################################################
#
# Automatic dependency generation
# -------------------------------
# When we configure with --enable-depend then we override the default
# compilation rule with the magic below. While or after creating the
# actual output file we also create a dependency list for the .c file.
# Next time we invoke make we will have top-notch information about
# whether this file needs to be updated. The dependency files are kept
# in the .deps subdirectory of each directory.

autodepend = @autodepend@

ifeq ($(autodepend), yes)

ifndef COMPILE.c
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
endif

DEPDIR = .deps

ifeq ($(GCC), yes)

# GCC allows us to create object and dependency file in one invocation.
%.o : %.c
	@if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi
	$(COMPILE.c) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po

endif # GCC

# Include all the dependency files generated for the current
# directory. Note that make would complain if include was called with
# no arguments.
Po_files := $(wildcard $(DEPDIR)/*.Po)
ifneq (,$(Po_files))
include $(Po_files)
endif

# hook for clean-up
clean distclean maintainer-clean: clean-deps

.PHONY: clean-deps
clean-deps:
	@rm -rf $(DEPDIR)

endif # autodepend


##########################################################################
#
# Native language support

ifeq ($(enable_nls), yes)
ifneq (,$(wildcard $(srcdir)/nls.mk))

include $(top_srcdir)/src/nls-global.mk

endif # nls.mk
endif # enable_nls


##########################################################################
#
# Coverage

# Explanation of involved files:
#   foo.c	source file
#   foo.o	object file
#   foo.gcno	gcov graph (a.k.a. "notes") file, created at compile time
#		(by gcc -ftest-coverage)
#   foo.gcda	gcov data file, created when the program is run (for
#		programs compiled with gcc -fprofile-arcs)
#   foo.c.gcov	gcov output file with coverage information, created by
#		gcov from foo.gcda (by "make coverage")
#   foo.c.gcov.out  stdout captured when foo.c.gcov is created, mildly
#		interesting
#   lcov.info	lcov tracefile, built from gcda files in one directory,
#		later collected by "make coverage-html"

ifeq ($(enable_coverage), yes)

# There is a strange interaction between lcov and existing .gcov
# output files.  Hence the rm command and the ordering dependency.

gcda_files := $(wildcard *.gcda)

lcov.info: $(gcda_files)
	rm -f *.gcov
	$(if $^,$(LCOV) -d . -c -o $@ $(LCOVFLAGS) --gcov-tool $(GCOV))

%.c.gcov: %.gcda | lcov.info
	$(GCOV) -b -f -p -o . $(GCOVFLAGS) $*.c >$*.c.gcov.out

coverage: $(gcda_files:.gcda=.c.gcov) lcov.info

.PHONY: coverage-html
coverage-html: coverage
	rm -rf coverage
	mkdir coverage
	$(GENHTML) --show-details --legend --output-directory=coverage --title=PostgreSQL --num-spaces=4 --prefix=$(abs_top_srcdir) `find . -name lcov.info -print`


# hook for clean-up
clean distclean maintainer-clean: clean-coverage

.PHONY: clean-coverage
clean-coverage:
	rm -rf coverage
	rm -f *.gcda *.gcno lcov.info *.gcov *.gcov.out


# User-callable target to reset counts between test runs
coverage-clean:
	rm -f `find . -name '*.gcda' -print`

endif # enable_coverage
