explo91
explo91

Reputation: 560

CMake cross compile target rpath

I am cross-compiling using CMake.

In my CMakeLists.txt (used for both compile and cross compile):

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
find_package(foo REQUIRED)
add_library(mylib SHARED ${SRCS})
target_link_libraries(mylib ${FOO_LIBRARIES)

In my toolchain.cmake:

set(CMAKE_CXX_FLAGS "... --sysroot=/path/to/sysroot/ ... ")
set(CMAKE_CXX_LINK_FLAGS "... --sysroot=/path/to/sysroot/ ... )
...
set(CMAKE_FIND_ROOT_PATH /path/to/sysroot)

Consider foo is located to /path/to/sysroot/usr/local/lib/foo.so, when i cross-compile the runtime path for mylib is /path/to/sysroot/usr/local/lib

I want that the runtime path is /usr/local/lib to reflect my target filesystem.

How can i do this without define a hard-coded CMAKE_INSTALL_RPATH variable in my CMakelists.txt ?

EDIT: I used /usr/local/lib for the example but foo lib are located to a specific folder that is not a part of the system dirs: /path/to/sysroot/usr/local/share/mypackage/lib/foo.so

Upvotes: 3

Views: 6086

Answers (3)

Alex B
Alex B

Reputation: 53

all you need is:

set(CMAKE_BUILD_RPATH "/my/libs/location")

specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). The entries will not be used for binaries in the install tree. See also the CMAKE_INSTALL_RPATH variable.

Upvotes: 0

benf
benf

Reputation: 1005

Check out the wiki on CMake RPATH handling.

By default, CMake compiles your executable with an RPATH pointing to the host-system library location (/crosssdk/sysroot/usr/lib/) and then (I believe) when installing (ie. make install) it edits the RPATH in the executable to replace it with the appropriate target RPATH (/usr/lib or wherever you've got it). I think the idea is that then you can make changes to the shared lib and execute the output on your host system without having to install both the shared lib and executable every time. In my case, my host is x86 and target is ARM, so I tell CMake to set the build RPATH the same as the install RPATH:

set_target_properties(mytarget PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)

Upvotes: 3

leodotcloud
leodotcloud

Reputation: 1960

I am not sure which cross compile toolchain you are using.

You need to specify the C/CXX compilers, Linker etc. Along with that some of the important variables are CMAKE_FIND_ROOT_PATH_MODE_LIBRARY and CMAKE_FIND_ROOT_PATH_MODE_INCLUDE. If you set them to "ONLY", when you make calls to FindXXX(), the search happens only in the target root file system directory but not the build machine.

In my case I don't have to specify the sysroot as the cross compiler already knows that it's cross compiling and it also knows the location of the target root file system.

With this toolchain file, I just compile the sources without any additional flags, load the executable on the target and it runs fine picking up the *.so file directly from the right path.

Give it a try with this and let me know how it goes.

Here is my toolchain file:

set(ELDK_DIR /opt/eldk/ppc-v42-1)


set (CMAKE_C_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-gcc)
set (CMAKE_CXX_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-g++)
set (CMAKE_LINKER ${ELDK_DIR}/usr/bin/ppc_6xx-ld CACHE STRING "Set the cross-compiler tool LD" FORCE)
set (CMAKE_AR ${ELDK_DIR}/usr/bin/ppc_6xx-ar CACHE STRING "Set the cross-compiler tool AR" FORCE)
set (CMAKE_NM ${ELDK_DIR}/usr/bin/ppc_6xx-nm CACHE STRING "Set the cross-compiler tool NM" FORCE)
set (CMAKE_OBJCOPY ${ELDK_DIR}/usr/bin/ppc_6xx-objcopy CACHE STRING "Set the cross-compiler tool OBJCOPY" FORCE)
set (CMAKE_OBJDUMP ${ELDK_DIR}/usr/bin/ppc_6xx-objdump CACHE STRING "Set the cross-compiler tool OBJDUMP" FORCE)
set (CMAKE_RANLIB ${ELDK_DIR}/usr/bin/ppc_6xx-ranlib CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
set (CMAKE_STRIP ${ELDK_DIR}/usr/bin/ppc_6xx-strip CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)

# Target environment
set (CMAKE_FIND_ROOT_PATH ${ELDK_DIR}/ppc_6xx)

# Don't search for programs in the host environment
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for the libraries and headers in the target environment
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Upvotes: 0

Related Questions