From 066e659d6e85b67b13e22e192771695cbc688cbd Mon Sep 17 00:00:00 2001 From: lancesix <98881381+lancesix@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:48:35 +0200 Subject: [PATCH] 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 --- cmake/Modules/Findlibdw.cmake | 79 ++++++++++++++++++ cmake/Modules/Findlibelf.cmake | 80 +++++++++++++++++++ cmake/rocprofiler_config_interfaces.cmake | 18 +++++ cmake/rocprofiler_interfaces.cmake | 2 + source/bin/CMakeLists.txt | 3 +- source/lib/CMakeLists.txt | 1 - .../rocprofiler-sdk-codeobj/CMakeLists.txt | 20 ++--- .../lib/rocprofiler-sdk-roctx/CMakeLists.txt | 1 + 8 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 cmake/Modules/Findlibdw.cmake create mode 100644 cmake/Modules/Findlibelf.cmake diff --git a/cmake/Modules/Findlibdw.cmake b/cmake/Modules/Findlibdw.cmake new file mode 100644 index 0000000000..5ab5ee9357 --- /dev/null +++ b/cmake/Modules/Findlibdw.cmake @@ -0,0 +1,79 @@ +# 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) diff --git a/cmake/Modules/Findlibelf.cmake b/cmake/Modules/Findlibelf.cmake new file mode 100644 index 0000000000..2b39f9f366 --- /dev/null +++ b/cmake/Modules/Findlibelf.cmake @@ -0,0 +1,80 @@ +# Try to find libelf headers and libraries. +# +# Usage of this module as follows: +# +# find_package(libelf) +# +# Variables used by this module, they can change the default behaviour and need +# to be set before calling find_package: +# +# libelf_ROOT Set this variable to the root installation of +# libelf if the module has problems finding the +# proper installation path. +# +# Variables defined by this module: +# +# libelf_FOUND System has libelf libraries and headers +# libelf_LIBRARIES The libelf library +# libelf_INCLUDE_DIRS The location of libelf headers +# +# Interface targets defined by this module: +# +# libelf::libelf +# + +find_package(PkgConfig) + +if(PkgConfig_FOUND) + pkg_check_modules(ELF libelf) + + if(ELF_FOUND) + set(libelf_INCLUDE_DIR + "${ELF_INCLUDE_DIRS}" + CACHE FILEPATH "libelf include directory") + set(libelf_LIBRARY + "${ELF_LIBRARIES}" + CACHE FILEPATH "libelf libraries") + endif() +endif() + +if(NOT PkgConfig_FOUND OR NOT ELF_FOUND) + find_path( + libelf_ROOT_DIR + NAMES include/gelf.h include/elf.h + HINTS ${libelf_ROOT} + PATHS ${libelf_ROOT}) + + mark_as_advanced(libelf_ROOT_DIR) + + find_path( + libelf_INCLUDE_DIR + NAMES gelf.h elf.h + HINTS ${libelf_ROOT} + PATHS ${libelf_ROOT} + PATH_SUFFIXES include) + + find_library( + libelf_LIBRARY + NAMES elf + HINTS ${libelf_ROOT} + PATHS ${libelf_ROOT} + PATH_SUFFIXES lib lib64) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(libelf DEFAULT_MSG libelf_LIBRARY libelf_INCLUDE_DIR) + +if(libelf_FOUND) + if(NOT TARGET libelf::libelf) + add_library(libelf::libelf INTERFACE IMPORTED) + endif() + + if(TARGET PkgConfig::ELF AND ELF_FOUND) + target_link_libraries(libelf::libelf INTERFACE PkgConfig::ELF) + else() + target_link_libraries(libelf::libelf INTERFACE ${libelf_LIBRARY}) + target_include_directories(libelf::libelf SYSTEM INTERFACE ${libelf_INCLUDE_DIR}) + endif() +endif() + +mark_as_advanced(libelf_INCLUDE_DIR libelf_LIBRARY) diff --git a/cmake/rocprofiler_config_interfaces.cmake b/cmake/rocprofiler_config_interfaces.cmake index 87a1e7f276..c4ae0d6cf0 100644 --- a/cmake/rocprofiler_config_interfaces.cmake +++ b/cmake/rocprofiler_config_interfaces.cmake @@ -177,6 +177,24 @@ target_link_libraries(rocprofiler-amd-comgr INTERFACE amd_comgr) target_link_libraries(rocprofiler-ptl INTERFACE PTL::ptl-static) +# ----------------------------------------------------------------------------------------# +# +# libelf +# +# ----------------------------------------------------------------------------------------# + +find_package(libelf REQUIRED) +target_link_libraries(rocprofiler-elf INTERFACE libelf::libelf) + +# ----------------------------------------------------------------------------------------# +# +# libdw +# +# ----------------------------------------------------------------------------------------# + +find_package(libdw REQUIRED) +target_link_libraries(rocprofiler-dw INTERFACE libdw::libdw) + # ----------------------------------------------------------------------------------------# # # amd aql diff --git a/cmake/rocprofiler_interfaces.cmake b/cmake/rocprofiler_interfaces.cmake index 07234c1553..3f25c76a4b 100644 --- a/cmake/rocprofiler_interfaces.cmake +++ b/cmake/rocprofiler_interfaces.cmake @@ -49,6 +49,8 @@ rocprofiler_add_interface_library(rocprofiler-fmt "C++ format string library" IN rocprofiler_add_interface_library(rocprofiler-cxx-filesystem "C++ filesystem library" INTERNAL) rocprofiler_add_interface_library(rocprofiler-ptl "Parallel Tasking Library" INTERNAL) +rocprofiler_add_interface_library(rocprofiler-elf "ElfUtils elf library" INTERNAL) +rocprofiler_add_interface_library(rocprofiler-dw "ElfUtils dw library" INTERNAL) # # interface for libraries (ROCm-specific) diff --git a/source/bin/CMakeLists.txt b/source/bin/CMakeLists.txt index e8d0562233..16d3988cd1 100644 --- a/source/bin/CMakeLists.txt +++ b/source/bin/CMakeLists.txt @@ -8,5 +8,6 @@ configure_file(rocprofv3 ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3 install( FILES rocprofv3 DESTINATION ${CMAKE_INSTALL_BINDIR} - PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ + WORLD_EXECUTE COMPONENT tools) diff --git a/source/lib/CMakeLists.txt b/source/lib/CMakeLists.txt index d69bab7869..0952ec7387 100644 --- a/source/lib/CMakeLists.txt +++ b/source/lib/CMakeLists.txt @@ -8,7 +8,6 @@ add_subdirectory(rocprofiler-sdk) set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "tools") add_subdirectory(rocprofiler-sdk-tool) add_subdirectory(rocprofiler-sdk-roctx) - add_subdirectory(rocprofiler-sdk-codeobj) if(ROCPROFILER_BUILD_TESTS) diff --git a/source/lib/rocprofiler-sdk-codeobj/CMakeLists.txt b/source/lib/rocprofiler-sdk-codeobj/CMakeLists.txt index 3ccfd8cbc9..6f45bd1dbd 100644 --- a/source/lib/rocprofiler-sdk-codeobj/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk-codeobj/CMakeLists.txt @@ -17,7 +17,6 @@ # ############################################################################## set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "codeobj") -find_package(amd_comgr REQUIRED) set(LIB_CODEOBJ_PARSER_SOURCES code_object_track.cpp code_printing.cpp disassembly.cpp) set(LIB_CODEOBJ_PARSER_HEADERS code_object_track.hpp code_printing.hpp disassembly.hpp @@ -27,28 +26,21 @@ add_library(rocprofiler-sdk-codeobj STATIC) target_sources(rocprofiler-sdk-codeobj PRIVATE ${LIB_CODEOBJ_PARSER_SOURCES}) target_link_libraries( rocprofiler-sdk-codeobj - PRIVATE amd_comgr dw elf rocprofiler::rocprofiler-build-flags + PRIVATE rocprofiler::rocprofiler-amd-comgr rocprofiler::rocprofiler-dw + rocprofiler::rocprofiler-elf rocprofiler::rocprofiler-build-flags rocprofiler::rocprofiler-memcheck rocprofiler::rocprofiler-common-library) -target_include_directories(rocprofiler-sdk-codeobj PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -if(ROCPROFILER_BUILD_CODECOV) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -coverage") - target_link_libraries(rocprofiler-sdk-codeobj PUBLIC gcov) -endif() +target_include_directories(rocprofiler-sdk-codeobj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) set_target_properties( rocprofiler-sdk-codeobj PROPERTIES LIBRARY_OUTPUT_DIRECTORY - ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/rocprofiler-sdk-codeobj - SOVERSION ${PROJECT_VERSION_MAJOR} - VERSION ${PROJECT_VERSION} - BUILD_RPATH "\$ORIGIN:\$ORIGIN/.." - INSTALL_RPATH "\$ORIGIN:\$ORIGIN/..") + ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/rocprofiler-sdk + POSITION_INDEPENDENT_CODE ON) install( TARGETS rocprofiler-sdk-codeobj - DESTINATION ${CMAKE_INSTALL_LIBDIR}/rocprofiler-sdk-codeobj + DESTINATION ${CMAKE_INSTALL_LIBDIR}/rocprofiler-sdk COMPONENT tools EXPORT rocprofiler-sdk-codeobj-targets) diff --git a/source/lib/rocprofiler-sdk-roctx/CMakeLists.txt b/source/lib/rocprofiler-sdk-roctx/CMakeLists.txt index ef51dd883a..eda8161a05 100644 --- a/source/lib/rocprofiler-sdk-roctx/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk-roctx/CMakeLists.txt @@ -1,6 +1,7 @@ # # ROCTx Marker Library # +set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "roctx") find_package(rocprofiler-register REQUIRED)