#
# Copyright (c) 2012-2019 Red Hat.
# Copyright (c) 2008 Aconex.  All Rights Reserved.
# Copyright (c) 2000,2003,2004 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program 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.
#
# This program 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.
#

# Common gmake macros for building
#
# src/include/builddefs.  Generated from builddefs.in by configure.
#
ifndef _BUILDDEFS_INCLUDED_
_BUILDDEFS_INCLUDED_ = 1

ifndef _PCP_CONF_INCLUDED_
_PCP_CONF_INCLUDED_ = 1
include $(PCP_DIR)/etc/pcp.conf
endif

# General package information
PACKAGE_VERSION ?= 4.3.4
PACKAGE_MAJOR ?= 4
PACKAGE_MINOR ?= 3
PACKAGE_REVISION ?= 4
PACKAGE_BUILD ?= 1
PACKAGE_NAME ?= pcp
PACKAGE_CONFIGURE ?=  --with-dstat-symlink=no
PACKAGE_DISTRIBUTION ?= arch
SGI_CHROOT_BUILD ?= 0
SGI_ISSP_BUILD ?= 0

# see build/containers/GNUlocalrules and build/containers/*/GNUmakefile
CONTAINER_VERSION = $(PACKAGE_VERSION)-$(PACKAGE_BUILD)
CONTAINER_ARCH = $(shell uname -p)

BUILDRULES = $(PCP_INC_DIR)/buildrules

# LCFLAGS, LLDFLAGS, LLDLIBS, LDIRT may be specified in local makefiles.

# turn on all warnings by default
ifeq "yes" "yes"
WARN_OFF = -Wall
else
WARN_OFF =
endif

LIBPCP_ABIDIR ?= src
PCPLIB_LDFLAGS = -L$(PCP_LIB_DIR)
# backward compatibility
PCP_LIBS = $(PCPLIB_LDFLAGS)

# platform-specific CFLAGS, LDLIBS, and shared library extension
#
# Note:
# 	Need PCFLAGS setting here to match the CFLAGS settings in
# 	../../configure.ac (likewise for PLDFLAGS and LDFLAGS).
#
TARGET_OS = linux
CROSS_COMPILING = no
CASE_INSENSITIVE_FS = no
PCFLAGS = -fPIC -fno-strict-aliasing -D_GNU_SOURCE
PLDFLAGS = -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now
ifneq (, $(filter linux kfreebsd gnu, $(TARGET_OS)))
DSOSUFFIX = so
endif
ifeq "$(TARGET_OS)" "darwin"
DSOSUFFIX = dylib
CASE_INSENSITIVE_FS = yes
endif
ifeq "$(TARGET_OS)" "mingw"
DSOSUFFIX = dll
EXECSUFFIX = .exe
SHELLSUFFIX = .sh
PLDLIBS = -lwsock32 -liphlpapi
endif
ifeq "$(TARGET_OS)" "solaris"
PLDFLAGS += -fPIC
PLDLIBS = -lnsl -lsocket -lresolv -ldl -lposix4
DSOSUFFIX = so
endif
ifeq "$(TARGET_OS)" "aix"
DSOSUFFIX = so
# -qcpluscmt
#	allow C++-style // as comment preamble
# -brtl
#       use run-time linker
# -bnoipath
#       do not use path to DSOs from the build, use default search path
#       rules
# (and does not accept -Wall as a valid option)
PLDFLAGS += -brtl -bnoipath
WARN_OFF =
endif
ifeq "$(TARGET_OS)" "freebsd"
DSOSUFFIX = so
endif
ifeq "$(TARGET_OS)" "netbsd"
DSOSUFFIX = so
LDFLAGS += -Wl,-rpath=/usr/pkg/lib
endif
ifeq "$(TARGET_OS)" "openbsd"
DSOSUFFIX = so
endif

CFLAGS_ABI = 
CFLAGS_OPT = -O2 -g
CFLAGS += $(PCFLAGS) $(LCFLAGS) $(WARN_OFF) $(CFLAGS_OPT) \
	-DPCP_VERSION=\"$(PCP_VERSION)\" \
	-I$(PCP_INC_DIR)/.. -I$(PCP_INC_DIR)

PIECFLAGS = -fPIE
PIELDFLAGS = -pie -Wl,-z,relro -Wl,-z,now
INVISIBILITY = -fvisibility=hidden	# hide shared library symbols

NSSCFLAGS = 
NSPRCFLAGS = 
SASLCFLAGS = -I/usr/include/sasl
AVAHICFLAGS = -D_REENTRANT 
CAIROCFLAGS = -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/lib/libffi-3.2.1/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/uuid 
NCURSESCFLAGS = -D_GNU_SOURCE -D_DEFAULT_SOURCE 
LIBMICROHTTPDCFLAGS = -I/usr/include/p11-kit-1 
ZLIBCFLAGS = 
LZMACFLAGS = 
LIBUVCFLAGS = 
OPENSSLCFLAGS = 

LDFLAGS += $(PLDFLAGS) $(WARN_OFF) $(PCP_LIBS) $(LLDFLAGS)

SRCFILES = GNUmakefile $(HFILES) $(CFILES) $(CXXFILES) $(MFILES) \
	$(LSRCFILES) $(LFILES) $(YFILES) $(PYFILES)

LDLIBS = $(LLDLIBS) $(PLDLIBS)
MAKEOPTS = --no-print-directory

