[CMake] RPATH cross-compile issue with CHECK_*_EXISTS

Jörg Krause joerg.krause at embedded.rocks
Mon Feb 6 18:16:06 EST 2017


On Mon, 2017-02-06 at 22:22 +0100, Jörg Krause wrote:
> Hi,
> 
> On Sun, 2017-02-05 at 23:03 +0100, Jörg Krause wrote:
> > Hi,
> > 
> > when using CHECK_{SYMBOL,FUNCTION}_EXISTS in a cross-compilation
> > environment, CMake passes the host rpath to the linker:
> > 
> > """ CMakeOutput.log
> > 
> > /home/joerg/host/usr/bin/i586-linux-gcc  
> > --sysroot=/home/joerg/host/usr/i586-buildroot-linux-musl/sysroot
> > -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
> > -Os   -
> > DNDEBUG    CMakeFiles/cmTC_cb8f6.dir/CheckSymbolExists.c.o  -o
> > cmTC_cb8f6 -Wl,-rpath,/usr/lib32 -rdynamic
> > /home/joerg/host/usr/i586-
> > buildroot-linux-musl/sysroot/usr/lib32/libmbedtls.so
> > /home/joerg/host/usr/i586-buildroot-linux-
> > musl/sysroot/usr/lib32/libmbedx509.so /home/joerg/host/usr/i586-
> > buildroot-linux-musl/sysroot/usr/lib32/libmbedcrypto.so 
> > 
> > """
> > 
> > This leads to a linker error if mbedtls is linked with zlib, as the
> > linker tries to link with the host zlib and does not find the host
> > libc:
> > 
> > """ CMakeError.txt
> > 
> > /home/joerg/host/usr/i586-buildroot-linux-musl/bin/ld: warning:
> > libc.so.6, needed by /usr/lib32/libz.so.1, not found (try using
> > -rpath
> > or -rpath-link)
> > /usr/lib32/libz.so.1: undefined reference to `strcpy at GLIBC_2.0'
> > /usr/lib32/libz.so.1: undefined reference to `free at GLIBC_2.0'
> > /usr/lib32/libz.so.1: undefined reference to `fseeko64 at GLIBC_2.1
> > [..]
> > 
> > """
> > 
> > I did not find any solution which allows me to remove the rpath
> > from
> > the check. Setting CMAKE_SKIP_RPATH does not change the build
> > behaviour of the check_symbol_exists macro.
> > 
> > From my understanding, the rpath flag should not be used when the
> > sysroot flag is passed to the linker, right?
> > 
> > For reference, I added a minimal example which uses Buildroot for
> > cross-compilation.
> > 
> > """ CMakeLists.txt
> > 
> > cmake_minimum_required(VERSION 2.8.12)
> > 
> > project(test)
> > 
> > list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
> > 
> > find_package(MbedTLS)
> > if(MBEDTLS_FOUND)
> > 	message(STATUS "Using mbedTLS")
> > endif()
> > 
> > """
> > 
> > """ FindMbedTLS.cmake
> > 
> > include(CheckSymbolExists)
> > 
> > find_path(MBEDTLS_INCLUDE_DIRS
> > 	NAMES mbedtls/ssl.h
> > 	PATH_SUFFIXES include
> > )
> > 
> > find_library(MBEDTLS_LIBRARY NAMES mbedtls)
> > find_library(MBEDX509_LIBRARY NAMES mbedx509)
> > find_library(MBEDCRYPTO_LIBRARY NAMES mbedcrypto)
> > 
> > if(MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
> > 	set(CMAKE_REQUIRED_INCLUDES ${MBEDTLS_INCLUDE_DIRS})
> > 	set(CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIBRARY}
> > ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
> > 	check_symbol_exists(mbedtls_ssl_init "mbedtls/ssl.h"
> > MBEDTLS_V2)
> > endif()
> > 
> > """
> > 
> > """ toolchainfile.cmake
> > 
> > string(REPLACE "/usr/share/buildroot" "" RELOCATED_HOST_DIR
> > ${CMAKE_CURRENT_LIST_DIR})
> > 
> > set(CMAKE_SYSTEM_NAME Linux)
> > set(CMAKE_SYSTEM_PROCESSOR i586)
> > 
> > set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
> > set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
> > set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release
> > CFLAGS")
> > set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release
> > CXXFLAGS")
> > 
> > # Build type from the Buildroot configuration
> > set(CMAKE_BUILD_TYPE Release CACHE STRING "Buildroot build
> > configuration")
> > 
> > # Buildroot defaults flags.
> > set(CMAKE_C_FLAGS "-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
> > -D_FILE_OFFSET_BITS=64 -Os" CACHE STRING "Buildroot CFLAGS")
> > set(CMAKE_CXX_FLAGS "-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
> > -D_FILE_OFFSET_BITS=64 -Os" CACHE STRING "Buildroot CXXFLAGS")
> > set(CMAKE_EXE_LINKER_FLAGS "" CACHE STRING "Buildroot LDFLAGS for
> > executables")
> > 
> > set(CMAKE_INSTALL_SO_NO_EXE 0)
> > 
> > set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/usr/bin")
> > set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/usr/i586-buildroot-linux-
> > musl/sysroot")
> > set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/usr/i586-buildroot-
> > linux-musl/sysroot")
> > set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
> > set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
> > set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
> > set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
> > set(ENV{PKG_CONFIG_SYSROOT_DIR} "${RELOCATED_HOST_DIR}/usr/i586-
> > buildroot-linux-musl/sysroot")
> > 
> > # This toolchain file can be used both inside and outside
> > Buildroot.
> > set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/usr/bin/i586-linux-
> > gcc")
> > set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/usr/bin/i586-linux-
> > g++")
> > 
> > """
> > 
> > What do I miss?
> 
> Note, that this project was build using CMake 3.7.2 on Debian. When
> using CMake 3.6.3, CMake does correctly not add rpath to the cross-
> linker and the check_symbol_exists() succeeds.

I did a git bisect. The behaviour was introduced in commit
896ad251de49f167f4ce3cbbcf9a6cce85a16681 [1].

My suggestion is to set FIND_LIBRARY_USE_LIB32_PATHS and
FIND_LIBRARY_USE_LIB64_PATHS to FALSE when cross-compiling on Linux.

[1] https://gitlab.kitware.com/cmake/cmake/commit/896ad251de49f167f4ce3
cbbcf9a6cce85a16681

Best regards,
Jörg Krause


More information about the CMake mailing list