
DESCR = "Linux with perf_event kernel support and libpfm4"

ifneq (/usr,$(PFM_PREFIX))
   PWD = $(shell pwd)
   ifeq (,$(PFM_LIB_PATH))
      ifeq (,$(PFM_ROOT))
         PFM_ROOT := $(PWD)/libpfm4
      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)/libpfm4
      endif
      PFM_INC_PATH := $(PFM_ROOT)/include
   endif
   ifneq (/usr/include,$(PFM_INC_PATH))
      LIBCFLAGS	+= -I$(PFM_INC_PATH)
   endif
endif

LIBCFLAGS += -fvisibility=hidden
MISCHDRS += linux-lock.h mb.h papi_libpfm4_events.h
MISCSRCS += papi_libpfm4_events.c
SHLIBDEPS = -Bdynamic -L$(PFM_LIB_PATH) -lpfm
PFM_OBJS=$(shell $(AR) t $(PFM_LIB_PATH)/libpfm.a 2>/dev/null)
MISCOBJS = $(PFM_OBJS) $(MISCSRCS:.c=.o)

ifeq (yes,$(MIC))
	FORCE_PFM_ARCH="CONFIG_PFMLIB_ARCH_X86=y"
endif

ifeq (,$(PFM_OBJS))
$(PFM_LIB_PATH)/libpfm.a:
ifneq (,${PFM_ROOT})
ifeq ("$(CC_COMMON_NAME)","icc")
	$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" CC="$(CC) $(BITFLAGS)" DBG="-g -Wall -Werror" $(FORCE_PFM_ARCH) lib
else
ifeq (1,$(HAVE_NO_OVERRIDE_INIT))
	$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" CC="$(CC) $(BITFLAGS) -Wno-override-init" $(FORCE_PFM_ARCH) lib
else 
	$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" CC="$(CC) $(BITFLAGS)" $(FORCE_PFM_ARCH) lib
endif
endif
else
	@echo '$@ not installed!'; exit 1
endif
	$(MAKE)
endif

include Makefile.inc

config.h:
	@echo 'Please clobber your build and run ./configure."

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

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

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

native_install:
ifneq (,${PFM_ROOT})
	-$(MAKE) -C $(PFM_ROOT) ARCH="$(ARCH)" DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) install_prefix=$(PREFIX) LIBDIR=$(LIBDIR) INCDIR=$(INCDIR) MANDIR=$(MANDIR) install
endif
	-install -d $(DESTDIR)$(LIBDIR)
# Makefile.inc already has installation of shared libraries so
# there is no need to do it here
	-install -d $(DESTDIR)$(DATADIR)
	cp -f ./papi_events.csv $(DESTDIR)$(DATADIR)

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