ifdef PROJECT
QTDIRDIRT = build debug release .obj .ui .moc .qrc *.xcodeproj *.app
QTDIRT = *.a *.o ui_* moc_* qrc_* Info.plist Makefile* object_script.*
endif
DEPDIRT = dep dep.bak
MANDIRT = *.[1-9].gz *.[1-9].bz2 *.[1-9].lzma *.[1-9].xz *.[1-9].tmp
LIBDIRT = $(LIBTARGET) $(STATICLIBTARGET)
CDIRT = $(OBJECTS) $(CMDTARGET) $(CXXMDTARGET)
DIRT = $(LDIRT) $(DEPDIRT) $(MANDIRT) $(QTDIRT) $(CDIRT) $(LIBDIRT)
DIRDIRT = $(LDIRDIRT) $(QTDIRDIRT)

OBJECTS = $(ASFILES:.s=.o) \
          $(CFILES:.c=.o) \
          $(CXXFILES:.cxx=.o) \
          $(FFILES:.f=.o) \
          $(LFILES:.l=.o) \
          $(YFILES:%.y=%.tab.o)

#NB: don't override $(MAKE); gnumake sets it well, propagating -j etc.
#MAKE	= /usr/bin/make
CC	= gcc
CXX	= g++
LD	= /usr/bin/ld
AWK	= /usr/bin/gawk --posix
SED	= /usr/bin/sed
CPP	= gcc -E
LEX	= flex
YACC	= bison
ECHO	= /usr/bin/echo
LN_S	= ln -s
GREP    = grep
GIT	= 
CLINT	= /bin/true --std=posix --inline-suppr --force --quiet --error-exitcode=1 -I$(PCP_INC_DIR) -I/usr/include/bits
PYLINT	= /bin/true --rcfile=$(TOPDIR)/.pylintrc --output-format=colorized
PYTHON	= python
PYTHON3	= python3
DTRACE  = 
ifeq "$(TARGET_OS)" "freebsd"
QMAKE	= /bin/qmake
else
QMAKE	= QT_SELECT=5 /bin/qmake
endif

INSTALL_SH = $(PCP_BINADM_DIR)/install-sh
INSTALL	= $(INSTALL_SH) -o $(PCP_USER_INSTALL) -g $(PCP_GROUP_INSTALL)

CCF	= $(CC) $(CFLAGS)
CXXF	= $(CXX) $(CFLAGS) $(CXXFLAGS)
# NB: don't use $(MAKEF), since that suppresses gnumake's subdir parallelization
#MAKEF	= $(MAKE) $(MAKEOPTS)
LDF	= $(LD) $(LDFLAGS)
MAKEDEPEND = /usr/bin/true

ifeq "$(TARGET_OS)" "freebsd"
# gmake on FreeBSD has a strange default rule that passes insufficient
# flags to cc/ld for the link step.  This change prevents errors like
# undefined reference to `__stack_chk_fail_local'
#
LDFLAGS += $(CFLAGS)
endif

ZIP	= /usr/bin/gzip
BZIP2	= /usr/bin/bzip2
LZMA	= /usr/bin/lzma
XZ	= /usr/bin/xz
TAR	= /usr/bin/tar
RPMPROG	= 
HDIUTIL = 
MKINSTALLP = 
DLLTOOL = 
RPMBUILD= 
RPM	= 
POD2MAN = /usr/bin/core_perl/pod2man
DPKG	= 
MAKEPKG	= /usr/bin/makepkg
GENPMDA = $(PCP_BIN_DIR)/genpmda
PKGMK	= 
MD5SUM	= /usr/bin/md5sum
XMLTO	= @xmlto@
DBLATEX	= @dblatex@
PUBLICAN = 
BOOK_TOOLCHAIN = 
BOOK_BRAND = common
INKSCAPE = 

HAVE_MANPAGES = false
HAVE_GZIPPED_MANPAGES = false
HAVE_BZIP2ED_MANPAGES = false
HAVE_LZMAED_MANPAGES = false
HAVE_XZED_MANPAGES = false
PCP_PREFIX = /usr
PCP_MAN_DIR = /usr/share/man
PCP_BOOKS_DIR = /usr/share/doc/pcp-doc
PCP_ICONS_DIR = /usr/share/pcp-gui/pixmaps
PCP_DESKTOP_DIR = /usr/share/applications
PCP_SELINUX_DIR = /var/lib/pcp/selinux

NEED_OLD_TBL_HEADER = false
RDYNAMIC_FLAG = -rdynamic
QT_RELEASE = release
QT_VERSION = 5

# configuration state affecting the entire build
ENABLE_SHARED = false
ENABLE_SECURE = false
ENABLE_PROBES = false
ENABLE_AVAHI = true
ENABLE_DSTAT = false
ENABLE_QT = true
ENABLE_QT3D = false
ENABLE_PERL = true
ENABLE_PYTHON2 = true
ENABLE_PYTHON3 = true
ENABLE_SYSTEMD = true
ENABLE_MANAGER = true
ENABLE_WEBAPI = true
ENABLE_SELINUX = false
ENABLE_DECOMPRESSION = true
ENABLE_LZMA = true

# selinux configuration bits
PCP_SELINUX_FILES_LIST_NON_AUTH_DIRS = false
PCP_SELINUX_CAP_USERNS_PTRACE = false
PCP_SELINUX_CONTAINER_RUNTIME = false
PCP_SELINUX_NSFS = false
PCP_SELINUX_DOCKER_VAR_LIB = false
PCP_SELINUX_UNRESERVED_PORT = false
PCP_SELINUX_TRACEFS = false
PCP_SELINUX_HOSTNAME_EXEC_MAP = false
PCP_SELINUX_UNCONFINED = false
PCP_SELINUX_NUMAD = false
PCP_SELINUX_BPF_STATUS = false
PCP_SELINUX_RPM_VAR_LIB = false
PCP_SELINUX_VIRT_VAR_RUN = false
PCP_SELINUX_FILES_MMAP_ALL_FILES = false
PCP_SELINUX_CAP2_SYSLOG = false

