[CMake] How to set RPATH of Swig module

greg gregvananders at gmail.com
Fri Oct 12 11:46:32 EDT 2012


Hello,

I'm using CMake to build a project that builds a shared library, and a
Swig wrapper that links against that shared library.

I'm able to set the RPATH correctly in the shared library, but the one
that I get from compiling the Swig wrapper points to the build tree and
not the install path.

I've produced a minimal example that shows this behaviour, and I append
as a postscript below.

I assume I am missing something obvious, but I didn't see it here:

http://www.cmake.org/Wiki/CMake_RPATH_handling

and I haven't been able to stumble on the right keywords in Google to
find what I need. If anyone can help I would really appreciate it.

Thanks!
Greg

p.s. The problem: here is a listing of the source files:

$ find /home/grva/sandbox/rpath
/home/grva/sandbox/rpath
/home/grva/sandbox/rpath/src
/home/grva/sandbox/rpath/src/hello.cpp
/home/grva/sandbox/rpath/src/hello.h
/home/grva/sandbox/rpath/src/CMakeLists.txt
/home/grva/sandbox/rpath/src/hello.i
/home/grva/sandbox/rpath/CMakeLists.txt
/home/grva/sandbox/rpath/CMake
/home/grva/sandbox/rpath/CMake/CMakeLists.txt
/home/grva/sandbox/rpath/CMake/FindMyPython.cmake

The CMakeLists.txt files are:
$ cat /home/grva/sandbox/rpath/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)

PROJECT(hello)
SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
  "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
ENDIF("${isSystemDir}" STREQUAL "-1")

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH})
add_subdirectory(CMake)

find_package( SWIG REQUIRED )
include(${SWIG_USE_FILE})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set (CMAKE_SWIG_FLAGS "")

find_package(MyPython)
link_directories(${PYTHON_LIBRARY_DIRS})
include_directories(${PYTHON_INCLUDE_DIRS})

add_subdirectory(src)

$ cat /home/grva/sandbox/rpath/src/CMakeLists.txt
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set_source_files_properties(${PROJECT_NAME}.i PROPERTIES CPLUSPLUS ON)

FILE(GLOB CPP_FILES "*.cpp")
FILE(GLOB HEADER_FILES "*.h")

ADD_LIBRARY(${PROJECT_NAME} SHARED ${CPP_FILES})
INSTALL(TARGETS ${PROJECT_NAME} DESTINATION lib)
INSTALL(FILES ${HEADER_FILES} DESTINATION include/${PROJECT_NAME})

swig_add_module(${PROJECT_NAME} python ${PROJECT_NAME}.i)
swig_link_libraries(${PROJECT_NAME} ${PROJECT_NAME})
swig_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})

set(LIB_TARGET "_${PROJECT_NAME}.so")
set(PY_TARGET "${PROJECT_NAME}.py")

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${LIB_TARGET}
  ${CMAKE_CURRENT_BINARY_DIR}/${PY_TARGET}
  DESTINATION lib/python)

If I make with the install path in $HOME/local I get that:
$ readelf -d /home/grva/local/lib/libhello.so | grep RPATH
 0x000000000000000f (RPATH)              Library rpath:
[/home/grva/local/lib]
$ readelf -d /home/grva/local/lib/python/_hello.so | grep RPATH
 0x000000000000000f (RPATH)              Library rpath:
[/home/grva/sandbox/build_rpath/src]

The first thing is right, but the second points to my build tree, and I
want it to point to the install path.

The source files:

$ cat /home/grva/sandbox/rpath/src/hello.h
#include <iostream>

void hello(void);

$ cat /home/grva/sandbox/rpath/src/hello.cpp
#include "hello.h"

void hello(void)
{
  std::cout << "Hello World!" << std::endl;
}

$ cat /home/grva/sandbox/rpath/src/hello.i
%module hello
%{
#include "hello.h"
%}

%include "hello.h"





More information about the CMake mailing list