DESCR = "Linux with PerfCtr $(VERSION) patch and library, and perfmon2 libpfm library" 

ifneq (/usr,$(PERFCTR_PREFIX))
PWD = $(shell pwd)
ifeq (,$(PERFCTR_LIB_PATH))
ifeq (,$(PERFCTR_ROOT))
	PERFCTR_ROOT := $(PWD)/perfctr-$(VERSION)
endif
	PERFCTR_LIB_PATH := $(PERFCTR_ROOT)/usr.lib
	CC_SHR += -Wl,-rpath-link -Wl,$(PERFCTR_LIB_PATH)
endif
ifeq (,$(PERFCTR_INC_PATH))
ifeq (,$(PERFCTR_ROOT))
	PERFCTR_ROOT := $(PWD)/perfctr-$(VERSION)
endif
	PERFCTR_INC_PATH := $(PERFCTR_ROOT)/usr.lib
	PERFCTR_KINC_PATH := $(PERFCTR_ROOT)/linux/include
else
	PERFCTR_KINC_PATH := $(PERFCTR_INC_PATH)
endif
ifneq (/usr/include,$(PERFCTR_INC_PATH))
CFLAGS += -I$(PERFCTR_INC_PATH) 
endif
ifneq (/usr/include/linux/include,$(PERFCTR_KINC_PATH))
CFLAGS += -I$(PERFCTR_KINC_PATH) 
endif
endif

CFLAGS-2.6.x := -DPERFCTR26
CFLAGS-2.7.x := $(CFLAGS-2.6.x)
CFLAGS += $(CFLAGS-$(VERSION))
MISCHDRS += linux-lock.h mb.h
MISCSRCS +=  $(ARCH_SPEC_EVTS)
PERFCTR_OBJS = $(shell $(AR) t $(PERFCTR_LIB_PATH)/libperfctr.a 2> /dev/null)

ifeq (,$(PERFCTR_OBJS))
$(PERFCTR_LIB_PATH)/libperfctr.a:
ifneq (,${PERFCTR_ROOT})
 ifeq (ppc64,${ARCH})
	$(MAKE) CC='$(CC) $(BITFLAGS)' -C $(PERFCTR_ROOT)
 endif
 ifeq (x86_64,${ARCH})
   ifeq (-m32,${BITFLAGS})
	$(MAKE) CC='$(CC) $(BITFLAGS)' LD_FLAGS="-melf_i386" ARCH="i386" -C $(PERFCTR_ROOT)
   else
	$(MAKE) CC="$(CC)" -C $(PERFCTR_ROOT)
   endif
 else
	$(MAKE) CC="$(CC) -C $(PERFCTR_ROOT)
 endif
else
	@echo '$@ not installed!'; exit 1
endif
	$(MAKE)
endif

#Dynamic linking loader needs correct version of libperfctr
ifeq ($(VERSION),2.7.x)
	SO_ABIVER=6
else
ifeq ($(VERSION),2.6.x)
	i386_ABIVER=5
	x86_64_ABIVER=6
	ppc_ABIVER=5
	SO_ABIVER=$($(ARCH)_ABIVER)
endif
endif

POST_BUILD = cp $(PERFCTR_LIB_PATH)/libperfctr.so .;ln -fs libperfctr.so libperfctr.so.$(SO_ABIVER)

####################################################################
# This section builds libpfm to get at its support for native events
ifneq (/usr,$(PFM_PREFIX))
PWD = $(shell pwd)
ifeq (,$(PFM_LIB_PATH))
ifeq (,$(PFM_ROOT))
	PFM_ROOT := $(PWD)/libpfm-3.y
endif
	PFM_LIB_PATH := $(PFM_ROOT)/lib
	CC_SHR += -Wl,-rpath-link -Wl,$(PFM_LIB_PATH)
endif
ifeq (,$(PFM_INC_PATH))
ifeq (,$(PFM_ROOT))
        PFM_ROOT := $(PWD)/libpfm-3.y
endif
        PFM_INC_PATH := $(PFM_ROOT)/include
endif
ifneq (/usr/include,$(PFM_INC_PATH))
CFLAGS	+= -I$(PFM_INC_PATH) 
endif
endif

CFLAGS	+= -DPERFCTR_PFM_EVENTS
PFM_OBJS=$(shell $(AR) t $(PFM_LIB_PATH)/libpfm.a 2>/dev/null)
PFM_LIBS=-L$(PFM_LIB_PATH) -lpfm
MISCSRCS += papi_libpfm3_events.c 
MISCOBJS += $(PERFCTR_OBJS) $(PFM_OBJS) $(MISCSRCS:.c=.o)
MISCHDRS += papi_libpfm_events.h
SHLIBDEPS = -Bdynamic -L$(PERFCTR_LIB_PATH) -lperfctr $(PFM_LIBS)

ifeq (,$(PFM_OBJS))
$(PFM_LIB_PATH)/libpfm.a:
	$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" CC="$(CC)" lib
	$(MAKE)
endif
####################################################################

include Makefile.inc

config.h:
	@echo 'Please clobber your build and run ./configure. Direct Makefile access has been deprecated.'

$(PERFCTR_OBJS): $(PERFCTR_LIB_PATH)/libperfctr.a
	$(AR) xv $<

$(PFM_OBJS): $(PFM_LIB_PATH)/libpfm.a
	$(AR) xv $<

#perfctr.o: perfctr.c $(HEADERS)
#	$(CC) $(LIBCFLAGS) $(OPTFLAGS) -c perfctr.c -o $@

papi_libpfm3_events.o: papi_libpfm3_events.c $(HEADERS)
	$(CC) $(LIBCFLAGS) $(OPTFLAGS) -c papi_libpfm3_events.c -o $@

native_clean:
	-rm -f $(MISCOBJS) libperfctr.so*
ifneq (,${PERFCTR_ROOT})
	$(MAKE) -C $(PERFCTR_ROOT) clean
endif
ifneq (,${PFM_ROOT})
	$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" clean
endif

native_install: 
	-install -d $(DESTDIR)$(DATADIR)
	-cp -f ./papi_events.csv $(DESTDIR)$(DATADIR)
ifeq ($(ARCH),ppc64)
	cp -Rf ./event_data $(DESTDIR)$(DATADIR)
endif
ifneq (,${PFM_ROOT})
	-$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" CC=$(CC) DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) install_prefix=$(PREFIX) LIBDIR=$(LIBDIR) INCDIR=$(INCDIR) MANDIR=$(MANDIR) install
endif
ifneq (,${PERFCTR_ROOT})
ifeq (-m32,${BITFLAGS})
	$(MAKE) -C $(PERFCTR_ROOT) PREFIX=$(DESTDIR)$(PREFIX) BINDIR=$(DESTDIR)$(BINDIR) LIBDIR=$(DESTDIR)$(LIBDIR) INCLDIR=$(DESTDIR)$(INCDIR) ARCH="i386" install
else
	$(MAKE) -C $(PERFCTR_ROOT) PREFIX=$(DESTDIR)$(PREFIX) BINDIR=$(DESTDIR)$(BINDIR) LIBDIR=$(DESTDIR)$(LIBDIR) INCLDIR=$(DESTDIR)$(INCDIR) install
endif
endif

native_clobber:
ifneq (,${PERFCTR_ROOT})
	$(MAKE) -C $(PERFCTR_ROOT) distclean
endif
ifneq (,${PFM_ROOT})
	 $(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" distclean
endif