# for code supporting any modern version of perl
HAVE_PERL = true

# for code supporting both/either python2 and/or python3
HAVE_PYTHON = true

# python dependent bits
HAVE_PYTHON_SIX = false
HAVE_PYTHON_REQUESTS = false
HAVE_PYTHON_SETUPTOOLS = false
HAVE_PYTHON_JSONPOINTER = false
HAVE_PYTHON_ORDEREDDICT = true
HAVE_PYTHON_INFLUXDB = false
HAVE_PYTHON_OPENPYXL = false
HAVE_PYTHON_BPF = false

# for building packages from external sources
HAVE_WEBAPPS = false

# for desktop icons in a variety of img sizes
HAVE_HICOLOR_ICONS = false

# additional libraries needed for particular functions
LIB_FOR_BASENAME = 
LIB_FOR_DLOPEN = -ldl
LIB_FOR_REGEX = 
LIB_FOR_MATH = -lm
LIB_FOR_READLINE = -lreadline
LIB_FOR_CURSES = 
LIB_FOR_PTHREADS = -lpthread
LIB_FOR_RT = 
LIB_FOR_NSS = 
LIB_FOR_NSPR = 
LIB_FOR_SASL = -lsasl2
LIB_FOR_SSL = 
LIB_FOR_AVAHI = -lavahi-common -lavahi-client
LIB_FOR_ATOMIC = -latomic
LIB_FOR_LZMA = -llzma

HAVE_LIBUV = false
LIB_FOR_LIBUV = 
HAVE_OPENSSL = true
LIB_FOR_OPENSSL = -lssl -lcrypto 
HAVE_LIBVARLINK = false
LIB_FOR_LIBVARLINK = 
HAVE_NCURSES = true
LIB_FOR_NCURSES = -lncursesw 
HAVE_DEVMAPPER = true
LIB_FOR_DEVMAPPER = -ldevmapper 

HAVE_CAIRO = true
LIB_FOR_CAIRO = -lfreetype -lcairo -lpng16 -lz 
HAVE_LIBMICROHTTPD = true
LIB_FOR_MICROHTTPD = -lmicrohttpd 
HAVE_ZLIB = true
LIB_FOR_ZLIB = -lz 
HAVE_BOOST = false
HAVE_BOOST_FLYWEIGHT_HPP = no

# configuration state for optional performance domains
SYSTEMD_CFLAGS = 
SYSTEMD_LIBS = -lsystemd 
PFM_LIBS = 
IB_LIBS = 
PMDA_BCC = false
PMDA_RPM = false
PMDA_JSON = false
PMDA_PODMAN = false
PMDA_LIBVIRT = false
PMDA_SYSTEMD = true
PMDA_PERFEVENT = false
PMDA_PROMETHEUS = false
PMDA_INFINIBAND = false
PMDA_MYSQL = false
PMDA_ORACLE = false
PMDA_POSTGRESQL = false
PMDA_ELASTICSEARCH = true
PMDA_SNMP = false
PMDA_NGINX = false
PMDA_ACTIVEMQ = false
PMDA_POSTFIX = false
PMDA_BIND2 = false
PMDA_NUTCRACKER = false
PMDA_LIO = false

SHELL = /bin/sh
IMAGES_DIR = $(TOPDIR)/all-images
DIST_DIR = $(TOPDIR)/dist

# env vars to be set before you can run a PCP binary in the build
# environment ... needed for tools like newhelp
#
# default, then special case for different platforms
#
RUN_IN_BUILD_ENV = PCP_CONF=$(TOPDIR)/src/include/pcp.conf PCP_ALT_CPP=$(TOPDIR)/src/pmcpp/pmcpp.static$(EXECSUFFIX)

# when cross-compiling, we cannot use RUN_IN_BUILD_ENV, so any binaries
# from PCP we need in the build must be installed on the build machine.
#
ifeq "$(CROSS_COMPILING)" "yes"
NEWHELP = . $$PCP_DIR/etc/pcp.conf && $$PCP_BINADM_DIR/newhelp
PMIECONF = . $$PCP_DIR/etc/pcp.conf && $$PCP_BIN_DIR/pmieconf
PMGENMAP = . $$PCP_DIR/etc/pcp.conf && $$PCP_BIN_DIR/pmgenmap
PMCPP = . $$PCP_DIR/etc/pcp.conf && $$PCP_BINADM_DIR/pmcpp
else
NEWHELP = $(RUN_IN_BUILD_ENV) $(TOPDIR)/src/newhelp/newhelp.static$(EXECSUFFIX)
PMIECONF = $(RUN_IN_BUILD_ENV) $(TOPDIR)/src/pmieconf/pmieconf.static$(EXECSUFFIX)
PMGENMAP = $(RUN_IN_BUILD_ENV) sh ./pmgenmap.sh
PMCPP = $(RUN_IN_BUILD_ENV) $(TOPDIR)/src/pmcpp/pmcpp.static$(EXECSUFFIX)
endif

SUBDIRS_MAKERULE = \
	+for d in `echo $^ `; do \
	    if test -d "$$d" -a -f "$$d/GNUmakefile"; then \
		$(ECHO) === $$d ===; \
		$(MAKE) $(MAKEOPTS) -C $$d $@ || exit $$?; \
	    fi; \
	done

# special variant of SUBDIRS_MAKERULE when running QA
#
QA_SUBDIRS_MAKERULE = \
	+@for d in `echo $^ `; do \
	    if test -d "$$d" ; then \
		if test -f "$$d/GNUmakefile.install"; then \
		    $(MAKE) --silent -C $$d -f GNUmakefile.install $@ || exit $$?; \
		else \
		    $(MAKE) --silent -C $$d $@ || exit $$?; \
		fi; \
	    fi; \
	done

