#
# Copyright (C) 2001-2002 Hewlett-Packard Co
# Contributed by Stephane Eranian <eranian@hpl.hp.com>
#
# This file is part of pfmon, a sample tool to measure performance 
# of applications on Linux/ia64.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
#

TOPDIR  := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)/..

include $(TOPDIR)/config.mk
include $(TOPDIR)/rules.mk

# pfmon needs libelf either from the old libelf package or from elfutils-devel
# elfutils-devel has libelf.h in /usr/include, whereas the old libelf has it
# in /usr/include/libelf. To make sure we can compile, we always add the
# include path to /usr/include/libelf. This is extraneous if using elfutils but
# harmless
#
LIBELF_INC=-I/usr/include/libelf

# 
#
LIBS += -L$(PFMLIBDIR) -lpfm -lelf  $(PTHREAD_LIBDIR) -lpthread

ifeq ($(CONFIG_PFMON_LIBUNWIND),y)
CFLAGS += -DCONFIG_PFMON_LIBUNWIND
LIBS += -lunwind
endif

#
# required to access fcntl(F_SETSIG)
#
CFLAGS += -D_GNU_SOURCE

#
# This is kind of broken because it assumes that if the static library
# has changed then the shared library must have also changed. But this 
# should cover our needs.
#
PFMLIB=$(PFMLIBDIR)/libpfm.a

SRCS=pfmon.c pfmon_smpl.c pfmon_util.c pfmon_util_ia64.c pfmon_system.c pfmon_task.c pfmon_symbols.c \
     pfmon_conf.c pfmon_results.c

OBJS=$(SRCS:.c=.o)

ALL_SUPPORT=pfmon_itanium.o pfmon_itanium2.o pfmon_generic_ia64.o


CFLAGS += -DDESTROOT=\"$(DESTROOT)\"

ifeq ($(CONFIG_PFMON_GENERIC_IA64),y)
OBJS   += pfmon_generic_ia64.o
CFLAGS += -DCONFIG_PFMON_GENERIC_IA64
endif

ifeq ($(CONFIG_PFMON_ITANIUM),y)
OBJS   += pfmon_itanium.o
CFLAGS += -DCONFIG_PFMON_ITANIUM
endif

ifeq ($(CONFIG_PFMON_ITANIUM2),y)
OBJS   += pfmon_itanium2.o
CFLAGS += -DCONFIG_PFMON_ITANIUM2
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_RAW_IA64),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_RAW_IA64
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_COMPACT_IA64),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_COMPACT_IA64
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_EXAMPLE_IA64),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_EXAMPLE_IA64
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_BTB_IA64),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_BTB_IA64
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_DET_ITA),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_DET_ITA
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_DET_ITA2),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_DET_ITA2
endif

ifeq ($(CONFIG_PFMON_SMPL_MOD_CSTACK_IA64),y)
CFLAGS += -DCONFIG_PFMON_SMPL_MOD_CSTACK_IA64
endif

ifeq ($(CONFIG_PFMON_DEBUG),y)
CFLAGS += -DPFMON_DEBUG
endif

DIRS=smpl_mod
SMPL_MOD_LIB=smpl_mod/libsmplfmt.a

INCDIR=-I$(PFMINCDIR) $(LIBELF_INC) -I.

TARGET=pfmon

all: check_support $(TARGET)

$(TARGET):  $(PFMLIB) $(OBJS) $(SMPL_MOD_LIB)
	$(CC) -o $@ $(OBJS) $(CFLAGS) $(LDFLAGS) $(LIBS) $(SMPL_MOD_LIB) 

$(OBJS) : $(TOPDIR)/config.mk $(TOPDIR)/rules.mk Makefile

clean: clean_subdirs
	rm -f $(OBJS) $(TARGET) 

distclean: clean clean_subdirs
	rm -f $(ALL_SUPPORT)
depend:
	$(MKDEP) $(SRCS)

pfmon : subdirs $(OBJS) $(SUPPORT)

check_support:
ifeq ($(CONFIG_PFMON_GENERIC_IA64)$(CONFIG_PFMON_ITANIUM)$(CONFIG_PFMON_ITANIUM2),nnn)
	@echo " missing CPU specific support in pfmon Makefile"
	@exit 1
endif

install: subdirs check_support $(TARGET)
	-mkdir -p $(DESTDIR)/bin
	$(INSTALL) -m 755 pfmon $(DESTDIR)/bin/pfmon
	-ln -s $(DESTDIR)/bin/pfmon $(DESTDIR)/bin/pfmon_gen

$(SMPL_MOD_LIB) subdirs: 
	@set -e ; for d in $(DIRS) ; do $(MAKE) -C $$d all; done

clean_subdirs: 
	@set -e ; for d in $(DIRS) ; do $(MAKE) -C $$d clean; done

