[cmake-developers] Cross compiling toolchain variables

Stephen Kelly steveire at gmail.com
Thu Nov 14 17:34:22 EST 2013


Stephen Kelly wrote:
> Sounds good to me. I haven't looked into the implementation details like
> that yet, but I'll do so tomorrow.

I looked into the implementation details and used the following two 
toolchains to test it:

rpitoolchain.cmake:
 
  set(CMAKE_SYSTEM_NAME Linux)

  set(CMAKE_SYSROOT /home/stephen/rpi/rasp-pi-rootfs)
  set(CMAKE_STAGING_PREFIX /home/stephen/dev/src/qtbase-rpi/extprefix)

  set(CMAKE_C_COMPILER /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-gcc)
  set(CMAKE_CXX_COMPILER /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-g++)

  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)


clangtargettoolchain.cmake:

  set(CMAKE_SYSTEM_NAME Linux)

  set(CMAKE_SYSROOT /home/stephen/rpi/rasp-pi-rootfs)
  set(CMAKE_STAGING_PREFIX /home/stephen/dev/src/qtbase-rpi/extprefix)

  set(triple arm-linux-gnueabihf)
  set(gcc_toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf)

  set(CMAKE_C_COMPILER /home/stephen/dev/build/qtbase/llvm/bin/clang)
  set(CMAKE_C_COMPILER_TARGET ${triple})
  set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN ${gcc_toolchain})
  set(CMAKE_CXX_COMPILER /home/stephen/dev/build/qtbase/llvm/bin/clang++)
  set(CMAKE_CXX_COMPILER_TARGET ${triple})
  set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN ${gcc_toolchain})

  # Debian bug 708744
  include_directories(SYSTEM "${CMAKE_SYSROOT}/usr/include/${triple}")

  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)


I tested with this CMakeLists.txt:

  cmake_minimum_required(VERSION 2.8.12)
  find_package(Qt5Widgets 5.2 REQUIRED)

  find_package(ZLIB REQUIRED)

  include_directories(${ZLIB_INCLUDE_DIRS})
  add_executable(main main.cpp)
  target_link_libraries(main ${ZLIB_LIBRARIES} Qt5::Widgets)



stephen at hal:~/dev/src/playground/cmake/build{master}$ cmake .. -
DCMAKE_TOOLCHAIN_FILE=../rpitoolchain.cmake -DCMAKE_INSTALL_PREFIX=/opt/kf5
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- Check for working C compiler: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-g++
-- Check for working CXX compiler: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/bin/arm-linux-gnueabihf-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found ZLIB: /home/stephen/rpi/rasp-pi-rootfs/usr/lib/arm-linux-
gnueabihf/libz.so (found version "1.2.7") 
-- Configuring done
-- Generating done
-- Build files have been written to: 
/home/stephen/dev/src/playground/cmake/build
Qt( base ) KDE ( ) 
stephen at hal:~/dev/src/playground/cmake/build{master}$ make VERBOSE=1
makeobj[0]: Entering directory 
`/home/stephen/dev/src/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -
H/home/stephen/dev/src/playground/cmake -
B/home/stephen/dev/src/playground/cmake/build --check-build-system 
CMakeFiles/Makefile.cmake 0
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_start 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
cd /home/stephen/dev/src/playground/cmake/build && 
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_depends "Unix 
Makefiles" /home/stephen/dev/src/playground/cmake 
/home/stephen/dev/src/playground/cmake 
/home/stephen/dev/src/playground/cmake/build 
/home/stephen/dev/src/playground/cmake/build 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/DependInfo.cmake 
--color=
Dependee 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/DependInfo.cmake" 
is newer than depender 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/depend.internal".
Dependee 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/CMakeDirectoryInformation.cmake" 
is newer than depender 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/depend.internal".
Scanning dependencies of target main
make[2]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
make[2]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_report 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++  
--sysroot=/home/stephen/rpi/rasp-pi-rootfs  -DQT_CORE_LIB -DQT_GUI_LIB -
DQT_NO_DEBUG -DQT_WIDGETS_LIB -fPIE -isystem /home/stephen/dev/src/qtbase-
rpi/extprefix/include -isystem /home/stephen/dev/src/qtbase-
rpi/extprefix/include/QtWidgets -isystem /home/stephen/dev/src/qtbase-
rpi/extprefix/include/QtGui -isystem /home/stephen/rpi/rasp-pi-
rootfs/opt/vc/include -isystem /home/stephen/dev/src/qtbase-
rpi/extprefix/include/QtCore -isystem /home/stephen/dev/src/qtbase-
rpi/hostprefix/mkspecs/devices/linux-rasp-pi-g++    -o 
CMakeFiles/main.dir/main.cpp.o -c 
/home/stephen/dev/src/playground/cmake/main.cpp
Linking CXX executable main
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_link_script 
CMakeFiles/main.dir/link.txt --verbose=1
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++  
--sysroot=/home/stephen/rpi/rasp-pi-rootfs      
CMakeFiles/main.dir/main.cpp.o  -o main -rdynamic -lz 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Widgets.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Gui.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Core.so.5.3.0 -Wl,-
rpath,/opt/kf5/lib:/opt/vc/lib -Wl,-rpath-link,/home/stephen/rpi/rasp-pi-
rootfs/opt/vc/lib 
make[2]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_report 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles  1
[100%] Built target main
make[1]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_start 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 0
makeobj[0]: Leaving directory `/home/stephen/dev/src/playground/cmake/build'