# prepare symbols file for the GNU toolchain (linker) for DSO PMDAs
VERSION_SCRIPT_MAKERULE = \
	@rm -f $@; \
	echo "$(DOMAIN) {" >$@; \
	echo "    global: $(PMDAINIT);" >>$@; \
	echo "    local: *;" >>$@; \
	echo "};" >>$@; \

# prepare symbols file for other platforms toolchain for shared libs
# (convert one canonical format - GNU toolchain style - into others)
ifeq "$(TARGET_OS)" "solaris"
EXPORTS_MAKERULE = \
	$(AWK) <$< >$@ '\
BEGIN	{ print "$$mapfile_version 2"; print } \
/\{$$/	{ print "SYMBOL_VERSION", $$0; next } \
	{ print }'
else
EXPORTS_MAKERULE =
endif

# prepare domain.h used during the PMDA build process for each PMDA
DOMAIN_MAKERULE = \
	@rm -f $@; \
	echo "/*" >$@; \
	echo " * built from $<" >>$@; \
	echo " */" >>$@; \
	$(AWK) <$< '\
	    $$1=="\#define" && $$2 == "$(DOMAIN)" {\
		print "\#define $(DOMAIN) " $$3 >>"$@"; found++ \
	    }\
	    END {\
		if (found == 0) { \
		    print "Botch: no define for domain $(DOMAIN) in $<"; \
		    system("rm '$@'"); \
		    exit(1) \
		}\
		if (found > 1) { \
		    print "Botch: multiple defines for domain $(DOMAIN) in $<";\
		    print "... see $@ for details"; \
		    system("rm '$@'"); \
		    exit(1) \
		}\
        	exit(0) \
            }' || ( rm -f $@ && false )

DOMAIN_PERLRULE = \
	@export perldomain=`sed -n \
		-e '/PCP::PMDA->new/s/[^0-9]*$$//' \
		-e '/PCP::PMDA->new/s/^[^0-9]*//p' pmda$(IAM).pl | \
		$(AWK) '{ print $$NF }'`; \
	$(AWK) <$< '\
	    BEGIN {\
		domain = toupper("$(IAM)") \
	    }\
	    $$1=="\#define" && $$2 == domain { \
		pmd=$$3; found++ \
	    }\
	    END {\
		if (found == 0) {\
		    print "Botch: no define for domain " domain " in $<"; \
		    exit(1) \
		}\
	        if (found > 1) {\
		    print "Botch: multiple defines of domain " domain " in $<";\
		    exit(1) \
		}\
		if (pmd != "'"$$perldomain"'") {\
		    print "Botch: domain number in ../../pmns/stdpmid (" pmd ") does not match domain number in Perl source ("'"$$perldomain"'")"; \
		    exit(1) \
		}\
		exit(0) \
	    }'

DOMAIN_PYTHONRULE = \
	@export pythondomain=`sed -n \
		-e '/PMDA(/s/[^0-9]*$$//' \
		-e '/PMDA(/s/^[^0-9]*//p' $(PYSCRIPT) | \
		$(AWK) '{ print $$NF }'`; \
	$(AWK) <$< '\
	    BEGIN {\
		domain = toupper("$(IAM)") \
	    }\
	    $$1=="\#define" && $$2 == domain { \
		pmd=$$3; found++ \
	    }\
	    END {\
		if (found == 0) {\
		    print "Botch: no define for domain " domain " in $<"; \
		    exit(1) \
		}\
	        if (found > 1) {\
		    print "Botch: multiple defines of domain " domain " in $<";\
		    exit(1) \
		}\
		if (pmd != "'"$$pythondomain"'") {\
		    print "Botch: domain number in ../../pmns/stdpmid (" pmd ") does not match domain number in Python source ("'"$$pythondomain"'")"; \
		    exit(1) \
		}\
		exit(0) \
	    }'

POD_OPTIONS = --section=$(MAN_SECTION) --release=$(PCP_VERSION) \
		--center="Performance Co-Pilot" --date="Performance Co-Pilot"
POD_MAKERULE = $(POD2MAN) $(POD_OPTIONS) $^ $@

