Fix rpath for private libraries on Linux
authorBenjamin Drung <benjamin.drung@canonical.com>
Thu, 22 Sep 2022 17:52:43 +0000 (19:52 +0200)
committerDennis Braun <snd@debian.org>
Wed, 1 Feb 2023 20:56:51 +0000 (21:56 +0100)
Installing audacity on Linux will produce private libraries that have
`RUNPATH` set to the build directory instead of the installation
directory.

The root cause is that the library directory is copied to the
installation directory without touching the libraries. The cmake wiki
says in RPATH handling caveats [1]: "Since install-side RPATH tweaking
is an operation that is done by target-specific installation handling,
any target that should have its install RPATH changed (e.g. to
`CMAKE_INSTALL_RPATH`) needs to end up in the installation via an
`install(TARGETS ...)` signature and not via directory-based copying."

So replace `install(DIRECTORY ...)` by individual `install(TARGETS ...)`
for the libraries and modules. Then cmake will replace the `RUNPATH` to
`$ORIGIN/../lib/audacity`, which is still incorrect. Therefore set
`INSTALL_RPATH` explicitly.

Fixes: https://github.com/audacity/audacity/issues/3289
Forwarded: https://github.com/audacity/audacity/pull/3671
[1] https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#caveats
Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
Gbp-Pq: Name Fix-rpath-for-private-libraries-on-Linux.patch

CMakeLists.txt
cmake-proxies/cmake-modules/AudacityFunctions.cmake
modules/CMakeLists.txt
modules/mod-script-pipe/CMakeLists.txt
src/CMakeLists.txt

index faef9647e25a7d027afa37893b9aca1db1449434..ce7eb5c83ed17ecd5f3f16e8b5277a65661e933a 100644 (file)
@@ -403,6 +403,7 @@ set( INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" )
 set( _LIBDIR "${CMAKE_INSTALL_LIBDIR}" )
 set( _DATADIR "${CMAKE_INSTALL_DATADIR}" )
 set( _PKGLIB "${_LIBDIR}/audacity" )
+set( _PKGMODULE "${_PKGLIB}/modules" )
 set( _PKGDATA "${_DATADIR}/audacity/" )
 set( _MANDIR "${CMAKE_INSTALL_MANDIR}" )
 set( _MODDIR "${_DEST}/modules" )
index 47682ebe836480bc902ec72e0034705f3388d033..a8f144f9a7a0255e0c5d53d33508acb97eef5c89 100644 (file)
@@ -338,6 +338,17 @@ function( canonicalize_node_name var node )
    set( "${var}" "${node}" PARENT_SCOPE )
 endfunction()
 
+# Call install(TARGETS...) only on Linux systems (i.e. exclude MacOS and Windows)
+macro( install_target_linux target destination )
+   if( NOT "${CMAKE_GENERATOR}" MATCHES "Xcode|Visual Studio*" AND NOT CMAKE_SYSTEM_NAME MATCHES "Darwin" )
+      install( TARGETS "${target}" DESTINATION "${destination}" )
+   endif()
+endmacro()
+
+macro( install_audacity_module target )
+   install_target_linux( "${target}" "${_PKGMODULE}" )
+endmacro()
+
 function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
    ADDITIONAL_DEFINES ADDITIONAL_LIBRARIES LIBTYPE )
 
@@ -382,6 +393,7 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
          PROPERTIES
             PREFIX ""
             FOLDER "modules" # for IDE organization
+            INSTALL_RPATH "$ORIGIN/.."
       )
       if( CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin" )
          add_custom_command(
@@ -423,6 +435,7 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
             PREFIX ""
             FOLDER "libraries" # for IDE organization
             INSTALL_NAME_DIR ""
+            INSTALL_RPATH "$ORIGIN"
             BUILD_WITH_INSTALL_NAME_DIR YES
       )
    endif()
@@ -525,6 +538,12 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
    endforeach()
    set( GRAPH_EDGES "${GRAPH_EDGES}" PARENT_SCOPE )
 
+   # Note: Some modules set EXCLUDE_FROM_ALL afterwards to not be installed.
+   # Therefore only install libraries, but not modules here.
+   if( NOT REAL_LIBTYPE STREQUAL "MODULE" )
+      install_target_linux( "${TARGET}" "${_PKGLIB}" )
+   endif()
+
    # collect unit test targets if they are present
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests")
       add_subdirectory(tests)
index d4643834cadd22484e9693981039a3269471aa2a..15dead4641d7c93ddf0cc05cdfe137633110b427 100644 (file)
@@ -16,12 +16,5 @@ foreach( MODULE ${MODULES} )
    add_subdirectory("${MODULE}")
 endforeach()
 
-if( NOT CMAKE_SYSTEM_NAME MATCHES "Darwin" )
-   if( NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio*")
-      install( DIRECTORY "${_DEST}/modules"
-               DESTINATION "${_PKGLIB}" )
-   endif()
-endif()
-
 #propagate collected edges up to root CMakeLists.txt
 set( GRAPH_EDGES "${GRAPH_EDGES}" PARENT_SCOPE )
index 5ee18627febdc87a4070710fe78690b5ae015e09..ccfd50f41c0e9d5cefbe8592d26aaf7c457fa37f 100644 (file)
@@ -15,3 +15,4 @@ set( DEFINES
 )
 audacity_module( mod-script-pipe "${SOURCES}" "Audacity"
    "${DEFINES}" "" )
+install_audacity_module( mod-script-pipe )
index a71cacd5310e18293c52311678323c3bfa9d17b6..9039256fb66e56aedbb4ac52cb1ba1c1f2ad9a9a 100644 (file)
@@ -1641,17 +1641,7 @@ else()
                DESTINATION "."
                RESOURCE DESTINATION "${_APPDIR}/Resources" )
    else()
-
       install( TARGETS ${TARGET} RUNTIME )
-
-      install( DIRECTORY "${_DEST}/${_LIBDIR}/"
-               DESTINATION "${_LIBDIR}"
-               USE_SOURCE_PERMISSIONS
-               FILES_MATCHING PATTERN "*.so*" )
-
-      install( CODE "set( _LIBDIR \"${_LIBDIR}\")" )
-      install( SCRIPT "${CMAKE_SOURCE_DIR}/scripts/build/linux/fix_rpath.cmake" )
-
       install( FILES "${_INTDIR}/audacity.desktop"
                DESTINATION "${_DATADIR}/applications" )
       install( FILES "${topdir}/LICENSE.txt" "${topdir}/README.txt"