Files
rocm-systems/cmake/Modules/Findlibdw.cmake
T
lancesix 066e659d6e rocprofiler-sdk-codeobj: use pkg-config to find libdw / libelf (#749)
* rocprofiler-sdk-codeobj: use pkg-config to find libdw / libelf

The current version of source/lib/rocprofiler-sdk-codeobj/CMakeLists.txt
adds -ldw and -lelf to target_link_libraries. However, on a system where
libdw-dev / libelf-dev is missing, the cmake configuration phase will
run properly and a compile time error will eventually be raised.

This patch changes the CMakelists.txt to search for libelf libdw and
configures the target as needed.  Systems missing the required support
should report an error when running cmake instead of in the middle of
the compilation.

* Use INTERFACE targets

* Resolve issues with Findlib{dw,elf}

---------

Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
2024-04-12 03:48:35 -05:00

80 lines
2.1 KiB
CMake

# Try to find libdw headers and libraries.
#
# Usage of this module as follows:
#
# find_package(libdw)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
# libdw_ROOT Set this variable to the root installation of
# libdw if the module has problems finding the
# proper installation path.
#
# Variables defined by this module:
#
# libdw_FOUND System has libdw libraries and headers
# libdw_LIBRARIES The libdw library
# libdw_INCLUDE_DIRS The location of libdw headers
#
# Interface targets defined by this module:
#
# libdw::libdw
#
find_package(PkgConfig)
if(PkgConfig_FOUND)
pkg_check_modules(DW libdw)
if(DW_FOUND)
set(libdw_INCLUDE_DIR
"${DW_INCLUDE_DIRS}"
CACHE FILEPATH "libdw include directory")
set(libdw_LIBRARY
"${DW_LIBRARIES}"
CACHE FILEPATH "libdw libraries")
endif()
endif()
if(NOT PkgConfig_FOUND OR NOT DW_FOUND)
find_path(
libdw_ROOT_DIR
NAMES include/elfutils/libdw.h
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT})
mark_as_advanced(libdw_ROOT_DIR)
find_path(
libdw_INCLUDE_DIR
NAMES elfutils/libdw.h
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT}
PATH_SUFFIXES include)
find_library(
libdw_LIBRARY
NAMES dw
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT}
PATH_SUFFIXES lib lib64)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libdw DEFAULT_MSG libdw_LIBRARY libdw_INCLUDE_DIR)
if(libdw_FOUND)
if(NOT TARGET libdw::libdw)
add_library(libdw::libdw INTERFACE IMPORTED)
endif()
if(TARGET PkgConfig::DW AND DW_FOUND)
target_link_libraries(libdw::libdw INTERFACE PkgConfig::DW)
else()
target_link_libraries(libdw::libdw INTERFACE ${libdw_LIBRARY})
target_include_directories(libdw::libdw SYSTEM INTERFACE ${libdw_INCLUDE_DIR})
endif()
endif()
mark_as_advanced(libdw_INCLUDE_DIR libdw_LIBRARY)