ifeq "$(TARGET_OS)" "mingw"
INSTALL_MAN =
else
INSTALL_MAN = \
    test -z "$$MAN_PAGES" && MAN_PAGES="$(MAN_PAGES)"; \
    if [ "$(HAVE_MANPAGES)" = "true" ] ; then \
    for d in `echo $$MAN_PAGES`; do \
	first=true; \
	base=`echo $$d | sed -e 's/\.[0-9]//g'`; \
	section=`echo $$d | sed -e 's/.*\.//g'`; \
	$(AWK) <$$d ' \
BEGIN { state=0; print "'"$$base"'" } \
$$1==".ds" { ds["\\\\\\*\\("$$2] = $$3 } \
$$1==".SH" && $$2=="NAME" { state=1; next } \
$$1==".SH" && state==1	{ exit } \
/^\./ { next } \
state==1 { for (i=1;i<=NF;i++) { \
             if ($$i=="\\-" || $$i=="-") exit; \
	     gsub ("\\\\f3", "", $$i); gsub ("\\\\f1.*", "", $$i); \
	     for ( d in ds ) sub (d, ds[d], $$i); \
	     print $$i \
	   } \
	 }' \
	| LC_COLLATE=POSIX $(PCP_SORT_PROG) -u \
	| while read m; do \
	    [ -z "$$m" -o "$$m" = "\\" ] && continue; \
	    t=$(PCP_MAN_DIR)/man$$section/$$m.$$section; \
	    if $$first; then \
		_tfx= ;\
		if $(NEED_OLD_TBL_HEADER) ; then \
		    $(SED) -e "1s/^'\\\\\"! tbl.*/'\\\\\" t/" $$d > $$d.tmp; _tfx=.tmp; \
		fi; \
		if $(HAVE_GZIPPED_MANPAGES) ; then \
		    $(ZIP) -c $$d$$_tfx > $$d.gz; _tfx=.gz; _sfx=.gz; \
		fi; \
		if $(HAVE_BZIP2ED_MANPAGES) ; then \
		    $(BZIP2) -c $$d$$_tfx > $$d.bz2; _tfx=.bz2; _sfx=.bz2; \
		fi; \
		if $(HAVE_LZMAED_MANPAGES) ; then \
		    $(LZMA) -c $$d$$_tfx > $$d.lzma; _tfx=.lzma; _sfx=.lzma; \
		fi; \
		if $(HAVE_XZED_MANPAGES) ; then \
		    $(XZ) -c $$d$$_tfx > $$d.xz; _tfx=.xz; _sfx=.xz; \
		fi; \
		u=$$m.$$section$$_sfx; \
		echo $(INSTALL) -m 644 $${d}$$_tfx $${t}$$_sfx; \
		$(INSTALL) -m 644 $${d}$$_tfx $${t}$$_sfx; \
	    else \
		if test "$(CASE_INSENSITIVE_FS)" = "yes"; then \
		    _upper_u=`echo $$u | tr 'a-z' 'A-Z'`; \
		    _upper_t=`basename $${t}$$_sfx | tr 'a-z' 'A-Z'`; \
		    if test "$$_upper_u" != "$$_upper_t"; then \
			echo $(INSTALL) -S $$u $${t}$$_sfx; \
			$(INSTALL) -S $$u $${t}$$_sfx; \
		    fi \
		else \
		    echo $(INSTALL) -S $$u $${t}$$_sfx; \
		    $(INSTALL) -S $$u $${t}$$_sfx; \
		fi \
	    fi; \
	    first=false; \
	done; \
    done; \
    fi
endif

PERL_INSTALL_BASE = /usr
PERL_INSTALLDIRS = vendor

# MakeMaker INSTALL_BASE needs to be unset for proper vendor_perl paths to be used for --prefix=/usr;
ifeq "$(PERL_INSTALL_BASE)" "/usr"
ifneq "$(TARGET_OS)" "darwin"
MAKEMAKER_EXTRA_OPTIONS=
else
MAKEMAKER_EXTRA_OPTIONS=INSTALL_BASE=$(PERL_INSTALL_BASE)  INSTALLBASE=$(PERL_INSTALL_BASE)
endif
else
MAKEMAKER_EXTRA_OPTIONS=INSTALL_BASE=$(PERL_INSTALL_BASE)  INSTALLBASE=$(PERL_INSTALL_BASE)
endif

PERL_MAKE_MAKEFILE = \
	export PCP_TOPDIR="`cd $(TOPDIR) && pwd`"; \
	NSS_CFLAGS="$(NSS_CFLAGS)" NSPR_CFLAGS="$(NSPR_CFLAGS)" \
        TARGET_OS="$(TARGET_OS)" CC="$(CC) $(CFLAGS_ABI)" perl Makefile.PL $(MAKEMAKER_EXTRA_OPTIONS) $(MAKEMAKER_OPTIONS)

# Collect files from a Perl "make -f Makefile install" below
# src/perl to build the PCP Perl packaging list, and also clean up
# the installed files to remove unwanted files and make the DSO
# executable
#
# Usage is $(call PERL_GET_FILELIST,target,listfile,base)
# where target is the Makefile target (install or install_perl),
# listfile is something like $(TOPDIR)/perl-pcp-pmda.list and
# base is the DSO basename like PMDA.
#
# We need different versions for the different installation and
# packaging regimes.
#
ifeq "$(PACKAGE_DISTRIBUTION)" "debian"
# For Debian packaging, this is not needed
PERL_GET_FILELIST =
else
ifeq "$(PACKAGE_DISTRIBUTION)" "gentoo"
# Gentoo cannot rely on the .packlist files from the install, so create
# a temporary pack.list file
PERL_GET_FILELIST = \
	$(PERLMAKE) -f Makefile $(1) DESTDIR="$$DIST_ROOT" \
	| tee pack.list; \
	sed -n -e '/\.bs$$/d' -e 's/\.[0-9]pm$$/&.gz/' -e "s@^Installing $$DIST_ROOT@@p" <pack.list >$(2) || exit 1; \
	if [ -s $(2) ]; then rm -f pack.list; \
	else echo "Arrgh ... no files to include in package via $(2), see pack.list"; exit 1; \
	fi; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name perllocal.pod -exec rm -f '{}' ';' ; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name \*.bs  -exec rm -f '{}' ';' ; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name $(3).so -exec chmod 755 '{}' ';'
else
ifeq "$(PACKAGE_DISTRIBUTION)" "freebsd"
# FreeBSD Perl packaging is a broken mystery at this point in time
# 1. there is no .packlist files being created
# 2. $(PERLMAKE) -f Makefile install DESTDIR=$$DIST_ROOT does not work
# so disable the packaging pro tem
PERL_GET_FILELIST =
else
# Everyone else ... includes the RPM-based packaging platforms
ifeq "$(PACKAGE_DISTRIBUTION)" "mandriva"
	man_suffix=lzma
else
	man_suffix=gz
