Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 170 additions & 102 deletions cmake/AddInstallRPATHSupport.cmake
Original file line number Diff line number Diff line change
@@ -1,95 +1,132 @@
#.rst:
# AddInstallRPATHSupport
# ----------------------
#
# Add support to RPATH during installation to your project::
#
# add_install_rpath_support([BIN_DIRS dir [dir]]
# [LIB_DIRS dir [dir]]
# [INSTALL_NAME_DIR [dir]]
# [DEPENDS condition [condition]]
# [USE_LINK_PATH])
#
# Normally (depending on the platform) when you install a shared
# library you can either specify its absolute path as the install name,
# or leave just the library name itself. In the former case the library
# will be correctly linked during run time by all executables and other
# shared libraries, but it must not change its install location. This
# is often the case for libraries installed in the system default
# library directory (e.g. ``/usr/lib``).
# In the latter case, instead, the library can be moved anywhere in the
# file system but at run time the dynamic linker must be able to find
# it. This is often accomplished by setting environmental variables
# (i.e. ``LD_LIBRARY_PATH`` on Linux).
# This procedure is usually not desirable for two main reasons:
#
# - by setting the variable you are changing the default behaviour
# of the dynamic linker thus potentially breaking executables (not as
# destructive as ``LD_PRELOAD``)
# - the variable will be used only by applications spawned by the shell
# and not by other processes.
#
# RPATH is aimed to solve the issues introduced by the second
# installation method. Using run-path dependent libraries you can
# create a directory structure containing executables and dependent
# libraries that users can relocate without breaking it.
# A run-path dependent library is a dependent library whose complete
# install name is not known when the library is created.
# Instead, the library specifies that the dynamic loader must resolve
# the library’s install name when it loads the executable that depends
# on the library. The executable or the other shared library will
# hardcode in the binary itself the additional search directories
# to be passed to the dynamic linker. This works great in conjunction
# with relative paths.
# This command will enable support to RPATH to your project.
# It will enable the following things:
#
# - If the project builds shared libraries it will generate a run-path
# enabled shared library, i.e. its install name will be resolved
# only at run time.
# - In all cases (building executables and/or shared libraries)
# dependent shared libraries with RPATH support will be properly
#
# The command has the following parameters:
#
# Options:
# - ``USE_LINK_PATH``: if passed the command will automatically adds to
# the RPATH the path to all the dependent libraries.
#
# Arguments:
# - ``BIN_DIRS`` list of directories when the targets (executable and
# plugins) will be installed.
# - ``LIB_DIRS`` list of directories to be added to the RPATH. These
# directories will be added "relative" w.r.t. the ``BIN_DIRS`` and
# ``LIB_DIRS``.
# - ``INSTALL_NAME_DIR`` directory where the libraries will be installed.
# This variable will be used only if ``CMAKE_SKIP_RPATH`` or
# ``CMAKE_SKIP_INSTALL_RPATH`` is set to ``TRUE`` as it will set the
# ``INSTALL_NAME_DIR`` on all targets
# - ``DEPENDS`` list of conditions that should be ``TRUE`` to enable
# RPATH, for example ``FOO; NOT BAR``.
#
# Note: see https://gitlab.kitware.com/cmake/cmake/issues/16589 for further
# details.

#=======================================================================
# Copyright 2014 Istituto Italiano di Tecnologia (IIT)
# @author Francesco Romano <francesco.romano@iit.it>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=======================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)

# SPDX-FileCopyrightText: 2012-2021 Istituto Italiano di Tecnologia (IIT)
# SPDX-License-Identifier: BSD-3-Clause

#[=======================================================================[.rst:
AddInstallRPATHSupport
----------------------

Add support to RPATH during installation to the project and the targets

.. command:: add_install_rpath_support

Add support to RPATH during installation to the project::

.. code-block:: cmake
add_install_rpath_support([BIN_DIRS dir [dir]]
[LIB_DIRS dir [dir]]
[INSTALL_NAME_DIR [dir]]
[DEPENDS condition [condition]]
[USE_LINK_PATH])

Normally (depending on the platform) when you install a shared
library you can either specify its absolute path as the install name,
or leave just the library name itself. In the former case the library
will be correctly linked during run time by all executables and other
shared libraries, but it must not change its install location. This
is often the case for libraries installed in the system default
library directory (e.g. ``/usr/lib``).
In the latter case, instead, the library can be moved anywhere in the
file system but at run time the dynamic linker must be able to find
it. This is often accomplished by setting environmental variables
(i.e. ``LD_LIBRARY_PATH`` on Linux).
This procedure is usually not desirable for two main reasons:

- by setting the variable you are changing the default behaviour
of the dynamic linker thus potentially breaking executables (not as
destructive as ``LD_PRELOAD``)
- the variable will be used only by applications spawned by the shell
and not by other processes.

RPATH aims in solving the issues introduced by the second
installation method. Using run-path dependent libraries you can
create a directory structure containing executables and dependent
libraries that users can relocate without breaking it.
A run-path dependent library is a dependent library whose complete
install name is not known when the library is created.
Instead, the library specifies that the dynamic loader must resolve
the library’s install name when it loads the executable that depends
on the library. The executable or the other shared library will
hardcode in the binary itself the additional search directories
to be passed to the dynamic linker. This works great in conjunction
with relative paths.
This command will enable support to RPATH to your project.
It will enable the following things:

- If the project builds shared libraries it will generate a run-path
enabled shared library, i.e. its install name will be resolved
only at run time.
- In all cases (building executables and/or shared libraries)
dependent shared libraries with RPATH support will have their name
resolved only at run time, by embedding the search path directly
into the built binary.

