[cfe-dev] Towards convenient cross compiling with CMake

Stephen Kelly steveire at gmail.com
Tue Nov 19 11:31:10 PST 2013


Hello,

Today some new features were merged to CMake master to interface with some 
Clang features.

 http://www.cmake.org/cmake/help/git-master/variable/CMAKE_LANG_COMPILER_TARGET.html

 http://www.cmake.org/cmake/help/git-master/variable/CMAKE_LANG_COMPILER_EXTERNAL_TOOLCHAIN.html

 http://www.cmake.org/cmake/help/git-master/variable/CMAKE_SYSROOT.html

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=76552d59

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=5096967e

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=7cd65c97

This simplifies the toolchain to something like this:

  set(CMAKE_SYSTEM_NAME Linux)

  set(CMAKE_SYSROOT /home/stephen/rpi/rasp-pi-rootfs)
  set(CMAKE_FIND_ROOT_PATH /home/stephen/dev/src/qtbase-rpi/extprefix)
  set(gcc_toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf)

  set(triple arm-linux-gnueabihf)

  set(CMAKE_C_COMPILER /usr/bin/clang)
  set(CMAKE_C_COMPILER_TARGET ${triple})
  set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN ${gcc_toolchain})
  set(CMAKE_CXX_COMPILER /usr/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)

As an alternative to specifying the -target, CMake master also knows how to 
use symlinked ${triple}-clang executables.

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=b84f5c2e

The commit at

 https://gitorious.org/cmake/steveires-cmake/commit/40f5fd5ac

will be merged when Clang 3.4 is released.

I have not been able to make cross-compiling work with my ubuntu supplied 
clang, nor with the version at http://llvm.org/apt/ (various issues related 
to binutils which I've posted to the list about before). It works with my 
self-build of trunk though. The Debian clang maintainer told me on IRC that 
cross-compiling is not a target use-case for his packages. If the 
llvm.org/apt packages also use his patches, that would explain it.

I would appreciate if you try it out with your cross-compiling setups and 
give feedback about how well it works. I am also interested in what your 
toolchain file looks like generally, and what else could/should be 
abstracted by CMake generally, or with user configuration in the toolchain 
file. 

Thanks,

Steve.





More information about the cfe-dev mailing list