Cross-compilation issue with CHECK_SYMBOL_EXISTS

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Cross-compilation issue with CHECK_SYMBOL_EXISTS

Jörg Krause
Hi,

I have an issue using CMake for cross-compiling bctoolbox on a Debian
64-bit host system as a 32-bit target. The issue is in how the linker
works when using CHECK_SYMBOL_EXISTS.

bctoolbox uses its own module FindMbedTLS.cmake to check for the symbol
`mbedtls_ssl_init`:

"""
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)
        cmake_push_check_state(RESET)
        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)
        cmake_pop_check_state()
endif()
"""

The check fails because the linker tries to link with the host zlib:

"""
/home/test/autobuild/run/instance-2/output/host/usr/bin/i586-linux-
gcc  --sysroot=/home/test/autobuild/run/instance-
2/output/host/usr/i586-buildroot-linux-musl/sysroot -D_LARGEFILE_SOURCE
-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os   -
DNDEBUG    CMakeFiles/cmTC_4cdd8.dir/CheckSymbolExists.c.o  -o
cmTC_4cdd8 -Wl,-rpath,/usr/lib32 -rdynamic
/home/test/autobuild/run/instance-2/output/host/usr/i586-buildroot-
linux-musl/sysroot/usr/lib32/libmbedtls.so
/home/test/autobuild/run/instance-2/output/host/usr/i586-buildroot-
linux-musl/sysroot/usr/lib32/libmbedx509.so
/home/test/autobuild/run/instance-2/output/host/usr/i586-buildroot-
linux-musl/sysroot/usr/lib32/libmbedcrypto.so 
/home/test/autobuild/run/instance-2/output/host/opt/ext-
toolchain/bin/../lib/gcc/i586-buildroot-linux-
musl/5.4.0/../../../../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@GLIBC_2.0'
/usr/lib32/libz.so.1: undefined reference to `free@GLIBC_2.0'
"""

As you can see, the linker uses `-Wl,-rpath,/usr/lib32` which should
not be used when cross-compiling. I had no success in using any CMAKE
symbol to get rid of this linker flag.

The toolchain file:

"""
#
# Automatically generated file; DO NOT EDIT.
# CMake toolchain file for Buildroot
#

# In order to allow the toolchain to be relocated, we calculate the
# HOST_DIR based on this file's location:
$(HOST_DIR)/usr/share/buildroot
# and store it in RELOCATED_HOST_DIR.
# All the other variables that need to refer to HOST_DIR will use the
# RELOCATED_HOST_DIR variable.
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++")
"""

Any idea how to fix the linking issue?

Best regards,
Jörg Krause
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake