#
# Copyright (c) 2001-2006 Hewlett-Packard Development Company, L.P.
# Contributed by Stephane Eranian <eranian@hpl.hp.com>
#
# This file is part of pfmon, a sample tool to measure performance 
# of applications on Linux.
#
# 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

LIBS += -L$(PFMLIBDIR) -lpfm $(PTHREAD_LIBDIR) -lpthread
CFLAGS += -DDATADIR=\"$(DATADIR)\" -I.

ifneq ($(ELFINCDIR),)
CFLAGS +=-I$(ELFINCDIR)
endif

ifneq ($(ELFLIBDIR),)
LIBS += -L$(ELFLIBDIR)
endif

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

ifeq ($(CONFIG_PFMON_DEMANGLE),y)
CFLAGS += -DCONFIG_PFMON_DEMANGLE
LIBS += -liberty
endif

LIBS += -lelf

#
# 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_system.c pfmon_task.c pfmon_symbols.c \
     pfmon_results.c pfmon_hash.c pfmon_smpl_dfl.c pfmon_os.c

ifeq ($(ARCH),ia64)
SRCS += pfmon_util_ia64.c
CFLAGS += -DCONFIG_PFMON_IA64
endif 

ifeq ($(ARCH),x86_64)
SRCS += pfmon_util_x86_64.c
endif 

ifeq ($(ARCH),ia32)
SRCS += pfmon_util_i386.c
endif 

ifeq ($(ARCH),mips64)
SRCS += pfmon_util_mips64.c
endif 

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

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

ifeq ($(CONFIG_PFMON_GEN_IA64),y)
OBJS   += pfmon_gen_ia64.o
CFLAGS += -DCONFIG_PFMON_GEN_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_MONTECITO),y)
OBJS   += pfmon_montecito.o
CFLAGS += -DCONFIG_PFMON_MONTECITO
endif

ifeq ($(CONFIG_PFMON_AMD64),y)
OBJS   += pfmon_amd64.o
CFLAGS += -DCONFIG_PFMON_AMD64
endif

ifeq ($(CONFIG_PFMON_GEN_IA32),y)
OBJS   += pfmon_gen_ia32.o
CFLAGS += -DCONFIG_PFMON_GEN_IA32
endif

ifeq ($(CONFIG_PFMON_I386_P6),y)
OBJS   += pfmon_i386_p6.o
CFLAGS += -DCONFIG_PFMON_I386_P6
endif

ifeq ($(CONFIG_PFMON_PENTIUM4),y)
OBJS   += pfmon_pentium4.o
CFLAGS += -DCONFIG_PFMON_PENTIUM4
endif

ifeq ($(CONFIG_PFMON_GEN_MIPS64),y)
OBJS   += pfmon_mips64.o
CFLAGS += -DCONFIG_PFMON_MIPS64
endif

DIRS=smpl_mod 
SMPL_MOD_LIB=smpl_mod/libsmplfmt.a
TARGET=pfmon

all: $(TARGET)

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

$(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) pfmon.h

install: subdirs $(TARGET)
	-mkdir -p $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 pfmon $(DESTDIR)$(BINDIR)/pfmon
	-ln -sf $(DESTDIR)$(BINDIR)/pfmon $(DESTDIR)$(BINDIR)/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