endif
PERL_GET_FILELIST = \
	$(PERLMAKE) -f Makefile $(1) DESTDIR="$$DIST_ROOT"; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name .packlist -exec mv '{}' $(2) ';' ; \
	if [ -s $(2) ] ; then \
	    _sfx=.gz; \
	    $(HAVE_BZIP2ED_MANPAGES) && _sfx=.bz2; \
	    $(HAVE_LZMAED_MANPAGES) && _sfx=.lzma; \
	    $(HAVE_XZED_MANPAGES) && _sfx=.xz; \
            if [ "$(HAVE_MANPAGES)" = "false" ] ; then \
		sed -e '/.*man[1-9].*/d' -e '/.*3pm.*/d' $(2) >$(2).tmp; \
		mv $(2).tmp $(2); \
		find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name "*3pm*" -exec rm -rf '{}' ';' ; \
	    fi ;\
	    sed -n -e '/\.bs$$/d' -e 's/\.[0-9]pm$$/&'"$$_sfx/" -e "s@^$$DIST_ROOT@@p" $(2) >$(2).tmp; \
	    mv $(2).tmp $(2); \
	else echo "Arrgh ... no files to include in package via $(2)"; exit 1; \
	fi; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name perllocal.pod -exec rm -f '{}' ';' ; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name \*.bs  -exec rm -f '{}' ';' ; \
	find "$$DIST_ROOT/$(PERL_INSTALL_BASE)" -name $(3).so -exec chmod 755 '{}' ';'
endif
endif
endif

# Create perl manifest files explicitly for some distributions
ifeq "$(shell [ '$(PACKAGE_DISTRIBUTION)' = cocoa \
                -o '$(PACKAGE_DISTRIBUTION)' = macosx \
                -o '$(PACKAGE_DISTRIBUTION)' = gentoo \
                -o '$(PACKAGE_DISTRIBUTION)' = solaris \
                -o '$(PACKAGE_DISTRIBUTION)' = freebsd \
                -o '$(PACKAGE_DISTRIBUTION)' = openbsd \
	      ] && echo 1)" "1"
# Gather installed Perl files before packaging
PERL_INSTALL = \
	if [ -n "$(DIST_MANIFEST)" ]; then \
	    if [ "`echo $(TOPDIR)/perl-pcp-*.list`" != "$(TOPDIR)/perl-pcp-*.list" ]; then \
		cat $(TOPDIR)/perl-pcp-*.list | while read f; do \
		    bn=`basename $$f .gz`; \
		    dn=`dirname $$f`; \
		    $(INSTALL) -d $$dn || exit 1; \
		    src=`find */blib -name $$bn`; \
		    if [ -x $$src ] ; then mode=0755; else mode=0644; fi; \
		    $(INSTALL) -m $$mode $$src $$dn/$$bn || exit 1; \
		done; \
	    fi; \
	fi
else
PERL_INSTALL =
endif

#
# Python platform-specific installation quirks
PYTHON_PREFIX=/usr
SETUP_PY_BUILD_OPTIONS = --include-dirs=$(TOPDIR)/src/include:$(TOPDIR)/src/include/pcp$(EXTRA_PY_INCLUDES)
SETUP_PY_BUILD_OPTIONS += --library-dirs=$(TOPDIR)/src/libpcp/src:$(TOPDIR)/src/libpcp_pmda/src:$(TOPDIR)/src/libpcp_gui/src:$(TOPDIR)/src/libpcp_import/src:$(TOPDIR)/src/libpcp_mmv/src
SETUP_PY_INSTALL_OPTIONS = --skip-build
SETUP_PY_INSTALL_OPTIONS += --root="$${DIST_ROOT:-/}"
ifeq "$(PYTHON_PREFIX)" "/usr"
ifeq "$(PACKAGE_DISTRIBUTION)" "debian"
SETUP_PY_INSTALL_OPTIONS += --install-layout=deb
endif
else
SETUP_PY_INSTALL_OPTIONS += --prefix=$(PYTHON_PREFIX)
endif
ifeq "$(ENABLE_PYTHON3)" "true"
SETUP_PY_BUILD_OPTIONS += -DENABLE_PYTHON3
endif
# RPM byte-compiles and installs results in our DIST_ROOT, cater for this:
ifeq "$(shell [ '$(TARGET_OS)' = linux -a '$(PACKAGE_DISTRIBUTION)' != gentoo \
	      ] && echo 1)" "1"
# Linux and not Gentoo (which needs tarball packaging)
PYTHON_INSTALL = \
	cat $(TOPDIR)/python-pcp.list | while read f; do \
	    dirname $$f | $(SED) -e '/.*packages$$/d'; \
	done | $(PCP_SORT_PROG) -u > $(TOPDIR)/python-pcp.list.rpm; \
	$(AWK) '{print} /\.pyc$$/ {sub(/\.pyc$$/,".pyo"); print}' \
	    < $(TOPDIR)/python-pcp.list >> $(TOPDIR)/python-pcp.list.rpm; \
	cat $(TOPDIR)/python-pcp.list.rpm | while read f; do \
	    touch "$${DIST_ROOT-/}$$f"; \
	done
PYTHON3_INSTALL = \
	cat $(TOPDIR)/python3-pcp.list | while read f; do \
	    dirname $$f | $(SED) -e '/.*packages$$/d'; \
	done | $(PCP_SORT_PROG) -u > $(TOPDIR)/python3-pcp.list.rpm; \
	$(AWK) '{print} /\.pyc$$/ {sub(/\.pyc$$/,".pyo"); print}' \
	    < $(TOPDIR)/python3-pcp.list >> $(TOPDIR)/python3-pcp.list.rpm; \
	$(AWK) '/\.pyc$$/ {sub(/\.pyc$$/,".opt-1.pyc"); print}' \
	    < $(TOPDIR)/python3-pcp.list >> $(TOPDIR)/python3-pcp.list.rpm; \
	cat $(TOPDIR)/python3-pcp.list.rpm | while read f; do \
	    touch "$${DIST_ROOT-/}$$f"; \
	done
