Skip to content

Commit

Permalink
Updates likwid-config.cmake to support find_package
Browse files Browse the repository at this point in the history
- added test/cmake for testing whether the find_package works
- modified installation location (required for find_package to work
  • Loading branch information
jrmadsen committed Jul 30, 2020
1 parent 80a088e commit 7e3ae8c
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,23 @@ $(L_HELPER):
$(STATIC_TARGET_LIB): $(BUILD_DIR) $(PERFMONHEADERS) $(OBJ) $(TARGET_HWLOC_LIB) $(TARGET_LUA_LIB)
@echo "===> CREATE STATIC LIB $(TARGET_LIB)"
$(Q)${AR} -crs $(STATIC_TARGET_LIB) $(OBJ) $(TARGET_HWLOC_LIB) $(TARGET_LUA_LIB)
@sed -e s+'@PREFIX@'+$(INSTALLED_PREFIX)+g make/likwid-config.cmake > likwid-config.cmake
@sed -e s+'@PREFIX@'+$(INSTALLED_PREFIX)+g \
-e s+'@NVIDIA_INTERFACE@'+$(NVIDIA_INTERFACE)+g \
-e s+'@FORTRAN_INTERFACE@'+$(FORTRAN_INTERFACE)+g \
-e s+'@LIBPREFIX@'+$(LIBPREFIX)+g \
-e s+'@BINPREFIX@'+$(BINPREFIX)+g \
make/likwid-config.cmake > likwid-config.cmake

$(DYNAMIC_TARGET_LIB): $(BUILD_DIR) $(PERFMONHEADERS) $(OBJ) $(TARGET_HWLOC_LIB) $(TARGET_LUA_LIB)
@echo "===> CREATE SHARED LIB $(TARGET_LIB)"
$(Q)${CC} $(DEBUG_FLAGS) $(SHARED_LFLAGS) -Wl,-soname,$(TARGET_LIB).$(VERSION).$(RELEASE) $(SHARED_CFLAGS) -o $(DYNAMIC_TARGET_LIB) $(OBJ) $(LIBS) $(TARGET_HWLOC_LIB) $(TARGET_LUA_LIB) $(RPATHS)
@ln -sf $(TARGET_LIB) $(TARGET_LIB).$(VERSION).$(RELEASE)
@sed -e s+'@PREFIX@'+$(INSTALLED_PREFIX)+g make/likwid-config.cmake > likwid-config.cmake
@sed -e s+'@PREFIX@'+$(INSTALLED_PREFIX)+g \
-e s+'@NVIDIA_INTERFACE@'+$(NVIDIA_INTERFACE)+g \
-e s+'@FORTRAN_INTERFACE@'+$(FORTRAN_INTERFACE)+g \
-e s+'@LIBPREFIX@'+$(LIBPREFIX)+g \
-e s+'@BINPREFIX@'+$(BINPREFIX)+g \
make/likwid-config.cmake > likwid-config.cmake

$(DAEMON_TARGET): $(SRC_DIR)/access-daemon/accessDaemon.c
@echo "===> BUILD access daemon likwid-accessD"
Expand Down Expand Up @@ -515,7 +525,8 @@ install: install_daemon install_freq install_appdaemon
@for F in $(FILTERS); do \
install -m 755 $$F $(abspath $(PREFIX)/share/likwid/filter); \
done
@install -m 644 likwid-config.cmake $(LIBPREFIX)
@echo "===> INSTALL cmake to $(abspath $(PREFIX)/share/likwid)"
@install -m 644 likwid-config.cmake $(PREFIX)/share/likwid

move: move_daemon move_freq move_appdaemon
@echo "===> MOVE applications from $(BINPREFIX) to $(INSTALLED_BINPREFIX)"
Expand Down Expand Up @@ -581,7 +592,9 @@ move: move_daemon move_freq move_appdaemon
@chmod 755 $(LIKWIDFILTERPATH)
@cp -f $(abspath $(PREFIX)/share/likwid/filter)/* $(LIKWIDFILTERPATH)
@chmod 755 $(LIKWIDFILTERPATH)/*
@install -m 644 $(LIBPREFIX)/likwid-config.cmake $(INSTALLED_LIBPREFIX)
@mkdir -p $(INSTALLED_LIBPREFIX)/cmake/likwid
@chmod 755 $(INSTALLED_LIBPREFIX)/cmake/likwid
@install -m 644 $(LIBPREFIX)/likwid-config.cmake $(INSTALLED_LIBPREFIX)/share/likwid

uninstall: uninstall_daemon uninstall_freq uninstall_appdaemon
@echo "===> REMOVING applications from $(PREFIX)/bin"
Expand Down Expand Up @@ -617,8 +630,8 @@ uninstall: uninstall_daemon uninstall_freq uninstall_appdaemon
@rm -rf $(PREFIX)/share/likwid/perfgroups
@rm -rf $(PREFIX)/share/likwid/docs
@rm -rf $(PREFIX)/share/likwid/examples
@rm -rf $(PREFIX)/share/likwid/likwid-config.cmake
@rm -rf $(PREFIX)/share/likwid
@rm -rf $(LIBPREFIX)/likwid-config.cmake

uninstall_moved: uninstall_daemon_moved uninstall_freq_moved uninstall_appdaemon_moved
@echo "===> REMOVING applications from $(INSTALLED_PREFIX)/bin"
Expand Down Expand Up @@ -654,8 +667,8 @@ uninstall_moved: uninstall_daemon_moved uninstall_freq_moved uninstall_appdaemon
@rm -rf $(INSTALLED_PREFIX)/share/likwid/perfgroups
@rm -rf $(INSTALLED_PREFIX)/share/likwid/docs
@rm -rf $(INSTALLED_PREFIX)/share/likwid/examples
@rm -rf $(INSTALLED_PREFIX)/share/likwid/likwid-config.cmake
@rm -rf $(INSTALLED_PREFIX)/share/likwid
@rm -rf $(INSTALLED_LIBPREFIX)/likwid-config.cmake

local: $(L_APPS) likwid.lua
@echo "===> Setting Lua scripts to run from current directory"
Expand Down
188 changes: 188 additions & 0 deletions make/likwid-config.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,192 @@
# legacy variables
set(LIKWID_INCLUDES @PREFIX@/include)
set(LIKWID_LIBRARIES @PREFIX@/lib/liblikwid.so -llikwid)

# uses IN_LIST so return if less than 3.3.2
if(CMAKE_VERSION VERSION_LESS 3.3.2)
return()
endif()

# Support new if() IN_LIST operator
cmake_policy(PUSH)
cmake_policy(SET CMP0057 NEW)

# updates
include(FindPackageHandleStandardArgs)

# variables configured during install
set(LIKWID_ROOT_DIR @PREFIX@ CACHE PATH "Path to LIKWID installation")
set(LIKWID_NVIDIA_INTERFACE @NVIDIA_INTERFACE@)
set(LIKWID_FORTRAN_INTERFACE @FORTRAN_INTERFACE@)

set(_LIKWID_PATH_HINTS @PREFIX@ @LIBPREFIX@ @BINPREFIX@)
set(_LIKWID_LIB_SUFFIXES lib lib64)

#------------------------------------------------------------------------------#
#
# Interface library
#
#------------------------------------------------------------------------------#

# create an imported target library that CMake projects can just "link" to.
# e.g. target_link_libraries(myexe PUBLIC likwid)
add_library(likwid-library INTERFACE)
add_library(likwid::likwid ALIAS likwid-library)
# add_library(likwid::Likwid ALIAS likwid)

#------------------------------------------------------------------------------#
#
# Include directory and library
#
#------------------------------------------------------------------------------#

# this is the cached variable to the include path
# if not found, will be LIKWID_INCLUDE_DIR-NOTFOUND
find_path(LIKWID_INCLUDE_DIR
NAMES likwid.h
HINTS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATHS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATH_SUFFIXES include
DOC "LIKWID profiler include directory"
NO_DEFAULT_PATH)

find_library(LIKWID_LIBRARY
NAMES likwid
PATH_SUFFIXES ${_LIKWID_LIB_SUFFIXES}
HINTS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATHS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
DOC "LIKWID library")

#------------------------------------------------------------------------------#
#
# Local (non-cached) variables
#
#------------------------------------------------------------------------------#

# this is local (directory-scoped) variable to the include path
# if cached variable is LIKWID_INCLUDE_DIR-NOTFOUND, then
# LIKWID_INCLUDE_DIRS will be empty
if(LIKWID_INCLUDE_DIR)
list(APPEND LIKWID_INCLUDE_DIRS ${LIKWID_INCLUDE_DIR})
endif()

# this is local (directory-scoped) variable to the likwid library
# if cached variable is LIKWID_LIBRARY-NOTFOUND, then
# LIKWID_LIBRARIES will be empty
if(LIKWID_LIBRARY)
get_filename_component(_LIB_DIR "${LIKWID_LIBRARY}" PATH)
list(APPEND LIKWID_LIBRARIES ${LIKWID_LIBRARY})
list(APPEND LIKWID_LIBRARY_DIRS ${_LIB_DIR})
target_link_libraries(likwid-library INTERFACE ${LIKWID_LIBRARY})
endif()

#------------------------------------------------------------------------------#
#
# Executables
#
#------------------------------------------------------------------------------#

set(LIKWID_EXECUTABLE_OPTIONS
bench
features
genTopoCfg
lua
memsweeper
mpirun
perfctr
perfscope
pin
powermeter
setFrequencies
topology)

foreach(_EXE ${LIKWID_EXECUTABLE_OPTIONS})
find_program(LIKWID_${_EXE}_EXECUTABLE
NAMES likwid-${_EXE}
HINTS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATHS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATH_SUFFIXES bin
DOC "LIKWID ${_EXE} executable"
NO_DEFAULT_PATH)
endforeach()

#------------------------------------------------------------------------------#
#
# Components
#
#------------------------------------------------------------------------------#

set(LIKWID_COMPONENT_OPTIONS
gotcha
hwloc
lua
pin
appDaemon
marker
nvmarker)

message(STATUS "COMPONENTS: ${likwid_FIND_COMPONENTS}")
if("marker" IN_LIST likwid_FIND_COMPONENTS)
target_compile_definitions(likwid-library INTERFACE LIKWID_PERFMON)
set(LIKWID_COMPILE_DEFINITIONS "-DLIKWID_PERFMON")
set(likwid_marker_FOUND ON)
list(APPEND likwid_FOUND_COMPONENTS marker)
endif()

if("nvmarker" IN_LIST likwid_FIND_COMPONENTS)
if(LIKWID_NVIDIA_INTERFACE)
target_compile_definitions(likwid-library INTERFACE LIKWID_NVMON)
set(LIKWID_COMPILE_DEFINITIONS "${LIKWID_COMPILE_DEFINITIONS} -DLIKWID_NVMON")
set(likwid_nvmarker_FOUND ON)
elseif(likwid_FIND_REQUIRED_nvmarker)
list(APPEND _LIKWID_MISSING_COMPONENTS nvmarker)
endif()
list(APPEND likwid_FOUND_COMPONENTS nvmarker)
endif()

# loop over the remaining components which have libraries, e.g. liblikwid-${NAME}.so
foreach(_COMP ${likwid_FIND_COMPONENTS})
if("${_COMP}" MATCHES "marker")
continue()
endif()
# find the library, e.g. likwid-lua, likwid-gotcha, likwidpin, etc.
find_library(LIKWID_${_COMP}_LIBRARY
NAMES likwid-${_COMP} likwid${_COMP}
HINTS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
PATHS ${LIKWID_ROOT_DIR} ${_LIKWID_PATH_HINTS}
DOC "LIKWID ${_COMP} library"
PATH_SUFFIXES ${_LIKWID_LIB_SUFFIXES})

# doesn't show up in GUI
mark_as_advanced(LIKWID_${_COMP}_LIBRARY)

if(LIKWID_${_COMP}_LIBRARY)
get_filename_component(_LIB_DIR "${LIKWID_${_COMP}_LIBRARY}" PATH)
list(APPEND LIKWID_LIBRARIES ${LIKWID_${_COMP}_LIBRARY})
list(APPEND LIKWID_LIBRARY_DIRS ${_LIB_DIR})
list(APPEND likwid_FOUND_COMPONENTS ${_COMP})
target_link_libraries(likwid-library INTERFACE ${LIKWID_${_COMP}_LIBRARY})
elseif(likwid_FIND_REQUIRED_${_COMP})
# only append to missing if required
list(APPEND _LIKWID_MISSING_COMPONENTS LIKWID_${_COMP}_LIBRARY)
endif()
endforeach()

#------------------------------------------------------------------------------#
# find package variables, e.g. LIKWID_FOUND
# if all required components were found _LIKWID_MISSING_COMPONENTS will
# be empty
find_package_handle_standard_args(
likwid
REQUIRED_VARS
LIKWID_ROOT_DIR LIKWID_INCLUDE_DIR LIKWID_LIBRARY ${_LIKWID_MISSING_COMPONENTS}
HANDLE_COMPONENTS)

#------------------------------------------------------------------------------#

# cleanup temporary variables
unset(_LIB_DIR)
unset(_LIKWID_PATH_HINTS)
unset(_LIKWID_LIB_SUFFIXES)

cmake_policy(POP)
8 changes: 8 additions & 0 deletions test/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(Likwid-Test LANGUAGES C)

find_package(likwid REQUIRED COMPONENTS marker OPTIONAL_COMPONENTS nvmarker)

add_executable(test ${CMAKE_SOURCE_DIR}/test.c)
target_link_libraries(test PRIVATE likwid::likwid)
16 changes: 16 additions & 0 deletions test/cmake/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include <likwid-marker.h>
#include <likwid.h>

#if !defined(LIKWID_PERFMON)
#error "LIKWID_PERFMON is not defined"
#endif

#if !defined(LIKWID_NVMON)
#warning "LIKWID_NVMON is not defined"
#endif

int main()
{
return 0;
}

0 comments on commit 7e3ae8c

Please sign in to comment.