Skip to content

Commit

Permalink
Libiberty cmake modernization (#1334)
Browse files Browse the repository at this point in the history
* LibIberty cmake modernization

* Use INCLUDE_DIRS directly
  • Loading branch information
hainest committed Dec 13, 2022
1 parent 7eb85ac commit c6b3648
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 128 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ find_package(Threads)
include(DyninstBoost)
include(DyninstTBB)
include(DyninstElfUtils)

if(UNIX)
include(DyninstLibIberty REQUIRED)
endif()
include(DyninstLibIberty)

include(DyninstLibrary)

Expand Down
1 change: 1 addition & 0 deletions cmake/DyninstConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
include(DyninstTBB)
include(DyninstBoost)
include(DyninstElfUtils)
include(DyninstLibIberty)

# compute paths

Expand Down
108 changes: 46 additions & 62 deletions cmake/Modules/FindLibIberty.cmake
Original file line number Diff line number Diff line change
@@ -1,80 +1,64 @@
#========================================================================================
# FindLibIberty.cmake
#
# Find LibIberty include dirs and libraries
#
# ----------------------------------------
#
# Use this module by invoking find_package with the form::
#
# find_package(LibIberty
# [REQUIRED] # Fail with error if LibIberty is not found
# )
#
# This module reads hints about search locations from variables::
#
# LibIberty_ROOT_DIR - Base directory the of LibIberty installation
# LibIberty_LIBRARYDIR - Hint directory that contains the LibIberty library files
# IBERTY_LIBRARIES - Alias for LibIberty_LIBRARIES (backwards compatibility only)
# LibIberty_INCLUDEDIR - Hint directory that contains the libiberty headers files
#
# and saves search results persistently in CMake cache entries::
#
# LibIberty_FOUND - True if headers and requested libraries were found
# IBERTY_FOUND - Alias for LibIberty_FOUND (backwards compatibility only)
# LibIberty_INCLUDE_DIRS - libiberty include directories
# LibIberty_LIBRARY_DIRS - Link directories for LibIberty libraries
# LibIberty_LIBRARIES - LibIberty library files
# IBERTY_LIBRARIES - Alias for LibIberty_LIBRARIES (backwards compatibility only)
#
#========================================================================================
#[=======================================================================[.rst:
FindLibIberty
-------------

if(LibIberty_FOUND)
return()
endif()
Find libiberty, a collection of subroutines used by various GNU programs.

# Keep the semantics of IBERTY_LIBRARIES for backward compatibility NB: If both are
# specified, LibIberty_LIBRARIES is ignored
if(NOT "${IBERTY_LIBRARIES}" STREQUAL "")
set(LibIberty_LIBRARIES ${IBERTY_LIBRARIES})
endif()
Imported targets
^^^^^^^^^^^^^^^^

This module defines the following :prop_tgt:`IMPORTED` target:

``LibIberty::LibIberty``
The libiberty library, if found.

include(DyninstSystemPaths)
Result variables
^^^^^^^^^^^^^^^^

# Non-standard subdirectories to search
set(_path_suffixes libiberty iberty)
This module will set the following variables in your project:

``LibIberty_INCLUDE_DIRS``
where to find libiberty.h, etc.
``LibIberty_LIBRARIES``
the libraries to link against to use libiberty.
``LibIberty_FOUND``
If false, do not try to use libiberty.

LibIberty does not have its own version number or release schedule.
See https://gcc.gnu.org/onlinedocs/libiberty/Using.html#Using for details.

#]=======================================================================]
cmake_policy(SET CMP0074 NEW) # Use <Package>_ROOT

find_path(
LibIberty_INCLUDE_DIRS
LibIberty_INCLUDE_DIR
NAMES libiberty.h
HINTS ${LibIberty_ROOT_DIR} ${LibIberty_ROOT_DIR}/include ${LibIberty_INCLUDEDIR}
PATHS ${DYNINST_SYSTEM_INCLUDE_PATHS}
PATH_SUFFIXES ${_path_suffixes}
DOC "LibIberty include directories")
PATH_SUFFIXES libiberty)
mark_as_advanced(LibIberty_INCLUDE_DIR)

# iberty_pic is for Debian <= wheezy
find_library(
LibIberty_LIBRARIES
NAMES iberty_pic iberty
HINTS ${LibIberty_ROOT_DIR} ${LibIberty_LIBRARYDIR} ${IBERTY_LIBRARIES}
PATHS ${DYNINST_SYSTEM_LIBRARY_PATHS}
PATH_SUFFIXES ${_path_suffixes})
LibIberty_LIBRARY
NAMES libiberty iberty
PATH_SUFFIXES libiberty)
mark_as_advanced(LibIberty_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
LibIberty
FOUND_VAR LibIberty_FOUND
REQUIRED_VARS LibIberty_LIBRARIES)

# For backwards compatibility only
set(IBERTY_FOUND ${LibIberty_FOUND})
REQUIRED_VARS LibIberty_LIBRARY LibIberty_INCLUDE_DIR)

if(LibIberty_FOUND)
foreach(l ${LibIberty_LIBRARIES})
get_filename_component(_dir ${l} DIRECTORY)
list(APPEND LibIberty_LIBRARY_DIRS ${_dir})
endforeach()
set(LibIberty_INCLUDE_DIRS ${LibIberty_INCLUDE_DIR})
set(LibIberty_LIBRARIES ${LibIberty_LIBRARY})