else
ifeq "$(shell [ '$(PACKAGE_DISTRIBUTION)' = cocoa \
                -o '$(PACKAGE_DISTRIBUTION)' = macosx \
                -o '$(PACKAGE_DISTRIBUTION)' = gentoo \
                -o '$(PACKAGE_DISTRIBUTION)' = solaris \
                -o '$(PACKAGE_DISTRIBUTION)' = freebsd \
	      ] && echo 1)" "1"
# Gather installed Python files before packaging
# Lines in python-pcp.list are like this
# /usr/lib/python2.7/site-packages/pcp.py
# /usr/lib/python2.7/site-packages/pcp.pyc
# /usr/lib/python2.7/site-packages/pmapi.so
# /usr/lib/python2.7/site-packages/pcp-0.2-py2.7.egg-info
#
# Matching build artifacts are below src/python/build
#
PYTHON_INSTALL = \
	if [ -n "$(DIST_MANIFEST)" ]; then \
	    cat $(TOPDIR)/python-pcp.list \
	    | while read f; do \
		bn=`basename $$f`; \
		dn=`dirname $$f`; \
		pydir="$(TOPDIR)/src/python"; \
		$(INSTALL) -d $$dn || exit 1; \
		src=`find $$pydir/build $$pydir/pcp.egg-info -name $$bn`; \
		$(INSTALL) $$src $$f || exit 1; \
	    done; \
	fi
PYTHON3_INSTALL = \
	if [ -n "$(DIST_MANIFEST)" ]; then \
	    cat $(TOPDIR)/python3-pcp.list \
	    | while read f; do \
		bn=`basename $$f`; \
		dn=`dirname $$f`; \
		pydir="$(TOPDIR)/src/python"; \
		$(INSTALL) -d $$dn || exit 1; \
		src=`find $$pydir/build $$pydir/pcp.egg-info -name $$bn`; \
		$(INSTALL) $$src $$f || exit 1; \
	    done; \
	fi
else
ifeq "$(shell [ '$(PACKAGE_DISTRIBUTION)' = openbsd \
	      ] && echo 1)" "1"
# similar to above, but files already installed in DIST_ROOT so
# just append to the manifest
PYTHON_INSTALL = \
	if [ -n "$(DIST_MANIFEST)" ]; then \
	    cat $(TOPDIR)/python-pcp.list \
	    | while read f; do \
		bn=`basename $$f`; \
		dn=`dirname $$f`; \
		$(INSTALL) -d $$dn || exit 1; \
		src=`find "$(DIST_ROOT)/usr/lib" -name $$bn`; \
		$(INSTALL) $$src $$f || exit 1; \
	    done; \
	fi
PYTHON3_INSTALL = \
	if [ -n "$(DIST_MANIFEST)" ]; then \
	    cat $(TOPDIR)/python3-pcp.list \
	    | while read f; do \
		bn=`basename $$f`; \
		dn=`dirname $$f`; \
		$(INSTALL) -d $$dn || exit 1; \
		src=`find "$(DIST_ROOT)/usr/lib" -name $$bn`; \
		$(INSTALL) $$src $$f || exit 1; \
	    done; \
	fi
else
# default case if all of the above tests fail
PYTHON_INSTALL =
endif	# openbsd if
endif	# cocoa ... freebsd if
endif	# linux && !gentoo if

# qmake botches the order of the -L arguments in the generated $(LIBS)
# in the Makefile ... need to get the Qt libraries dir (begins with a /)
# out from the head of the list and insert it before the first -l argument
# (or the end of the list).
# So, for example
# LIBS = $(SUBLIBS) -L/usr/lib -L../libpcp/src -L../libpcp_qmc/src ... -lpcp_qmc ...
# becomes
# LIBS = $(SUBLIBS) -L../libpcp/src -L../libpcp_qmc/src ... -L/usr/lib -lpcp_qmc ...
#
QTFIX = sed -e 's/Makefile.new/Makefile/g' <Makefile.new | $(AWK) '$$1 == "LIBS" { printf $$1; for (i=2;i<=NF;i++) { if ($$i~/^-L\//) { save=save " " $$i; continue } else if (save!="" && $$i~/^-l/) { printf " %s",save; save="" } printf " %s",$$i } if (save!="") printf " %s",save; print ""; next } { print }' >Makefile.fix

# Qt magic for build/installs across all the supported platforms
# because qmake does not always get it right, e.g. omitting -fPIC for
# static libraries in Qt5 on Ubuntu x86_64, we need to push our CFLAGS, CXXFLAGS
# (usually empty) and LDFLAGS down
#
QTMAKE = rm -f Makefile.new Makefile.fix; CFLAGS='$(CFLAGS)' CXXFLAGS='$(CXXFLAGS)' LDFLAGS='$(LDFLAGS)' $(QMAKE) -o Makefile.new CONFIG+=$(QT_RELEASE) && $(QTFIX) && ( if [ -f Makefile ]; then if diff Makefile Makefile.fix >/dev/null; then :; else rm -f Makefile; mv Makefile.fix Makefile; fi; else mv Makefile.fix Makefile; fi ); rm -f Makefile.new Makefile.fix; $(MAKE) $(MAKEOPTS) -f Makefile
ifeq ($(PCP_PLATFORM),darwin)
MACBUILD = build/$(QT_RELEASE)/$(COMMAND).app/Contents
BINARY = $(MACBUILD)/MacOS/$(COMMAND)
LNMAKE = test ! -f $(BINARY) -o -L $(COMMAND) || ( rm -f $(COMMAND) && $(LN_S) $(BINARY) $(COMMAND) )
WINDOW = mac
endif
ifeq ($(PCP_PLATFORM),mingw)
BINARY = build/$(QT_RELEASE)/$(COMMAND)$(EXECSUFFIX)
LNMAKE =
WINDOW = win
endif
ifeq "$(findstring $(PCP_PLATFORM),darwin mingw)" ""
BINARY = build/$(QT_RELEASE)/$(COMMAND)
LNMAKE = test ! -f $(BINARY) -o -L $(COMMAND) || ( rm -f $(COMMAND) && $(LN_S) $(BINARY) $(COMMAND) )
WINDOW = x11
endif

