# Set default if the root environment variable is not already set.
# Note PAPI_LIBMSR_ROOT is an environment variable that must be set.
# It is the ONLY environment variable that must be set, all other
# settings are optional.

PAPI_LIBMSR_ROOT ?= /sw/libmsr/0.1.17

# For non-typical system configurations, the following 'runtime overrides' can
# be set, as just a library name, or a full path and name. There cannot be any
# spaces between the double quotes (which must be escaped as \"). An example:

# PAPI_LIBMSR_MAIN = \"$(PAPI_LIBMSR_ROOT)/lib/libmsr.so\"

# By default, all overrides are empty strings.

# If an override is not an empty string, it must work, or the component will be
# disabled. 

# Both at compile time and run time, the software depends on PAPI_LIBMSR_ROOT.
# There is one library used by the LIBMSR component: 
# libmsr.so

# The standard installed locations for this library, with override:
# $(PAPI_LIBMSR_ROOT)/lib/libmsr.so      #O.R. PAPI_LIBMSR_MAIN
# 
# There are many ways to cause this path to be known. 
# Spack is a package manager used on supercomputers, Linux and MacOS. If Spack
# is aware of LIBMSR, it encodes the path to the library.

# The environment variable LD_LIBRARY_PATH encodes a list of paths to search for
# libraries; separated by a colon (:). This path could be added to 
# LD_LIBRARY_PATH. 
#
# Warning: LD_LIBRARY_PATH often contains a list of directories that are
# searched for libraries, some of these may be needed by other packages you are
# using. Always add to LD_LIBRARY_PATH recursively; for example: 
# >export LD_LIBRARY_PATH=someNewLibraryDirectory:$LD_LIBRARY_PATH 
# which would append the existing LD_LIBRARY_PATH to the new directory you wish
# to add.  Alternatively, you can prepend it: 
# >export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:someNewLibraryDirectory 
# Which will search the existing libraries first, then your new directory.

# You can check on the value of LD_LIBRARY_PATH with 
# >echo $LD_LIBRARY_PATH

# There may be other package managers or utilities, for example on a system with
# modules; the command 'module load libmsr' may modify LD_LIBRARY_PATH.

# A Linux system will also search for libraries by default in the directories
# listed by /etc/ld.so.conf, and /usr/lib64, /lib64, /usr/lib, /lib. 

# OVERRIDES: These are by default empty strings (""), if set they must work.
PAPI_LIBMSR_MAIN = \"\"

# An example of an override:
# PAPI_LIBMSR_MAIN = \"$(PAPI_LIBMSR_ROOT)/lib/libmsr.so.3\"

# Note:  If you change this override, PAPI should be rebuilt from scratch.
#        From papi/src/
#        make clobber
#        ./configure --with-components="libmsr"
#        make  

# OPERATION, per library:
# 1) If an override string is not empty, we will use it explicitly and fail if
# it does not work. This means disabling the component; a reason for disabling
# is shown using the papi utility, papi/src/utils/papi_component_avail

# 2) We will attempt to open the library using the normal system library search
# paths; if Spack is present and configured correctly it should deliver the
# proper library. A failure here will be silent; we will proceed to (3).

# 3) If that fails, we will try to find the library in the standard installed
# locations listed above. If this fails, we disable the component, the reason
# for disabling is shown using the papi utility,
# papi/src/utils/papi_component_avail. 

# DEFFLAGS is the macro defines for the three overrides. In the code we convert
# these to string variables with the following lines:
# static char libmsr_main[]=PAPI_LIBMSR_MAIN;

LIBMSR_MACS = -DPAPI_LIBMSR_MAIN=$(PAPI_LIBMSR_MAIN) 

COMPSRCS += components/libmsr/linux-libmsr.c 
COMPOBJS += linux-libmsr.o
# CFLAGS specifies compile flags; need include files here, and macro defines.
CFLAGS += -I$(PAPI_LIBMSR_ROOT)/include -I$(PAPI_LIBMSR_ROOT)/include/msr -g $(LIBMSR_MACS)
LDFLAGS += $(LDL) -g

linux-libmsr.o: components/libmsr/linux-libmsr.c $(HEADERS)
	$(CC) $(LIBCFLAGS) $(OPTFLAGS) -c components/libmsr/linux-libmsr.c -o linux-libmsr.o