if(NOT TARGET LibIberty::LibIberty)
add_library(LibIberty::LibIberty UNKNOWN IMPORTED)
set_target_properties(LibIberty::LibIberty PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${LibIberty_INCLUDE_DIRS}")

# For backwards compatibility only
set(IBERTY_LIBRARIES ${LibIberty_LIBRARIES})
set_target_properties(
LibIberty::LibIberty PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${LibIberty_LIBRARIES}")
endif()
endif()
70 changes: 15 additions & 55 deletions cmake/tpls/DyninstLibIberty.cmake
Original file line number Diff line number Diff line change
@@ -1,73 +1,33 @@
#======================================================================================
# LibIberty.cmake
#================================================================
#
# Configure LibIberty for Dyninst
# Configure libiberty
#
# ----------------------------------------
#
# Directly exports the following CMake variables
# LibIberty_ROOT_DIR - Directory hint for libiberty installation
#
# LibIberty_ROOT_DIR - Computed base directory the of LibIberty installation
# LibIberty_LIBRARY_DIRS - Link directories for LibIberty libraries
# LibIberty_LIBRARIES - LibIberty library files
# LibIberty_INCLUDE - LibIberty include files
#
# NOTE:
# The exported LibIberty_ROOT_DIR can be different from the value provided by the user
# in the case that it is determined to build LibIberty from source. In such a case,
# LibIberty_ROOT_DIR will contain the directory of the from-source installation.
#
# See Modules/FindLibIberty.cmake for details
#
#======================================================================================
#================================================================

if(LibIberty_FOUND)
return()
endif()
include_guard(GLOBAL)

# libiberty is only available on Unixes; provide a dummy target on other platforms
if(NOT UNIX)
if(NOT TARGET Dyninst::LibIberty)
add_library(Dyninst::LibIberty INTERFACE)
endif()
return()
endif()

# -------------- PATHS --------------------------------------------------------

# Base directory the of LibIberty installation
set(LibIberty_ROOT_DIR
"/usr"
CACHE PATH "Base directory the of LibIberty installation")

# Hint directory that contains the LibIberty library files
set(LibIberty_LIBRARYDIR
"${LibIberty_ROOT_DIR}/lib"
CACHE PATH "Hint directory that contains the LibIberty library files")

# -------------- PACKAGES -----------------------------------------------------
mark_as_advanced(LibIberty_ROOT_DIR)

find_package(LibIberty REQUIRED)

# -------------- EXPORT VARIABLES ---------------------------------------------

add_library(LibIberty STATIC IMPORTED GLOBAL)
set_target_properties(LibIberty PROPERTIES IMPORTED_LOCATION ${LibIberty_LIBRARIES})
set_target_properties(LibIberty PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${LibIberty_INCLUDE_DIRS})

set(LibIberty_ROOT_DIR
${LibIberty_ROOT_DIR}
CACHE PATH "Base directory the of LibIberty installation" FORCE)
set(LibIberty_INCLUDE_DIRS
${LibIberty_INCLUDE_DIRS}
CACHE PATH "LibIberty include directories" FORCE)
set(LibIberty_LIBRARY_DIRS
${LibIberty_LIBRARY_DIRS}
CACHE PATH "LibIberty library directory" FORCE)
set(LibIberty_LIBRARIES
${LibIberty_LIBRARIES}
CACHE FILEPATH "LibIberty library files" FORCE)

# For backward compatibility only
set(IBERTY_LIBRARIES ${LibIberty_LIBRARIES})

message(STATUS "LibIberty include dirs: ${LibIberty_INCLUDE_DIRS}")
message(STATUS "LibIberty library dirs: ${LibIberty_LIBRARY_DIRS}")
message(STATUS "LibIberty libraries: ${LibIberty_LIBRARIES}")
if(NOT TARGET Dyninst::LibIberty)
add_library(Dyninst::LibIberty INTERFACE IMPORTED)
target_include_directories(Dyninst::LibIberty SYSTEM INTERFACE ${LibIberty_INCLUDE_DIRS})
target_link_libraries(Dyninst::LibIberty INTERFACE LibIberty::LibIberty)
endif()
10 changes: 3 additions & 7 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,16 @@ add_definitions(-DCOMMON_LIB)

dyninst_library(common)

if(TARGET LibIberty)
add_dependencies(common LibIberty)
target_link_libraries(common PRIVATE LibIberty)

if(${ENABLE_STATIC_LIBS})
target_link_libraries(common_static PRIVATE LibIberty)
endif()
if(${ENABLE_STATIC_LIBS})
target_link_libraries(common_static PRIVATE Dyninst::LibIberty)
endif()

if(PLATFORM MATCHES nt OR PLATFORM MATCHES windows)
target_link_private_libraries(common Psapi WS2_32 dbghelp)
endif()

target_link_libraries(common PUBLIC Dyninst::TBB Dyninst::Boost)
target_link_libraries(common PRIVATE Dyninst::LibIberty)

if(USE_OpenMP)
set_target_properties(common PROPERTIES COMPILE_FLAGS ${OpenMP_CXX_FLAGS}
Expand Down

0 comments on commit c6b3648

Please sign in to comment.