ifeq ($(PCP_PLATFORM),darwin)
INSTALL_DIRECTORY_HIERARCHY=\
	d=$(1); while [ "$$d" != "$(2)" -a "$$d" != "/" -a "$$d" != "." ] ; do \
                echo $$d; d=`dirname $$d`; done | sort | while read id; do \
                $(INSTALL) -m 755 -d $$id || exit 1; done

INSTALL_QT_RESOURCES=\
	printf "[Paths]\nPlugins=/Library/PCP/Plugins\n\n" > qt.conf; \
	$(INSTALL) -m 644 qt.conf $(1)/qt.conf; \
	rm qt.conf; \
	find frameworks -type d -name qt_menu.nib | while read nib; do \
                $(INSTALL) -m 755 -d $(1)/qt_menu.nib || exit 1; \
                find $$nib -type f | while read nibs; do \
                        f=`basename $$nibs`; \
                        $(INSTALL) -m 644 $$nibs $(1)/qt_menu.nib/$$f || exit 1; \
                done; \
        done

MAC_APPSUPPORT_DIR=/Library/PCP
MAC_FRAMEWORKS_DIR=$(MAC_APPSUPPORT_DIR)/Frameworks

# WARNING!
# This rule modified the binary it was given, once modified the
# binary cannot be used with this rule again.
# If the binary is installed then it's important to call this
# rule before calling install rule for the binary.
INSTALL_QT_FRAMEWORKS=\
    otool -L $(1) | awk '{if (NR != 1) {print $$1}}' |\
    egrep 'Qt.*\.framework' | while read qt; do \
	tdir=`dirname $$qt`; \
	install_name_tool -change $$qt $(MAC_FRAMEWORKS_DIR)/$$qt $(1);\
	$(call INSTALL_DIRECTORY_HIERARCHY,$(MAC_FRAMEWORKS_DIR)/$$tdir,/Library/PCP); \
	mkdir -p frameworks/$$tdir || exit 1; \
	fwqt="frameworks/$$qt"; \
	cp /Library/Frameworks/$$qt frameworks/$$qt || exit 1; \
	otool -L $$fwqt | awk '{if (NR != 1) {print $$1}}' |\
	egrep 'Qt.*\.framework' | while read dep; do \
	    install_name_tool -change $$dep $(MAC_FRAMEWORKS_DIR)/$$dep $$fwqt;\
	done; \
	$(INSTALL) frameworks/$$qt $(MAC_FRAMEWORKS_DIR)/$$qt; \
	if [ -d /Library/Frameworks/$$tdir/Resources ] ; then \
	    $(INSTALL) -d $(MAC_FRAMEWORKS_DIR)/$$tdir/Resources; \
	    (cd /Library/Frameworks/$$tdir; find Resources -type f) | \
		while read rf; do \
		    rfpath="$$tdir/$$rf"; rfd=`dirname $$rfpath`; \
		    fwpath="frameworks/$$rfpath"; brfd=`basename $$rfd`; \
		    mkdir -p frameworks/$$rfd || exit 1; \
		    cp /Library/Frameworks/$$rfpath $$fwpath || exit 1; \
		    [ $$brfd != qt_menu.nib ] || continue;  \
		    $(INSTALL) -d $(MAC_FRAMEWORKS_DIR)/$$rfd || exit 1; \
		    $(INSTALL) $$fwpath $(MAC_FRAMEWORKS_DIR)/$$rfpath;\
		done \
	fi; done
endif


# For targets that should always be rebuilt,
# define a target that is never up-to-date.
# Targets needing this should depend on $(_FORCE)
_FORCE = __force_build

PCP_USER = pcp
PCP_GROUP = pcp
PCP_USER_INSTALL = root
PCP_GROUP_INSTALL = root

PCPLIB = -lpcp
PCPLIB_EXTRAS = $(LIB_FOR_MATH) $(LIB_FOR_PTHREADS) $(LIB_FOR_DLOPEN) $(LIB_FOR_RT)
ifneq "$(PCPLIB)" "$(LIB_FOR_BASENAME)"
PCPLIB_EXTRAS += $(LIB_FOR_BASENAME)
endif
ifeq "$(ENABLE_SHARED)" "no"
PCPLIB += $(PCPLIB_EXTRAS)
endif
PCP_GUILIB = -lpcp_gui $(PCPLIB)
PCP_PMDALIB = -lpcp_pmda $(PCPLIB)
PCP_TRACELIB = -lpcp_trace $(PCPLIB)
PCPWEBLIB_EXTRAS = $(LIB_FOR_OPENSSL) $(LIB_FOR_LIBUV) $(PCP_PMDALIB)
PCP_WEBLIB = -lpcp_web $(PCPWEBLIB_EXTRAS)

ifdef PCP_ALTLIBS
ifeq ($(PCP_LIB_DIR),$(PCP_LIB32_DIR))
PCP_ALTLIBS =
else
ifneq "$(findstring $(MAKECMDGOALS),clean clobber)" ""
PCP_ALTLIBS =
endif
endif
endif

endif # _BUILDDEFS_INCLUDED_