So, determining compiler information works, find_package works, the zlib 
include dir is stripped as it is implicit (and I verified that the sysroot 
zlib.h is used by adding an #error), the libz.so.1.2.7 is simplified to -lz 
because of the implicit link directories, the -rpath info is properly 
transformed with the CMAKE_INSTALL_PREFIX replacing the 
CMAKE_STAGING_PREFIX, and the -rpath-link info is properly not transformed. 

I also tested without setting the CMAKE_INSTALL_PREFIX and the default 
/usr/local path is the result of the transformation.

The clang toolchain also works fine with clang trunk (I made some changes 
there to find the crti.o etc properly).

stephen at hal:~/dev/src/playground/cmake/build{master}$ cmake .. -
DCMAKE_TOOLCHAIN_FILE=../clangtargettoolchain.cmake
-- The C compiler identification is Clang 3.4.0
-- The CXX compiler identification is Clang 3.4.0
-- Check for working C compiler: 
/home/stephen/dev/build/qtbase/llvm/bin/clang
-- Check for working C compiler: 
/home/stephen/dev/build/qtbase/llvm/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: 
/home/stephen/dev/build/qtbase/llvm/bin/clang++
-- Check for working CXX compiler: 
/home/stephen/dev/build/qtbase/llvm/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found ZLIB: /home/stephen/rpi/rasp-pi-rootfs/usr/lib/arm-linux-
gnueabihf/libz.so (found version "1.2.7") 
-- Configuring done
-- Generating done
-- Build files have been written to: 
/home/stephen/dev/src/playground/cmake/build
Qt( base ) KDE ( ) 
stephen at hal:~/dev/src/playground/cmake/build{master}$ make VERBOSE=1
makeobj[0]: Entering directory 
`/home/stephen/dev/src/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -
H/home/stephen/dev/src/playground/cmake -
B/home/stephen/dev/src/playground/cmake/build --check-build-system 
CMakeFiles/Makefile.cmake 0
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_start 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
cd /home/stephen/dev/src/playground/cmake/build && 
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_depends "Unix 
Makefiles" /home/stephen/dev/src/playground/cmake 
/home/stephen/dev/src/playground/cmake 
/home/stephen/dev/src/playground/cmake/build 
/home/stephen/dev/src/playground/cmake/build 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/DependInfo.cmake 
--color=
Dependee 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/DependInfo.cmake" 
is newer than depender 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/depend.internal".
Dependee 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/CMakeDirectoryInformation.cmake" 
is newer than depender 
"/home/stephen/dev/src/playground/cmake/build/CMakeFiles/main.dir/depend.internal".
Scanning dependencies of target main
make[2]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
make[2]: Entering directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_report 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
/home/stephen/dev/build/qtbase/llvm/bin/clang++  -target arm-linux-gnueabihf 
-gcc-toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf --
sysroot=/home/stephen/rpi/rasp-pi-rootfs  -DQT_CORE_LIB -DQT_GUI_LIB -
DQT_NO_DEBUG -DQT_WIDGETS_LIB -fPIE -isystem /home/stephen/rpi/rasp-pi-
rootfs/usr/include/arm-linux-gnueabihf -isystem 
/home/stephen/dev/src/qtbase-rpi/extprefix/include -isystem 
/home/stephen/dev/src/qtbase-rpi/extprefix/include/QtWidgets -isystem 
/home/stephen/dev/src/qtbase-rpi/extprefix/include/QtGui -isystem 
/home/stephen/rpi/rasp-pi-rootfs/opt/vc/include -isystem 
/home/stephen/dev/src/qtbase-rpi/extprefix/include/QtCore -isystem 
/home/stephen/dev/src/qtbase-rpi/hostprefix/mkspecs/devices/linux-rasp-pi-
g++    -o CMakeFiles/main.dir/main.cpp.o -c 
/home/stephen/dev/src/playground/cmake/main.cpp
Linking CXX executable main
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_link_script 
CMakeFiles/main.dir/link.txt --verbose=1
/home/stephen/dev/build/qtbase/llvm/bin/clang++  -target arm-linux-gnueabihf 
-gcc-toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf --
sysroot=/home/stephen/rpi/rasp-pi-rootfs      CMakeFiles/main.dir/main.cpp.o  
-o main -rdynamic -lz /home/stephen/dev/src/qtbase-
rpi/extprefix/lib/libQt5Widgets.so.5.3.0 /home/stephen/dev/src/qtbase-
rpi/extprefix/lib/libQt5Gui.so.5.3.0 /home/stephen/dev/src/qtbase-
rpi/extprefix/lib/libQt5Core.so.5.3.0 -Wl,-rpath,/usr/local/lib:/opt/vc/lib 
-Wl,-rpath-link,/home/stephen/rpi/rasp-pi-rootfs/opt/vc/lib 
make[2]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_report 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles  1
[100%] Built target main
make[1]: Leaving directory `/home/stephen/safe/dev/playground/cmake/build'
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_progress_start 
/home/stephen/dev/src/playground/cmake/build/CMakeFiles 0
makeobj[0]: Leaving directory `/home/stephen/dev/src/playground/cmake/build'


All the same things work as with the other toolchain, and now the -target 
and -gcc-toolchain are also properly passed, which result in finding 
binutils etc properly.

I also tested a static and shared lib. 

Linking CXX shared library libuser2.so
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_link_script 
CMakeFiles/user2.dir/link.txt --verbose=1
/home/stephen/dev/build/qtbase/llvm/bin/clang++  -target arm-linux-gnueabihf 
-gcc-toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf --
sysroot=/home/stephen/rpi/rasp-pi-rootfs -fPIC    -shared -Wl,-
soname,libuser2.so -o libuser2.so CMakeFiles/user2.dir/lib.cpp.o 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Widgets.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Gui.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Core.so.5.3.0 -Wl,-
rpath,/opt/kf5/lib:/opt/vc/lib -Wl,-rpath-link,/home/stephen/rpi/rasp-pi-
rootfs/opt/vc/lib 
Linking CXX static library libuser.a
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -P 
CMakeFiles/user.dir/cmake_clean_target.cmake
/home/stephen/dev/prefix/qtbase/kde/bin/cmake -E cmake_link_script 
CMakeFiles/user.dir/link.txt --verbose=1
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-ar cr 
libuser.a  CMakeFiles/user.dir/lib.cpp.o
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-
ranlib libuser.a

So, the correct ar and ranlib are used when building the static library. 
When building the shared library, the correct ld is used:

stephen at hal:~/dev/src/playground/cmake/build{master}$ 
/home/stephen/dev/build/qtbase/llvm/bin/clang++  -target arm-linux-gnueabihf 
-gcc-toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf --
sysroot=/home/stephen/rpi/rasp-pi-rootfs -fPIC    -shared -Wl,-
soname,libuser2.so -o libuser2.so CMakeFiles/user2.dir/lib.cpp.o 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Widgets.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Gui.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Core.so.5.3.0 -Wl,-
rpath,/opt/kf5/lib:/opt/vc/lib -Wl,-rpath-link,/home/stephen/rpi/rasp-pi-
rootfs/opt/vc/lib -v
clang version 3.4 
Target: arm--linux-gnueabihf
Thread model: posix
Found candidate GCC installation: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/lib/gcc/arm-linux-gnueabihf/4.7.2
Selected GCC installation: /home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/lib/gcc/arm-linux-gnueabihf/4.7.2
 "/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/lib/gcc/arm-linux-
gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld" --
sysroot=/home/stephen/rpi/rasp-pi-rootfs -z relro -X --hash-style=gnu --
build-id --eh-frame-hdr -m armelf_linux_eabi -shared -dynamic-linker 
/lib/ld-linux-armhf.so.3 -o libuser2.so /home/stephen/rpi/rasp-pi-
rootfs/usr/lib/arm-linux-gnueabihf/crti.o /home/stephen/rpi/gcc-4.7-linaro-
rpi-gnueabihf/lib/gcc/arm-linux-gnueabihf/4.7.2/crtbeginS.o -
L/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/lib/gcc/arm-linux-
gnueabihf/4.7.2 -L/home/stephen/rpi/gcc-4.7-linaro-rpi-
gnueabihf/lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-
gnueabihf/lib/../lib32 -L/home/stephen/rpi/rasp-pi-rootfs/lib/arm-linux-
gnueabihf -L/home/stephen/rpi/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf -
L/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/lib/gcc/arm-linux-
gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/lib -
L/home/stephen/rpi/rasp-pi-rootfs/lib -L/home/stephen/rpi/rasp-pi-
rootfs/usr/lib -soname libuser2.so CMakeFiles/user2.dir/lib.cpp.o 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Widgets.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Gui.so.5.3.0 
/home/stephen/dev/src/qtbase-rpi/extprefix/lib/libQt5Core.so.5.3.0 -rpath 
/opt/kf5/lib:/opt/vc/lib -rpath-link /home/stephen/rpi/rasp-pi-
rootfs/opt/vc/lib -lstdc++ -lm -lgcc_s -lc -lgcc_s 
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/lib/gcc/arm-linux-
gnueabihf/4.7.2/crtendS.o /home/stephen/rpi/rasp-pi-rootfs/usr/lib/arm-
linux-gnueabihf/crtn.o
Qt( base ) KDE ( ) 
stephen at hal:~/dev/src/playground/cmake/build{master}$ realpath 
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/lib/gcc/arm-linux-
gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld
/home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf/arm-linux-gnueabihf/bin/ld


I'll do further tests later with other projects and with the QNX toolchain 
file.

Thanks,

Steve.





More information about the cmake-developers mailing list