The command has the following parameters:

Options:
- ``USE_LINK_PATH``: if passed the command will automatically adds to
the RPATH the path to all the dependent libraries.

Arguments:
- ``BIN_DIRS`` list of directories when the targets (executable and
plugins) will be installed.
- ``LIB_DIRS`` list of directories to be added to the RPATH. These
directories will be added "relative" w.r.t. the ``BIN_DIRS`` and
``LIB_DIRS``.
- ``INSTALL_NAME_DIR`` directory where the libraries will be installed.
This variable will be used only if ``CMAKE_SKIP_RPATH`` or
``CMAKE_SKIP_INSTALL_RPATH`` is set to ``TRUE`` as it will set the
``INSTALL_NAME_DIR`` on all targets
- ``DEPENDS`` list of conditions that should be ``TRUE`` to enable
RPATH, for example ``FOO; NOT BAR``.

Note: see https://gitlab.kitware.com/cmake/cmake/issues/16589 for further
details.

.. command:: target_append_install_rpath

Add extra paths to RPATH for a specific target::

.. code-block:: cmake

target_append_install_rpath(<target>
<INSTALL_DESTINATION destination>
[LIB_DIRS dir [dir]]
[DEPENDS condition [condition]])

Arguments:
- ``INSTALL_DESTINATION`` path where the target will be installed.
- ``LIB_DIRS`` list of directories to be added to the RPATH. These
directories will be added "relative" w.r.t. the ``INSTALL_DESTINATION``.
- ``DEPENDS`` list of conditions that should be ``TRUE`` to enable
RPATH, for example ``FOO; NOT BAR``.
#]=======================================================================]

include(CMakeParseArguments)


macro(__AddInstallRPATHSupport_GET_SYSTEM_LIB_DIRS _var)
# Find system implicit lib directories
set(${_var} ${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES})
if(EXISTS "/etc/debian_version") # is this a debian system ?
if(CMAKE_LIBRARY_ARCHITECTURE)
list(APPEND ${_var} "/lib/${CMAKE_LIBRARY_ARCHITECTURE}"
"/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
endif()
endif()
endmacro()


macro(__AddInstallRPATHSupport_APPEND_RELATIVE_RPATH _var _bin_dir _lib_dir)
file(RELATIVE_PATH _rel_path ${_bin_dir} ${_lib_dir})
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND ${_var} "@loader_path/${_rel_path}")
else()
list(APPEND ${_var} "\$ORIGIN/${_rel_path}")
endif()
endmacro()



function(ADD_INSTALL_RPATH_SUPPORT)

set(_options USE_LINK_PATH)
Expand Down Expand Up @@ -131,25 +168,14 @@ function(ADD_INSTALL_RPATH_SUPPORT)
# Enable RPATH on OSX.
set(CMAKE_MACOSX_RPATH TRUE PARENT_SCOPE)

# Find system implicit lib directories
set(_system_lib_dirs ${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES})
if(EXISTS "/etc/debian_version") # is this a debian system ?
if(CMAKE_LIBRARY_ARCHITECTURE)
list(APPEND _system_lib_dirs "/lib/${CMAKE_LIBRARY_ARCHITECTURE}"
"/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
endif()
endif()
__AddInstallRPATHSupport_get_system_lib_dirs(_system_lib_dirs)

# This is relative RPATH for libraries built in the same project
foreach(lib_dir ${_ARS_LIB_DIRS})
list(FIND _system_lib_dirs "${lib_dir}" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
foreach(bin_dir ${_ARS_LIB_DIRS} ${_ARS_BIN_DIRS})
file(RELATIVE_PATH _rel_path ${bin_dir} ${lib_dir})
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND CMAKE_INSTALL_RPATH "@loader_path/${_rel_path}")
else()
list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/${_rel_path}")
endif()
__AddInstallRPATHSupport_append_relative_rpath(CMAKE_INSTALL_RPATH ${bin_dir} ${lib_dir})
endforeach()
endif()
endforeach()
Expand All @@ -165,3 +191,45 @@ function(ADD_INSTALL_RPATH_SUPPORT)
endif()

endfunction()


function(TARGET_APPEND_INSTALL_RPATH _target)
set(_options )
set(_oneValueArgs INSTALL_DESTINATION)
set(_multiValueArgs LIB_DIRS
DEPENDS)

if (CMAKE_SKIP_RPATH OR (CMAKE_SKIP_INSTALL_RPATH AND CMAKE_SKIP_BUILD_RPATH))
return()
endif()

cmake_parse_arguments(_ARS "${_options}"
"${_oneValueArgs}"
"${_multiValueArgs}"
"${ARGN}")

set(_rpath_available 1)
if(DEFINED _ARS_DEPENDS)
foreach(_dep ${_ARS_DEPENDS})
string(REGEX REPLACE " +" ";" _dep "${_dep}")
if(NOT (${_dep}))
set(_rpath_available 0)
endif()
endforeach()
endif()

if(_rpath_available)

__AddInstallRPATHSupport_get_system_lib_dirs(_system_lib_dirs)

get_target_property(_current_rpath ${_target} INSTALL_RPATH)
foreach(lib_dir ${_ARS_LIB_DIRS})
list(FIND _system_lib_dirs "${lib_dir}" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
__AddInstallRPATHSupport_append_relative_rpath(_current_rpath ${_ARS_INSTALL_DESTINATION} ${lib_dir})
endif()
endforeach()
set_target_properties(${_target} PROPERTIES INSTALL_RPATH "${_current_rpath}")
endif()

endfunction()
Loading